Skip to content

Commit 4561928

Browse files
Rehan Ahmedclaude
andcommitted
docs(mcp): reduce diff by preserving original comments and structure
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6169518 commit 4561928

2 files changed

Lines changed: 46 additions & 34 deletions

File tree

‎docs/docs/learn/programming/mcp.md‎

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,77 @@ MCP enables you to:
2020

2121
- **Use standardized tools** - Connect to any MCP-compatible server.
2222
- **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.
2424

2525
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.
2626

2727
## Using MCP with DSPy
2828

29-
### 1. Stdio Server (Local Process)
29+
### 1. HTTP Server (Remote)
3030

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:
3232

3333
```python
3434
import asyncio
3535
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
3837

3938
async def main():
40-
server_params = StdioServerParameters(
41-
command="python",
42-
args=["path/to/your/mcp_server.py"],
43-
)
44-
45-
async with stdio_mcp_tools(server_params) as tools:
46-
class QuestionAnswer(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+
async with http_mcp_tools("http://localhost:8000/mcp") as tools:
41+
# Create and use ReAct agent
42+
class TaskSignature(dspy.Signature):
43+
task: str = dspy.InputField()
44+
result: str = dspy.OutputField()
5045

5146
react_agent = dspy.ReAct(
52-
signature=QuestionAnswer,
47+
signature=TaskSignature,
5348
tools=tools,
5449
max_iters=5,
5550
)
5651

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)
5954

6055
asyncio.run(main())
6156
```
6257

63-
### 2. HTTP Server (Remote)
58+
### 2. Stdio Server (Local Process)
6459

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:
6661

6762
```python
6863
import asyncio
6964
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
7167

7268
async def main():
73-
async with http_mcp_tools("http://localhost:8000/mcp") as tools:
74-
class TaskSignature(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+
async with stdio_mcp_tools(server_params) as tools:
77+
# Create a ReAct agent with the tools
78+
class QuestionAnswer(dspy.Signature):
79+
"""Answer questions using available tools."""
80+
question: str = dspy.InputField()
81+
answer: str = dspy.OutputField()
7782

7883
react_agent = dspy.ReAct(
79-
signature=TaskSignature,
84+
signature=QuestionAnswer,
8085
tools=tools,
8186
max_iters=5,
8287
)
8388

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?")
91+
print(result.answer)
8692

93+
# Run the async function
8794
asyncio.run(main())
8895
```
8996

‎docs/docs/tutorials/mcp/index.md‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ create a file named `dspy_mcp_agent.py`, and follow the guide to add code to it.
293293
### Gather Tools from MCP Servers
294294

295295
We first need to gather all available tools from the MCP server and make them
296-
usable by DSPy. DSPy provides `stdio_mcp_tools` which handles the entire MCP session
297-
lifecycle — connecting, initializing, listing tools, and converting them to
298-
[`dspy.Tool`](https://dspy.ai/api/primitives/Tool/) objects:
296+
usable by DSPy. DSPy provides an API [`dspy.Tool`](https://dspy.ai/api/primitives/Tool/)
297+
as the standard tool interface. The `stdio_mcp_tools` helper handles the entire MCP session
298+
lifecycle — connecting, initializing, listing tools, and converting them to `dspy.Tool`:
299299

300300
```python
301301
from mcp import StdioServerParameters
@@ -305,6 +305,7 @@ from dspy.utils.mcp import stdio_mcp_tools
305305
server_params = StdioServerParameters(
306306
command="python", # Executable
307307
args=["path_to_your_working_directory/mcp_server.py"],
308+
env=None,
308309
)
309310

310311
async def run():
@@ -318,7 +319,8 @@ if __name__ == "__main__":
318319
asyncio.run(run())
319320
```
320321

321-
With the code above, we have successfully collected all available MCP tools as DSPy tools.
322+
With the code above, we have successfully collected all available MCP tools and converted
323+
them to DSPy tools.
322324

323325

324326
### Build a DSPy Agent to Handle Customer Requests
@@ -363,8 +365,9 @@ from dspy.utils.mcp import stdio_mcp_tools
363365

364366
# Create server parameters for stdio connection
365367
server_params = StdioServerParameters(
366-
command="python",
367-
args=["path_to_your_working_directory/mcp_server.py"],
368+
command="python", # Executable
369+
args=["script_tmp/mcp_server.py"], # Optional command line arguments
370+
env=None, # Optional environment variables
368371
)
369372

370373

@@ -386,7 +389,9 @@ dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
386389

387390
async def run(user_request):
388391
async with stdio_mcp_tools(server_params) as tools:
392+
# Create the agent
389393
react = dspy.ReAct(DSPyAirlineCustomerService, tools=tools)
394+
390395
result = await react.acall(user_request=user_request)
391396
print(result)
392397

0 commit comments

Comments
 (0)