fix(mcp-agents): keep recording resumption tokens while a request is resumed #786
<!-- ccr-overview-v2 -->
🟢 Approval recommended
The narrow fix preserves fresh-request behavior and adds focused regression coverage, with no blocking issues identified in static review.
No open issues 🎉
<details>
<summary><strong>What changed in this PR</strong></summary>
Fixes MCP tutorial client resumption so resumed requests continue recording newer event IDs, allowing subsequent reconnects to use the latest saved token.
Changes:
| File | Description |
| ---- | ----------- |
| 11-agentic-protocols/code_samples/mcp-agents/client/test_client_resumption.py | Adds focused regression tests for fresh and resumed requests. |
| 11-agentic-protocols/code_samples/mcp-agents/client/client.py | Shares the token-update callback across both request paths. |
</details>
<sub>Review effort: Balanced</sub>
LGTM. I pulled the latest head and ran the three new resumption tests and the six existing event-store tests. All nine passed. I also verified that a resumed request keeps the starting token, persists the newer token, and forwards the callback. I found no blocking issues.
skytin1004
aashish254
What
In
11-agentic-protocols/code_samples/mcp-agents,execute_tool_with_resumption()built theClientMessageMetadatatwo different ways:on_resumption_token_update=enhanced_callbackresumption_token=...and left the callback unsetSo while a resumed request is streaming, the newest event ids are never recorded. This adds a regression test suite for that behaviour and hoists the callback out of the
elsebranch so both paths register it. One coherent change, no drive-by edits.Why it matters
The MCP client calls the callback for every SSE event it receives —
mcp/client/streamable_http.py:239and:258doif sse.id and resumption_callback: await resumption_callback(sse.id), andClientMessageMetadata.on_resumption_token_updatedefaults toNone(mcp/shared/message.py:26). With the resume branch passing no callback, that guard is false and the event id is dropped.Concretely, for the tutorial's
long_running_agent(50 steps, one event id per step):1. The client disconnects at step 10 and saves
event-10.2. It reconnects and resumes from
event-10; the server streams steps 11-40.3. Nothing calls the callback, so the token file still says
event-10.4. A second disconnect replays from
event-10— the client re-delivers 30 events it already consumed and never advances toward completion.The resume path is also taken for ordinary tool calls whenever a token file exists at startup:
interactive_mode()loadsexisting_tokensonce (line 116) and passes it into everyexecute_tool_with_resumption()call in the command loop (lines 372 and 396), so those requests hit the same branch.How it was verified
Test file added:
client/test_client_resumption.py(3 tests,unittest.IsolatedAsyncioTestCase— the idiom the existingserver/test_event_store.pyuses). A_FakeSessionrecords themetadataobject the client builds, and a realTokenManagerpointed at a PID-scoped temp file proves persistence end-to-end:event-99) writesevent-99to the token file and awaits the original callback with("session-resume", "event-99", "long_running_agent", {"destination": "Paris"})Mutation check — removing only the added
on_resumption_token_update=enhanced_callbackline from the resume branch:Restoring it:
OK(3 tests). So the tests fail on the old code and pass on the new, which is the proof the change is what closes the gap.No regression to the previously merged resumption fix — the existing server suite still passes unchanged (6 tests,
OK). It uses a barefrom event_store import ..., so it is run from insideserver/, as before:Both commands, from the
mcp-agentsdirectory (needsrichandmcp<2, since 2.x changedJSONRPCMessage):The new tests import the module under test as
client.clientrather thanclient, soclient.py's relativefrom .utils import ...resolves and the suite runs from themcp-agentsdirectory.