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 32bcd36
Browse filesBrowse the repository at this point in the historyBrowse files
authored
feat: support MCP SDK v2 without changing result semantics (#10188)
Builds a `Tool` whose `func` is an `async def` closure: it awaits `session.call_tool(tool.name, arguments=kwargs)` and unpacks the result. The unpacker pulls text from any `TextContent` entries (returning a single string when there’s one, a list otherwise) and returns non-text entries as-is. An `isError=True` response raises `RuntimeError`. The MCP tool’s `inputSchema` is mapped to DSPy’s `args`/`arg_types`/`arg_desc` via `convert_input_schema_to_tool_args`.
110
+
Builds a `Tool` whose `func` is an `async def` closure: it awaits `session.call_tool(tool.name, arguments=kwargs)` and unpacks the result. The bridge supports the camelCase fields in mcp SDK v1 and their snake_case replacements in v2. An error result raises `RuntimeError`; text and non-text content retain their existing behavior. The tool’s input schema is mapped to DSPy’s `args`/`arg_types`/`arg_desc` via `convert_input_schema_to_tool_args`.
Walks an MCP-style JSON schema and produces the three dicts a `Tool` needs. Resolves `$defs`, picks up `description` fields as `arg_desc`, and translates JSON-schema types to Python types via a `_TYPE_MAPPING` table. Exported in case callers want to write their own bridge against a non-MCP protocol with similar JSON-schema arguments.
Copy file name to clipboardExpand all lines: docs/docs/learn/programming/mcp.md
+18-3Lines changed: 18 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,13 +22,28 @@ MCP enables you to:
22
22
-**Share tools across stacks** - Use the same tools across different frameworks.
23
23
-**Simplify integration** - Convert MCP tools to DSPy tools with one line.
24
24
25
-
DSPy does not handle MCP server connections directly. You can use client interfaces of the `mcp` library to establish the connection and pass `mcp.ClientSession`to `dspy.Tool.from_mcp_tool` in order to convert mcp tools into DSPy tools.
25
+
DSPy does not handle MCP server connections directly. Use a client interface from the `mcp` library, then pass the client or session to `dspy.Tool.from_mcp_tool`. DSPy supports both major versions of the `mcp` Python SDK.
26
26
27
27
## Using MCP with DSPy
28
28
29
29
### 1. HTTP Server (Remote)
30
30
31
-
For remote MCP servers over HTTP, use the streamable HTTP transport:
31
+
With `mcp` SDK v2, use the high-level `Client` for a remote server:
32
+
33
+
```python
34
+
import asyncio
35
+
import dspy
36
+
from mcp.client import Client
37
+
38
+
asyncdefmain():
39
+
asyncwith Client("http://localhost:8000/mcp") as client:
40
+
response =await client.list_tools()
41
+
dspy_tools = [dspy.Tool.from_mcp_tool(client, tool) for tool in response.tools]
42
+
43
+
asyncio.run(main())
44
+
```
45
+
46
+
With SDK v1, use the streamable HTTP transport and a `ClientSession`:
32
47
33
48
```python
34
49
import asyncio
@@ -69,7 +84,7 @@ asyncio.run(main())
69
84
70
85
### 2. Stdio Server (Local Process)
71
86
72
-
The most common way to use MCP is with a local server process communicating via stdio:
87
+
The most common way to use MCP is with a local server process communicating via stdio. This example works with both SDK versions:
0 commit comments