Implementation:CrewAIInc CrewAI MCP Server Adapter
| Knowledge Sources | |
|---|---|
| Domains | MCP, Tool_Integration, Protocol_Adapter |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete adapter for integrating Model Context Protocol (MCP) servers with CrewAI provided by CrewAI.
Description
The MCPServerAdapter class manages the full lifecycle of an MCP server connection and makes its tools available to CrewAI agents. It supports both STDIO (local) and SSE (remote) server connections with configurable connection timeouts. Internally, it uses the CrewAIToolAdapter class (implementing the ToolAdapter interface from mcpadapt) to convert MCP tool definitions into CrewAI BaseTool instances. The adapter sanitizes tool names, converts MCP input schemas to Pydantic models using CrewAI's own schema utilities, and extracts text content from CallToolResult responses. Tool access is provided through a ToolCollection wrapper that supports both name-based and index-based access, with optional filtering by tool names. The adapter supports context manager usage for automatic cleanup and includes interactive prompting to install missing MCP dependencies.
Usage
Import and use MCPServerAdapter when you need to connect CrewAI agents to MCP-compatible tool servers. Use it as a context manager or manage the lifecycle manually with start() and stop() methods. This adapter opens CrewAI to the entire MCP ecosystem of community-built tools.
Code Reference
Source Location
- Repository: CrewAI
- File: lib/crewai-tools/src/crewai_tools/adapters/mcp_adapter.py
- Lines: 1-235
Signature
class MCPServerAdapter:
def __init__(
self,
serverparams: StdioServerParameters | dict[str, Any],
*tool_names: str,
connect_timeout: int = 30,
) -> None:
Import
from crewai_tools import MCPServerAdapter
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| serverparams | dict[str, Any] | Yes | MCP server parameters; StdioServerParameters for STDIO servers or dict with "url" key for SSE servers |
| *tool_names | str (variadic) | No | Optional tool names to filter; only tools with matching names will be available |
| connect_timeout | int | No | Connection timeout in seconds to the MCP server (default: 30) |
Outputs
| Name | Type | Description |
|---|---|---|
| tools (property) | ToolCollection[BaseTool] | Collection of CrewAI tools adapted from MCP server, accessible by name or index |
| __enter__() return | ToolCollection[BaseTool] | Same as tools property, for context manager usage |
Usage Examples
Context Manager with STDIO
from mcp import StdioServerParameters
from crewai_tools import MCPServerAdapter
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"],
)
with MCPServerAdapter(server_params) as tools:
# tools is a ToolCollection of CrewAI BaseTool instances
agent = Agent(role="researcher", tools=tools)
SSE Connection with Tool Filtering
from crewai_tools import MCPServerAdapter
with MCPServerAdapter(
{"url": "http://localhost:8000/sse"},
"search", "read_file",
connect_timeout=60,
) as filtered_tools:
# Only "search" and "read_file" tools are available
agent = Agent(role="researcher", tools=filtered_tools)
Manual Lifecycle Management
from crewai_tools import MCPServerAdapter
mcp_server = MCPServerAdapter(server_params)
try:
tools = mcp_server.tools
# Use tools with crew
finally:
mcp_server.stop()
Related Pages
- Principle needed: MCP integration principle for CrewAI adapter pattern