Skip to content

Connecting Ecosystems: MCP Integration

The AI landscape is shifting from isolated models to interconnected ecosystems. The Model Context Protocol (MCP) has emerged as the standard for this connectivity, acting like a ‘USB-C port’ for AI applications. In this chapter, we will explore how to supercharge your CrewAI agents by connecting them to external MCP servers.

MCP is an open standard that enables AI models to interact with external data and tools in a standardized way. Instead of writing custom integration code for every database, file system, or API, you connect to an ‘MCP Server’ that exposes these resources as ready-to-use tools.

CrewAI treats MCP tools just like any other tool (like SerperDevTool or FileReadTool). This means you can instantly give your agents capabilities provided by the wider MCP community.

To bridge CrewAI with an MCP server, we use the crewai_tools package. The integration abstracts the complexity of the handshake and protocol negotiation, presenting the server’s capabilities as a list of CrewAI-compatible tools.

MCP supports different transport mechanisms depending on where your server is running. CrewAI supports the three primary methods:

  • Stdio (Standard Input/Output): Best for running local servers directly as subprocesses.
  • SSE (Server-Sent Events): Useful for real-time, unidirectional server communication.
  • HTTP: The standard for connecting to remote web services.

This is the most common setup for local development. You run the MCP server as a command, and CrewAI communicates with it via standard input and output. Here is how you connect to a local file system server:

from crewai import Agent, Task, Crew
from crewai_tools import from_mcp
# Import the Stdio transport class
from mcp import ClientSession, StdioServerParameters
# define the connection parameters
# In this example, we assume you have an MCP server executable
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/folder"]
)
# Create the tool list from the MCP server
mcp_tools = from_mcp(server_params)
# Assign these tools to an agent
researcher = Agent(
role="Researcher",
goal="Analyze files in the directory",
backstory="You are an expert at data analysis.",
tools=mcp_tools,
verbose=True
)

If your MCP server is running on a remote machine or a distinct local port, you can connect via URL. This is ideal for distributed architectures.

# Connecting to a remote MCP server via HTTP
remote_mcp_tools = from_mcp("http://localhost:8080/mcp")
agent = Agent(
role="Remote Analyst",
goal="Query the remote database",
backstory="...",
tools=remote_mcp_tools
)

Agents are not limited to a single source of truth. You can aggregate tools from multiple MCP servers and combine them with native CrewAI tools. Imagine an agent that can search the web (native tool), read local files (MCP Stdio), and query a remote database (MCP HTTP).

from crewai_tools import SerperDevTool
search_tool = SerperDevTool()
filesystem_tools = from_mcp(fs_params) # Defined previously
database_tools = from_mcp("http://db-mcp:8000")
# Combine all tools into a single list
agent_tools = [search_tool] + filesystem_tools + database_tools
super_agent = Agent(
role="Super Connected Agent",
goal="Correlate web data with internal records",
backstory="You have access to everywhere.",
tools=agent_tools
)

When integrating MCP, remember that you are effectively giving the LLM executable access to the connected systems. Ensure your MCP servers are configured with the Principle of Least Privilege—only expose the specific directories or database tables necessary for the task.