Multiple MCP Servers

Connect an Agent to multiple MCP servers with one MCPTools instance per server.

Create one MCPTools instance per server and pass every instance to the agent.

Prerequisites

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install the Python dependencies and Node.js, then verify the runtimes:

uv pip install -U "agno[mcp]" openai
node --version
npx --version

Export the keys used by the examples you run:

export OPENAI_API_KEY="your_openai_api_key_here"
export GOOGLE_MAPS_API_KEY="your_google_maps_api_key_here"
export BRAVE_API_KEY="your_brave_api_key_here"

The @modelcontextprotocol/server-google-maps and @modelcontextprotocol/server-brave-search npm packages are deprecated and no longer supported. The snippets below document these legacy servers. Use maintained MCP servers for new projects.

Connect multiple servers

multiple_mcp_servers.py
import asyncio
import os
from datetime import date, timedelta

from agno.agent import Agent
from agno.tools.mcp import MCPTools


async def run_agent(message: str) -> None:
    """Run the Airbnb and Google Maps agent with the given message."""

    env = {
        **os.environ,
        "GOOGLE_MAPS_API_KEY": os.getenv("GOOGLE_MAPS_API_KEY"),
    }

    async with (
        MCPTools(command="npx -y @openbnb/mcp-server-airbnb --ignore-robots-txt") as airbnb_tools,
        MCPTools(command="npx -y @modelcontextprotocol/server-google-maps", env=env) as google_maps_tools,
    ):
        agent = Agent(
            tools=[airbnb_tools, google_maps_tools],
            markdown=True,
        )
        await agent.aprint_response(message, stream=True)


# Example usage
if __name__ == "__main__":
    check_in = date.today() + timedelta(days=30)
    check_out = check_in + timedelta(days=3)
    # Pull request example
    asyncio.run(
        run_agent(
            f"What listings are available in Cape Town for 2 people "
            f"from {check_in.isoformat()} to {check_out.isoformat()}?"
        )
    )

Avoiding tool name collisions

When using multiple MCP servers, you may encounter tool name collisions. This often happens when the same tool is available in multiple of the servers you are using.

To avoid this, you can use the tool_name_prefix parameter. This will add the given prefix to all tool names coming from the MCPTools instance.

import asyncio

from agno.agent import Agent
from agno.tools.mcp import MCPTools


async def run_agent():
    async with MCPTools(
        transport="streamable-http",
        url="https://docs.agno.com/mcp",
        tool_name_prefix="dev",
    ) as dev_tools:
        agent = Agent(tools=[dev_tools])
        await agent.aprint_response("Which tools do you have access to? List them all.")


if __name__ == "__main__":
    asyncio.run(run_agent())