Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CrewAIInc CrewAI MCP Server Config

From Leeroopedia

Overview

Concrete configuration classes for connecting to MCP tool servers via stdio, HTTP, or SSE transport provided by the CrewAI framework.

Source Reference

  • Repository: crewAIInc/crewAI
  • File: lib/crewai/src/crewai/mcp/config.py
  • Lines: L12-124

Signatures

MCPServerStdio

class MCPServerStdio(BaseModel):
    command: str
    args: list[str] = []
    env: dict[str, str] | None = None
    tool_filter: ToolFilter | None = None
    cache_tools_list: bool = False

MCPServerHTTP

class MCPServerHTTP(BaseModel):
    url: str
    headers: dict[str, str] | None = None
    streamable: bool = True
    tool_filter: ToolFilter | None = None

MCPServerSSE

class MCPServerSSE(BaseModel):
    url: str
    headers: dict[str, str] | None = None
    tool_filter: ToolFilter | None = None

MCPServerConfig Type Alias

MCPServerConfig = MCPServerStdio | MCPServerHTTP | MCPServerSSE

Fields

MCPServerStdio Fields

Field Type Default Description
command str (required) The command to launch the MCP server process (e.g., "npx", "python")
args list[str] [] Command-line arguments passed to the server process
env None None Environment variables to set for the server process
tool_filter None None Optional filter to restrict which tools are exposed from this server
cache_tools_list bool False If True, cache the server's tool list to avoid re-querying on each connection

MCPServerHTTP Fields

Field Type Default Description
url str (required) The HTTP URL of the MCP server endpoint
headers None None Optional HTTP headers (e.g., for authentication)
streamable bool True Whether to use streamable HTTP transport
tool_filter None None Optional filter to restrict which tools are exposed from this server

MCPServerSSE Fields

Field Type Default Description
url str (required) The SSE endpoint URL of the MCP server
headers None None Optional HTTP headers (e.g., for authentication)
tool_filter None None Optional filter to restrict which tools are exposed from this server

Import

from crewai.mcp import MCPServerStdio, MCPServerHTTP, MCPServerSSE

How It Works

Each configuration class captures the information needed to establish a connection to an MCP server using a specific transport:

  • MCPServerStdio launches a local process using the specified command and args. Communication occurs over the process's standard input/output streams. This is commonly used with npx to launch Node.js-based MCP servers.
  • MCPServerHTTP connects to a remote HTTP endpoint. The streamable flag controls whether bidirectional streaming is used. Authentication headers can be provided.
  • MCPServerSSE connects to an SSE endpoint for real-time streaming responses. This is useful for servers that push incremental updates during long-running tool operations.

The MCPServerConfig type alias is a union of all three, used in agent configuration to accept any transport type.

The optional tool_filter field accepts a ToolFilter callable or configuration that restricts which of the server's tools are made available to the agent. This is important when an MCP server exposes many tools but only a subset is relevant.

Example

MCPServerStdio with npx Command

from crewai import Agent
from crewai.mcp import MCPServerStdio

# Configure a filesystem MCP server launched via npx
filesystem_server = MCPServerStdio(
    command="npx",
    args=["-y", "@modelcontextprotocol/server-filesystem", "/home/user/documents"],
    env={"NODE_ENV": "production"},
    cache_tools_list=True,
)

agent = Agent(
    role="Document Analyst",
    goal="Read and analyze documents from the filesystem",
    backstory="A meticulous analyst who reviews documents carefully.",
    mcp_servers=[filesystem_server],
)

MCPServerHTTP with Remote URL

from crewai import Agent
from crewai.mcp import MCPServerHTTP

# Configure a remote MCP server with authentication
api_server = MCPServerHTTP(
    url="https://mcp.example.com/tools",
    headers={"Authorization": "Bearer your-api-token"},
    streamable=True,
)

agent = Agent(
    role="API Integrator",
    goal="Interact with external services via MCP tools",
    backstory="A skilled integrator who connects to external APIs.",
    mcp_servers=[api_server],
)

MCPServerSSE for Streaming

from crewai.mcp import MCPServerSSE

# Configure an SSE-based MCP server
sse_server = MCPServerSSE(
    url="https://mcp-stream.example.com/sse",
    headers={"X-API-Key": "your-key"},
)

Combining Multiple MCP Servers

from crewai import Agent
from crewai.mcp import MCPServerStdio, MCPServerHTTP

agent = Agent(
    role="Full-Stack Agent",
    goal="Use both local and remote tool servers",
    backstory="An agent with access to diverse tool sources.",
    mcp_servers=[
        MCPServerStdio(
            command="npx",
            args=["-y", "@modelcontextprotocol/server-filesystem", "/data"],
        ),
        MCPServerHTTP(
            url="https://mcp.example.com/tools",
            headers={"Authorization": "Bearer token"},
        ),
    ],
)

Principle Link

Principle:CrewAIInc_CrewAI_MCP_Server_Connection

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment