Skip to content

DSPy

DSPy does not have native MCP support. Bridge AllMCP into DSPy using langchain-mcp-adapters to convert MCP tools into DSPy-compatible functions.


install
pip install dspy langchain-mcp-adapters langchain-anthropic

agent.py
import asyncio
import dspy
from langchain_mcp_adapters.client import MultiServerMCPClient
async def get_allmcp_tools_as_dspy():
"""Load AllMCP tools and wrap them for DSPy."""
client = MultiServerMCPClient({
"allmcp": {
"url": "https://go.allmcp.co/mcp/",
"transport": "streamable_http",
"headers": {"X-API-Key": "YOUR_API_KEY"},
}
})
lc_tools = await client.get_tools()
dspy_tools = []
for tool in lc_tools:
def make_fn(t):
def fn(**kwargs):
return t.invoke(kwargs)
fn.__name__ = t.name
fn.__doc__ = t.description
return fn
dspy_tools.append(dspy.Tool(make_fn(tool)))
return dspy_tools

agent.py
import asyncio
import dspy
async def main():
dspy.configure(lm=dspy.LM("anthropic/claude-sonnet-4-6"))
tools = await get_allmcp_tools_as_dspy()
react = dspy.ReAct("question -> answer", tools=tools)
result = react(question="How many open deals are in my Bitrix24 CRM?")
print(result.answer)
asyncio.run(main())

agent.py
async def get_bitrix24_tools_for_dspy():
client = MultiServerMCPClient({...})
lc_tools = await client.get_tools()
crm_tools = [t for t in lc_tools if t.name.startswith("bitrix24_")]
return [dspy.Tool(wrap_lc_tool(t)) for t in crm_tools]