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. Thecrewai-tools
library extends CrewAI’s capabilities by allowing you to seamlessly integrate tools from these MCP servers into your agents.
This gives your crews access to a vast ecosystem of functionalities.
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 HTTP: for remote servers (flexible, potentially bi-directional communication over HTTP, often utilizing SSE for server-to-client streams)
Video Tutorial
Watch this video tutorial for a comprehensive guide on MCP integration with CrewAI:Installation
Before you start using MCP withcrewai-tools
, you need to install the mcp
extra crewai-tools
dependency with the following command:
Key Concepts & Getting Started
TheMCPServerAdapter
class from crewai-tools
is the primary way to connect to an MCP server and make its tools available to your CrewAI agents. It supports different transport mechanisms and simplifies connection management.
Using a Python context manager (with
statement) is the recommended approach for MCPServerAdapter
. 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
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 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.
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
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,
MCPServerAdapter
primarily supports adapting MCPtools
. Other MCP primitives likeprompts
orresources
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.