MCPAdapt

repository·main·Indexed 19 days ago

https://github.com/grll/mcpadapt

A bridge that adapts Model Context Protocol (MCP) servers as tools for agentic frameworks including Smolagents, LangChain, CrewAI, and Google GenAI. It allows developers to unlock over 650+ MCP servers by providing specialized adapters and support for both Stdio and SSE transports.

Tokens
10.8K
Snippets
34
Records
38
Agent score
64%

What's inside mcpadapt

  1. How MCPAdapt works

    main

    MCPAdapt is designed around two main components to bridge the gap between the Model Context Protocol (MCP) and various agentic frameworks:

    1. Core Component: Manages the lifecycle of MCP servers in both synchronous and asynchronous contexts. This core logic is shared across all adapters.
    2. Framework-specific Adapters: Subclasses that transform MCP server tools into the specific Tool classes, functions, or signatures required by frameworks like Langchain, CrewAI, or Smolagents.

    This architecture ensures consistent server management while allowing seamless integration with the unique requirements of different agentic frameworks.

  2. Integrate remote MCP servers via SSE with Google GenAI

    main
    MCPAdapt supports remote MCP servers using Server-Sent Events (SSE). When using the GoogleGenAIAdapter with an SSE-based server, provide the appropriate server parameters (e.g., URL and headers) to MCPAdapt to enable tool integration with Google GenAI.
  3. Use the MCPAdapt Smolagents Adapter

    main

    Instead of using ToolCollection.from_mcp directly, you can use the MCPAdapt core class combined with the SmolAgentsAdapter. This provides a unified way to adapt MCP servers into smolagents tools using the mcpadapt framework.

    Installation Note

    If you need audio support alongside the smolagents integration, install the following: uv add mcpadapt[smolagents,audio]

    import os
    from mcp import StdioServerParameters
    from smolagents import CodeAgent, HfApiModel
    from mcpadapt.core import MCPAdapt
    from mcpadapt.smolagents_adapter import SmolAgentsAdapter
    
    with MCPAdapt(
        StdioServerParameters(
            command="uvx",
            args=["--quiet", "pubmedmcp@0.1.3"],
            env={"UV_PYTHON": "3.12", **os.environ},
        ),
        SmolAgentsAdapter(),
    ) as tools:
        agent = CodeAgent(tools=tools, model=HfApiModel())
        agent.run("Find studies about hangover?")
  4. Basic usage of MCPAdapt with Stdio servers

    main

    To use MCP tools within an agentic framework, use the MCPAdapt context manager. You must provide StdioServerParameters (defining how to run the MCP server) and an instance of the specific framework's adapter.

    When the context manager is active, the tools variable contains a list of tools that are 100% compatible with your chosen agentic framework.

    from mcp import StdioServerParameters
    from mcpadapt.core import MCPAdapt
    from mcpadapt.<agentic-framework>_adapter import <AgenticFramework>Adapter
    
    with MCPAdapt(
        StdioServerParameters(command="uv", args=["run", "src/echo.py"]),
        <AgenticFramework>Adapter(),
    ) as tools:
        # tools is a list of tools 100% compatible with your agentic framework.
        ...
  5. Use the Smolagents Adapter to adapt MCP servers

    main

    The mcpadapt.smolagents_adapter allows you to convert Model Context Protocol (MCP) servers into tools compatible with the smolagents framework.

    To use the adapter, you must provide:

    1. The MCP server command or connection details: You can specify the command to run your favorite MCP server (including support for smithery and others) or a dictionary of SSE (Server-Sent Events) server parameters (e.g., {"url": "http://localhost:8000", "headers": ...}).
    2. The adapter type: Explicitly specify smolagents as the adapter you want to use to adapt the MCP server into a tool.
    # Conceptual usage pattern for the smolagents adapter
    # Note: Exact implementation details depend on the smolagents integration
    from mcpadapt.smolagents_adapter import SmolagentsAdapter
    
    adapter = SmolagentsAdapter(
        command="npx -y @modelcontextprotocol/server-everything", # or SSE dict
        adapter_type="smolagents"
    )
  6. Install MCP support for Smolagents

    main

    Smolagents version 1.4.1 and above has MCPAdapt integrated directly into its tool collections. You can install the necessary dependencies using uv with the [mcp] extra.

    uv add smolagents[mcp]
  7. Use MCPAdapt with SSE (Server-Sent Events) servers

    main

    To connect to an MCP server via SSE, provide a configuration dictionary to MCPAdapt instead of StdioServerParameters. This configuration is passed to the MCP Python SDK's SSE client.

    Supported configuration keys:

    • url (str, Required): The SSE endpoint URL.
    • headers (dict[str, Any], Optional): Custom HTTP headers (e.g., {"Authorization": "Bearer token"}).
    • timeout (float, Optional): Connection timeout in seconds. Default is 5.
    • sse_read_timeout (float, Optional): SSE read timeout in seconds. Default is 300 (5 minutes).
    from mcpadapt.core import MCPAdapt
    from mcpadapt.<framework>_adapter import <Framework>Adapter
    
    async with MCPAdapt(
        {
            "url": "http://127.0.0.1:8000/sse",
            # "headers": {"Authorization": "Bearer token"},
            # "timeout": 5,
            # "sse_read_timeout": 300,
        },
        <Framework>Adapter()
    ) as tools:
        # 'tools' contains framework-compatible tools
        ...
  8. Install mcpadapt for your agentic framework

    main

    Install mcpadapt along with the specific adapter for your chosen agentic framework using pip. Replace <agentic-framework> with one of the supported frameworks:

    • smolagents
    • crewai
    • langchain
    • google-genai
    • llamaindex
    pip install mcpadapt[smolagents]