MCP Servers as Tools in CrewAI

no
Summary: Learn how to integrate MCP servers as tools in your CrewAI agents using the crewai-tools library.

Original Documentation

Documentation Index#

Fetch the complete documentation index at: https://docs.crewai.com/llms.txt Use this file to discover all available pages before exploring further.

Learn how to integrate MCP servers as tools in your CrewAI agents using the crewai-tools library.

Overview#

The Model Context Protocol (MCP) provides a standardized way for AI agents to provide context to LLMs by communicating with external services, known as MCP Servers.

CrewAI offers two approaches for MCP integration:

Use the mcps field directly on agents for seamless MCP tool integration. The DSL supports both string references (for quick setup) and structured configurations (for full control).

String-Based References (Quick Setup)#

Perfect for remote HTTPS servers and CrewAI AMP marketplace:

from crewai import Agent

agent = Agent(
    role="Research Analyst",
    goal="Research and analyze information",
    backstory="Expert researcher with access to external tools",
    mcps=[
        "https://mcp.exa.ai/mcp?api_key=your_key",           # External MCP server
        "https://api.weather.com/mcp#get_forecast",          # Specific tool from server
        "crewai-amp:financial-data",                         # CrewAI AMP marketplace
        "crewai-amp:research-tools#pubmed_search"            # Specific AMP tool
    ]
)
# MCP tools are now automatically available to your agent!

Structured Configurations (Full Control)#

For complete control over connection settings, tool filtering, and all transport types:

from crewai import Agent
from crewai.mcp import MCPServerStdio, MCPServerHTTP, MCPServerSSE
from crewai.mcp.filters import create_static_tool_filter

agent = Agent(
    role="Advanced Research Analyst",
    goal="Research with full control over MCP connections",
    backstory="Expert researcher with advanced tool access",
    mcps=[
        # Stdio transport for local servers
        MCPServerStdio(
            command="npx",
            args=["-y", "@modelcontextprotocol/server-filesystem"],
            env={"API_KEY": "your_key"},
            tool_filter=create_static_tool_filter(
                allowed_tool_names=["read_file", "list_directory"]
            ),
            cache_tools_list=True,
        ),
        # HTTP/Streamable HTTP transport for remote servers
        MCPServerHTTP(
            url="https://api.example.com/mcp",
            headers={"Authorization": "Bearer your_token"},
            streamable=True,
            cache_tools_list=True,
        ),
        # SSE transport for real-time streaming
        MCPServerSSE(
            url="https://stream.example.com/mcp/sse",
            headers={"Authorization": "Bearer your_token"},
        ),
    ]
)

🔧 Advanced: MCPServerAdapter (For Complex Scenarios)#

For advanced use cases requiring manual connection management, the crewai-tools library provides the MCPServerAdapter class.

We currently support the following transport mechanisms:

  • Stdio: for local servers (communication via standard input/output between processes on the same machine)
  • Server-Sent Events (SSE): for remote servers (unidirectional, real-time data streaming from server to client over HTTP)
  • Streamable HTTPS: for remote servers (flexible, potentially bi-directional communication over HTTPS, often utilizing SSE for server-to-client streams)

Video Tutorial#

Watch this video tutorial for a comprehensive guide on MCP integration with CrewAI: