AI Hints
Home›MCP Tutorial›Servers and Models
MCP · Lesson 55 of 120

Several Servers

One client each.

⏱ 2 min read▶ Video at 48:45🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

A host often connects to several servers at once. Northwind's assistant might use an orders server and a docs server. Each server gets its own client, and the host merges their tools.

Several Servers
From the video: Several Servers
  • Several servers
  • One client each
  • Merge the tools

Two servers, one tool list

Async exit stack keeps both clients open together. We connect to each server, and record which client owns each tool. Then a call goes to the right server. Run it. Tools from both servers, and a call routed to the docs server.

two_servers.pypython
import asyncio
from contextlib import AsyncExitStack
from mcp import Client
from northwind import mcp as orders
from docs_server import mcp as docs

async def main():
    async with AsyncExitStack() as stack:
        owner = {}
        for server in [orders, docs]:
            client = await stack.enter_async_context(Client(server))
            for tool in (await client.list_tools()).tools:
                owner[tool.name] = client
        print("tools:", list(owner))
        result = await owner["read_doc"].call_tool("read_doc", {"name": "stores.md"})
        print(result.content[0].text.splitlines()[2])

asyncio.run(main())
Output
tools: ['order_status', 'delivery_cost', 'read_doc']
Northwind Bikes has three stores.
Also uses northwind.py
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

mcp = MCPServer("northwind")
ORDERS = {1042: "shipped", 1043: "packing", 1044: "delivered"}

@mcp.tool()
def order_status(order_id: int) -> str:
    """Look up the status of a Northwind order by its number."""
    if order_id not in ORDERS:
        raise ToolError(f"Order {order_id} not found")
    return ORDERS[order_id]

@mcp.tool()
def delivery_cost(order_total: float) -> str:
    """UK delivery cost for an order total in pounds."""
    return "free" if order_total >= 50 else "£4.99"

@mcp.resource("policy://returns")
def returns_policy() -> str:
    """Northwind's returns policy."""
    return "You can return any unused item within 30 days of delivery."

@mcp.prompt()
def support_reply(question: str) -> str:
    """Draft a polite reply to a customer."""
    return f"Write a short, polite reply to this Northwind customer: {question}"

if __name__ == "__main__":
    mcp.run()
Also uses docs_server.py
from pathlib import Path
from mcp.server.mcpserver import MCPServer

DOCS = Path("data/rag/northwind").resolve()
mcp = MCPServer("docs")

@mcp.tool()
def read_doc(name: str) -> str:
    """Read one Northwind help page, like stores.md."""
    path = (DOCS / name).resolve()
    if not path.is_relative_to(DOCS):
        raise ValueError("outside the docs folder")
    return path.read_text()

Name clashes

Two servers might both have a tool called search. Hosts usually add the server's name as a prefix, like docs search and orders search. Keep a map from each tool name to its client.

keyvalue
Clashsame tool name
Fixprefix with server
Maptool → client
One clientper server

Summary

Use one client per server, and a map from tool names to clients. Next, giving MCP tools to a model.

Key takeaways
  • AsyncExitStack
  • Tool → client map
  • Prefix names
Next up: MCP Tools for an LLM — converting schemas