MCP servers and clients in TypeScript · 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 know what each protocol era does with them), 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
- TypeScript
- In the all labs plan
The first 2 chapters of MCP servers and clients in TypeScript 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.ts: a report that says how far it has got
In report-server.ts, export createServer() with one tool, sales_report(store_id, days): store_id a string, days a whole number from 1 to 14 in the schema. Serve it with serveStdio inside if (import.meta.main).
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 waits 10 ms (setTimeout from node:timers/promises, standing in for a real day's work) and, when the request carried a progress token, sends a progress notification: the days done, out of days, with that day's date as the message.
It returns structured content with store_id, days, units, amount and gst (amount and gst rounded to 2 decimals). A store that does not exist is an error result that names it.
- Task 2
watch.ts: watch the report arrive
In watch.ts, start report-server.ts over stdio (auto mode) and call sales_report for BB23 over 7 days, asking for progress. 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 (numbers in Indian grouping, toLocaleString('en-IN'), rupees with 2 decimals). Close the client. Do not open the database in this file.
- Task 3
logs.ts: two kinds of log
In logs.ts, export createServer() with one tool, out_of_stock(store_id), returning structured content with store_id and products, the sorted ids of the products the store carries at 0 units. Declare the logging capability.
For each of those products it sends an MCP log message at the warning level naming the product id. After the query it writes one line to stderr with console.error, naming the store and how many products it is out of, like out_of_stock BB12: 3 products.
Nothing may be printed on stdout. The check listens on both protocols.
- Task 4
log-client.ts: a client that listens
In log-client.ts, start audit-server.ts over stdio in auto mode (the 2026-07-28 protocol) 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. Close the client.
- Task 5 · interview question
Interview: slow-client.ts, a client that waits forever
slow-client.ts asks audit-server.ts 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 before the stock level is asked: the call itself has the limit, 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.mcpReq.notify on the server, an onprogress option 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: what the handshake era and the 2026-07-28 era each need from a client.
- Timeouts and cancellation: giving up on a call with timeout or an AbortSignal, and what the server sees through ctx.mcpReq.signal.
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. You build it in TypeScript. 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 in TypeScript are free. This one comes with the all labs plan.