AI Hints
Home›MCP Tutorial›Performance and Versioning
MCP · Lesson 88 of 120

Performance

Fast tools.

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

A slow tool slows the whole conversation. The model waits, and the user waits. The biggest win is usually doing slow things at the same time, instead of one after another.

Performance
From the video: Performance
  • Slow tools
  • Everyone waits
  • Do things together

Checking three warehouses

Each warehouse check takes about a third of a second. The first tool checks them one after another. The second uses gather, to check them all at once. Run it. The same answer, about three times faster.

fast_tools.pypython
import asyncio, time
from mcp.server.mcpserver import MCPServer
from tester import call
mcp = MCPServer("stock")
async def check(warehouse):
    await asyncio.sleep(0.3)  # a slow network call
    return 2

@mcp.tool()
async def stock_slow() -> int:
    return sum([await check(w) for w in ["Leeds", "York", "Hull"]])

@mcp.tool()
async def stock_fast() -> int:
    return sum(await asyncio.gather(*[check(w) for w in ["Leeds", "York", "Hull"]]))

for name in ["stock_slow", "stock_fast"]:
    t = time.time(); r = call(mcp, name).content[0].text
    print(f"{name}: {r} in {time.time() - t:.1f}s")
Output
stock_slow: 6 in 0.9s
stock_fast: 6 in 0.3s
Also uses tester.py
import asyncio
from mcp import Client

def call(server, tool, args=None, **options):
    async def go():
        async with Client(server, **options) as client:
            return await client.call_tool(tool, args or {})
    return asyncio.run(go())

def tools(server):
    async def go():
        async with Client(server) as client:
            return (await client.list_tools()).tools
    return asyncio.run(go())

Other speed tips

Open connections once, in the lifespan. Cache answers that rarely change. Return only what the model needs, not huge pages of data. And give slow jobs progress updates.

keyvalue
Connectionsopen once
Rare changescache them
Resultssmall
Slow jobsreport progress

Summary

Run slow steps at the same time with gather, open connections once, and keep results small. Next, versioning your server.

Key takeaways
  • asyncio.gather
  • Lifespan
  • Small results
Next up: Versioning Your Server — changing tools safely