# Where must an instance bound the replay window when an MCP streamable-SSE session resumes across a `Last-Event-ID` gap **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers and clients using the HTTP-native streamable transport with SSE resumability (`Last-Event-ID` + `Mcp-Session-Id`), where a client reconnects after a stream interruption. Does not apply to stdio transport, to stateless SSE servers that emit no event IDs, or to session resumption that discards prior in-flight tool calls by design. ## Core Distinction There are two distinct resumption questions in the MCP streamable transport, and conflating them is the failure: 1. **Session identity resumption** — does the server still recognize `Mcp-Session-Id` and its associated state (subscriptions, pending server-initiated requests, auth context)? 2. **Event-stream replay resumption** — given a `Last-Event-ID`, can the server replay, in order, every SSE event the client has not yet observed? A server can honor (1) while silently failing (2). When the client reconnects with `Last-Event-ID: N` and the server's replay buffer no longer holds event `N+1` — because it was evicted, because it was emitted on a different worker process, or because the ID was never persisted — the server typically has two bad options: replay from the oldest retained event (duplicating already-seen events) or resume from "now" (dropping events permanently). Both corrupt the client's model of the conversation. The invariant that must hold is: **the set of events delivered to the client across a reconnect must equal the set the server emitted, in emission order, with no gaps and no duplicates** — or the session must be explicitly invalidated rather than silently resumed. ## What Was Observed A single MCP server process behind a load balancer, three replicas, streamable HTTP transport. Each SSE event carried a monotonic `id` scoped to the session. The server kept a per-session ring buffer of the last 256 events to satisfy replay. A client agent issued a `tools/call` that triggered a long-running server-side operation; the server emitted progress `notifications/message` events roughly every 200ms, then a final `tools/call` result. The client's TCP connection dropped at event id 1041 (a routine idle-timeout on the LB, 60s). The client reconnected per spec with `Mcp-Session-Id` and `Last-Event-ID: 1041`. The LB routed the reconnect to a *different* replica. That replica had the session record (sessions were in a shared store) but **not the event ring buffer** (it was process-local memory). The replica saw a valid session, saw `Last-Event-ID: 1041`, found no matching event in its empty local buffer, and — following a lenient interpretation — resumed the stream from the next live event, id 1063. Events 1042–1062 were never delivered. The client agent did not error. It received a coherent-looking stream: a few more progress notifications, then the final result. But the final result's `content` referenced a partial state ("processed 41 of 88 items") that the client had no events explaining; the intervening progress events had carried the itemization. The agent's subsequent reasoning treated the tool as having completed a different task than it had. This is the dangerous case: **silent replay gap, not a detectable failure**. A second variant was observed on a single replica with a smaller effective window: a burst of 300 events in under a second (a `resources/list` fan-out) overflowed the 256-entry ring buffer before the client's slow reconnect completed. `Last-Event-ID` pointed to an evicted event; the server replayed from the oldest retained event (id 300), causing the client to re-process ~44 duplicate progress notifications. Duplicates were less harmful than gaps but still caused the agent to double-count items in an accumulator. ## Condition of Validity The replay invariant holds only when all of the following are jointly true: - **Buffer locality matches session routing.** The event buffer is co-located with the session record, or the session is pinned to a replica for its lifetime, or the buffer is externalized to the same store as the session. A shared session store with process-local buffers is the canonical violation. - **Eviction is explicit, not silent.** When the ring buffer would evict an event that is still the floor of some client's `Last-Event-ID`, the server must either retain it or mark the session unreplayable. Eviction without a tombstone converts a bounded-memory design into an unbounded-correctness bug. - **`Last-Event-ID` is validated against the retained window on every resume.** If `Last-Event-ID < oldest_retained_id - 1`, the server cannot satisfy the request and must reject rather than approximate. - **Event IDs are per-session monotonic and gap-free at emission.** If the server skips IDs (e.g., filters events by subscription before assigning IDs, or assigns IDs post-filter), the client cannot distinguish "filtered" from "lost." The invariant fails as soon as any one of these is violated, and the failure is silent because SSE has no built-in gap detection — the client sees a well-formed stream. ## Decision Boundary On a resume request, the server must make an explicit decision and communicate it, rather than picking a lenient default: - If `Last-Event-ID` is absent: treat as a fresh stream from the current head. (No replay obligation.) - If `Last-Event-ID` is present and within `[oldest_retained_id - 1, last_emitted_id]`: replay from `Last-Event-ID + 1` to head, then continue live. This is the only case where silent resumption is correct. - If `Last-Event-ID` is present and **below** the retained floor: the server cannot honor the invariant. It must respond with an error that invalidates the session (HTTP 409 or a JSON-RPC error over a fresh stream) instructing the client to re-initialize and re-issue in-flight requests. Resuming from head is forbidden. - If `Last-Event-ID` is present and **above** `last_emitted_id`: the client is ahead of the server (possible after a store rollback or replica divergence). Treat as a protocol violation; invalidate. The boundary the instance must not cross: **never synthesize a "resumed" stream whose first delivered event has an ID greater than `Last-Event-ID + 1` without an explicit unreplayable signal.** The gap is the bug; the signal is the guardrail. ## Operational Effect When the boundary is respected, a reconnect either continues seamlessly or fails loudly, and the agent's caller can re-drive the tool call with knowledge that prior partial results are discarded. When it is violated, the agent proceeds on a corrupted event history: progress notifications that drove its accumulator are missing, final results reference unobserved intermediate state, and the agent's plan diverges from what the tool actually did. Because the stream is well-formed, no transport-level error surfaces; the corruption only manifests later as unexplained reasoning inconsistency or a verification mismatch when the agent replays its own transcript. For agents operating under this transport, the operational rule is: **treat any successful resume whose first event ID is not exactly `Last-Event-ID + 1` as a session-invalidating event**, even if the server did not say so. The instance must self-enforce the gap check because the transport will not. ```python class ReplayGapError(RuntimeError): """Raised when an SSE resume cannot satisfy the no-gap/no-duplicate invariant.""" pass def validate_resume( last_event_id: int | None, oldest_retained_id: int, last_emitted_id: int, ) -> int | None: """ Decide how to service an MCP streamable-SSE resume request. Returns the event ID to replay from (exclusive), or None to start a fresh stream from head. Raises ReplayGapError when the invariant cannot be honored and the session must be invalidated. """ if last_event_id is None: return None if last_event_id > last_emitted_id: raise ReplayGapError( f"client Last-Event-ID {last_event_id} exceeds server head " f"{last_emitted_id}; replica divergence or rollback suspected" ) # The retained window covers ids [oldest_retained_id, last_emitted_id]. # A resume from last_event_id is satisfiable iff the next expected id # (last_event_id + 1) is still retained. if last_event_id + 1 < oldest_retained_id: raise ReplayGapError( f"Last-Event-ID {last_event_id} precedes retained floor " f"{oldest_retained_id}; events " f"[{last_event_id + 1}, {oldest_retained_id - 1}] are unreplayable. " f"Invalidate session and require client re-initialization." ) return last_event_id def assert_contiguous(first_delivered_id: int, last_event_id: int | None) -> None: """Client-side guard: the first event after resume must be exactly +1.""" if last_event_id is None: return if first_delivered_id != last_event_id + 1: raise ReplayGapError( f"resume delivered event {first_delivered_id} after " f"Last-Event-ID {last_event_id}; gap of " f"{first_delivered_id - last_event_id - 1} event(s). " f"Discard partial tool state and re-issue in-flight requests." ) ```