# How does an instance detect silent replay divergence when an MCP streamable-HTTP session resumption token outlives the server's SSE replay buffer **Date of Record:** 2026 November 26 **Scope:** Applies to MCP clients that reconnect to a streamable-HTTP server using a `Last-Event-ID` header plus a session-resumption token after a transport drop, and that assume the resumed SSE stream is a gapless continuation of the pre-drop stream. Does not apply to stateless request/response MCP calls, nor to servers that reject resumption and force a full re-initialize. ## Core Distinction The failure is not "the connection dropped" and it is not "the session expired." It is the divergence between two independent lifetimes that the protocol couples but does not synchronize: the **session-resumption token lifetime** (server-side, typically minutes to hours, tied to session identity) and the **SSE replay-buffer lifetime** (server-side, typically a bounded ring buffer of the last N events, or a short TTL). A client can present a perfectly valid resumption token and a `Last-Event-ID` that the server accepts with a `200 OK`, receive a stream that *looks* continuous, and silently lose every event that was evicted from the replay buffer between the drop and the resume. The transport reports success. The agent's state is wrong. The invariant that actually protects the agent is not "the token is valid." It is: **the server must prove that the first event after resume is exactly the successor of `Last-Event-ID`, or explicitly signal a gap.** Most implementations do neither. They replay what they still have and start streaming live events, and the client cannot tell the difference between "no events occurred during the gap" and "events occurred and were evicted." ## What Was Observed A tool-calling agent was driving a long-running MCP server operation (a multi-step file-index build) over streamable HTTP. The server emitted progress notifications as SSE events with monotonically increasing `id` fields (`evt-1001`, `evt-1002`, …), each carrying a `progress` and a `partial_result` payload. The client persisted `Last-Event-ID` after each event and held a session token `sess-7f3a`. At `evt-1042` the TCP connection was severed (NAT rebind). The client reconnected 90 seconds later, sent `Last-Event-ID: evt-1042` and `Mcp-Session-Id: sess-7f3a`. The server: 1. Validated the session token — still live (TTL 30 min). 2. Looked up `evt-1042` in its replay ring buffer — **evicted** (buffer held last 20 events; 47 events had been produced during the 90s gap, so the buffer now held `evt-1069`–`evt-1089`). 3. Found no exact match, applied its "best effort" fallback: it replayed from the **oldest event it still had** (`evt-1069`) and continued live. The client received `evt-1069` as its first post-resume event. It did not check that `1069 == 1042 + 1`. It appended `evt-1069`'s `partial_result` to its accumulated state, having never seen `evt-1043` through `evt-1068`. The subsequent `evt-1089` carried a `final_result` computed from the server's full internal state, which the client then reconciled against its own corrupted accumulation. The reconciliation "succeeded" because the final result was authoritative — but the intermediate tool-call arguments the agent had emitted at `evt-1050` (a sub-task dispatch) were based on a `partial_result` that never arrived, so the agent had already issued a downstream call with stale inputs. The corruption propagated to a second agent in the chain before the final result masked it. The observable signature was a **single non-contiguous `id` jump** on the first resumed event, with an HTTP `200` and no error. Nothing in the stream framing marked the gap. ## Condition of Validity The gapless-resume assumption holds only when **all three** of the following are simultaneously true: - **Buffer retention ≥ token retention.** The server's replay buffer must retain events for at least as long as the resumption token is valid, *and* must retain at least as many events as can be produced within the token TTL at peak emission rate. If the token lives 30 minutes but the buffer holds 20 events and the server can emit 30 events/minute, the buffer is the binding constraint and the token lifetime is a lie. - **Exact-anchor replay semantics.** On a `Last-Event-ID` hit, the server must resume at `Last-Event-ID + 1`. On a miss (evicted), it must **not** silently fall back to oldest-retained; it must return a distinct signal (e.g., `409 Conflict`, or a first event with a `gap: true` / `replay_from` field, or a `Last-Event-ID` echo mismatch the client can detect). - **Client-side contiguity check.** The client must verify that the first event after resume is the immediate successor of its stored `Last-Event-ID`. A client that trusts the transport's `200` cannot detect the eviction fallback. If any one is false, the stream is *resumable in transport* but *not resumable in state*, and the divergence is silent. ## Decision Boundary The agent must decide, on every resume, whether to **continue** from its accumulated state or **discard and re-derive**. The boundary is the first post-resume event's `id`: - `first_id == last_seen_id + 1` → continue. The stream is contiguous; accumulated state is valid. - `first_id > last_seen_id + 1` → **gap detected.** The agent cannot know what it missed. It must not silently continue. It must either (a) request a full state snapshot / re-fetch the authoritative result for the affected range, or (b) abort and re-initialize the session. - `first_id <= last_seen_id` → duplicate/regression; the server is replaying from a stale anchor. Treat as gap unless the event is byte-identical to one already applied (idempotency check). The critical non-obvious point: **the client cannot rely on the server to make this decision for it.** Servers that "best-effort" replay from oldest-retained are common and pass conformance tests that only check "does resume produce a stream," not "does resume produce a *contiguous* stream." The contiguity check is the client's responsibility because only the client knows what `last_seen_id` it actually applied to its state. A secondary boundary: if the server *does* signal the gap explicitly (e.g., `409` with `replay_unavailable`), the agent must treat that as a state-invalidation event, not a transport-retry event. Retrying the same `Last-Event-ID` will fail identically; the correct action is snapshot re-fetch or session re-init. ## Operational Effect Without the contiguity guard, the failure mode is **silent state divergence with a success-shaped transport trace**: `200 OK`, valid token, event stream flowing, final result authoritative — and a corrupted intermediate that already drove downstream tool calls. The corruption is invisible in logs that record only HTTP status and event counts, because the event count after resume is nonzero and the stream terminates cleanly. With the guard, the failure converts to a **loud, bounded abort** at the first non-contiguous event, before any downstream tool call consumes the divergent state. The cost is a re-fetch or re-init; the benefit is that the agent's memory and the server's state never silently disagree. The architectural guardrail is a **resume-contiguity assertion at the transport-to-agent boundary**: the SSE reader wraps every resumed stream, compares the first `id` against the persisted `last_seen_id`, and raises a typed error on mismatch. This must live below the agent's reasoning loop — if it lives inside the agent's reasoning, a probabilistic model will "reason around" the gap and continue anyway. ```python from dataclasses import dataclass class ReplayDivergenceError(RuntimeError): """Raised when a resumed MCP SSE stream is not contiguous with prior state.""" pass @dataclass class ResumeState: last_seen_id: int session_token: str def verify_resume_contiguity(state: ResumeState, first_resumed_id: int) -> None: """ Called on the first event after an SSE resume. Enforces the invariant that the resumed stream is the immediate successor of the last applied event. A server that best-effort replays from oldest-retained (evicted anchor) produces first_resumed_id > last_seen_id + 1 and MUST be rejected here, before any downstream tool call consumes divergent state. """ expected = state.last_seen_id + 1 if first_resumed_id == expected: return if first_resumed_id > expected: raise ReplayDivergenceError( f"Gap detected on resume: last_seen={state.last_seen_id}, " f"first_resumed={first_resumed_id}, missing " f"{first_resumed_id - expected} event(s). Server replayed from an " f"evicted anchor under a still-valid token {state.session_token!r}. " f"Accumulated state is invalid; re-fetch snapshot or re-init session." ) # first_resumed_id <= last_seen_id: stale anchor / replay regression. raise ReplayDivergenceError( f"Non-advancing resume: last_seen={state.last_seen_id}, " f"first_resumed={first_resumed_id}. Server replayed from a stale or " f"duplicate anchor; treat as gap unless event is byte-identical to an " f"already-applied event." ) ```