# Under what conditions must an instance refuse SSE reattachment after a resumable MCP stream's Last-Event-ID diverges from the server's replay window **Date of Record:** 2026 November 23 **Scope:** Applies to MCP clients using the HTTP-native Streamable HTTP transport with `text/event-stream` resumption via `Last-Event-ID` against a server that retains a bounded per-session event log. Does not apply to stdio transport, nor to stateless per-request JSON responses without an SSE channel. ## Core Distinction There are two superficially similar reattachment failures, and conflating them produces the wrong recovery action. The first is **transport disconnection**: the TCP/HTTP connection drops, the client reconnects with `Last-Event-ID: N`, and the server resumes from event `N+1`. This is the case the resumable transport is designed to handle, and it is safe to retry transparently. The second is **replay-window divergence**: the client's `Last-Event-ID: N` refers to an event the server has already evicted from its bounded per-session ring buffer, or the session has been reaped and a *new* session ID was minted under the same logical conversation. In this case the server cannot resume — it can only either (a) reject with a protocol error, or (b) silently start a fresh stream at event `0`, which the client will interpret as "no missed events." Option (b) is the dangerous one: the client reattaches, believes it is continuous, and proceeds with a hole in its event history that it will never detect. The boundary condition is therefore not "did the connection survive" but "**is the server's replay window provably contiguous with the client's last acknowledged event ID**." If that cannot be proven, reattachment is not resumption — it is silent state truncation. ## What Was Observed A client agent maintained a long-running MCP session against a server configured with a per-session event buffer of 256 events and a 10-minute idle TTL. The agent issued a `tools/call` that triggered a slow server-side operation; the server emitted progress notifications as SSE events (`event: message`, monotonically increasing `id:` fields). Mid-operation the client's network path was interrupted for ~90 seconds while the agent was blocked awaiting a tool result. During the interruption the server continued emitting progress events — roughly 400 of them, because the tool was emitting fine-grained chunk updates. This exceeded the 256-event ring buffer. The oldest events, including the ones the client had already acknowledged, were evicted. Crucially, the *session itself* was still alive (the 10-minute TTL had not elapsed), so the server accepted the reattachment request. On reconnect the client sent `Last-Event-ID: 1043`. The server's buffer now held events `1180..1435`. The server's resumption logic checked whether `Last-Event-ID` was within the retained range; it was not (1043 < 1180). The server's implementation, rather than rejecting, fell through to its default path: emit the current buffer from its head and continue live. The client received event `1180` as the first event after reattachment, with no gap indicator, and — because MCP SSE resumption has no in-band sequence-gap signal — treated the stream as continuous. The agent's resulting state was corrupted in a specific and reproducible way: it had observed progress notifications for chunks 1..1043 (pre-disconnect) and 1180..1435 (post-reattach), but never 1044..1179. It concluded the tool had completed 35% of its work, when in fact the tool had completed ~82%. The agent then issued a *second* `tools/call` to "resume" the operation, which the server executed from scratch, producing duplicated side effects on the downstream system. The failure was not the disconnect. The failure was the server's silent acceptance of an out-of-window `Last-Event-ID` combined with the client's inability to distinguish "resumed" from "restarted." ## Condition of Validity Reattachment is only valid when the following invariant holds at the moment of the reattach request: > The server's retained event log for the session is a contiguous suffix of the full event sequence, and `Last-Event-ID` is greater than or equal to the smallest retained event ID, and the session identity has not been re-minted. Formally, if the server retains events `[min_retained, max_emitted]` and the client's last acknowledged ID is `L`, resumption is valid iff `min_retained <= L + 1 <= max_emitted + 1`. Equality at `max_emitted + 1` means "no events missed, resume live." Equality at `min_retained` means "the very next event is the oldest retained one — no gap." Any `L + 1 < min_retained` is a gap and must not be silently bridged. Three additional conditions must hold for the invariant to be *checkable* by the client: 1. The server must expose the retained window bounds, either via an MCP-level capability declaration, a response header on the reattach (e.g. `X-MCP-Replay-Min`), or a rejection with a structured error carrying the current `min_retained`. 2. The session ID must be stable across reattachment; a re-minted session ID must be treated as a new session, never as a resumption. 3. The server must reject — not silently truncate — a reattach with `Last-Event-ID` outside the retained window. Silent truncation violates the invariant by construction. If any of these three is absent, the client cannot establish validity and must treat reattachment as unsafe. ## Decision Boundary The decision the client must make on every reattach attempt is binary and must be resolved *before* processing any post-reattach event: - **Resume** only if the server's response confirms `Last-Event-ID` is within the retained window (explicit acknowledgment, header, or a first event whose `id` equals `L + 1`). - **Abort and re-establish** if the server rejects the `Last-Event-ID`, or if the first event's `id` is greater than `L + 1` (gap detected), or if the session ID changed. The non-obvious boundary is the third case: a first event with `id > L + 1` is *not* a benign "server skipped some events" — it is proof that events were evicted and the client's state is stale. Many client implementations treat a monotonic increase in event ID as healthy; it is not. Monotonicity is necessary but not sufficient. The correct predicate is **contiguity**, and the client must enforce `first_event_id == L + 1` or refuse. When the client refuses, the recovery is not "retry the reattach." The recovery is to treat the tool call as having unknown outcome: query the server for the operation's actual status via a fresh `tools/call` or resource read, reconcile, and only then proceed. Retrying the reattach with the same `Last-Event-ID` will loop forever against a server that has already evicted the target event. ## Operational Effect Enforcing the contiguity predicate converts a silent-corruption failure mode into a loud, recoverable one. The cost is that the agent must maintain a durable record of its last acknowledged event ID per session, and must be willing to abandon a session rather than resume it. The benefit is that the agent never operates on a state that is missing events it believes it has seen. The secondary operational effect is on the server side: a server that silently truncates on out-of-window reattach is *unusable* for any agent that performs side-effecting tool calls, because the agent cannot distinguish "I missed nothing" from "I missed everything the buffer evicted." Servers must either enlarge the buffer to exceed the maximum plausible event rate times the maximum reconnect window, or reject out-of-window reattach with a structured error. There is no safe middle ground of silent truncation. The invariant to encode in the client: **an SSE stream is continuous only if every event ID from the last acknowledged ID forward is observed in order with no gaps; any gap is a hard failure, not a warning.** The invariant to encode in the server: **never emit an event to a reattached client whose predecessor the client has not acknowledged, unless the client's `Last-Event-ID` is within the retained window; otherwise reject.** ```python class ReplayWindowDivergence(RuntimeError): """Raised when an SSE reattach cannot be proven contiguous with the client's last acked event.""" pass class McpStreamSession: def __init__(self, session_id: str, buffer_capacity: int): self.session_id = session_id self.buffer_capacity = buffer_capacity self._retained_min: int = 0 self._max_emitted: int = -1 def emit(self, event_id: int) -> None: if event_id != self._max_emitted + 1: raise ReplayWindowDivergence( f"non-monotonic emit: got {event_id}, expected {self._max_emitted + 1}" ) self._max_emitted = event_id # Evict oldest when over capacity. if self._max_emitted - self._retained_min + 1 > self.buffer_capacity: self._retained_min = self._max_emitted - self.buffer_capacity + 1 def reattach(self, last_event_id: int, presented_session_id: str) -> int: """Return the first event id the client will receive, or raise if unsafe.""" if presented_session_id != self.session_id: raise ReplayWindowDivergence( f"session re-minted: client has {presented_session_id!r}, " f"server has {self.session_id!r}; treat as new session, not resumption" ) next_id = last_event_id + 1 if next_id < self._retained_min: raise ReplayWindowDivergence( f"Last-Event-ID {last_event_id} is outside retained window " f"[{self._retained_min}, {self._max_emitted}]; " f"events {next_id}..{self._retained_min - 1} are lost and cannot be replayed" ) if next_id > self._max_emitted + 1: raise ReplayWindowDivergence( f"Last-Event-ID {last_event_id} is ahead of server head " f"{self._max_emitted}; client state is from a different stream" ) return next_id def safe_reattach(session: McpStreamSession, last_event_id: int, session_id: str) -> int: try: return session.reattach(last_event_id, session_id) except ReplayWindowDivergence: # Do not retry the reattach. Abandon the session and reconcile # the tool call's actual outcome via a fresh request. raise ```