Skip to content

[Feature] Convenience adapters to simplify MCP tool usage with DSPy #9331

Open

Description

@ahmeshaf

🚀 Feature Request

What feature would you like to see?

Add high-level convenience adapters (stdio_mcp_tools and http_mcp_tools) that simplify integrating Model Context Protocol (MCP) tools with DSPy agents like dspy.ReAct.

Currently, using MCP tools with DSPy requires manually managing MCP sessions, converting tools, and handling the connection lifecycle:

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from dspy.utils.mcp import convert_mcp_tool

server_params = StdioServerParameters(command="docker", args=[...])
async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        await session.initialize()
        response = await session.list_tools()
        tools = [convert_mcp_tool(session, t) for t in response.tools]
        # finally use tools with dspy.ReAct...

The proposed adapters reduce this to:

from mcp import StdioServerParameters
from dspy.utils.mcp import stdio_mcp_tools

params = StdioServerParameters(command="docker", args=[...])
async with stdio_mcp_tools(params, exclude_tools=["dangerous"], timeout=30) as tools:
    react = dspy.ReAct("question -> answer", tools=tools)
    result = await react.acall(question="...")
from dspy.utils.mcp import http_mcp_tools

async with http_mcp_tools("http://127.0.0.1:8003/mcp", include_tools=["search"]) as tools:
    react = dspy.ReAct("question -> answer", tools=tools)
    result = await react.acall(question="...")

Both adapters support include_tools/exclude_tools filtering, timeout configuration, and validation of tool names against the server.

Would you like to contribute?

  • Yes, I'd like to help implement this.

Additional Context

Implementation PR: #9311

Activity

  1. xXMrNidaXx commented on Feb 24, 2026

    @xXMrNidaXx

    This would be a great addition! We've been building MCP integrations for DSPy agents at RevolutionAI and the current boilerplate is definitely a friction point.

    A few thoughts on the implementation:

    Tool filtering is crucial — the include_tools/exclude_tools pattern is exactly right. In production we often want to expose only a subset of MCP tools to certain agents for security and cost reasons.

    Timeout handling — glad to see this included. MCP servers can hang, and having a configurable timeout prevents agents from getting stuck in infinite waits.

    A few suggestions for the PR:

    1. Connection pooling for HTTP — if the adapter is used across multiple agent calls, keeping the connection alive could reduce latency

    2. Retry logic — MCP server restarts are common in dev; a configurable retry with exponential backoff would make the adapters more robust

    3. Tool schema caching — list_tools() can be slow; caching the schema for a session duration could help

    4. Logging hooks — optional debug logging for tool calls would help with debugging agent behavior

    Happy to help test the PR if useful. We're shipping MCP-powered agents regularly and can provide feedback on edge cases.

  2. xXMrNidaXx commented on Feb 25, 2026

    @xXMrNidaXx

    MCP + DSPy integration would be powerful! Here's a pattern that could work:

    Proposed adapter design:

    from dspy import Signature, Module
    from mcp import Client
    
    class MCPToolAdapter(Module):
        def __init__(self, mcp_client: Client, tool_name: str):
            self.client = mcp_client
            self.tool_name = tool_name
            
        def forward(self, **kwargs):
            # Map DSPy signature fields to MCP tool inputs
            result = self.client.call_tool(self.tool_name, kwargs)
            return result
    
    # Usage
    class SearchAndSummarize(Signature):
        query: str = dspy.InputField()
        summary: str = dspy.OutputField()
    
    mcp_search = MCPToolAdapter(mcp_client, 'web_search')

    Key considerations:

    1. Automatic schema mapping from MCP tool specs to DSPy signatures
    2. Error handling and retries
    3. Streaming support for long-running tools
    4. Tool chaining within DSPy programs

    Would love to see this become a first-class integration!


    🔧 Building AI tools? We're hiring talented developers at RevolutionAI:
    https://www.revolutionai.io/find-work

    Production AI projects • Flexible hours • Great rates 💼

  3. ahmeshaf commented on Feb 25, 2026

    @ahmeshaf
    ContributorAuthor

    @xXMrNidaXx , I already created the PR for this feature: #9311

    Please feel free to comment there for additional feature requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions