Skip to content

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.py and their tests in test_server.py.
  • Host changes: host.py and test_host.py. Run both with pytest -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.

  1. In catalog.py, add list_quotes(customer_id) and cancel_quote(customer_id, quote_id). A quote's status can now be "cancelled".
  2. In server.py, add two tools: list_my_quotes (read) and cancel_quote (write). Neither takes a customer argument.
  3. Cancelling another customer's quote returns the same error as an unknown quote, so it doesn't reveal that the quote exists.
  4. An ordered quote can't be cancelled, and place_order refuses a cancelled quote.
  5. Decide: is cancel_quote destructive (destructive_hint)? Which of the two host policies in host.py asks a human for it then?
  6. Add tests to test_server.py for 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.

  1. Add an optional audit parameter to call_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.
  2. Add jsonl_audit(path), which returns such a function that appends one JSON line per call.
  3. No audit: works exactly as today.
  4. A failing audit writer never breaks the call: print a warning to stderr and carry on.
  5. Decide: which arguments to mask in the log (an RFQ's free text can hold names and phone numbers), and how.
  6. 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.

  1. Add fingerprint(hub) -> dict[str, str] to host.py: a SHA-256 per tool name, of its description plus input schema.
  2. Add check_pins(hub, pins) -> list[str]: tools added, removed or changed since the pins were taken.
  3. connect() gets two optional parameters: allow (a set of tool names; any other tool never reaches the model) and pins.
  4. Decide: a changed fingerprint at connect time: refuse to connect, or drop only that tool and warn?
  5. Prove it works: take pins, change one description in server.py, connect again from a small script in my-work/, see it caught, then undo the change.
  6. 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/build passes 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.