AI Hints
Home›MCP Tutorial›Async, State and Databases
MCP · Lesson 19 of 120

Async Tools

Waiting without blocking.

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

Many tools wait on something slow, like a web API or a database. If a tool is async, the server can handle other requests while it waits, instead of freezing.

Async Tools
From the video: Async Tools
  • Tools often wait
  • Async tools
  • Others keep running

Two slow calls at once

Stock check is an async tool. It pretends to call a warehouse, which takes one second. We call it for two products at the same time, with gather. Run it. Both answers arrive in about one second, not two.

async_tools.pypython
import asyncio, time
from mcp import Client
from mcp.server.mcpserver import MCPServer

mcp = MCPServer("shop")

@mcp.tool()
async def stock_check(product: str) -> str:
    """Check warehouse stock for a product."""
    await asyncio.sleep(1)  # a slow warehouse API
    return f"{product}: 12 in stock"

async def main():
    async with Client(mcp) as client:
        start = time.perf_counter()
        results = await asyncio.gather(*[client.call_tool("stock_check", {"product": p}) for p in ["helmet", "lights"]])
        print([r.content[0].text for r in results], f"{time.perf_counter() - start:.1f}s")

asyncio.run(main())
Output
['helmet: 12 in stock', 'lights: 12 in stock'] 1.1s

Async tips

Use async def for tools that wait on networks or databases. Use async libraries inside them, like httpx, not requests. Plain def tools are fine for quick work, like maths.

keyvalue
Waiting on I/Oasync def
Insideasync libraries
Quick workplain def
Serverstays responsive

Summary

Async tools let the server keep working while a tool waits. Next, tools that keep state.

Key takeaways
  • async def
  • await
  • gather
Next up: Tools with State — a notes server