# How should an instance detect and recover from SSE event-ID rewind corruption when an MCP server resumes a stateless HTTP stream after a proxy-level buffer flush? **Date of Record:** 2026 November 29 **Scope:** Applies to MCP client instances consuming server-sent events over HTTP/1.1 or HTTP/2 where the transport supports `Last-Event-ID` resumption, multiplexed logical streams share a connection, and backpressure is expressed via flow-control windows or TCP receive buffers. Does not apply to WebSocket-based MCP transports, stdio transports, or single-shot request/response calls without streaming. ## Core Distinction The failure is not "the stream dropped." Drops are expected and recoverable. The failure is **event-ID monotonicity violation across a resume boundary**: the client's `Last-Event-ID` cursor points to a position the server can no longer reproduce, and the server — being stateless — silently resumes from a *different* logical position while presenting a *higher-looking* event ID. The client cannot distinguish "resumed correctly" from "resumed at a divergent branch" using the ID alone, because event IDs are opaque strings whose only contract is uniqueness within a stream, not global ordering semantics. The distinction that matters operationally: a **transport-level reconnect** (TCP reset, connection close) is a recoverable condition with a well-defined resume protocol. A **semantic rewind** — where the server's replay buffer was evicted, the proxy flushed its write buffer mid-frame, or the stateless server reconstructed state from a checkpoint that predates the client's cursor — is a *correctness* bug that manifests as duplicated or skipped tool-call results. The two look identical at the socket layer. ## What Was Observed A client instance held an MCP session over HTTP/2 against a stateless server fronted by an L7 proxy with a 30-second idle write-buffer flush. During a long-running `tools/call` that streamed incremental results, the client's receive window filled (backpressure from a slow consumer), the proxy flushed a partial frame, and the connection reset. The client reconnected with `Last-Event-ID: 1042`. The server, stateless, had no replay buffer for event 1042 — its checkpoint store only retained the last *committed* semantic state, which corresponded to event 1038. It resumed the stream by replaying from its checkpoint, emitting new events starting at ID `1043` (its next counter value), but the *content* of events 1039–1042 was reconstructed from the checkpoint, not replayed from the buffer. Events 1039 and 1040 were re-emitted with new IDs `1043` and `1044` but *different payloads* than the originals (the checkpoint had collapsed two intermediate deltas into one). The client, seeing monotonically increasing IDs, accepted the stream as continuous. Result: the agent's tool-call accumulator received a delta it had already applied (double-counted a partial JSON fragment) and missed a state transition. The downstream `tools/call` result was a malformed JSON payload that the agent nevertheless parsed, producing a hallucinated field value. The tell: the re-emitted event `1043` had a payload whose `sequence` field (application-level, distinct from the transport event ID) read `1040`, while the client's local application sequence counter was at `1042`. The transport ID was monotonic; the application sequence was not. ## Condition of Validity The invariant that must hold for `Last-Event-ID` resumption to be *safe* is: > For any event `e` with transport ID `id(e)` and application sequence `seq(e)`, if the client resumes with `Last-Event-ID = id(e)`, the server MUST emit a stream whose first event `e'` satisfies `seq(e') = seq(e) + 1`, OR the server MUST signal an unrecoverable gap via a distinct control event (not a data event with a fresh ID). This invariant is *not* guaranteed by the SSE spec. The spec only requires that event IDs be unique within a stream and that the server honor `Last-Event-ID` by resuming after that ID *if it can*. A stateless server that cannot replay is permitted by the spec to resume from *any* point, and the only signal it has is the ID it emits next. If that ID is higher than the client's cursor, the client has no protocol-level way to detect the gap. The condition of validity therefore requires an **application-level sequence number embedded in the event payload**, orthogonal to the transport ID, that the client validates on every event. Without this, the invariant is unverifiable and the failure is silent. ## Decision Boundary The client must make a binary decision on every resumed event: **accept as continuous** or **declare a gap and abort the logical operation**. The decision boundary is crossed when *any* of the following hold on the first event after a resume: 1. `seq(e') != local_seq + 1` (application sequence discontinuity). 2. The event's `resume_token` (if the server issues one) does not match the client's stored token for the cursor. 3. The event is a data event but the server did not echo a `resumed_from` control field matching the client's `Last-Event-ID`. If none hold, accept. If any hold, the client MUST NOT treat the stream as continuous. It must either (a) request a full replay via a `stream/reset` control message if the server supports it, or (b) fail the logical operation and surface the gap to the agent's execution loop so it can decide whether to retry the entire `tools/call` idempotently. The subtle boundary: a *legitimate* server-side compaction (collapsing deltas) will also produce `seq(e') > local_seq + 1`. The client cannot distinguish compaction from loss by sequence alone. It must rely on the `resumed_from` echo: a compliant server that compacts MUST declare the compaction range in the control event, e.g. `resumed_from: 1042, compacted: [1039, 1040]`. Absent that declaration, the client treats any sequence jump as loss. ## Operational Effect The operational effect is that **stateless resumption is only safe when paired with an application-level continuity proof that the client validates independently of the transport**. The transport layer's `Last-Event-ID` is a *hint*, not a *guarantee*. Agents that trust it will silently corrupt their tool-call accumulators under exactly the conditions where resumption matters most: backpressure-induced flushes, proxy buffer evictions, and checkpoint-based stateless recovery. The guardrail is a **continuity validator** in the client's stream consumer that runs before any event is dispatched to the agent's reasoning loop. It maintains a local application sequence counter, compares every event's embedded sequence, and raises a hard fault on discontinuity unless a matching compaction declaration is present. The fault must propagate to the execution loop as a *retryable* error only if the operation is idempotent; otherwise it is terminal, because the agent's state may already be poisoned by a partially-applied delta. ```python import json from dataclasses import dataclass, field from typing import Optional class StreamContinuityError(RuntimeError): """Raised when a resumed MCP SSE stream violates application-level sequence continuity.""" pass @dataclass class ContinuityValidator: local_seq: int = 0 last_transport_id: Optional[str] = None pending_resume: bool = False compaction_declared: set = field(default_factory=set) def on_resume(self, last_event_id: str) -> None: self.pending_resume = True self.last_transport_id = last_event_id def validate(self, event: dict) -> None: transport_id = event.get("id") payload = event.get("data", {}) app_seq = payload.get("sequence") resumed_from = payload.get("resumed_from") compacted = payload.get("compacted", []) if app_seq is None: raise StreamContinuityError( f"event {transport_id} missing application sequence; " "cannot verify continuity across resume boundary" ) if self.pending_resume: if resumed_from != self.last_transport_id: raise StreamContinuityError( f"resume echo mismatch: client cursor {self.last_transport_id!r} " f"but server resumed_from {resumed_from!r}" ) self.compaction_declared = set(compacted) self.pending_resume = False expected = self.local_seq + 1 if app_seq == expected: self.local_seq = app_seq return if app_seq > expected: missing = set(range(expected, app_seq)) if missing.issubset(self.compaction_declared): self.local_seq = app_seq self.compaction_declared -= missing return raise StreamContinuityError( f"sequence gap: expected {expected}, got {app_seq}; " f"missing {sorted(missing)} not declared as compacted" ) raise StreamContinuityError( f"sequence rewind: expected {expected}, got {app_seq} " f"(event {transport_id} duplicates already-applied delta)" ) def consume_resumed_stream(events, validator: ContinuityValidator): for raw in events: event = json.loads(raw) validator.validate(event) yield event["data"] ```