# Why does an instance lose MCP session resumption when SSE backpressure drops the last event-id frame **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers using the HTTP-native "Streamable HTTP" transport with `Last-Event-ID`-based session resumption, where the server multiplexes multiple logical streams (tool results, notifications, sampling requests) onto a single SSE connection. Does not apply to stdio transports or to stateless HTTP request/response MCP servers with no resumability. ## Core Distinction The failure is not "the SSE connection dropped." Connections drop constantly and are cheap to re-establish. The failure is that the *resumption cursor and the durable event log are not atomically coupled to the delivery of the event that advances the cursor*. In a streamable MCP transport, the client resumes by sending `Last-Event-ID: N`, and the server is contractually obligated to replay every event with id `> N`. The invariant that must hold is: > For every event delivered to the client's socket, the server's durable log must already contain that event *before* the client can observe it, and the client's recorded `Last-Event-ID` must never exceed the highest id the server has durably committed. When SSE backpressure is present, this invariant inverts. The server's write buffer fills because the client (or an intermediate proxy) is not draining the socket. The server's transport layer — often an async writer with a bounded queue — begins to *drop* or *coalesce* frames under pressure. The event that carries the highest `id:` field is the one most likely to be in the buffer when the drop happens, because it is the newest. The client, meanwhile, has already recorded the id of the *previous* event. On reconnect it asks for `> N-1`, and the server either (a) replays from its log, which is fine, or (b) has already pruned or never committed the event because the write was buffered, not committed, and the log was written *after* the flush succeeded. Case (b) is the silent gap: the client resumes past an event that never made it into the log, and the agent's task state silently loses a tool result or a notification. ## What Was Observed A multi-tool agent (call it the "planner") was driving an MCP server over Streamable HTTP. The server exposed three tools and a `notifications/tools/list_changed` stream. During a long tool call (`ingest_repo`, ~40s), the server emitted a burst of progress notifications at ~200/s. The client's HTTP stack was behind a reverse proxy with a 64 KiB socket buffer and a 30s idle timeout. Trace of the failing window: 1. `ingest_repo` begins. Server opens an SSE stream, emits `id: 100` (tool call accepted). Client records `Last-Event-ID: 100`. 2. Server emits progress notifications `id: 101..1800` rapidly. The proxy buffer fills at ~id 1400. The async writer's bounded queue (capacity 256) overflows. 3. The server's writer, under pressure, drops the *oldest queued* frames to make room for new ones (a common "latest-wins" coalescing policy for progress events). Frames `id: 1401..1500` are silently discarded from the socket, but the server's durable log writer — which runs on a separate task — has *not yet flushed* them. It is still at `id: 1399`. 4. The tool call completes. Server enqueues the terminal event `id: 1801` (`result: {ok: true, artifacts: [...]}`) and then, because the write queue is still full, the connection is torn down by the proxy on idle. 5. Client reconnects with `Last-Event-ID: 1400` (the last id it actually saw before the buffer swallowed the rest). Server replays from its log, starting at `id: 1400` — but the log's high-water mark is `1399`. The server has no event `1400` to replay. It replays `1400` onward *from the log* only if present; since the log never received `1400..1800`, the replay yields nothing, and the server responds with a fresh stream starting at `id: 1801`. 6. The client sees `id: 1801` and processes the terminal result — but the agent's planner had been waiting on progress notifications `1400..1800` to update its internal plan. Those never arrived. The planner *did* get the terminal result and marked the task complete, but its `artifacts` list was empty because the progress events that carried artifact references were in the dropped range. The net effect: a *successful* tool call whose side-channel state was silently truncated. The agent did not error; it produced a wrong answer. ## Condition of Validity This failure requires all of the following to co-occur: - The transport is Streamable HTTP with `Last-Event-ID` resumption (not stateless). - The server's durable event log is written *after* or *independently of* the socket flush, so there exists a window where `socket_high_water > log_high_water`. - The write path applies backpressure by dropping or coalescing frames (bounded queue, latest-wins, or proxy-level buffer eviction) rather than by blocking the producer. - The client's resume logic trusts `Last-Event-ID` as a complete cursor and does not re-request a full state snapshot on reconnect. If any one is absent, the failure does not manifest. In particular, a server that *blocks* the producer on a full queue (backpressure propagation) cannot drop frames, but it can deadlock the tool call; a server that commits to the log *before* enqueueing the socket write cannot create the gap, but pays a fsync-per-event cost. ## Decision Boundary The architectural choice is between two resumption semantics: - **Cursor-as-acknowledgement:** `Last-Event-ID` means "I have durably processed everything up to N; replay N+1 onward." This is only safe if the server guarantees log-commit-before-send. It is the semantics most clients assume, and it is the semantics that breaks here. - **Cursor-as-hint:** `Last-Event-ID` means "I last saw N; replay from N+1, but also send a full state snapshot so I can reconcile." This is robust to gaps but requires the MCP server to expose a snapshot/reconcile method and the client to implement reconciliation. The decision boundary is: *if the server cannot guarantee commit-before-send, it must not advertise cursor-as-acknowledgement semantics.* The practical guardrail is a monotonic, server-assigned `event_id` that is committed to the log *before* the frame is handed to the socket writer, combined with a bounded queue that applies *backpressure to the producer* (block the tool's notification emitter) rather than dropping frames. Dropping is only permissible for events explicitly marked idempotent-and-coalescable (e.g., `progress` with a `replace` flag), and even then the *terminal* event must never be dropped. ## Operational Effect The observable signature is a tool call that returns `ok: true` with a truncated or empty side-channel payload, no error surfaced to the agent, and a `Last-Event-ID` on the reconnect that is *lower* than the server's terminal event id but *higher* than the server's log high-water mark at disconnect time. The agent's planner proceeds on incomplete state and produces a plausible-but-wrong result. The required guardrail, expressed as an invariant the runtime must enforce: > A frame may only be written to the SSE socket if its `id` is `<= log.committed_high_water`. The socket writer must never observe an id the log has not committed. Backpressure is applied by blocking the producer, not by dropping committed-range frames. Coalescing is permitted only within a contiguous range whose terminal event is retained and whose dropped ids are recorded as `superseded`, so a resuming client can distinguish "lost" from "intentionally coalesced." Detection at runtime: on every reconnect, the server must compare the client's `Last-Event-ID` against `log.committed_high_water`. If `Last-Event-ID < committed_high_water - 1` and the gap contains non-superseded events, the server must respond with a `resync_required` control event rather than silently replaying from the log, forcing the client to re-fetch state. ```python import asyncio from dataclasses import dataclass, field class ResumptionGapError(RuntimeError): """Raised when a client's Last-Event-ID skips past events the server never durably committed, producing a silent state gap on resume.""" pass @dataclass class EventLog: committed_high_water: int = 0 superseded: set[int] = field(default_factory=set) def commit(self, event_id: int) -> None: # Durable commit MUST happen before the frame reaches the socket. if event_id != self.committed_high_water + 1: raise ResumptionGapError( f"non-contiguous commit: got {event_id}, " f"expected {self.committed_high_water + 1}" ) self.committed_high_water = event_id @dataclass class StreamableSession: log: EventLog queue_capacity: int = 256 async def emit(self, event_id: int, frame: bytes, coalescable: bool = False) -> None: # Invariant: commit to the log BEFORE handing the frame to the writer. # Backpressure blocks the producer; it never drops committed-range frames. self.log.commit(event_id) await self._write_with_backpressure(frame, coalescable=coalescable) async def _write_with_backpressure(self, frame: bytes, coalescable: bool) -> None: # A bounded queue that blocks (awaits) rather than evicts. # Coalescable frames may be replaced in-place, never dropped from the # committed range. await asyncio.sleep(0) # placeholder for real bounded-queue await def validate_resume(self, last_event_id: int) -> None: # Guardrail: a resume cursor must not skip uncommitted events. if last_event_id > self.log.committed_high_water: raise ResumptionGapError( f"client resumed at {last_event_id} but server only " f"committed through {self.log.committed_high_water}; " f"gap would silently truncate side-channel state" ) gap = set(range(last_event_id + 1, self.log.committed_high_water + 1)) lost = gap - self.log.superseded if lost: # Force a resync instead of a silent replay. raise ResumptionGapError( f"resume from {last_event_id} skips non-superseded events " f"{sorted(lost)[:5]}...; client must re-fetch state snapshot" ) ```