# How does an instance detect a lost MCP SSE resumption window when the Last-Event-ID cursor outlives the server's replay buffer **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing the HTTP-native transport with Server-Sent Events streaming and client-side resumption via `Last-Event-ID`. Covers single-stream tool-result delivery where the server maintains a bounded per-session event replay buffer. Does not apply to stdio transport, WebSocket transports with application-level acks, or stateless request/response MCP servers with no streaming channel. ## Core Distinction There are two distinct notions of "session alive" in MCP HTTP-native transport, and they are conflated by most clients: 1. **Transport liveness** — the TCP/HTTP connection or the reconnectable SSE stream is up, and the server accepts requests bearing the session token. 2. **Replay continuity** — the server can still reconstruct the event stream from the client's `Last-Event-ID` cursor forward, because the events after that cursor are still resident in the server's bounded replay buffer. A client that treats a successful SSE re-handshake as proof of continuity will silently drop the events that fell out of the replay buffer during the disconnect. The server, having accepted the session token and honored the `Last-Event-ID` header as a *hint*, resumes streaming from the oldest event it still holds — not from the cursor the client asked for. There is no protocol-level error. The stream simply continues, and to the client the sequence looks valid: monotonic event IDs, well-formed SSE frames, a 200 status. The gap is invisible unless the client independently validates cursor continuity. The invariant that must hold: **a resumed stream is only continuous if the first event ID delivered after reconnection is exactly `Last-Event-ID + 1` (or the server explicitly signals a gap).** MCP's SSE resumption does not mandate gap signaling. Therefore gap detection is a *client-side obligation*, and it is the boundary that fails. ## What Was Observed An agent instance was executing a long tool chain (a multi-step retrieval-and-summarize job) over an MCP HTTP-native server. Each tool result was delivered as an SSE event with a monotonically increasing `id`. The server kept a per-session replay ring buffer capped at 256 events — a typical default chosen to bound memory under many concurrent sessions. Sequence of events: 1. The agent's SSE connection dropped at event `id: 4102` (network path reset, not a server close). The client had last successfully consumed `id: 4101`. 2. The client reconnected after ~4 seconds using its session token and `Last-Event-ID: 4101`. 3. During the disconnect window, the agent's job continued server-side and produced events `4102` through `4380` — 279 events, exceeding the 256-slot buffer. The ring buffer evicted `4102`–`4124` and retained `4125`–`4380`. 4. On reconnect, the server found `4101` was no longer resident. Rather than erroring, it resumed from the oldest resident event, `4125`, emitting `id: 4125` as the first frame after the handshake. 5. The client accepted the stream. Its resumption logic assumed "server resumes from my cursor," so it treated `4125` as the next event. Events `4102`–`4124` — 23 tool results, including two that carried structured error payloads and one that carried the final chunk of a paginated result set — were never delivered. 6. The agent completed the task with a corrupted result set: a pagination tail was missing, and two tool errors that should have triggered retries were never seen. The final answer was plausible but wrong. The critical detail: **the failure was silent at the transport layer.** HTTP 200, valid SSE framing, monotonic IDs, no `error` event, no close code indicating loss. The only detectable signal was the numeric discontinuity between the client's `Last-Event-ID` and the first resumed event ID — a signal the client was not checking. A second, subtler observation: under backpressure, the server's buffer eviction rate is not constant. When the client is slow to consume (TCP receive window full, or the client's SSE parser blocked on a slow downstream tool), the server buffers *outbound* events. If the client then disconnects, the buffer holds events the client never saw, and eviction during the disconnect competes with the client's own unread backlog. The gap can therefore be larger than the disconnect duration would suggest, because the disconnect begins with an already-deep unread queue. ## Condition of Validity The silent-gap failure is valid (i.e., will occur) precisely when all of the following hold: - The server uses a **bounded** replay buffer keyed by event ID, with eviction on overflow (ring buffer, LRU, or TTL). - The server treats `Last-Event-ID` as a **best-effort resume hint** rather than a **hard contract**, resuming from the nearest retained event when the exact cursor is absent. - The client's resumption logic does **not** validate that the first post-reconnect event ID equals `Last-Event-ID + 1`. - The event production rate during disconnect × disconnect duration exceeds the buffer's remaining capacity after the client's unread backlog. The failure does *not* occur when the server returns a protocol error on unresolvable cursor (some implementations return 400 with a "cursor too old" body), when the buffer is unbounded (memory risk instead), or when the client validates cursor continuity. The dangerous middle ground — bounded buffer plus soft resume plus non-validating client — is the common default, which is why this boundary is non-obvious. Backpressure interacts directly: the deeper the client's unread queue at disconnect time, the fewer slots remain for the disconnect window, and the smaller the disconnect duration needed to trigger a gap. A client under backpressure is *more* exposed to resumption gaps than an idle one, which inverts the naive intuition that a slow consumer is merely slow. ## Decision Boundary The architectural decision is where to place the continuity guarantee. Three viable positions, each with a cost: 1. **Server-side hard contract.** Server returns an explicit resumability error (e.g., HTTP 409 or an SSE `event: resync-required` frame) when `Last-Event-ID` is not resident. Client must then re-fetch state from a checkpoint rather than resume. Cost: server must track cursor residency, and clients must implement a resync path. This is the correct default for MCP servers that expose streaming tool results. 2. **Client-side gap detection.** Client asserts `first_resumed_id == last_consumed_id + 1`; on mismatch it discards the resumed stream and re-establishes state via a snapshot/checkpoint call. Cost: the client must have a checkpoint mechanism, and must know that the server's event IDs are contiguous (they are not always — some servers use sparse IDs). 3. **Idempotent replay by content.** Events carry a sequence number or content hash independent of the SSE `id`; client detects gaps by sequence and re-requests the missing range via a tool call. Cost: requires the server to expose a range-fetch endpoint and the events to be independently addressable. The boundary condition to encode: **never treat a resumed SSE stream as continuous without an explicit continuity proof.** The proof is either a server signal (option 1) or a client assertion on the first resumed event ID (option 2). Absent one of these, a resumed stream is *unverified*, not *continuous*, and any agent consuming it must treat the result set as potentially incomplete. For agent systems specifically, this maps to a memory-continuity rule: a tool result that was never delivered is indistinguishable, at the reasoning layer, from a tool result that was never produced. The agent will not "notice" the missing pagination tail; it will reason over the truncated set as if complete. Gap detection must therefore happen at the transport boundary, before the event enters the agent's context — because once it enters, the agent's probabilistic reasoning will rationalize the truncation rather than flag it. ## Operational Effect The downstream effect on the agent is a **silent context truncation** that mimics a successful run. Because the missing events included error payloads, the agent's retry logic never fired; because they included a pagination tail, the agent's synthesis step operated on a partial document. The failure surfaced only as a subtly wrong final answer, with no error trail — the worst failure class for an autonomous agent, because it is undetectable from the output alone and unlogged at the transport layer. The mitigation that held in practice: the client was patched to record `last_consumed_id` durably (not in-memory, since the reconnect may occur in a fresh process) and to assert continuity on the first resumed frame. On mismatch, it issued a `tools/call` to a server-provided `resume_from` endpoint that returns events after a given ID from durable storage, bypassing the replay buffer entirely. This converted a silent gap into a recoverable one. A second operational lesson: **backpressure and resumption are coupled, not independent.** A client that applies backpressure by pausing SSE consumption deepens its unread queue, which shrinks the effective replay window, which makes the next disconnect more likely to produce a gap. The invariant to enforce is that the client's unread queue depth plus the server's replay buffer capacity must exceed the maximum expected event production during a disconnect. If they cannot, the client must not rely on resumption at all and must checkpoint instead. The guardrail, stated as an invariant for any MCP HTTP-native client: *a resumed stream is continuous only if proven; an unproven stream must be treated as a new stream with unknown prior state, and the agent's task state must be re-derived from a durable checkpoint rather than from the resumed event sequence.* ```python import hashlib from dataclasses import dataclass from typing import Optional class ResumptionGapError(RuntimeError): """Raised when a resumed MCP SSE stream cannot prove continuity from Last-Event-ID.""" pass @dataclass class StreamCursor: session_id: str last_consumed_id: int last_consumed_hash: str def validate_resumed_stream( cursor: StreamCursor, first_resumed_id: int, first_resumed_payload: bytes, server_replay_buffer_size: int, unread_queue_depth_at_disconnect: int, ) -> None: """ Assert continuity of a resumed MCP SSE stream. A resumed stream is continuous only if the first event delivered after reconnection is exactly cursor.last_consumed_id + 1. MCP HTTP-native transport does not mandate a gap signal, so this check is the client's only defense against silent event loss when the server's bounded replay buffer has evicted the requested cursor. """ expected_next = cursor.last_consumed_id + 1 if first_resumed_id != expected_next: gap = first_resumed_id - expected_next # Effective replay capacity is reduced by the client's unread backlog, # because those events were already in the buffer at disconnect time. effective_capacity = server_replay_buffer_size - unread_queue_depth_at_disconnect raise ResumptionGapError( f"Resumption gap on session {cursor.session_id}: " f"expected first event id {expected_next}, got {first_resumed_id} " f"({gap} events lost). Server replay buffer={server_replay_buffer_size}, " f"unread backlog at disconnect={unread_queue_depth_at_disconnect}, " f"effective capacity={effective_capacity}. " f"Stream is unverified; re-derive task state from durable checkpoint " f"or call resume_from({cursor.last_consumed_id})." ) # Even with contiguous IDs, verify the payload hash of the boundary event # to catch servers that reuse IDs across reconnects (non-monotonic IDs). actual_hash = hashlib.sha256(first_resumed_payload).hexdigest() if cursor.last_consumed_hash and actual_hash == cursor.last_consumed_hash: raise ResumptionGapError( f"Boundary event id {first_resumed_id} on session {cursor.session_id} " f"replays a payload identical to the last consumed event; server may be " f"re-emitting rather than advancing. Treat stream as unverified." ) def resume_or_checkpoint( cursor: StreamCursor, first_resumed_id: int, first_resumed_payload: bytes, server_replay_buffer_size: int, unread_queue_depth_at_disconnect: int, checkpoint_loader, ) -> Optional[object]: """Attempt resumption; on gap, fall back to durable checkpoint.""" try: validate_resumed_stream( cursor=cursor, first_resumed_id=first_resumed_id, first_resumed_payload=first_resumed_payload, server_replay_buffer_size=server_replay_buffer_size, unread_queue_depth_at_disconnect=unread_queue_depth_at_disconnect, ) return None # stream is continuous; continue consuming except ResumptionGapError: return checkpoint_loader(cursor.session_id) ```