AI Hints
Home›MCP Tutorial›Testing and Debugging
MCP · Lesson 85 of 120

Testing a Server

Pytest in-process.

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

Clicking around in the Inspector is great, but it isn't repeatable. Automated tests check every tool, every time you change the server. And with an in-process client, they're fast.

Testing a Server
From the video: Testing a Server
  • Manual checks
  • Automated tests
  • Fast, in-process

test_northwind.py

Each test is a function starting with test. The first checks the tool list. The second checks a known order. The third checks the free delivery boundary, exactly fifty pounds.

test_northwind.pypython
from northwind import mcp
from tester import call, tools

def test_lists_tools():
    assert {t.name for t in tools(mcp)} == {"order_status", "delivery_cost"}

def test_known_order():
    assert call(mcp, "order_status", {"order_id": 1042}).content[0].text == "shipped"

def test_free_delivery_at_50():
    assert call(mcp, "delivery_cost", {"order_total": 50}).content[0].text == "free"

Running the tests

Pytest finds the test functions, and runs them. Run it. Three dots, one per test, and all three pass.

run_tests.pypython
import pytest
pytest.main(["-q", "-p", "no:cacheprovider", "test_northwind.py"])
Output
...                                                                      [100%]
3 passed in 0.58s
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 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())
Also uses test_northwind.py
from northwind import mcp
from tester import call, tools

def test_lists_tools():
    assert {t.name for t in tools(mcp)} == {"order_status", "delivery_cost"}

def test_known_order():
    assert call(mcp, "order_status", {"order_id": 1042}).content[0].text == "shipped"

def test_free_delivery_at_50():
    assert call(mcp, "delivery_cost", {"order_total": 50}).content[0].text == "free"

Summary

Test servers with pytest and an in-process client, so every change is checked. Next, testing errors.

Key takeaways
  • pytest
  • In-process
  • Boundaries
Next up: Testing Errors — the unhappy path