Skip to content

fix(transport): replay only events after Last-Event-ID on resume - #1578

Open
v3rysp3d wants to merge 1 commit into
modelcontextprotocol:mainfrom
v3rysp3d:fix/sse-resume-duplicate-event
Open

v3rysp3d wants to merge 1 commit into
modelcontextprotocol:mainfrom
v3rysp3d:fix/sse-resume-duplicate-event

Conversation

@v3rysp3d

@v3rysp3d v3rysp3d commented Dec 5, 2026

Copy link
Copy Markdown

Fixes #1577

When a client resumes an SSE stream, LocalSessionManager sends the event named by Last-Event-ID again. It now replays only the events after it.

Motivation and Context

Last-Event-ID is the last event the client received, and the transport spec lets the server "replay messages that would have been sent after the last event ID". CachedTx::sync(index) skips the cached events before index and sends the rest, so the event at index goes out a second time.

With the default configuration, a request stream holds 0/0 (priming), 1/0 and 2/0. A client that read up to 1/0 and reconnects with Last-Event-ID: 1/0 gets 1/0 and 2/0. The rmcp client records the id of every event it processes and reconnects with it, so it handles 1/0 twice. If that event is a server request routed to the request's stream (elicitation, sampling), the client runs it twice.

The standalone GET stream has the same fault, plus a clash of ids. The priming event that opens it has id 0 and is not cached; the first cached event also got id 0. Last-Event-ID: 0 could mean "read the priming event only" or "read the first event", and the server could not tell which. The spec asks for ids that are unique within a session.

The change:

  1. resume passes the index after Last-Event-ID to sync, for request streams and for the standalone stream. sync still means "replay from this index", which is what a new standalone stream needs (it replays the whole cache from 0).
  2. Events on the standalone stream start at index 1, so 0 is only ever the priming event and Last-Event-ID: 0 replays everything cached.

The existing bound check in sync now also rejects an id one past the last event sent, which was accepted before and replayed nothing. No client can hold such an id.

Resume through an external EventStore is unchanged: replay_events_after already has this contract.

#1165 made the same observation and went stale without review; its other half (keeping the cache of a completed request) landed in #1487. This PR is a separate, smaller patch for the replay offset.

How Has This Been Tested?

  • Automated unit / integration tests
  • Tested manually
  • Tested in a real application

New tests, all failing on main (3725772) except the last, which guards the behavior that must not change:

test main this PR
resume_replay_tests::request_wise_resume_replays_only_events_after_last_event_id fails: ["1/0", "2/0"], expected ["2/0"] passes
resume_replay_tests::common_resume_replays_only_events_after_last_event_id fails: first event has id 0 passes
resume_replay_tests::request_wise_resume_rejects_an_event_id_that_was_never_sent fails passes
test_sse_concurrent_streams::resume_does_not_replay_the_event_named_by_last_event_id (over HTTP) fails: notification sent twice passes
resume_replay_tests::new_standalone_stream_replays_every_cached_event passes passes

Run locally on Linux, Rust 1.96: cargo +nightly fmt --all -- --check, the five clippy steps from ci.yml, cargo test --all-features, cargo test -p rmcp --lib --no-default-features, cargo test -p rmcp --features <all but local>, the docs build and typos.

Breaking Changes

None in the API. On the wire, event ids of the standalone stream now start at 1 instead of 0. Ids are opaque to clients.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Not changed here: the priming event of the initialize response also has id 0 (tower.rs), on a stream that cannot be resumed on its own. A client that reconnects with it lands on the standalone stream and gets every cached event, which is the right outcome for a client that has read none.

AI disclosure: this PR was written primarily with Claude Code (an AI coding agent): it found the bug, wrote the fix, the tests and this description, and ran the checks above locally.

🤖 Generated with Claude Code

Last-Event-ID names the last event the client received, but
LocalSessionWorker::resume passed its index to CachedTx::sync, which
replays from that index inclusive. A resuming client got the last event
it had already read a second time. The rmcp client does not drop
repeats, so it handled that event twice.

