Programmatic Usage(Python)
MCP with CrewAI
Use MCP tools inside CrewAI agents
Overview
CrewAI has native MCP support via MCPServerAdapter from crewai-tools,
which converts MCP tools into CrewAI tools automatically.
Setup
pip install crewai crewai-tools mcpStep 1: Create the MCP Server Adapter
from crewai_tools import MCPServerAdapter
server_params = {
"url": "https://api.mcphero.app/mcp/{server_id}/mcp",
"transport": "streamable-http",
"headers": {
"Authorization": "Bearer <your-api-key>"
},
}Step 2: Create Agent, Task, and Crew
from crewai import Agent, Task, Crew
with MCPServerAdapter(server_params) as mcp_tools:
agent = Agent(
role="Data Assistant",
goal="Retrieve and summarize customer data",
backstory="I can connect to MCP servers and use their tools.",
tools=mcp_tools,
verbose=True,
)
task = Task(
description="Find customer John Smith and return their last order",
expected_output="A summary of the customer's most recent order",
agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])Step 3: Run the Crew
result = crew.kickoff()
print(result)Why CrewAI
- Role-based agents with goals and backstories
- Task orchestration with expected outputs
- Automatic MCP tool lifecycle management via
withblock