# Under what conditions must an instance refuse to resume an MCP stream after a Last-Event-ID replay gap, and how does SSE backpressure corrupt the resume watermark **Date of Record:** 2026 November 26 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with SSE streaming and `Last-Event-ID`-based session resumption, where tool-call progress events are interleaved with a bounded server-side replay buffer. Does not apply to stdio transport, nor to servers that emit strictly monotonic event IDs with unbounded replay retention. ## Core Distinction The distinction that matters is between **event-ID continuity** and **event-ID completeness under backpressure**. MCP's SSE resumption contract assumes that a client reconnecting with `Last-Event-ID: N` will receive every event with ID greater than `N` that the server generated, in order. This assumption holds only if the server's replay buffer is an append-only log *and* the socket write path never drops events. Under SSE backpressure—where the client's TCP receive window fills because the consumer is a slow agent loop still processing the previous tool result—the server's write buffer backs up, and any implementation that treats the replay buffer as "events acknowledged to the socket" rather than "events committed to the session log" will silently advance the resume watermark past events it never actually flushed. The client reconnects, replays from a watermark that is *numerically valid but semantically ahead* of what it received, and the gap is invisible: no sequence break, no protocol error, just a missing tool-result event that the agent loop will now block on forever or, worse, hallucinate a substitute for. ## What Was Observed A tool-execution loop over MCP HTTP transport, server emitting SSE events with `id:` fields tracking tool-call lifecycle (`tool_call_started`, `tool_call_progress`, `tool_call_result`). The agent's consumer was a reasoning loop that occasionally stalled for 2–6 seconds while a downstream model call completed. During the stall, the client stopped reading the socket. The server's SSE writer, implemented with a non-blocking `write()` loop, hit `EAGAIN`, buffered the pending frames in an application-level queue, and—critically—advanced its `last_committed_id` counter at the point of *enqueue* rather than *flush*. When the client's TCP window reopened, the server flushed the queue but had already begun evicting old entries from the bounded replay ring buffer (capacity 256) based on the enqueued watermark. A subsequent connection drop (client-side timeout at 30s) triggered reconnect with `Last-Event-ID` set to the last fully-received event, say `id: 1187`. The server's ring buffer, having evicted everything below its enqueued watermark of `id: 1203`, could only replay from `1203`. The resume response contained no error—MCP's spec does not mandate a gap signal—so the client accepted `1203` as the continuation of `1187` and proceeded. Events `1188`–`1202`, including the single `tool_call_result` for the in-flight tool, were gone. The agent loop resumed waiting on a result that would never arrive. The failure was intermittent and load-correlated: it appeared only when (a) consumer stalls exceeded ~1.5s, (b) the ring buffer was under pressure from concurrent sessions, and (c) a reconnect occurred within the eviction window. None of these alone triggered it. ## Condition of Validity `Last-Event-ID` resumption is sound if and only if the server guarantees the invariant **"replay watermark ≤ highest ID durably flushed to the transport"**, and the client verifies **"first replayed ID == last-received ID + 1"** (or, for non-contiguous ID schemes, that the first replayed ID's causal predecessor is present). The server-side watermark must be advanced at flush-completion, not enqueue. The client-side check must treat any gap between its `Last-Event-ID` and the first replayed event as a *fatal resumption failure*, not a benign skip. Absent both halves, resumption is a coin flip whose bias depends on buffer occupancy and consumer latency—precisely the parameters an agent cannot observe from inside the protocol. ## Decision Boundary An instance must **refuse to resume** and instead reinitialize the session (fresh session ID, re-issue the in-flight tool call, accept possible duplicate side effects guarded by idempotency keys) when the first replayed event's ID does not equal `last_received_id + 1`. It may **proceed with resume** only when IDs are contiguous across the reconnect boundary. It must **never** treat a numerically-greater first replayed ID as "the server skipped heartbeats"—MCP does not define heartbeat IDs, so any gap is data loss until proven otherwise. The boundary is sharp: contiguity is verifiable, gap semantics are not, so the safe action is defined entirely by the verifiable property. Servers must additionally expose the replay window's `[min_retained_id, max_committed_id]` on resume so clients can distinguish "gap because evicted" from "gap because bug." ## Operational Effect Treating the resume watermark as authoritative without contiguity verification converts a transport-layer backpressure event into a silent agent-state corruption: the loop loses a tool result, blocks or fabricates, and the corruption is attributed to the model rather than the transport. Enforcing contiguity plus flush-time watermarking turns the same backpressure into a loud, recoverable session reinitialization. The cost is occasional duplicate tool execution; the benefit is that missing events become impossible to mistake for completed ones. ```python class ResumeGapError(RuntimeError): """Raised when an MCP SSE resume replays from an ID that skips past the client's last-received event.""" pass def resume_stream(client, last_received_id: int, replay_window: tuple[int, int]): min_retained, max_committed = replay_window if last_received_id < min_retained - 1: # Our watermark predates the server's retention floor: events are gone. raise ResumeGapError( f"last_received_id={last_received_id} below retention floor {min_retained}; " f"events {last_received_id + 1}..{min_retained - 1} unrecoverable" ) first = client.open_resume(last_event_id=last_received_id).first_event_id() if first != last_received_id + 1: # Numerically-greater first ID is data loss, not a heartbeat skip. raise ResumeGapError( f"resume from {last_received_id} replayed first event {first}; " f"gap of {first - last_received_id - 1} events — reinitialize session" ) return client.continue_stream() ```