MCP Servers as Tools in CrewAI
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.
The crewai-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. For now, we support Standard Input/Output (Stdio) and Server-Sent Events (SSE) transport mechanisms.
We will also be integrating Streamable HTTP transport in the near future. Streamable HTTP is designed for efficient, bi-directional communication over a single HTTP connection.
Installation
Before you start using MCP with crewai-tools
, you need to install the mcp
extra crewai-tools
dependency with the following command:
Integrating MCP Tools with MCPServerAdapter
The MCPServerAdapter
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, primarily Stdio (for local servers) and SSE (Server-Sent Events).You have two main options for managing the connection lifecycle:
Option 1: Fully Managed Connection (Recommended)
Using a Python context manager (with
statement) is the recommended approach. It automatically handles starting and stopping the connection to the MCP server.
For a local Stdio-based MCP server:
For a remote SSE-based MCP server:
Option 2: More control over the MCP server connection lifecycle
If you need finer-grained control over the MCP server connection lifecycle, you can instantiate MCPServerAdapter
directly and manage its start()
and stop()
methods.
You MUST call mcp_server_adapter.stop()
to ensure the connection is closed and resources are released. Using a try...finally
block is highly recommended.
Stdio Transport Example (Manual)
SSE Transport Example (Manual)
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
Without these protections, attackers could use DNS rebinding to interact with local MCP servers from remote websites.
For more details, see the MCP Transport Security documentation.
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.