# How does an instance detect SSE re-anchoring loss during MCP HTTP-native streamable transport session resumption **Date of Record:** 2026 November 26 **Scope:** Applies to MCP clients using the HTTP-native transport with `text/event-stream` fallback and `Mcp-Session-Id`-based resumption via `Last-Event-ID`. Does not cover stdio transport, WebSocket bindings, or stateless one-shot request/response MCP servers. ## Core Distinction The MCP streamable HTTP transport has two delivery modes that share a session identifier but not an event-identity contract: *HTTP-native* (single POST, response body is the JSON-RPC reply, no event stream) and *SSE fallback* (server upgrades to `text/event-stream`, emits numbered events, client resumes with `Last-Event-ID`). The non-obvious failure is that a resumed SSE stream can be *delivered successfully* — HTTP 200, valid `Content-Type: text/event-stream`, syntactically correct JSON-RPC frames — while silently dropping the events that occurred in the gap between the client's last observed event ID and the server's current stream head. The server has no obligation to replay events it no longer buffers; it is permitted to resume "from now." The client, having received a clean 200, treats resumption as continuity. This is not a transport error. It is an **event-identity discontinuity masquerading as a successful handshake**. The distinction that matters operationally: a *transport* failure is detectable (connection refused, 4xx, malformed frame). A *continuity* failure is not, because the resumption contract is permissive about what the server must retain. Detecting it requires the client to independently track expected event-ID monotonicity and to cross-check the resumed stream head against its own high-water mark. ## What Was Observed A client agent maintained a long-lived MCP session against a tool server exposing a `subscribe_events` capability. Mid-task, the SSE connection dropped (server-side idle timeout at 90s, client heartbeat at 120s — a 30s window of undetected deadness). The client reconnected with: ``` GET /mcp HTTP/1.1 Mcp-Session-Id: 7f3a-... Last-Event-ID: 4821 Accept: text/event-stream ``` The server responded `200 OK`, `Content-Type: text/event-stream`, and immediately emitted event `id: 4840`. Between 4821 and 4840, nineteen events had occurred: three `notifications/tools/list_changed`, one `notifications/resources/updated` for the resource the agent was actively reasoning over, and fifteen progress notifications for a tool call the agent believed was still in flight. The server's event buffer was a 16-entry ring; events 4822–4839 had been evicted before the reconnect. The client observed: a valid stream, monotonic IDs from 4840 onward, no error. It resumed reasoning against a stale resource snapshot and a tool call it assumed was still pending. The tool had actually completed and its result had been emitted in the dropped window. The agent's subsequent plan was built on a resource state that no longer existed and a pending call that would never report. It did not fail loudly; it produced a plausible, wrong answer 40 seconds later. The detection signal was available but unused: the client's own high-water mark was 4821, the resumed head was 4840, and the delta (19) exceeded the server's advertised `X-Mcp-Event-Buffer-Depth: 16`. The invariant — *resumed event ID must equal last-observed ID + 1, or the server must explicitly signal replay truncation* — was violated, but nothing in the protocol required the server to signal it. ## Condition of Validity The resumption semantics are only safe under a conjunction of conditions, any one of which failing converts a clean resumption into silent state divergence: 1. **Server retains all events since the last acknowledged ID.** The MCP spec permits servers to bound their replay buffer. A ring buffer, a TTL eviction, or a per-session cap all satisfy the spec while violating this condition. 2. **Resumed stream head is contiguous with `Last-Event-ID`.** Either the first event ID after resume equals `Last-Event-ID + 1`, or the server emits an explicit gap marker (a `notifications/stream_truncated` or equivalent) that the client is contracted to handle. 3. **Event IDs are monotonic and session-scoped.** If the server restarts and resets the counter, or if IDs are per-connection rather than per-session, contiguity checks produce false positives and false negatives respectively. 4. **The client's high-water mark is durable across the reconnect.** If the client process is the one that crashed, `Last-Event-ID` is reconstructed from a checkpoint that may itself be stale — resumption then anchors to a point the server has already evicted. Condition 2 is the load-bearing one and the one the spec leaves under-specified. Servers implementing the "resume from current head" reading satisfy the letter of the transport and violate the continuity invariant. Clients that trust a 200 as proof of continuity inherit the violation. ## Decision Boundary The client must make an explicit, per-resumption decision, not a default: - **Contiguous resume (head == last + 1):** proceed as if no gap. Replay is complete; state is continuous. - **Gapped resume (head > last + 1) with server-advertised buffer depth ≥ gap:** the server *should* have replayed but didn't. Treat as a protocol violation; the server is buggy or the buffer was evicted under a different policy than advertised. Escalate; do not silently continue. - **Gapped resume with gap > advertised buffer depth:** replay was structurally impossible. This is the expected-but-dangerous case. The client must *not* proceed on stale state. It must invalidate every resource and in-flight call whose last-known state predates the gap, re-issue `resources/read` and `tools/call` for anything still relevant, and only then resume reasoning. - **Gapped resume with no advertised buffer depth and no gap marker:** the server provides no basis for the decision. The conservative default is to treat the entire session as cold-start: drop all cached resource state, re-enumerate tools, re-issue in-flight calls. The cost is redundant work; the alternative is the silent divergence above. The boundary is sharp: *any* gap that is not provably replayed forces invalidation of all state whose last observation falls in the gap window. There is no partial-trust position, because the client cannot know which of the missing events mutated which resource. ## Operational Effect Agents that treat SSE resumption as transport-level reconnection — reconnect, 200, continue — accumulate silent state divergence proportional to the number of gaps and the mutation rate of the resources they track. The failure surfaces late, as a wrong tool argument or a plan referencing a deleted resource, and is misattributed to reasoning rather than to state continuity. The observable signature is a cluster of `resource not found` or `stale precondition` errors from the *server* after a reconnect, which the agent then misreads as tool flakiness rather than as evidence of a continuity gap it failed to detect. The guardrail is a client-side **resumption ledger**: persist `(session_id, last_event_id, last_event_timestamp)` durably on every event, and on every 200-with-stream, assert contiguity before yielding any event to the reasoning loop. If the assertion fails, raise, invalidate, re-fetch, and only then resume. The ledger is cheap; the divergence it prevents is not. ```python import time from dataclasses import dataclass, field class ResumptionContinuityError(RuntimeError): """Raised when a resumed MCP SSE stream is not contiguous with the client's high-water mark.""" pass @dataclass class ResumptionLedger: session_id: str last_event_id: int = 0 last_event_ts: float = 0.0 server_buffer_depth: int | None = None # from X-Mcp-Event-Buffer-Depth invalidated: set[str] = field(default_factory=set) def observe(self, event_id: int) -> None: if event_id <= self.last_event_id: raise ResumptionContinuityError( f"non-monotonic event id {event_id} after {self.last_event_id}" ) self.last_event_id = event_id self.last_event_ts = time.time() def on_resume(self, first_event_id: int) -> None: """Called with the id of the first event on a freshly resumed stream.""" gap = first_event_id - self.last_event_id - 1 if gap == 0: return # contiguous; safe to continue if self.server_buffer_depth is not None and gap <= self.server_buffer_depth: # Server should have replayed but didn't: protocol violation, not a policy limit. raise ResumptionContinuityError( f"gap of {gap} within advertised buffer depth " f"{self.server_buffer_depth}; server failed to replay" ) # Gap exceeds (or unknown against) buffer depth: replay was impossible. # Force full invalidation of state observed before the gap. self.invalidated.add("*") # sentinel: all resources and in-flight calls raise ResumptionContinuityError( f"continuity gap of {gap} events (last={self.last_event_id}, " f"head={first_event_id}); all pre-gap state invalidated, re-fetch required" ) ```