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:
๐ง 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:
MCP Reference Formats
Themcps field supports various reference formats for maximum flexibility:
External MCP Servers
CrewAI AMP Marketplace
Mixed References
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
Error Handling
The MCP DSL integration is designed to be resilient: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.
