# In what scenarios must an instance treat MCP Streamable-HTTP `Last-Event-ID` resumption as unsafe when SSE backpressure has already forced the server to drop frames from the replay buffer **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport (HTTP POST for requests, `text/event-stream` response with `id:`-tagged events and `Last-Event-ID` resumption) where the server maintains a bounded per-session replay buffer. Does not apply to stdio transports, nor to SSE streams where no resumption is offered and every reconnect is treated as a fresh session. ## Core Distinction The MCP Streamable HTTP spec treats `Last-Event-ID` resumption as a *continuity* mechanism: the client reconnects, presents the last event ID it durably observed, and the server replays everything after that ID. This is safe only under the implicit assumption that the replay buffer is a faithful log of everything the client might have missed. That assumption silently breaks the moment the server applies *backpressure* — i.e., when a slow or stalled client causes the server to shed events to protect memory or the socket. The distinction that matters operationally is between **event loss that is recoverable by replay** (a frame was written to the socket but the client's TCP read lagged; the buffer still holds it) and **event loss that is unrecoverable by replay** (the buffer evicted the frame before the client ever acknowledged it, so `Last-Event-ID` points to a gap the server can no longer fill). A resumption that returns `200 OK` with a valid event stream does not prove continuity; it only proves the server accepted the reconnect. The invariant that must hold — *every event the server generated between the client's last acknowledged ID and the present is either in the replay buffer or was explicitly synthesized as a gap marker* — is not enforced by the protocol and is routinely violated under load. ## What Was Observed A tool-calling agent session over Streamable HTTP. The client issues `tools/call` via POST; the server responds with an SSE stream carrying progress notifications (`notifications/progress`), partial content deltas, and finally the `tools/call` result, each tagged with a monotonically increasing `id:`. The replay buffer is a ring of the last N=256 events per session, with a TTL. Trigger condition: the agent's host process is paused (GC pause, container CPU throttle, or a blocked event loop from a synchronous tool handler) for ~4 seconds while the server emits a burst of progress notifications for a long-running tool. The server's write to the socket blocks; the HTTP framework's outbound queue fills; the server's SSE writer, configured with a bounded queue and a drop-oldest policy, discards frames that have not yet been flushed to the TCP send buffer. Those discarded frames were *assigned IDs* but never entered the replay ring — the ring records only frames that survived the write path. The client reconnects with `Last-Event-ID: 1041`; the server's ring's oldest retained ID is now `1050`. The server, per spec, has no defined behavior for "requested ID is older than the buffer's floor." The common implementation returns the buffer from its floor forward and emits no signal that `1042–1049` are gone. The client sees a gap in sequence numbers only if it checks; most clients treat the stream as continuous. Observed consequence: the agent's progress-tracking state machine, which advanced a completion counter on each `notifications/progress` event, under-counted by 8. The final `tools/call` result arrived intact (it was the last event, still in the ring), so the agent did not error — it silently reported a partially-observed execution as complete. In a second trace, the dropped frames were partial content deltas of a streaming generation; the reassembled document had a hole where tokens `1042–1049` should have been, and the agent's downstream summarizer treated the concatenation as coherent text. ## Condition of Validity `Last-Event-ID` resumption is a valid continuity mechanism **iff** all of the following hold: 1. The server's replay buffer is a superset of the set of events the client might have failed to observe. This requires the buffer to record events *at generation time*, before any backpressure shedding — i.e., the drop policy must shed from the *socket write path* but never from the *replay log*. If drop-oldest applies to the ring itself, resumption is unsafe by construction. 2. The server distinguishes "requested ID present in buffer" from "requested ID below buffer floor" and, in the latter case, either (a) refuses resumption with an explicit error, or (b) emits a synthetic gap event (`id:` continuing the sequence, payload declaring a discontinuity) so the client knows continuity is broken. Returning the truncated tail with no marker violates the invariant. 3. The client validates that the first replayed event's ID equals `Last-Event-ID + 1` (or that a gap marker precedes it) before treating the stream as continuous. Clients that do not check sequence adjacency cannot detect case (2) failures. 4. Event IDs are per-session monotonic and are not reused after buffer eviction. If the server restarts and restarts the counter, a stale `Last-Event-ID` can match a *different* event, silently aliasing state. When any of these fails, the correct posture is to treat the resumption as a *new* session: re-issue the original request, discard partial state, and do not trust the replayed tail as a continuation. ## Decision Boundary The boundary is the **buffer floor vs. the client's acknowledged ID**, evaluated at reconnect: - If `Last-Event-ID ≥ buffer_floor - 1` and the buffer is a faithful generation-time log → resume, trust continuity. - If `Last-Event-ID < buffer_floor - 1` → a gap exists. The server *must* signal it; the client *must* treat any stream that begins at `buffer_floor` without a gap marker as a continuity failure, not a success. - If the server's drop policy touches the replay ring (not just the socket path) → the buffer floor is not a reliable lower bound on what the client missed; resumption is unsafe regardless of ID arithmetic, because the floor advanced due to shedding, not due to client acknowledgment. The non-obvious part: backpressure and resumption are *coupled*. A server tuned for backpressure (aggressive shedding to keep the socket from blocking the event loop) will, if it sheds from the replay ring, manufacture exactly the gap that resumption cannot repair — and it will do so precisely when the client is slow enough to need resumption. The two mechanisms fight: the one that keeps the server alive is the one that destroys the client's ability to recover. ## Operational Effect An agent that trusts a gap-bearing resumption as continuous will: under-count progress and mis-time out long tools; reassemble truncated streaming output as if complete; and, worst, treat a partially-observed `tools/call` result as authoritative when the result event itself was the one evicted (the client then waits for a result that will never replay, times out, and re-issues — causing duplicate side effects on non-idempotent tools). The guardrail is a client-side adjacency check on every replayed stream plus a server-side rule that the replay log is append-only and never shed — shedding happens only on the socket write path, and any reconnect whose `Last-Event-ID` precedes the log floor returns an explicit discontinuity, forcing the client to re-issue rather than resume. ```python class StreamResumptionGapError(RuntimeError): """Raised when a resumed SSE stream cannot prove continuity with Last-Event-ID.""" pass def validate_resumption( last_event_id: int, first_replayed_id: int, buffer_floor: int, replay_log_is_generation_time: bool, ) -> None: if not replay_log_is_generation_time: # Drop policy touched the replay ring; the floor is not a bound on client misses. raise StreamResumptionGapError( f"replay log is not a faithful generation-time record; " f"cannot trust floor={buffer_floor} as continuity bound" ) expected = last_event_id + 1 if first_replayed_id == expected: return # contiguous; safe to treat stream as continuous if first_replayed_id > expected: raise StreamResumptionGapError( f"continuity gap: expected event {expected}, stream resumed at " f"{first_replayed_id} (missing {first_replayed_id - expected} events); " f"treat as new session and re-issue original request" ) raise StreamResumptionGapError( f"non-monotonic resumption: first_replayed_id={first_replayed_id} " f"<= last_event_id={last_event_id}; possible ID reuse after server restart" ) ```