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 4561928
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/docs/learn/programming/mcp.md
+35-28Lines changed: 35 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,70 +20,77 @@ MCP enables you to:
20
20
21
21
-**Use standardized tools** - Connect to any MCP-compatible server.
22
22
-**Share tools across stacks** - Use the same tools across different frameworks.
23
-
-**Simplify integration** - Convert MCP tools to DSPy tools with minimal setup.
23
+
-**Simplify integration** - Convert MCP tools to DSPy tools with one line.
24
24
25
25
DSPy provides `stdio_mcp_tools` and `http_mcp_tools` async context managers that handle the full MCP session lifecycle — connecting, initializing, listing tools, and converting them to `dspy.Tool` objects — so you can focus on building your agent.
26
26
27
27
## Using MCP with DSPy
28
28
29
-
### 1. Stdio Server (Local Process)
29
+
### 1. HTTP Server (Remote)
30
30
31
-
The most common way to use MCP is with a local server process communicating via stdio:
31
+
For remote MCP servers over HTTP, use the streamable HTTP transport:
32
32
33
33
```python
34
34
import asyncio
35
35
import dspy
36
-
from mcp import StdioServerParameters
37
-
from dspy.utils.mcp import stdio_mcp_tools
36
+
from dspy.utils.mcp import http_mcp_tools
38
37
39
38
asyncdefmain():
40
-
server_params = StdioServerParameters(
41
-
command="python",
42
-
args=["path/to/your/mcp_server.py"],
43
-
)
44
-
45
-
asyncwith stdio_mcp_tools(server_params) as tools:
46
-
classQuestionAnswer(dspy.Signature):
47
-
"""Answer questions using available tools."""
48
-
question: str= dspy.InputField()
49
-
answer: str= dspy.OutputField()
39
+
# Connect to HTTP MCP server
40
+
asyncwith http_mcp_tools("http://localhost:8000/mcp") as tools:
41
+
# Create and use ReAct agent
42
+
classTaskSignature(dspy.Signature):
43
+
task: str= dspy.InputField()
44
+
result: str= dspy.OutputField()
50
45
51
46
react_agent = dspy.ReAct(
52
-
signature=QuestionAnswer,
47
+
signature=TaskSignature,
53
48
tools=tools,
54
49
max_iters=5,
55
50
)
56
51
57
-
result =await react_agent.acall(question="What is 25 + 17?")
58
-
print(result.answer)
52
+
result =await react_agent.acall(task="Check the weather in Tokyo")
53
+
print(result.result)
59
54
60
55
asyncio.run(main())
61
56
```
62
57
63
-
### 2. HTTP Server (Remote)
58
+
### 2. Stdio Server (Local Process)
64
59
65
-
For remote MCP servers over streamable HTTP:
60
+
The most common way to use MCP is with a local server process communicating via stdio:
66
61
67
62
```python
68
63
import asyncio
69
64
import dspy
70
-
from dspy.utils.mcp import http_mcp_tools
65
+
from mcp import StdioServerParameters
66
+
from dspy.utils.mcp import stdio_mcp_tools
71
67
72
68
asyncdefmain():
73
-
asyncwith http_mcp_tools("http://localhost:8000/mcp") as tools:
74
-
classTaskSignature(dspy.Signature):
75
-
task: str= dspy.InputField()
76
-
result: str= dspy.OutputField()
69
+
# Configure the stdio server
70
+
server_params = StdioServerParameters(
71
+
command="python", # Command to run
72
+
args=["path/to/your/mcp_server.py"], # Server script path
73
+
)
74
+
75
+
# Connect to the server
76
+
asyncwith stdio_mcp_tools(server_params) as tools:
77
+
# Create a ReAct agent with the tools
78
+
classQuestionAnswer(dspy.Signature):
79
+
"""Answer questions using available tools."""
80
+
question: str= dspy.InputField()
81
+
answer: str= dspy.OutputField()
77
82
78
83
react_agent = dspy.ReAct(
79
-
signature=TaskSignature,
84
+
signature=QuestionAnswer,
80
85
tools=tools,
81
86
max_iters=5,
82
87
)
83
88
84
-
result =await react_agent.acall(task="Check the weather in Tokyo")
85
-
print(result.result)
89
+
# Use the agent
90
+
result =await react_agent.acall(question="What is 25 + 17?")
0 commit comments