AI Hints
Home›MCP Tutorial›Tool Design and Practice
MCP · Lesson 29 of 120

Practice: An Orders Server

Three tools.

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

Let's build a small orders server, with three focused tools. Look up an order, list a customer's orders, and cancel an order.

Practice: An Orders Server
From the video: Practice: An Orders Server
  • Three tools
  • Focused
  • Typed

The orders server

Each tool does one job, with typed inputs and a short docstring. Cancel is marked destructive, so a host can ask first. The data is a small dictionary, standing in for a database.

orders_server.pypython
from mcp.server.mcpserver import MCPServer
from mcp_types import ToolAnnotations
from tester import call, tools

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

@mcp.tool()
def get_order(order_id: int) -> str:
    """Status of one order, by its receipt number."""
    return ORDERS[order_id][1]
@mcp.tool()
def list_orders(customer: str) -> list[int]:
    """All order numbers for a customer."""
    return [n for n, (c, _) in ORDERS.items() if c == customer]
@mcp.tool(annotations=ToolAnnotations(destructive_hint=True))
def cancel_order(order_id: int) -> str:
    """Cancel an order that hasn't shipped."""
    return "cancelled"

Trying it out

We import the server, and list its tools. Then we list Sam's orders, and look up the first one. Run it. Three tools are listed, Sam has two orders, and the first has shipped.

try_orders.pypython
from orders_server import mcp
from tester import call, tools

print("tools:", [t.name for t in tools(mcp)])
orders = call(mcp, "list_orders", {"customer": "sam"}).structured_content["result"]
print("sam's orders:", orders)
print("first:", call(mcp, "get_order", {"order_id": orders[0]}).content[0].text)
Output
tools: ['get_order', 'list_orders', 'cancel_order']
sam's orders: [1042, 1043]
first: shipped
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 orders_server.py
from mcp.server.mcpserver import MCPServer
from mcp_types import ToolAnnotations

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

@mcp.tool()
def get_order(order_id: int) -> str:
    """Status of one order, by its receipt number."""
    return ORDERS[order_id][1]
@mcp.tool()
def list_orders(customer: str) -> list[int]:
    """All order numbers for a customer."""
    return [n for n, (c, _) in ORDERS.items() if c == customer]
@mcp.tool(annotations=ToolAnnotations(destructive_hint=True))
def cancel_order(order_id: int) -> str:
    """Cancel an order that hasn't shipped."""
    return "cancelled"

Summary

Small, focused, typed tools make a server easy to use. Next, the tools quiz.

Key takeaways
  • One job each
  • Typed inputs
  • Annotations
Next up: Tools Quiz — test yourself