Skip to main content

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 connected MCP integrations from the CrewAI catalog:

Structured Configurations (Full Control)

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

🔧 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:

Installation

CrewAI MCP integration requires the mcp library:

Quick Start: Simple DSL Integration

The easiest way to integrate MCP servers is using the mcps field on your agents. You can use either string references or structured configurations.

Quick Start with String References

Quick Start with Structured Configurations

That’s it! The MCP tools are automatically discovered and available to your agent.

MCP Reference Formats

The mcps field supports both string references (for quick setup) and structured configurations (for full control). You can mix both formats in the same list.

String-Based References

External MCP Servers

Connected MCP Integrations

Connect MCP servers from the CrewAI catalog or bring your own. Once connected in your account, reference them by slug:

Structured Configurations

Stdio Transport (Local Servers)

Perfect for local MCP servers that run as processes:

HTTP/Streamable HTTP Transport (Remote Servers)

For remote MCP servers over HTTP/HTTPS:

SSE Transport (Real-Time Streaming)

For remote servers using Server-Sent Events:

Mixed References

You can combine string references and structured configurations:

Tool Filtering

Structured configurations support advanced tool filtering:

Configuration Parameters

Each transport type supports specific configuration options:

MCPServerStdio Parameters

  • command (required): Command to execute (e.g., "python", "node", "npx", "uvx")
  • args (optional): List of command arguments (e.g., ["server.py"] or ["-y", "@mcp/server"])
  • env (optional): Dictionary of environment variables to pass to the process
  • tool_filter (optional): Tool filter function for filtering available tools
  • cache_tools_list (optional): Whether to cache the tool list for faster subsequent access (default: False)

MCPServerHTTP Parameters

  • url (required): Server URL (e.g., "https://api.example.com/mcp")
  • headers (optional): Dictionary of HTTP headers for authentication or other purposes
  • streamable (optional): Whether to use streamable HTTP transport (default: True)
  • tool_filter (optional): Tool filter function for filtering available tools
  • cache_tools_list (optional): Whether to cache the tool list for faster subsequent access (default: False)

MCPServerSSE Parameters

  • url (required): Server URL (e.g., "https://api.example.com/mcp/sse")
  • headers (optional): Dictionary of HTTP headers for authentication or other purposes
  • tool_filter (optional): Tool filter function for filtering available tools
  • cache_tools_list (optional): Whether to cache the tool list for faster subsequent access (default: False)

Common Parameters

All transport types support:
  • tool_filter: Filter function to control which tools are available. Can be:
    • None (default): All tools are available
    • Static filter: Created with create_static_tool_filter() for allow/block lists
    • Dynamic filter: Created with create_dynamic_tool_filter() for context-aware filtering
  • cache_tools_list: When True, caches the tool list after first discovery to improve performance on subsequent connections

Key Features

  • 🔄 Automatic Tool Discovery: Tools are automatically discovered and integrated
  • 🏷️ Name Collision Prevention: Server names are prefixed to tool names
  • Performance Optimized: On-demand connections with schema caching
  • 🛡️ Error Resilience: Graceful handling of unavailable servers
  • ⏱️ Timeout Protection: Built-in timeouts prevent hanging connections
  • 📊 Transparent Integration: Works seamlessly with existing CrewAI features
  • 🔧 Full Transport Support: Stdio, HTTP/Streamable HTTP, and SSE transports
  • 🎯 Advanced Filtering: Static and dynamic tool filtering capabilities
  • 🔐 Flexible Authentication: Support for headers, environment variables, and query parameters

Error Handling

The MCP DSL integration is designed to be resilient and handles failures gracefully:
All connection errors are handled gracefully:
  • Connection failures: Logged as warnings, agent continues with available tools
  • Timeout errors: Connections timeout after 30 seconds (configurable)
  • Authentication errors: Logged clearly for debugging
  • Invalid configurations: Validation errors are raised at agent creation time

Advanced: MCPServerAdapter

For complex scenarios requiring manual connection management, use the MCPServerAdapter class from crewai-tools. Using a Python context manager (with statement) is the recommended approach as it automatically handles starting and stopping the connection to the MCP server.

Connection Configuration

The MCPServerAdapter supports several configuration options to customize the connection behavior:
  • connect_timeout (optional): Maximum time in seconds to wait for establishing a connection to the MCP server. Defaults to 30 seconds if not specified. This is particularly useful for remote servers that may have variable response times.
This general pattern shows how to integrate tools. For specific examples tailored to each transport, refer to the detailed guides below.

Filtering Tools

There are two ways to filter tools:
  1. Accessing a specific tool using dictionary-style indexing.
  2. Pass a list of tool names to the MCPServerAdapter constructor.

Accessing a specific tool using dictionary-style indexing.

Pass a list of tool names to the MCPServerAdapter constructor.

Using with CrewBase

To use MCPServer tools within a CrewBase class, use the get_mcp_tools method. Server configurations should be provided via the mcp_server_params attribute. You can pass either a single configuration or a list of multiple server configurations.
When a crew class is decorated with @CrewBase, the adapter lifecycle is managed for you:
  • The first call to get_mcp_tools() lazily creates a shared MCPServerAdapter that is reused by every agent in the crew.
  • The adapter automatically shuts down after .kickoff() completes thanks to an implicit after-kickoff hook injected by @CrewBase, so no manual cleanup is required.
  • If mcp_server_params is not defined, get_mcp_tools() simply returns an empty list, allowing the same code paths to run with or without MCP configured.
This makes it safe to call get_mcp_tools() from multiple agent methods or selectively enable MCP per environment.

Connection Timeout Configuration

You can configure the connection timeout for MCP servers by setting the mcp_connect_timeout class attribute. If no timeout is specified, it defaults to 30 seconds.

Filtering Tools

You can filter which tools are available to your agent by passing a list of tool names to the get_mcp_tools method.
The timeout configuration applies to all MCP tool calls within the crew:

Explore MCP Integrations

Simple DSL Integration

Recommended: Use the simple mcps=[] field syntax for effortless MCP integration.

Stdio Transport

Connect to local MCP servers via standard input/output. Ideal for scripts and local executables.

SSE Transport

Integrate with remote MCP servers using Server-Sent Events for real-time data streaming.

Streamable HTTP Transport

Utilize flexible Streamable HTTP for robust communication with remote MCP servers.

Connecting to Multiple Servers

Aggregate tools from several MCP servers simultaneously using a single adapter.

Security Considerations

Review important security best practices for MCP integration to keep your agents safe.
Checkout this repository for full demos and examples of MCP integration with CrewAI! 👇

GitHub Repository

CrewAI MCP Demo

Staying Safe with MCP

Always ensure that you trust an MCP Server before using it.

Security Warning: DNS Rebinding Attacks

SSE transports can be vulnerable to DNS rebinding attacks if not properly secured. To prevent this:
  1. Always validate Origin headers on incoming SSE connections to ensure they come from expected sources
  2. Avoid binding servers to all network interfaces (0.0.0.0) when running locally - bind only to localhost (127.0.0.1) instead
  3. Implement proper authentication for all SSE connections
Without these protections, attackers could use DNS rebinding to interact with local MCP servers from remote websites. For more details, see the Anthropic’s MCP Transport Security docs.

Limitations

  • Supported Primitives: Currently, MCPServerAdapter primarily supports adapting MCP tools. Other MCP primitives like prompts or resources are not directly integrated as CrewAI components through this adapter at this time.
  • Output Handling: The adapter typically processes the primary text output from an MCP tool (e.g., .content[0].text). Complex or multi-modal outputs might require custom handling if not fitting this pattern.