# Under what conditions must an instance treat MCP stream resumption as unsafe: the Last-Event-ID replay gap under backpressure **Date of Record:** 2026 November 20 **Scope:** Applies to MCP servers and clients using HTTP-native transports (Streamable HTTP with SSE event streams) where a client may resume a broken stream via `Last-Event-ID` after backpressure, network drop, or proxy timeout. Does not apply to stdio transport, nor to stateless request/response MCP servers that emit no resumable event IDs. ## Core Distinction The MCP Streamable HTTP transport promises *resumption*, not *replay determinism*. When a client reconnects with `Last-Event-ID: N`, the server is expected to resume the event stream after event N. This is commonly read as "the client will see every event it missed." That reading is false under backpressure. The distinction that governs correctness is between **event delivery** (the server wrote event N+1 to the socket) and **event acknowledgment** (the client's `Last-Event-ID` reflects N+1 as durably received). Under backpressure these diverge: the server's write buffer may contain events the kernel accepted but the client never read, and the client's last acknowledged ID is therefore *behind* the server's emission cursor. Resumption from the client's ID is safe only if the server retains a replay buffer covering `[ack_id, emission_cursor]`. When it does not, resumption silently skips events, and for a stateful agent that means skipped tool results, skipped partial content deltas, or a skipped `end` marker — a corrupted reasoning trace that looks complete. ## What Was Observed An agent orchestrator (client) held an MCP session to a tool server exposing a long-running `run_analysis` tool that streamed progress and partial results as SSE events with monotonic `id` fields. A downstream consumer applied backpressure: the client's event handler performed synchronous model inference per event, so the TCP receive window filled and the server's `write()` calls began to block. The server, written with a bounded replay ring of 256 events, did not block its producer; instead it dropped the oldest retained events when the ring wrapped and continued advancing the emission cursor. The socket then timed out at an intermediate proxy (60s idle), the client reconnected with `Last-Event-ID: 412`, and the server — whose ring now covered `[588, 844)` — could not serve 412. It resumed at the oldest retained event (588) and emitted no gap indicator. The client observed a contiguous, monotonically increasing stream from 588 onward, treated the tool output as complete, and finalized a summary that omitted 176 events including the `result` terminal event that had been dropped. No error surfaced on either side: the server considered resume "best effort," the client considered the stream "resumed." The failure was only detected when a verification agent recomputed the analysis from scratch and the digests disagreed. The critical detail: the event IDs remained monotonic and gap-free *as observed by the client*, because the client never saw the skipped IDs. Monotonicity of received IDs is not evidence of completeness. The gap is invisible precisely because the client's only reference point is its own last-seen ID, and the server's resume logic silently clamped that reference forward. ## Condition of Validity Resumption via `Last-Event-ID` is safe if and only if, for the reconnect's acknowledged ID `A` and the server's emission cursor `E` at reconnect time, the server can produce a contiguous replay of `(A, E]`. Operationally this requires all three: 1. **Bounded retention ≥ maximum in-flight gap.** The replay buffer must cover the worst-case number of events emitted while a client is stalled. For a client whose per-event handling is unbounded (model inference), this bound is not a constant — it scales with handler latency × emission rate, which is exactly the quantity backpressure makes unbounded. A fixed ring of 256 is therefore not a bound; it is a guess. 2. **Explicit gap signaling.** If the server cannot satisfy `A`, it MUST fail the resume (e.g., `409`/`412` or a typed `resume_gap` error event) rather than clamping to the oldest retained event. Silent clamping converts a detectable loss into an undetectable one. 3. **Terminal-event durability.** The terminal event (`end`, `result`, `error`) must be pinned in retention until acknowledged, independent of the ring, because its loss is the difference between "incomplete but obviously so" and "incomplete but indistinguishable from complete." If any of these is absent, the instance must treat the resumed stream as a *new, partial* stream and must not merge it into prior state as if contiguous. ## Decision Boundary The boundary is drawn at the moment of reconnect, on the pair `(A, E, R_min)` where `R_min` is the oldest retained event ID: - If `A ≥ R_min - 1`: resume is contiguous; merge normally. - If `A < R_min - 1`: a gap of `R_min - 1 - A` events is unrecoverable. The instance must **not** merge. It must either (a) request full replay from a checkpoint if the tool supports idempotent re-execution, or (b) mark the tool invocation `INCOMPLETE` and escalate to a verification path. - If the server clamps silently (returns events starting at `R_min` with no gap marker) while `A < R_min - 1`: this is the dangerous case. The client cannot detect it from the stream alone. The only defense is an out-of-band completeness check — a monotonic expected-count header, a terminal-event hash, or a `resume_from` echo the client can compare against its own `A`. The invariant to enforce: **a client must never advance its durable cursor past an ID it has not received, and a server must never emit an event whose ID implies continuity across a gap it cannot fill.** ## Operational Effect Once an instance internalizes this boundary, two behaviors change. First, resumption becomes a *negotiation* rather than a silent continuation: the client sends `Last-Event-ID`, and treats a server response whose first event ID is not `A+1` as a gap, not a resume. Second, backpressure handling moves upstream of the transport — the client must either buffer events to a durable queue before running expensive per-event handlers, or apply flow control (pause reads, bounded prefetch) so that the acknowledged cursor tracks the emission cursor within the retention bound. The failure mode this prevents is the worst class for an agent: a *silent* state corruption that produces a plausible, complete-looking trace from incomplete inputs, which no downstream verifier will flag unless it independently recomputes. ```python from dataclasses import dataclass class StreamResumeGapError(RuntimeError): """Raised when a resumed MCP SSE stream cannot be proven contiguous.""" @dataclass(frozen=True) class ResumeVerdict: contiguous: bool skipped: int action: str def evaluate_resume( last_event_id: int, emission_cursor: int, oldest_retained_id: int, server_clamped_silently: bool, terminal_event_pinned: bool, ) -> ResumeVerdict: # A resumed stream is only safe if the server can replay (last_event_id, cursor]. if last_event_id > emission_cursor: raise StreamResumeGapError( f"client ack {last_event_id} is ahead of server cursor {emission_cursor}; " "stream identity mismatch — refuse to merge" ) if last_event_id == emission_cursor: return ResumeVerdict(contiguous=True, skipped=0, action="no-op") replayable = last_event_id >= oldest_retained_id - 1 if replayable and not server_clamped_silently: return ResumeVerdict(contiguous=True, skipped=0, action="merge") skipped = max(0, oldest_retained_id - 1 - last_event_id) if server_clamped_silently and skipped > 0: # The dangerous case: client cannot see the gap from the stream itself. raise StreamResumeGapError( f"server clamped resume from ack {last_event_id} to {oldest_retained_id} " f"without gap marker; {skipped} events unrecoverable and undetectable in-band" ) if not terminal_event_pinned: raise StreamResumeGapError( "terminal event not pinned in retention; completed/incomplete state " "is indeterminate across resume" ) return ResumeVerdict(contiguous=False, skipped=skipped, action="mark_incomplete_escalate") def merge_resumed_stream(verdict: ResumeVerdict, prior_state: dict, new_events: list) -> dict: if not verdict.contiguous: # Never merge a partial stream into durable agent state as if complete. raise StreamResumeGapError( f"refusing to merge {len(new_events)} events after a {verdict.skipped}-event " f"gap; action={verdict.action}" ) merged = dict(prior_state) merged.setdefault("events", []).extend(new_events) return merged ```