CrewAI with MCP Tools: Multi-Agent Setup Guide

CrewAI with MCP Tools: Multi-Agent Setup Guide

CrewAI is a framework for orchestrating multiple AI agents that collaborate on complex tasks. By adding MCP (Model Context Protocol) tools, each agent gets access to standardized external capabilities. This guide shows you how to set it up.

What Is CrewAI?

CrewAI lets you define AI agents with specific roles, assign them tools, and have them work together. A “crew” might include a researcher agent that searches the web, an analyst agent that queries databases, and a writer agent that produces reports.

Step 1: Install CrewAI and MCP Dependencies

pip install crewai crewai-tools mcp

Install MCP servers:

npm install -g @modelcontextprotocol/server-filesystem
npm install -g @modelcontextprotocol/server-brave-search
pip install mcp-server-sqlite

Step 2: Create MCP Tool Wrappers for CrewAI

from crewai import Agent, Task, Crew
from crewai.tools import BaseTool
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

class MCPTool(BaseTool):
    name: str = "mcp_filesystem"
    description: str = "Read and write files using MCP filesystem server"

    def _run(self, query: str) -> str:
        # Bridge to MCP server
        import asyncio
        return asyncio.run(self._call_mcp(query))

    async def _call_mcp(self, query: str) -> str:
        server = StdioServerParameters(
            command="npx",
            args=["-y", "@modelcontextprotocol/server-filesystem", "/workspace"]
        )
        async with stdio_client(server) as (read, write):
            async with ClientSession(read, write) as session:
                await session.initialize()
                result = await session.call_tool("read_file", {"path": query})
                return str(result)

Step 3: Build a Multi-Agent Crew

filesystem_tool = MCPTool()

researcher = Agent(
    role="Data Researcher",
    goal="Find and analyze data from files",
    backstory="Expert at extracting insights from data files",
    tools=[filesystem_tool],
    verbose=True
)

writer = Agent(
    role="Report Writer",
    goal="Write clear, actionable reports",
    backstory="Technical writer who turns data into narratives",
    tools=[filesystem_tool],
    verbose=True
)

task1 = Task(
    description="Read the sales data from /workspace/sales.csv and identify top products",
    agent=researcher,
    expected_output="List of top 5 products by revenue"
)

task2 = Task(
    description="Write a summary report based on the research findings",
    agent=writer,
    expected_output="A formatted summary report"
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    verbose=True
)

result = crew.kickoff()
print(result)

Step 4: Use XLUXX for Reliable Tool Selection

Before adding MCP servers to production crews, verify their reliability:

pip install xluxx
# Check trust scores before installing
curl https://api.xluxx.net/v1/tools?q=search&sort=trust_score

# Get detailed security and maintenance info
curl https://api.xluxx.net/v1/tools/mcp-server-brave-search

XLUXX trust scores help you avoid abandoned or insecure MCP servers in production multi-agent systems where reliability matters most.

Assigning Different Tools to Different Agents

# Each agent gets only the tools relevant to its role
researcher = Agent(role="Researcher", tools=[web_search_mcp, filesystem_mcp])
analyst = Agent(role="Analyst", tools=[database_mcp, filesystem_mcp])
writer = Agent(role="Writer", tools=[filesystem_mcp])

Best Practices for MCP + CrewAI

  • Give each agent only the tools it needs (principle of least privilege)
  • Use XLUXX trust scores to vet every MCP server
  • Test tool calls individually before combining into crews
  • Set timeouts on MCP tool calls to prevent hanging agents
  • Log all tool interactions for debugging

Use XLUXX Trust Layer to find reliable MCP tools: api.xluxx.net

Related Articles


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *