Principle:CrewAIInc CrewAI MCP Server Connection
Overview
A protocol integration pattern for connecting agents to external tool servers using the Model Context Protocol (MCP), enabling dynamic tool discovery and invocation across process boundaries.
Description
MCP Server Connection enables agents to use tools hosted on external servers via the standardized Model Context Protocol (MCP). Instead of tools being defined in-process as Python classes or functions, MCP allows tools to run in separate processes or on remote machines. The agent connects to configured MCP servers at startup, discovers available tools dynamically, and invokes them as if they were local tools.
The MCP protocol supports three transport mechanisms:
- stdio (Standard I/O): The MCP server runs as a local child process. Communication happens over the process's stdin/stdout streams. This is the most common transport for local tool servers, such as those launched via
npxor other CLI commands. - HTTP (Hypertext Transfer Protocol): The MCP server runs as a remote HTTP endpoint. Requests and responses are exchanged over HTTP, optionally using streamable HTTP for bidirectional communication. This is suitable for hosted tool services.
- SSE (Server-Sent Events): The MCP server uses an HTTP connection with Server-Sent Events for streaming responses. This provides real-time updates from long-running tool operations.
Connection Lifecycle
- Configuration: The developer specifies one or more MCP server configurations (transport type, connection details, optional filters).
- Connection: At agent startup, the framework connects to each configured MCP server.
- Discovery: The framework queries each server for its available tools, receiving tool names, descriptions, and argument schemas.
- Registration: Discovered tools are registered alongside local tools, making them available for LLM selection.
- Invocation: When the LLM selects an MCP tool, the framework serializes the arguments, sends them to the appropriate server, and returns the result.
- Cleanup: When the agent finishes, connections are closed gracefully.
Key Considerations
- Tool filtering: Not all tools from an MCP server may be relevant. Use tool filters to expose only the tools an agent should use.
- Latency: MCP tools involve inter-process or network communication, adding latency compared to local tools. Consider this when designing time-sensitive workflows.
- Error handling: Network failures, server crashes, and timeout conditions must be handled gracefully. The framework should surface meaningful error messages when MCP tool invocations fail.
- Security: HTTP-based MCP servers may require authentication via headers. Ensure credentials are managed securely and not hardcoded.
- Caching: For stdio servers that are slow to enumerate tools, the
cache_tools_listoption avoids re-querying the server's tool list on every connection.
Theoretical Basis
This principle is grounded in the Remote Procedure Call (RPC) pattern with service discovery. The MCP protocol standardizes tool interface advertisement and invocation across heterogeneous systems. This is analogous to how gRPC or REST APIs define service contracts, but MCP is specifically designed for the LLM tool-use paradigm, where tools must advertise typed schemas and natural language descriptions.
The standardization of the protocol enables interoperability: an MCP server built for one framework (e.g., a TypeScript tool server) can be consumed by agents in a different framework (e.g., CrewAI in Python), provided both implement the MCP specification.
Relationship to Implementation
Implementation:CrewAIInc_CrewAI_MCP_Server_Config
The MCPServerStdio, MCPServerHTTP, and MCPServerSSE configuration classes in CrewAI provide the concrete mechanism for specifying MCP server connections.
See Also
- Principle:CrewAIInc_CrewAI_Tool_Design -- The tool interface that MCP tools are mapped to
- Principle:CrewAIInc_CrewAI_Tool_Assignment -- How MCP servers are assigned to agents
- Principle:CrewAIInc_CrewAI_Tool_Execution_And_Monitoring -- How MCP tool invocations are monitored