Practice: MCP servers¶
Three exercises that build on the labs, then one open build task. Do them in your own copy (a fork) of this repo, commit as you go, and keep short notes.
Read the labs first: b2b_catalog/server.py, b2b_catalog/catalog.py, host.py, 02_call_tools_directly.py, 05_approval_policies.py.
Where your work goes:
- Server changes:
b2b_catalog/catalog.py,b2b_catalog/server.pyand their tests intest_server.py. - Host changes:
host.pyandtest_host.py. Run both withpytest -q. - Your own files:
my-work/in this folder. - Notes:
my-work/mcp-server.md. For each exercise, a few lines on what you changed, why, and what you would do differently in production.
Some rules below say Decide. Make the choice yourself and write it in your notes.
If your team gave you an ASSIGNMENTS.md, it makes those choices for you: follow it.
ex1: List and cancel quotes¶
Why: every write needs its undo, and a buyer needs to see their quotes before cancelling one.
- In
catalog.py, addlist_quotes(customer_id)andcancel_quote(customer_id, quote_id). A quote'sstatuscan now be"cancelled". - In
server.py, add two tools:list_my_quotes(read) andcancel_quote(write). Neither takes a customer argument. - Cancelling another customer's quote returns the same error as an unknown quote, so it doesn't reveal that the quote exists.
- An ordered quote can't be cancelled, and
place_orderrefuses a cancelled quote. - Decide: is
cancel_quotedestructive (destructive_hint)? Which of the two host policies inhost.pyasks a human for it then? - Add tests to
test_server.pyfor each rule, including identity: C-101 never sees C-102's quotes.
Done when pytest -q passes and python 01_list_capabilities.py shows the new tools.
ex2: An audit log in the host¶
Why: AI-initiated actions need an audit trail, and the host is the one place every call passes through.
- Add an optional
auditparameter tocall_tool(): a function that receives one dict per call with the time, tool name, arguments, approval (approved, declined, or not needed), status (ok,error,declined,unknown_tool,transport) and milliseconds. - Add
jsonl_audit(path), which returns such a function that appends one JSON line per call. - No
audit: works exactly as today. - A failing audit writer never breaks the call: print a warning to stderr and carry on.
- Decide: which arguments to mask in the log (an RFQ's free text can hold names and phone numbers), and how.
- Add model-free tests to
test_host.py.
Done when pytest -q passes and your notes show three audit lines from a run of 05_approval_policies.py.
ex3: Pin and allow-list tools¶
Why: a server can change its tool descriptions after you approved it (a "rug pull"), and the model reads descriptions as instructions.
- Add
fingerprint(hub) -> dict[str, str]tohost.py: a SHA-256 per tool name, of its description plus input schema. - Add
check_pins(hub, pins) -> list[str]: tools added, removed or changed since the pins were taken. connect()gets two optional parameters:allow(a set of tool names; any other tool never reaches the model) andpins.- Decide: a changed fingerprint at connect time: refuse to connect, or drop only that tool and warn?
- Prove it works: take pins, change one description in
server.py, connect again from a small script inmy-work/, see it caught, then undo the change. - Add model-free tests to
test_host.py.
Done when pytest -q passes and your notes say what the host did with the changed tool.
Names these exercises build on¶
Please keep these names as they are. In host.py: connect, Hub, call_tool, run_mcp_agent, to_tool_spec, result_to_text, is_read_only, is_destructive, ask_for_writes, ask_for_destructive, catalog_server. In b2b_catalog/: create_server and the tool names. Checks and later topics rely on them.
Build it yourself: a delivery tracking server¶
No starter code and no steps this time: a short brief, like one you'd get at work. Plan for half a day.
Your code goes in my-work/build/; follow docs_server.py for a Python server and host.py to connect it. Notes go in a "Build" section of my-work/mcp-server.md.
The brief. The logistics team owns shipments. They want to publish them once, as an MCP server, so the buyer assistant today and the support team's assistant later can both answer "where is my order?".
It must:
- Be a Python MCP server with its own fake data: shipments per order with a status history, carrier and ETA.
- Offer tools to track one shipment, list the signed-in customer's shipments, and report a delivery issue (a write).
- Publish one resource (delivery times by city) and one prompt (a delay apology for an order).
- Bind the customer at launch, never from a tool argument. Another customer's order looks unknown.
- Annotate every tool, and return business failures as errors the model can read.
- Run next to the catalog server with
connect(catalog=..., delivery=...), so one question can need both ("ORD-0001 kab aayega, and is there stock to repeat it?").
Decide: does reporting an issue need approval? What does the server say when the carrier has given no ETA?
Done when:
pytest -q my-work/buildpasses with model-free tests through an MCP client: listing, calls, identity and errors.- The server works in MCP Inspector.
- A demo asks five questions on qwen3:8b through the host, and your notes say which tools the model picked for each.
Or bring your own problem. Build the same pattern on a problem from your own work instead. Write a five-line brief first (who uses it, what goes in, what comes out, what must never go wrong, how you'll measure it), then meet the same "Done when" list. Use synthetic data only, never real customer data.