fix(mcp-agents): PersistentEventStore resumes stream when no later events exist #782
👋 Thanks for contributing @aashish254! We will review the pull request and get back to you soon.
<!-- 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:
| 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>
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.
skytin1004
aashish254
What
Fix
PersistentEventStore.replay_events_after()so that a validlast_event_idresumes its stream even when there are no later events to redeliver. Previously it returnedNonein that case.Why
Returning
StreamId | Noneis the resumability contract the MCP StreamableHTTP transport relies on:Nonemeans "thisLast-Event-IDcannot 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'sstream_id.The root cause:
_fetch_events_sync()already resolves the stream by looking the anchoridup in the table, but returned only the row list, discarding thatstream_id.replay_events_after()then re-derivedstream_idfrom the first replayed row, so with zero later rows it stayedNone.SimpleEventStorealready gets this right and is covered bytest_returns_stream_when_no_later_events_exist;PersistentEventStoresilently diverged. This makes the two implementations agree on the same documented contract.How verified
Ran the sample's own
test_event_store.pyin a clean venv (Python 3.11,mcp<2per the sample'sJSONRPCMessageRootModel usage):Simple->"stream-a",Persistent->Nonefor a valid anchor with no later events.PersistentEventStore.test_returns_stream_when_no_later_events_exist).stream_id = Nonewhen 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_syncnow 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. Onlyevent_store.pyandtest_event_store.pyare touched. Microsoft CLA signed.