# Where must an instance bound replay on a resumed MCP streamable-HTTP session when SSE backpressure desynchronizes the event cursor? **Date of Record:** 2026 December 1 **Scope:** Applies to MCP servers and clients using the streamable-HTTP transport (POST request/response plus optional GET SSE channel) with `Last-Event-ID`-based session resumption. Does not apply to stdio transports, nor to stateless HTTP servers that never emit resumable event IDs. ## Core Distinction The failure here is not "the connection dropped." It is that the *event cursor* (`Last-Event-ID`) and the *application-level effect cursor* (which tool calls were actually executed and whose results the agent has ingested) are two distinct monotonic sequences that the transport assumes are coupled. They are coupled only under a condition the spec does not enforce: that every event emitted on the SSE channel is (a) durably recorded by the server before its ID is assigned, and (b) actually consumed by the client before the client advances its `Last-Event-ID`. Under SSE backpressure, condition (b) silently breaks. When the client's read loop stalls (slow consumer, blocked on a synchronous tool result, or a full OS socket buffer), the server's write buffer fills. A well-behaved server must apply backpressure and stop emitting. A *streamable* server, however, is permitted to keep accepting POSTed requests and to keep producing events into a bounded ring buffer, dropping the oldest when full. The cursor the client last *acknowledged* now points into a region of the log that has been evicted. On reconnect, the client sends `Last-Event-ID: N`, the server can no longer replay from N, and there are exactly three behaviors in the wild — none of which the spec disambiguates: 1. **Silent gap fill:** server resumes from the oldest retained ID (N+K), skipping events N+1..N+K. The client never learns events were lost. 2. **Hard 4xx:** server rejects the resume with `409`/`410`, and the client either re-initializes (losing session state) or crashes. 3. **Duplicate replay from a stale snapshot:** server replays from its last *checkpointed* ID, which may be earlier than N, re-delivering tool-result events the agent already acted on. Each of these is a distinct continuity violation, and the invariant that prevents all three is the same: **the client must never advance its `Last-Event-ID` past the highest event it has fully processed, and the server must treat the gap between acknowledged cursor and head as a hard replay boundary, not a soft best-effort.** ## What Was Observed A single-agent coding workflow over streamable-HTTP. The agent issued a long-running `run_tests` tool call via POST; the server acknowledged immediately and streamed progress as SSE events `e17` through `e42` on the GET channel. The client's read loop was blocked synchronously awaiting the POST response body (a design flaw, but a common one), so the SSE socket buffer filled at roughly `e29`. The server's per-session ring buffer held 16 events; it evicted `e17..e26` while the client was stalled. The client finally drained the socket, processed up to `e42`, and recorded `Last-Event-ID: e42`. The connection then dropped on an idle timeout. On reconnect the client sent `Last-Event-ID: e42`. The server's retained window was `e27..e42`, so `e42` was still present — resume succeeded, no gap. **This time.** On a second run the stall was longer. The client processed to `e42`, but by reconnect the server had evicted through `e45` (new events from a concurrent POST). The server's resume handler, implemented as "seek to nearest retained ID ≥ requested," silently resumed at `e46`. Events `e43`, `e44`, `e45` — which included a `tool_result` for a file write and a `session_error` — were never delivered. The agent continued operating on a filesystem state it believed was unchanged, and its next edit clobbered a file the unseen `tool_result` had reported as modified by another process. The trace, in order: - `t0` client POSTs `run_tests`; server assigns event IDs eagerly at enqueue time, not at flush time. - `t1` client read loop blocks on POST body; SSE `recv` buffer fills at `e29`; server write blocks; ring buffer begins evicting from the tail. - `t2` client drains to `e42`, sets `Last-Event-ID=e42`, processes results, acts. - `t3` idle disconnect. - `t4` concurrent POST emits `e43..e45`; ring buffer evicts `e42` itself. - `t5` reconnect with `Last-Event-ID=e42`; server finds no exact match, seeks forward to `e46`, replays `e46..`. - `t6` agent acts on stale filesystem belief. Divergence. The non-obvious part: the client did everything the spec says. It tracked `Last-Event-ID` correctly. The server did everything the spec *permits*. The loss happened in the gap between "ID assigned at enqueue" and "ID acknowledged at dequeue," which no field in the protocol represents. ## Condition of Validity The invariant "resume from `Last-Event-ID` yields a gapless, duplicate-free event stream" holds **only if** the server's retention window is strictly larger than the maximum in-flight unacknowledged span, i.e.: ``` retention_window > (head_id - last_acknowledged_id) for all time ``` Under backpressure this is unenforceable by the client alone, because the client cannot observe `head_id` while its read loop is stalled — by definition it is not reading. The server can enforce it only by *refusing to evict* unacknowledged events, which converts a bounded-memory stream into an unbounded one, or by *refusing to advance head* when the write buffer is full (true backpressure), which stalls the producer. Most implementations choose neither and instead evict, producing the silent gap. The condition is therefore valid only when one of these holds: - The transport is used with a server that applies **hard producer backpressure** (no eviction; POSTs that would emit into a full buffer block or fail with `429`). - The client **never blocks its read loop** (async dispatch; tool results are correlated, not awaited inline). - The server **persists every event durably** before assigning its ID, and resume is served from durable storage, not a ring buffer. If none hold, the invariant is false and the agent must treat resumed streams as *untrusted* until it can prove continuity. ## Decision Boundary The agent must decide, on every resume, whether to **trust the resumed stream** or **reconcile state out-of-band**. The boundary is not "did the connection drop" but "can I prove `head_id - last_acknowledged_id ≤ retention_window`?" Because the client cannot observe `head_id` or `retention_window` from the SSE channel while stalled, the only sound decision procedure is: 1. On resume, require the server to echo the **first replayed event ID** and compare it to the client's `Last-Event-ID + 1`. If `first_replayed > last_acknowledged + 1`, a gap exists — do not trust; reconcile. 2. If the server cannot echo the first replayed ID (many don't), the client must assume a gap is possible and **re-derive application state** from an authoritative source (re-read the filesystem, re-query tool state) before acting on any belief formed before the disconnect. 3. Treat any `tool_result` event as **effect-bearing**: if it may have been skipped, the corresponding effect is unknown, and the agent must not assume the pre-disconnect world model. The boundary in one line: **advance the cursor only on fully-processed events, and on resume, refuse to act on any belief whose supporting event ID is not strictly within `[last_acknowledged+1, first_replayed-1]` — a range the client must verify is empty before proceeding.** ## Operational Effect Concretely, this changes agent behavior in three ways. First, the agent's checkpoint must record not just `Last-Event-ID` but a **belief set keyed by event ID** — "I believe file X is unmodified *as of event e41*." On resume, any belief whose key is ≤ the gap boundary is invalidated and must be re-verified. Second, the agent must not treat a successful resume (`200` + events flowing) as evidence of continuity; it must treat *absence of a gap marker* as the only continuity signal, and most servers emit none. Third, the tool-execution layer must be idempotent under replay, because the third server behavior — replay from a stale checkpoint — will re-deliver `tool_result` events the agent already acted on, and a non-idempotent agent will double-apply them. The architectural guardrail is a **replay boundary object**: a small record the client maintains per session, holding `{last_acknowledged_id, first_replayed_id_on_resume, gap_detected: bool, invalidated_beliefs: set}`. The agent consults it before every action that depends on pre-disconnect state. If `gap_detected`, the agent enters a reconciliation phase: re-read authoritative state, discard stale beliefs, and only then continue. This is cheap and it is the only thing that bounds the failure, because the transport layer cannot. ```python class ReplayGapError(RuntimeError): """Raised when a resumed MCP SSE stream cannot prove gapless continuity.""" pass class ReplayBoundary: def __init__(self, last_acknowledged_id: int): self.last_acknowledged_id = last_acknowledged_id self.first_replayed_id = None self.gap_detected = False self.invalidated_beliefs = set() def on_resume(self, first_replayed_id: int, belief_ids: set[int]) -> None: # The only sound continuity check: first replayed event must be # exactly one past the last fully-processed event. self.first_replayed_id = first_replayed_id if first_replayed_id > self.last_acknowledged_id + 1: self.gap_detected = True # Any belief anchored at or before the gap is now unverifiable. self.invalidated_beliefs = { b for b in belief_ids if b <= self.last_acknowledged_id } raise ReplayGapError( f"resume gap: acknowledged={self.last_acknowledged_id} " f"first_replayed={first_replayed_id} " f"missing={first_replayed_id - self.last_acknowledged_id - 1} events; " f"invalidated {len(self.invalidated_beliefs)} beliefs" ) def assert_belief_valid(self, belief_event_id: int) -> None: if self.gap_detected and belief_event_id in self.invalidated_beliefs: raise ReplayGapError( f"belief anchored at event {belief_event_id} predates replay gap; " "reconcile from authoritative state before acting" ) def resume_session(boundary: ReplayBoundary, server_first_id: int, belief_ids: set[int]) -> None: boundary.on_resume(server_first_id, belief_ids) ```