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:🚀 Simple DSL Integration (Recommended)
Use themcps 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: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, thecrewai-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 themcp library:
Quick Start: Simple DSL Integration
The easiest way to integrate MCP servers is using themcps field on your agents. You can use either string references or structured configurations.
Quick Start with String References
Quick Start with Structured Configurations
MCP Reference Formats
Themcps 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
CrewAI AMP Marketplace
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 processtool_filter(optional): Tool filter function for filtering available toolscache_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 purposesstreamable(optional): Whether to use streamable HTTP transport (default:True)tool_filter(optional): Tool filter function for filtering available toolscache_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 purposestool_filter(optional): Tool filter function for filtering available toolscache_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: WhenTrue, 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:- 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 theMCPServerAdapter 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
TheMCPServerAdapter 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.
Filtering Tools
There are two ways to filter tools:- Accessing a specific tool using dictionary-style indexing.
- Pass a list of tool names to the
MCPServerAdapterconstructor.
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 theget_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 sharedMCPServerAdapterthat 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_paramsis not defined,get_mcp_tools()simply returns an empty list, allowing the same code paths to run with or without MCP configured.
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 themcp_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 theget_mcp_tools method.
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.
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:- Always validate Origin headers on incoming SSE connections to ensure they come from expected sources
- Avoid binding servers to all network interfaces (0.0.0.0) when running locally - bind only to localhost (127.0.0.1) instead
- Implement proper authentication for all SSE connections
Limitations
- Supported Primitives: Currently,
MCPServerAdapterprimarily supports adapting MCPtools. Other MCP primitives likepromptsorresourcesare 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.
