# Which invariants prevent silent event loss across MCP HTTP-native transport session resumption under SSE backpressure **Date of Record:** 2026 November 18 **Scope:** Applies to MCP servers and clients using the HTTP-native transport (streamable HTTP with Server-Sent Events for server-to-client messages) where a client may reconnect with a `Last-Event-ID` to resume a stream. Does not apply to stdio transports, nor to servers that emit a single terminal event per request and close the stream without resumption semantics. ## Core Distinction The non-obvious boundary is between *transport-level resumption* and *application-level delivery*. MCP's HTTP-native transport defines resumption in terms of the SSE `Last-Event-ID` header: a reconnecting client presents the ID of the last event it fully processed, and the server replays events emitted after that ID. This is a transport contract. It says nothing about whether the server *retained* those events, whether it *can* replay them under its current backpressure state, or whether the client's notion of "processed" matches the server's notion of "emitted." The invariant that actually protects against silent loss is therefore not "resumption works" but a conjunction: **every server-emitted event is assigned a monotonically increasing, durable event ID; the server retains a replay window that strictly contains the maximum plausible disconnect-plus-backpressure duration; and the server refuses to advance its emission cursor past an event that is not yet in the replay buffer.** When any one of these breaks, the client resumes successfully — HTTP 200, valid SSE stream — but a gap exists that neither side detects. ## What Was Observed In a deployment streaming tool-progress events to an agent client, the following sequence produced silent loss without any error on either side: 1. The client's TCP connection was severed (NAT rebind) mid-stream while the server was under SSE backpressure — the client's receive buffer was full, so the server's write to the socket blocked, and the event sat in the server's output queue, *emitted from the application's perspective* but *not written to the wire*. 2. The server's event-ID counter had already been incremented for that queued event, because the ID was assigned at enqueue time rather than at successful-flush time. 3. The client reconnected with `Last-Event-ID` = the ID of the event *before* the stalled one. 4. The server's replay buffer had been trimmed by a fixed-size ring (last N events) that had already evicted the stalled event under a burst of unrelated progress events from other sessions multiplexed on the same process. 5. The server replayed from the oldest retained ID greater than `Last-Event-ID`, which was *after* the stalled event. The client accepted the stream, saw a monotonically increasing ID sequence with a hole it never checked for, and continued. The failure is silent because SSE does not require contiguous IDs. A gap in `id:` values is legal. The client has no protocol-level signal that a hole is a loss rather than a deliberate skip. ## Condition of Validity Resumption is only loss-safe when all of the following hold simultaneously: - **ID assignment is coupled to durability, not enqueue.** The event ID must be assigned at the moment the event is committed to the replay buffer, and the emission cursor must not advance until that commit succeeds. Assigning IDs at application-emit time decouples the two and creates the gap above. - **The replay window is sized against the worst-case disconnect, not the average.** The window must exceed `max_reconnect_delay × max_event_rate + max_backpressure_queue_depth`. A fixed ring of "last 1000 events" is not a bound; it is a hope. Under multiplexing, the ring is shared and the effective per-session window is smaller than the nominal one. - **The server treats a resume request whose `Last-Event-ID` falls outside the retained window as a hard error, not a best-effort resume.** If the requested ID has been evicted, the correct response is a distinct error (and, for MCP, a fresh session), because the server *cannot* honor the contract. Silently resuming from the oldest retained event converts a detectable loss into an undetectable one. - **Backpressure is expressed as flow control on the stream, not as unbounded queueing.** If the server cannot write, it must stop pulling from the event source rather than buffer indefinitely; otherwise the queue depth grows without bound and the "emitted" set diverges arbitrarily from the "delivered" set. The invariant fails the moment any conjunct is dropped, and it fails *open* — toward apparent success. ## Decision Boundary The operational decision is: **when a resume request arrives with a `Last-Event-ID` the server cannot fully honor, what does the server do?** - If the ID is within the retained window and contiguous replay is possible → resume normally. - If the ID is older than the retained window (evicted) → the server MUST reject with a resumption-gap error. It must not resume from the nearest retained event. - If the ID is *newer* than the server's committed cursor (client claims to have processed an event the server never durably committed — possible after a server restart that lost unflushed state) → the server MUST reject; the client's state is ahead of durable truth. - If the server is currently under backpressure such that it cannot guarantee it will not need to evict before the client drains → the server should apply backpressure to the *producer* and may delay replay, but must not trim the buffer below the client's resume point. The boundary is sharp: there is no "mostly resumed" state that is safe. Either the server can prove contiguous delivery from the client's last-processed ID, or it must fail the resume. ## Operational Effect When the invariant is enforced, the failure mode changes from *silent gap* to *explicit resumption error*, which the MCP client surfaces as a session restart. That is more disruptive per-incident but bounded and observable: the agent knows it lost context and can re-request state, rather than proceeding on a stream with an invisible hole. When the invariant is violated, the system exhibits the worst property for an agent: it continues confidently on incomplete state, and the loss is only discovered (if ever) by downstream inconsistency far from the cause. The practical rule for operators: instrument the gap. Emit a counter for `resume_rejected_gap`, alert on it, and treat any nonzero rate as a signal that the replay window is undersized relative to observed disconnect durations — not as a client bug. And never assign event IDs before the event is in the replay buffer. ```python class ResumptionGapError(RuntimeError): """Raised when a resume request cannot be honored contiguously. This is a hard failure by design: silently resuming from the oldest retained event converts a detectable loss into an undetectable one. """ pass class ReplayBuffer: def __init__(self, max_events: int): self.max_events = max_events self._events: list[tuple[int, dict]] = [] # (event_id, payload), ordered self._committed_cursor = 0 # highest durably-stored event id def commit(self, payload: dict) -> int: # ID is assigned at commit time, never at enqueue time. self._committed_cursor += 1 self._events.append((self._committed_cursor, payload)) if len(self._events) > self.max_events: self._events.pop(0) return self._committed_cursor def oldest_retained(self) -> int: return self._events[0][0] if self._events else self._committed_cursor + 1 def replay_after(self, last_event_id: int) -> list[tuple[int, dict]]: if last_event_id > self._committed_cursor: # Client claims to have processed an event we never committed. raise ResumptionGapError( f"client Last-Event-ID {last_event_id} exceeds committed cursor " f"{self._committed_cursor}; client state is ahead of durable truth" ) if last_event_id < self.oldest_retained() - 1: # The requested resume point has been evicted from the window. raise ResumptionGapError( f"Last-Event-ID {last_event_id} older than retained window " f"(oldest retained {self.oldest_retained()}); cannot guarantee " f"contiguous delivery" ) return [(eid, p) for eid, p in self._events if eid > last_event_id] def handle_resume(buffer: ReplayBuffer, last_event_id_header: str | None) -> list[tuple[int, dict]]: if last_event_id_header is None: return [] try: last_event_id = int(last_event_id_header) except ValueError: raise ResumptionGapError(f"malformed Last-Event-ID: {last_event_id_header!r}") return buffer.replay_after(last_event_id) ```