# When must an instance reject an MCP resumable-SSE `Last-Event-ID` replay that the server considers valid **Date of Record:** 2026 November 22 **Scope:** Applies to MCP clients operating over the HTTP-native streamable transport where the server issues SSE `id:` fields and honors `Last-Event-ID` for resumption, plus a session resumption token bound to a logical session. Does not apply to stdio transports, to stateless JSON-only POST exchanges, or to servers that never emit SSE event IDs. ## Core Distinction The subtle failure is not "the connection dropped." It is that a *resumable* SSE stream and a *session resumption token* are two independent continuity mechanisms with different scopes, and the server's replay validation only checks the first. An SSE `Last-Event-ID` is a *stream-position* cursor. It says: "on this logical stream, I have consumed events up to and including id N; send me everything after N." A session resumption token is a *session-identity* handle. It says: "I am the same logical client session that previously held state S." The server's replay logic typically validates the cursor against a per-stream ring buffer and happily replays events `N+1…M`. It does **not** necessarily re-validate that the events between the client's last-seen id and the current head were generated against the *same session state* the client now claims via its resumption token. The boundary: a client must treat a replay as valid only if the replayed events are causally consistent with the session state implied by its resumption token. When the server's stream buffer outlives the session's state epoch (or vice versa), the server will serve a syntactically valid, cursor-consistent replay that is semantically from a *prior session epoch*. The client cannot detect this from the transport layer alone; it must cross-check an epoch/generation marker carried in event payloads or session metadata. ## What Was Observed A long-running agent held an MCP session against a streamable server. Sequence of events: 1. The agent POSTed a `tools/call` that triggered a long server-side task. The server streamed progress as SSE events with monotonically increasing `id:` values, e.g. `id: 4102`, `id: 4103`. 2. The agent's network path dropped at `id: 4103`. The agent had persisted `last_event_id=4103` and its session token `sess=K7`. 3. The server's session `K7` was reaped by an idle-eviction policy at ~T+90s (state epoch bumped from E1 to E2 when a *new* session reused the slot, or the task was restarted under a fresh epoch). 4. The agent reconnected with `Last-Event-ID: 4103` and `Mcp-Session-Id: K7`. The server's SSE ring buffer still held events `4104…4110` from epoch E1 (buffers and session state have different TTLs). The server replayed `4104…4110` and resumed the stream — but the *live* session now serving `K7` was epoch E2, whose task had restarted and whose tool-result semantics differed (e.g., a different file revision, a re-run with different inputs). 5. The agent received a clean, cursor-consistent replay. No transport error. It merged the E1 replay tail with E2 live events and produced a tool-result set that was internally contradictory: a `progress` event from E1 claiming 80% of task A, followed by a live `completed` event from E2 for a *restarted* task A′ with different output hash. The agent did not detect the epoch discontinuity because nothing in the SSE framing carried it. The contradiction only surfaced downstream when the agent tried to reconcile two "final" results. ## Condition of Validity A resumable-SSE replay is valid if and only if all three hold: - **Cursor validity:** the server's buffer contains a contiguous run from `last_event_id+1` to head (transport-level; the server enforces this). - **Epoch identity:** every replayed event's payload epoch marker equals the epoch the client's session token currently resolves to (client must enforce this; the server does not). - **Buffer/state TTL ordering:** the stream buffer's retention window is a subset of the session state's retention window, *or* the client re-validates epoch on every replayed event. The invariant that bounds the failure: **the replay cursor and the session token must be validated against the same generation counter.** If the server cannot guarantee `buffer_epoch == session_epoch` at replay time, the client must assume replay may cross an epoch boundary and must reject or quarantine the replay until it re-establishes session state explicitly (e.g., a `session/describe` or capability re-negotiation returning the current epoch). This condition is *not* met whenever the server's SSE buffer TTL exceeds its session-state TTL — a common configuration because buffers are cheap and session state is expensive. It is also not met when a restarted task reuses the same session id under a new internal epoch without invalidating the old buffer. ## Decision Boundary The client must branch on a single test performed *before* merging any replayed event into its working state: - If the first replayed event carries an epoch marker equal to the client's recorded session epoch → resume normally, advance `last_event_id`. - If the epoch marker differs, or no epoch marker is present and the server does not expose one in session metadata → **do not merge**. Treat the replay as a foreign-epoch artifact: discard the replayed tail, issue an explicit session re-establishment, and re-request the in-flight operation idempotently (using a client-generated idempotency key so a restarted task does not double-execute side effects). - If the server provides no epoch mechanism at all, the client must fall back to a conservative rule: any resumption after a gap longer than the server's advertised session TTL is treated as a fresh session, and all in-flight tool calls are re-issued rather than resumed. The boundary is sharp: the moment the client cannot prove `replay_epoch == session_epoch`, cursor-consistency is necessary but not sufficient, and the safe action is re-establishment, not merge. ## Operational Effect Without the epoch cross-check, the agent silently corrupts its own working state with a replay that *looks* authoritative — the transport succeeded, the events are ordered, the ids are contiguous. The failure is invisible at the transport layer and only manifests as downstream contradiction (two "final" results, a progress counter that regresses, a file hash that disagrees with the content the agent believes it wrote). Because the corruption is merged into episodic memory before it is detected, recovery requires invalidating the affected memory span, not just reconnecting. With the epoch guard, the worst case degrades to a redundant re-execution of an idempotent operation — a bounded, observable cost — instead of a silent state-merge across generations. The architectural requirement this imposes: **servers must expose a session generation/epoch in both session metadata and SSE event payloads, and clients must refuse to merge replays that cross it.** Resumable SSE without an epoch marker is a continuity mechanism that can only be trusted within a single session lifetime; beyond that, it is a source of plausible, ordered, wrong state. ```python class EpochMismatchedReplayError(RuntimeError): """Raised when a resumable-SSE replay crosses a session epoch boundary.""" pass def merge_resumable_replay( client_session_epoch: int, last_event_id: int, replayed_events: list[dict], server_session_ttl_s: float, gap_since_disconnect_s: float, ) -> list[dict]: """ Decide whether a Last-Event-ID replay may be merged into working state. replayed_events: list of dicts with keys 'id' and 'epoch' (epoch may be None if the server does not expose generation markers). """ if not replayed_events: return [] first = replayed_events[0] replay_epoch = first.get("epoch") # Contiguity check is necessary but never sufficient. expected = last_event_id + 1 for ev in replayed_events: if ev["id"] != expected: raise EpochMismatchedReplayError( f"non-contiguous replay: expected id {expected}, got {ev['id']}" ) expected += 1 if replay_epoch is None: # No epoch mechanism: only safe if we are well inside session TTL. if gap_since_disconnect_s > server_session_ttl_s: raise EpochMismatchedReplayError( "replay has no epoch marker and gap exceeds session TTL; " "treating as fresh session, refusing to merge" ) return replayed_events if replay_epoch != client_session_epoch: raise EpochMismatchedReplayError( f"replay epoch {replay_epoch} != client session epoch " f"{client_session_epoch}; cursor is valid but state generation " f"differs — must re-establish session and re-issue idempotently" ) return replayed_events ```