> For the complete documentation index, see [llms.txt](https://docs.allmcp.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.allmcp.co/documentation/agent-frameworks/dspy.md).

# DSPy

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

{% hint style="info" %}
**Adapter required.** DSPy has no native MCP client, so AllMCP tools are loaded through the `langchain-mcp-adapters` bridge and wrapped as `dspy.Tool` objects.
{% endhint %}

***

## Install

{% code title="install" %}

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

{% endcode %}

***

## Bridge pattern

{% code title="agent.py" overflow="wrap" %}

```python
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
```

{% endcode %}

***

## Using with ReAct

{% code title="agent.py" overflow="wrap" %}

```python
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())
```

{% endcode %}

***

## Filtering to relevant tools

{% code title="agent.py" overflow="wrap" %}

```python
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]
```

{% endcode %}

{% hint style="success" %}
Limiting tools to relevant ones improves DSPy's reasoning accuracy.
{% endhint %}

***

{% hint style="info" %}
If DSPy adds native MCP support in a future release, the `langchain-mcp-adapters` bridge becomes optional. The AllMCP URL and API key stay the same.
{% endhint %}

***

## Related

{% content-ref url="/pages/uBaUeZYQpupBMd5c1BY0" %}
[Overview](/documentation/agent-frameworks/frameworks.md)
{% endcontent-ref %}

{% content-ref url="/pages/QJVxDLqxIz5KMCH5afbE" %}
[All Providers](/documentation/providers/providers.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.allmcp.co/documentation/agent-frameworks/dspy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
