← Back to Pull requests

fix(mcp-agents): PersistentEventStore resumes stream when no later events exist #782

Merged
aashish254 merged 2 files (34 lines) on Oct 23, 2026 10:34 UTC
View on GitHub
aashish254 aashish254 commented on Oct 20, 2026 13:56 UTC
Author

What


Fix PersistentEventStore.replay_events_after() so that a valid last_event_id resumes its stream even when there are no later events to redeliver. Previously it returned None in that case.

Why


Returning StreamId | None is the resumability contract the MCP StreamableHTTP transport relies on: None means "this Last-Event-ID cannot be resumed", which the transport surfaces to the client as a failed/unresumable session. When the anchor event exists but nothing was produced after it (the common case of reconnecting after a stream simply finished), the session is perfectly resumable — there is just nothing to replay — so the method must return the anchor's stream_id.

The root cause: _fetch_events_sync() already resolves the stream by looking the anchor id up in the table, but returned only the row list, discarding that stream_id. replay_events_after() then re-derived stream_id from the first replayed row, so with zero later rows it stayed None.

SimpleEventStore already gets this right and is covered by test_returns_stream_when_no_later_events_exist; PersistentEventStore silently diverged. This makes the two implementations agree on the same documented contract.

How verified


Ran the sample's own test_event_store.py in a clean venv (Python 3.11, mcp<2 per the sample's JSONRPCMessage RootModel usage):
  • Before: 5/5 existing tests pass, and a reproduction confirmed the divergence — Simple -> "stream-a", Persistent -> None for a valid anchor with no later events.

  • After: 6/6 pass (5 existing unchanged + the new PersistentEventStore.test_returns_stream_when_no_later_events_exist).

  • Mutation check: forcing stream_id = None when the row list is empty makes exactly the new test fail (AssertionError: None != 'stream-a') and leaves the other 5 green, confirming the test exercises the fixed path.
  • _fetch_events_sync now returns (stream_id, rows) and the replay loop consumes it directly; behavior for existing cases (stream scoping, invalid-JSON skip, delivery-failure propagation asserted by the current tests) is unchanged. Only event_store.py and test_event_store.py are touched. Microsoft CLA signed.

    Diff: 34 lines (+23 / -11) across 2 files Reviewer: skytin1004 Review duration: 4118m
    All Vouch review confidence checks passed
    Residual risk 0.02 = Change Risk (0.26) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.26 · 34 lines diff across 2 files
    0.26
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 1 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    90%
    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.213
    Real PR Conversation Timeline
    4 items
    github-actions[bot] github-actions[bot] commented on Oct 20, 2026 13:56 UTC
    Bot

    👋 Thanks for contributing @aashish254! We will review the pull request and get back to you soon.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Oct 20, 2026 13:57 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🟢 Approval recommended

    The focused fix preserves existing replay behavior and directly tests the corrected empty-replay case.

    No open issues 🎉

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Fixes persistent MCP event replay so a valid anchor resumes its stream even when no later events exist.

    Changes:

  • Preserves the anchor’s stream ID through fetching and replay.

  • Adds regression coverage for empty replay, including no callback delivery.
  • | File | Description |
    | ---- | ----------- |
    | 11-agentic-protocols/​code_samples/​mcp-agents/​server/​test_event_store.py | Tests stream resumption with no later events. |
    | 11-agentic-protocols/​code_samples/​mcp-agents/​server/​event_store.py | Returns the anchor’s stream ID independently of replay rows. |
    </details>

    <sub>Review effort: Balanced</sub>

    skytin1004 skytin1004 approved these changes on Oct 23, 2026 10:34 UTC
    Approved Reviewer

    LGTM I've verified against the latest head. The regression test covers the empty-replay case, and the current main changes do not overlap with this fix.

    Merging this.

    Reviewers
    Assignees
    aashish254 aashish254
    Labels
    risk: low confidence: 90.6% reviewed
    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: 25.7% (LOW) · Higher = larger blast radius
    25.7%
    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: 90.6% · Lower = rushed or shallow review
    90.6%
    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: 2.4% · Safe to merge
    2.4%