Dspy mcp tool integration
Skill OmidZamani/dspy-skills/skills/dspy-mcp-tool-integration
Use for MCP tools with DSPy, Model Context Protocol servers, dspy.Tool.from_mcp_tool, and ReAct agents over MCP-compatible tools.From its SKILL.md
npx -y skills add OmidZamani/dspy-skills --skill dspy-mcp-tool-integrationAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- runs commandsInstructs the agent to run 1 command, including `pip install -U "dspy[mcp]>=3.2.1,<3.3"`.
SKILL.md
2.9 KB, 634 tokens by cl100k_base, as published. Nobody here has run it
DSPy MCP Tool Integration
Goal
Connect an MCP server with the MCP Python client, convert its tools to dspy.Tool, and use them in an async DSPy agent.
Install
pip install -U "dspy[mcp]>=3.2.1,<3.3"
DSPy converts tools but does not manage MCP connections. Keep the ClientSession alive for as long as the DSPy tools are in use.
Streamable HTTP Server
import asyncio
import dspy
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async def main():
async with streamablehttp_client("http://localhost:8000/mcp") as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
response = await session.list_tools()
tools = [dspy.Tool.from_mcp_tool(session, tool) for tool in response.tools]
agent = dspy.ReAct("task -> result", tools=tools, max_iters=5)
output = await agent.acall(task="Check the weather in Tokyo")
print(output.result)
asyncio.run(main())
Local Stdio Server
import asyncio
import dspy
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
params = StdioServerParameters(
command="python3",
args=["path/to/server.py"],
env=None,
)
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
response = await session.list_tools()
tools = [dspy.Tool.from_mcp_tool(session, tool) for tool in response.tools]
agent = dspy.ReAct("question -> answer", tools=tools, max_iters=5)
print((await agent.acall(question="What is 25 + 17?")).answer)
asyncio.run(main())
Best Practices
- Use
acall()because MCP tools are asynchronous. - Initialize the session before listing tools.
- Keep tool descriptions precise at the MCP server boundary.
- Apply authentication and authorization before exposing sensitive tools.
- Set a modest
max_itersand trace tool use in production.
Related Skills
- Build agents: dspy-react-agent-builder
- Configure native function calling: dspy-adapters-multimodal
- Add async runtime patterns: dspy-production-deployment
Official Documentation
- DSPy MCP guide: https://dspy.ai/learn/programming/mcp/
- DSPy MCP tutorial: https://dspy.ai/tutorials/mcp/
- MCP Python SDK: https://github.com/modelcontextprotocol/python-sdk
What ships with it: 1 file
342 B alongside SKILL.md, 1 of them executable
- example.pyruns342 B
Gives 0 of the 12 instructions most mcp tooling skills give in 634 tokens
Counted across 780 of the 1,136 authors here whose files we hold, read 2026-09-06
- Use Zod for input validationin 34 of 780, across 21 files
- Use stdio for local clientsin 27 of 780, across 10 files
- Restart Claude Code after configurationin 26 of 780, across 23 files
- Verify MCP server connection before using toolsin 23 of 780, across 17 files
- Define input schemas for every toolin 20 of 780, across 11 files
- Use Streamable HTTP for remote clientsin 18 of 780, across 8 files
- Pin SDK version in package.jsonin 17 of 780, across 6 files
- Keep server logic independent of transportin 16 of 780, across 6 files
- Verify SDK methods against official documentationin 15 of 780, across 5 files
- Format evaluation results as an XML filein 15 of 780, across 12 files
- Test servers using the MCP Inspectorin 15 of 780, across 14 files
- Create ten complex and independent evaluation questionsin 14 of 780, across 11 files
Said here and by no other author read
- Keep ClientSession alive during tool use
- Use acall for asynchronous tool execution
- Apply authentication before exposing sensitive tools
- Set modest max_iters for agents
- Trace tool use in production
Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once. Length counted with cl100k_base; the agent that loads this file may tokenize it differently.