resume now passes the index after Last-Event-ID. sync keeps its meaning
(replay from an index), which a new standalone stream relies on to
replay the whole cache.

On the standalone stream the uncached priming event and the first
cached event both had id 0, so Last-Event-ID: 0 was ambiguous. Events
there now start at 1; 0 is the priming event alone.
@v3rysp3d
v3rysp3d requested a review from a team as a code owner December 5, 2026 04:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T04:15:11.982715Z e2d9e90 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jokemanfire jokemanfire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at e2d9e904 (base 37257723, matches the PR base). Everything below was run locally; the fork's CI has not started (action_required), so this is the only evidence there is.

What I ran

  • cargo test -p rmcp --features transport-streamable-http-server --lib resume_replay_tests → 4 passed.
  • cargo test -p rmcp --features "server client transport-streamable-http-server transport-streamable-http-client reqwest" --test test_sse_concurrent_streams → 18 passed, including the new resume_does_not_replay_the_event_named_by_last_event_id.
  • Full suite with CI's feature set (cargo metadata, every feature except local): 2011 passed, 0 failed, 18 ignored across 109 test binaries.
  • Red before green: I moved the additions onto base 37257723 (the new resume_replay_tests module pasted into the base file, plus the new integration test):
    • request_wise_resume_replays_only_events_after_last_event_id → left: ["1/0", "2/0"], right: ["2/0"]
    • common_resume_replays_only_events_after_last_event_id → sent ids were ["0", "1"]
    • request_wise_resume_rejects_an_event_id_that_was_never_sent → resume returned Ok, as the body says
    • resume_does_not_replay_the_event_named_by_last_event_id → the event named by Last-Event-ID "0" must not be replayed

The red run also proves the second half of the body: on base the first cached standalone notification really is id 0, the same id as the priming event written by ServerSseMessage::priming("0", …) (tower.rs:2114, tower.rs:2355). That is not just an offset bug.

Checked by reading

  • EventId::next_index keeps the /<http_request_id> part, so request-wise resume ids stay inside their channel; only the per-channel index moves.
  • New standalone streams still pass Some(0) from establish_common_channel, and sync uses saturating_sub(front.index), so starting cached ids at 1 does not skip the first event of a fresh stream.
  • External EventStore sessions never hit these indices: store_event assigns the ids (the priming event included, tower.rs:1590-1600) and replay_events_after (tower.rs:2025) does the resume, so the "unchanged" claim holds.
  • EventId::from_str accepts index only, so Last-Event-ID: 0 = common-channel priming event, unchanged.

Two notes, neither blocking

  1. A resume id of exactly last_sent + 1 now returns InvalidEventId instead of replaying nothing. tower.rs:2079 turns a resume error into a 200 with an empty stream, so the client sees the same thing — but the failed resume also skips installing the replacement primary, and the dead channel keeps caching (cache_and_send stores before it tries the sender), so those events arrive on the next GET without Last-Event-ID. Unreachable for a real client, so I would leave it; just worth knowing the two paths differ.
  2. sync still replays the whole cache when Last-Event-ID is older than cache.front() (trimmed by channel_capacity), re-sending events the client already has. Pre-existing and out of scope, but it is the one remaining path where "only after Last-Event-ID" is not true; one line in sync's doc would say so.

No request for changes from me. I cannot approve, and I would not before CI runs on a maintainer-approved workflow anyway.

@connor4312 connor4312 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Resume now replays strictly after Last-Event-ID on the same stream, as 2025-11-25 "Resumability and Redelivery" requires. Standalone stream ids start at 1, so 0 only means the priming event. Malformed or out-of-range ids fail without panicking. The in-memory path is still limited to legacy sessions, and replay is an offset skip with no extra copying. The session lib tests and test_sse_concurrent_streams pass, and clippy is clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streamable HTTP server replays the event named by Last-Event-ID on resume

3 participants