Leveraging MCP in CrewAI
MCP (Model Context Protocol) lets your agents use external services (MCP servers) as if they were native tools, using a standard protocol. In CrewAI you can plug MCP in with a simple DSL on agents (recommended) or with an advanced adapter for complex setups.
1. Two Ways to Use MCP in CrewAI
CrewAI supports two main integration modes:
Simple DSL on agents (mcps=...)
- Easiest path, no extra library wiring.
- You declare MCP servers directly on the agent, CrewAI discovers tools automatically.
Advanced MCPServerAdapter (from crewai-tools)
- Gives you manual control over starting/stopping servers and selecting tools.
- Best for complex, multi-server or highly tuned production setups.
2. Simple DSL Integration (Recommended)
You attach MCP servers directly to an agent via the mcps field.
2.1 Quick start with string references
Use string URLs and AMP IDs when you just want to connect and go.
Patterns you can use:
<https://server/mcp>→ all tools from that MCP server.<https://server/mcp#get_tool>→ only one specific tool.- crewai-amp:service → all tools from an AMP service.
- crewai-amp:service#tool_name → one tool from that service.
CrewAI automatically:
- Connects to each server.
- Lists available tools.
- Exposes them to the agent’s tool-calling system.
2.2 Structured MCP configs on the agent
Use structured objects when you need control over transport, headers, environment, or filtering.
Key ideas to highlight:
- Stdio – local processes (python, node, npx) talk over stdin/stdout.
- HTTP / streamable HTTP – remote servers over HTTPS, optionally streaming.
- SSE – remote, real‑time, server‑sent events (ideal for live data).
- tool_filter – lets you whitelist or blacklist tools from a server.
- cache_tools_list – avoids re‑fetching tool schemas every run.
3. MCP Reference Patterns and Tool Filtering
3.1 Mixed references
You can mix strings and structured configs in one mcps list:
3.2 Static vs dynamic tool filters
Use static filters when you just want allow/block lists.
Use dynamic filters when you want role‑aware behavior.
4. Advanced Mode: MCPServerAdapter (crewai-tools)
When you need manual control (e.g., connect once, reuse tools across agents, inspect tools programmatically), use MCPServerAdapter from crewai-tools.
4.1 Basic adapter usage
You can also:
- Use SSE or streamable HTTP by passing appropriate server_params.
- Filter tools by name:
The context manager ensures connections are started and stopped cleanly.
5. Using MCP with CrewBase (YAML‑driven crews)
When you build crews with @CrewBase, you can declare MCP servers at the class level and fetch tools via get_mcp_tools().
This pattern is ideal for configurable, production crews where MCP endpoints are part of your infra configuration.
6. Reliability, Performance, and Security
Key behaviors:
-
Automatic tool discovery – CrewAI lists tools from servers and namespaces them to avoid collisions.
-
On‑demand connections – servers are contacted when needed; schema can be cached.
-
Graceful failure – if a server is down or slow:
- errors are logged as warnings,
- working servers’ tools remain available,
- default timeout ~30 seconds (configurable).
Security considerations (especially for SSE servers):
- Validate Origin headers to avoid DNS rebinding.
- Avoid binding local dev servers to 0.0.0.0; prefer 127.0.0.1.
- Always require authentication (headers, env vars, or query params).
7. Teaching Angle: MCP as “USB‑C for Tools”
As “Agentic Architect” think about it this way:
- Without MCP: each tool requires a bespoke Python wrapper.
- With MCP: tools from many systems conform to one standard protocol; CrewAI just plugs them into agents.
Guidelines:
- Start with local tools (crewai tools / custom @tool).
- Add MCP via mcps=[...] to reach external services (search, weather, DBs).
- Introduce tool filtering & timeouts for governance.
- Finish with MCPServerAdapter + CrewBase for large, multi‑server architectures.