MCP servers and clients · Chapter 10 of 16

Errors and validation

By the end of this chapter you can tell the two kinds of failure in MCP apart, validate a tool's input twice (in the schema and in the tool, by the policy), write messages a model can act on without giving away how your server works, and make a tool that changes data fail safely. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Clients and production
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of MCP servers and clients are free. This one comes with the all labs plan.

Why you need this

Rajat wants the assistant to draft purchase orders and cancel them, not just read stock. Nisha has read chapter 5's tools and agrees, on three conditions. Bad input is refused with a message the model can fix. The store policy is enforced in the tool, because a model will cheerfully ask to cancel a delivered order. And a change that fails half-way changes nothing.

Until now a tool failing meant a wrong answer. A tool that changes data and fails badly means a purchase order that exists twice, or a cancel that went through when the policy said no.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    orders.py: a draft order, checked twice

    In orders.py, an MCPServer (any name), copy /workspace/data/bazaar.db to /tmp/bazaar-orders.db when the file starts, and make every change in the copy. Write create_draft_order(store_id, product_id, cases):

    in the schema, store_id has the pattern ^BB\d{2}$, product_id ^P\d{3}$, and cases is an int from 1 to 50;

    a ToolError that names the id for an unknown store or product; one that names the existing draft when the store already has a draft for that product; one that gives the supplier's minimum order value and the number of cases that would reach it (like order at least 3 cases) when the total is below it.

    The order goes to the product's supplier: quantity is cases times case_size, unit_cost the selling price times 0.8 rounded to 2 decimals, total unit_cost times quantity rounded to 2 decimals, the next id (PO-10361 on a fresh copy), status draft, ordered_on 2026-10-22 and expected_on that day plus the supplier's lead days. Insert it and return a DraftOrder with order_id, store_id, product_id, supplier_id, quantity, unit_cost, total, status and expected_on.

  2. Task 2

    orders.py: cancel by the policy

    Add cancel_order(order_id), with the pattern ^PO-\d{5}$ in its schema. Policy 6.3: a draft or sent order is cancelled; a delivered one is refused with a ToolError naming the order and rule 6.3; one already cancelled, or an unknown one, is refused with a ToolError naming it.

    Change the copy in a transaction, so that a refused or failed cancel changes nothing, and return order_id, status (cancelled) and was (the status before).

  3. Task 3

    classify.py: protocol error or tool error?

    In classify.py, write async def outcome(client, call): call is ("tool", name, arguments), ("resource", uri) or ("prompt", name, arguments). Make that request and return success, tool error (a result with is_error) or protocol error (an MCPError was raised).

    When run, connect to desk_server.py and print one line per call in the starter, like 1. tool stock_level: success (the number, the kind, the name or URI, the outcome).

  4. Task 4 · interview question

    Interview: leaky.py, failures that give too much away

    leaky.py's failures show the database path, its SQL and a traceback, one failure comes back as a success, and one tool pastes its argument into the SQL. Fix it, changing as little as you can:

    a failed tool returns an error result whose message names the bad value and shows what a good id looks like, with no path, SQL, traceback or the word sqlite;

    a supplier that does not exist is not found (-32602), with a message naming it and nothing internal;

    no value is pasted into SQL.

    Keep the tools and the resource, and what they return when they work.

What you will learn

  • Protocol errors and tool errors: which is which, and who handles each.
  • Validation in the schema: patterns and bounds, checked before your code.
  • Validation in the tool: business rules from the policy.
  • Messages that help a retry and never leak internals.
  • Failing safe: a transaction around every change.

More in Clients and production

Build "Errors and validation" yourself

Bazaar Bhai, a chain of 40 kirana stores in Jaipur, exposes its stock and orders once, to every AI tool. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of MCP servers and clients are free. This one comes with the all labs plan.