← Back to Pull requests

fix(mcp-agents): keep recording resumption tokens while a request is resumed #786

Merged
aashish254 merged 2 files (118 lines) on Oct 24, 2026 03:39 UTC
View on GitHub
aashish254 aashish254 commented on Oct 23, 2026 19:18 UTC
Author

What

In 11-agentic-protocols/code_samples/mcp-agents, execute_tool_with_resumption() built the ClientMessageMetadata two different ways:

  • a new request registered on_resumption_token_update=enhanced_callback

  • a resumed request passed only resumption_token=... and left the callback unset
  • So 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 else branch 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:239 and :258 do if sse.id and resumption_callback: await resumption_callback(sse.id), and ClientMessageMetadata.on_resumption_token_update defaults to None (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() loads existing_tokens once (line 116) and passes it into every execute_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 existing server/test_event_store.py uses). A _FakeSession records the metadata object the client builds, and a real TokenManager pointed at a PID-scoped temp file proves persistence end-to-end:

  • a fresh request registers the callback and sends no resumption token

  • a resumed request sends its starting token and registers the callback

  • awaiting the registered callback with a newer id (event-99) writes event-99 to the token file and awaits the original callback with ("session-resume", "event-99", "long_running_agent", {"destination": "Paris"})
  • $ python -m unittest client.test_client_resumption -v
    Ran 3 tests in 0.018s
    OK
    

    Mutation check — removing only the added on_resumption_token_update=enhanced_callback line from the resume branch:

    Ran 3 tests ... FAILED (failures=2, errors=1)
    

    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 bare from event_store import ..., so it is run from inside server/, as before:

    $ cd server && python -m unittest test_event_store -v
    Ran 6 tests in 0.031s
    OK
    

    Both commands, from the mcp-agents directory (needs rich and mcp<2, since 2.x changed JSONRPCMessage):

    python -m unittest client.test_client_resumption
    (cd server && python -m unittest test_event_store)
    

    The new tests import the module under test as client.client rather than client, so client.py's relative from .utils import ... resolves and the suite runs from the mcp-agents directory.

    Diff: 118 lines (+108 / -10) across 2 files Reviewer: skytin1004 Review duration: 501m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.56) × (1 − Review Confidence (0.63)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.56 · 118 lines diff across 2 files
    0.56
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.35 · 1 substantive comment(s)
    0.35
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    62%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.286
    Sensitive Paths sensitive_paths
    0.056
    Real PR Conversation Timeline
    3 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Oct 23, 2026 19:20 UTC
    Reviewer

    <!-- 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:

  • Registers the existing token-update callback for both fresh and resumed requests.

  • Adds three regression tests covering callback registration, token persistence, and forwarding to the original callback.
  • | 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>

    skytin1004 skytin1004 approved these changes on Oct 24, 2026 03:39 UTC
    Approved Reviewer

    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.

    Reviewers
    Assignees
    aashish254 aashish254
    Labels
    risk: low confidence: 62.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 56.2% (MEDIUM) · Higher = larger blast radius
    56.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 62.7% · Lower = rushed or shallow review
    62.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.9% · Safe to merge
    20.9%