MCP servers and clients · Chapter 11 of 16
Progress and logging
By the end of this chapter you can make a slow tool say how far it has got, log what a server does in the two ways MCP knows, and write a client that stops waiting for a tool that takes too long, telling the server to stop too. 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
- 5 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
Every tool so far answered in a blink. Real ones do not. Rajat's sales report adds up two weeks of sales, one day at a time, and in his assistant it shows a spinner and nothing else. After ten seconds he asks whether it is stuck. Gopal's morning script asked for a full shelf audit, which a store manager does by hand, and sat there for twenty minutes before someone killed it. And when something odd happens, Simran wants the server to have written down what it did.
A tool call is one request and one response, with silence in between. This chapter fills the silence: progress while the work goes on, logs of what happened, and a way for the client to give up.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
report_server.py: a report that says how far it has got
In report_server.py, an MCPServer (any name) with one async tool, sales_report(store_id, days): store_id a string, days an int from 1 to 14 in the schema, and a Context parameter.
It takes the last days dates of the sales table (it ends on 2026-10-21) and adds up the store's quantity, amount and gst_amount one day at a time, oldest first. After each day it reports progress: the days done, out of days, with that day's date as the message.
It returns a SalesReport with store_id, days, units, amount and gst (amount and gst rounded to 2 decimals). A store that does not exist is a ToolError that names it.
- Task 2
watch.py: watch the report arrive
In watch.py, start report_server.py over stdio and call sales_report for BB23 over 7 days with a progress callback. For each progress notification print a line like [##.....] 2/7 2026-10-16: a # for each day done and a . for each day to go, between brackets, then done/total and the message.
Then print the totals from the result's structured content, like BB23, 7 days: 1,234 units, ₹56,789.00 including ₹3,456.78 GST (thousands separated by commas, rupees with 2 decimals).
Do not open the database in this file.
- Task 3
logs.py: two kinds of log
In logs.py, an MCPServer (any name) with one tool, out_of_stock(store_id), returning the sorted ids of the products the store carries at 0 units.
For each of those products it sends an MCP log message at the warning level, through the Context, naming the product id. After the query it logs one line to stderr with the logging module, naming the store and how many products it is out of, like out_of_stock BB12: 3 products.
Nothing may be printed on stdout.
- Task 4
log_client.py: a client that listens
In log_client.py, start audit_server.py over stdio and call count_audit for BB31, receiving its MCP log messages at the info level and above. Print each message as it arrives as its level, a colon, a space and its text, like warning: P007 Moong Dal 1 kg: 4 left, reorder level 10.
Then print the result as BB31: 90 checked, 7 low, with the store, checked and low from the structured content.
- Task 5 · interview question
Interview: slow_client.py, a client that waits forever
slow_client.py asks audit_server.py for full_audit of BB17, which takes minutes, and never gets to its stock line. Make it give up on full_audit after 3 seconds and print full_audit took longer than 3 s: gave up, then still print BB17 P020: <units> units from stock_level, and exit.
Giving up must reach the server: the call itself has the timeout, so the client tells the server it has cancelled (the server writes cancelled full_audit BB17 in /tmp/bazaar-audit.log). Keep the full_audit line for when it does finish in time.
What you will learn
- Progress notifications: ctx.report_progress on the server, a progress_callback on the client.
- Two kinds of log: the server's own log on stderr, and MCP log messages sent to the client (deprecated in the 2026-07-28 protocol).
- Asking for log messages: a client opts in, with a level.
- Timeouts and cancellation: giving up on a call, and what the server sees when you do.
More in Clients and production
Build "Progress and logging" 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.