You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 98dbfa6
Browse filesBrowse the repository at this point in the historyBrowse files
1. Converts the MCP input schema into DSPy's `args`, `arg_types`, and `arg_desc`.
98
104
2. Creates an async callable that invokes `session.call_tool(...)`.
99
-
3. Unpacks MCP text content into a string or list and preserves non-text content.
100
-
4. Raises an execution error when the MCP response has `isError=True`.
105
+
3. By default, unpacks MCP text content into a string or list and preserves the existing non-text fallback.
106
+
4. With `result_mode="structured"`, returns `structuredContent` exactly when present and otherwise uses the default conversion.
107
+
5. Raises an execution error when the MCP response has `isError=True`.
101
108
102
-
The bridge supports both the camelCase result fields in MCP SDK v1 and their snake_case replacements in v2 without changing text or non-text result behavior.
109
+
The bridge supports both the camelCase result fields in MCP SDK v1 and their snake_case replacements in v2. Structured mode does not parse values or unwrap a single `result` field, because MCP structured content can be any JSON value and a `result` field may belong to the application.
103
110
104
111
MCP tools are asynchronous because `mcp.ClientSession` is asynchronous. Use a module's async entry point, such as `acall`, or explicitly enable async-to-sync conversion when appropriate.
105
112
@@ -116,8 +123,8 @@ Validates, coerces, and executes tool arguments through synchronous or asynchron
Wraps a remote MCP tool as an asynchronous DSPy tool. Set `result_mode="structured"` for exact machine-readable MCP results; the default keeps the model-facing content behavior.
121
128
122
129
**`Tool.from_langchain(tool)`** → `Tool`
123
130
Wraps a LangChain tool in the same DSPy interface.
result =await dspy_tool.acall(param1="value", param2=123)
159
159
```
160
160
161
+
### Choosing the result representation
162
+
163
+
By default, DSPy uses the MCP result's model-facing `content` field. This preserves the existing behavior: one text block becomes a string, multiple text blocks become a list, and results without text fall back to their non-text content.
164
+
165
+
For programmatic workflows that need the MCP result's machine-readable JSON value, opt into structured content when converting the tool:
166
+
167
+
```python
168
+
structured_tool = dspy.Tool.from_mcp_tool(
169
+
session,
170
+
mcp_tool,
171
+
result_mode="structured",
172
+
)
173
+
174
+
result =await structured_tool.acall(param1="value", param2=123)
175
+
```
176
+
177
+
Structured mode returns `structuredContent` exactly as the server sent it. Objects, arrays, strings, numbers, booleans, JSON `null`, and empty values are preserved without parsing or unwrapping. For example, the official MCP Python SDK may represent a tool annotated `-> int` as `{"result": 3}`; DSPy returns that entire object rather than guessing that the `result` field is an SDK-generated envelope. If a result has no structured content, DSPy falls back to the default content conversion.
178
+
179
+
Use the default mode for tools primarily observed by a ReAct agent or another language model, since MCP `content` is the server's model-facing representation. Use structured mode when application code needs native JSON values for validation, indexing, or explicit tool chaining. In both modes, treat tool results as untrusted server data and validate them before passing them to sensitive operations.
180
+
161
181
## Learn More
162
182
163
183
-[MCP Official Documentation](https://modelcontextprotocol.io/)
0 commit comments