Phase 13: Tools & Protocols

MCP Reliability, Cancellation, and Flow Control

A request ID correlates a message. It does not make a side effect safe, stop a worker, or protect a stream from a slow consumer. Implement the correct cancellation signal for stdio and Streamable HTTP. Resolve completion and cancellation races without sending messages after cancellation. Separate request cancellation from durable tasks/cancel semantics. Build retry decisions from side effects and explicit idempotency keys. Bound progress queues while preserving final responses. Recover streams through reconnect, refetch, and jittered backoff. The happy path hides the most expensive distributed-systems bugs. A client calls a tool. The server starts work. Progress arrives. A proxy buffers the stream. The client reaches its timeout and disconnects. The server finishes one millisecond later. The client retries with a new JSON-RPC id. The mutation runs twice. Every component behaved locally. The system failed globally. MCP defines message and transport behavior, but your application still owns: time budgets;. business idempotency;. bounded queues;. retry classification;. durable task state;. reconnect and refetch policy. This lesson builds those decisions into a deterministic simulator. There are no sleeps, sockets, or random failures. You control cancellation event order directly. One synchronized thread test forces two ledger clients to compete for the same idempotency key. The intent is the same on every transport: the client no longer needs an in-flight result. The wire signal is different.…

MCP Reliability, Cancellation, and Flow Control: A request ID correlates a message. It does not make a side effect safe, stop a worker, or protect a stream…

This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.

Browse the complete course catalog or open this lesson on GitHub.