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.
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)
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 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.
Copy
Ask AI
from crewai import Agentfrom crewai_tools import MCPServerAdapterfrom mcp import StdioServerParameters # For Stdio Server# Example server_params (choose one based on your server type):# 1. Stdio Server:server_params=StdioServerParameters( command="python3", args=["servers/your_server.py"], env={"UV_PYTHON": "3.12", **os.environ},)# 2. SSE Server:server_params = { "url": "http://localhost:8000/sse", "transport": "sse"}# 3. Streamable HTTP Server:server_params = { "url": "http://localhost:8001/mcp", "transport": "streamable-http"}# Example usage (uncomment and adapt once server_params is set):with MCPServerAdapter(server_params) as mcp_tools: print(f"Available tools: {[tool.name for tool in mcp_tools]}") my_agent = Agent( role="MCP Tool User", goal="Utilize tools from an MCP server.", backstory="I can connect to MCP servers and use their tools.", tools=mcp_tools, # Pass the loaded tools to your agent reasoning=True, verbose=True ) # ... rest of your crew setup ...
This general pattern shows how to integrate tools. For specific examples tailored to each transport, refer to the detailed guides below.
Accessing a specific tool using dictionary-style indexing.
Copy
Ask AI
with MCPServerAdapter(server_params) as mcp_tools: print(f"Available tools: {[tool.name for tool in mcp_tools]}") my_agent = Agent( role="MCP Tool User", goal="Utilize tools from an MCP server.", backstory="I can connect to MCP servers and use their tools.", tools=[mcp_tools["tool_name"]], # Pass the loaded tools to your agent reasoning=True, verbose=True ) # ... rest of your crew setup ...
Pass a list of tool names to the MCPServerAdapter constructor.
Copy
Ask AI
with MCPServerAdapter(server_params, "tool_name") as mcp_tools: print(f"Available tools: {[tool.name for tool in mcp_tools]}") my_agent = Agent( role="MCP Tool User", goal="Utilize tools from an MCP server.", backstory="I can connect to MCP servers and use their tools.", tools=mcp_tools, # Pass the loaded tools to your agent reasoning=True, verbose=True ) # ... rest of your crew setup ...
To use MCPServer tools within a CrewBase class, use the 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.
Copy
Ask AI
@CrewBaseclass CrewWithMCP: # ... define your agents and tasks config file ... mcp_server_params = [ # Streamable HTTP Server { "url": "http://localhost:8001/mcp", "transport": "streamable-http" }, # SSE Server { "url": "http://localhost:8000/sse", "transport": "sse" }, # StdIO Server StdioServerParameters( command="python3", args=["servers/your_stdio_server.py"], env={"UV_PYTHON": "3.12", **os.environ}, ) ] @agent def your_agent(self): return Agent(config=self.agents_config["your_agent"], tools=self.get_mcp_tools()) # get all available tools # ... rest of your crew setup ...
You can filter which tools are available to your agent by passing a list of tool names to the get_mcp_tools method.
Copy
Ask AI
@agentdef another_agent(self): return Agent( config=self.agents_config["your_agent"], tools=self.get_mcp_tools("tool_1", "tool_2") # get specific tools )
Supported Primitives: Currently, MCPServerAdapter primarily supports adapting MCP tools.
Other MCP primitives like prompts or resources 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.
Assistant
Responses are generated using AI and may contain mistakes.