MCP · Lesson 86 of 120
Testing Errors
The unhappy path.
Most bugs hide in the unhappy path. Missing records, wrong types, unknown tools. Test that each one returns a clear error, and never crashes the server.

- Unhappy path
- Clear errors
- No crashes
test_errors.py
A missing order should be an error, mentioning not found. A wrong type should be an error. And an unknown tool should be an error too.
test_errors.pypython
from northwind import mcp
from tester import call
def test_missing_order():
r = call(mcp, "order_status", {"order_id": 7})
assert r.is_error and "not found" in r.content[0].text
def test_wrong_type():
assert call(mcp, "order_status", {"order_id": "soon"}).is_error
def test_unknown_tool():
assert call(mcp, "track_parcel", {}).is_errorRunning the error tests
We run the error tests. Run it. All three pass, so every failure is handled cleanly.
run_tests.pypython
import pytest
pytest.main(["-q", "-p", "no:cacheprovider", "test_errors.py"])Output
... [100%] 3 passed in 0.47s
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_errors.py
from northwind import mcp
from tester import call
def test_missing_order():
r = call(mcp, "order_status", {"order_id": 7})
assert r.is_error and "not found" in r.content[0].text
def test_wrong_type():
assert call(mcp, "order_status", {"order_id": "soon"}).is_error
def test_unknown_tool():
assert call(mcp, "track_parcel", {}).is_errorSummary
Test the unhappy path, so every bad input gets a clear error. Next, debugging common failures.
Key takeaways- Missing data
- Wrong types
- Unknown tools
Next up: Debugging — common failures