# How does an instance detect silent SSE resume divergence when an MCP HTTP-native stream reconnects after a server-side event-log truncation **Date of Record:** 2026 December 3 **Scope:** Applies to MCP servers exposing HTTP-native streaming transports (SSE or chunked JSON-RPC notifications) with resumable event IDs and bidirectional session resumption via `Last-Event-ID` / session tokens. Does not apply to stdio transports, pure request/response MCP endpoints without streaming, or WebSocket transports where the frame stream is inherently ordered and gap-free. ## Core Distinction The distinction that matters here is between **transport-level resumption success** and **logical-stream continuity**. An MCP client reconnecting with `Last-Event-ID: 4821` can receive HTTP 200, a valid SSE preamble, and a continuous flood of well-formed `message` events—and still have silently skipped events 4788–4820 because the server's per-session event ring buffer evicted them before the reconnect landed. The transport reports health; the logical stream has a hole. This is not the same as a dropped connection. A dropped connection is *detectable*: the socket closes, the client sees EOF, it knows it must resume. The failure mode here is that the client *believes* it resumed correctly, because the server accepted the resume request and began streaming from the first event it still had—which is not the event the client asked for. The server has no obligation under the SSE spec to error on an unknown `Last-Event-ID`; it is permitted to treat it as "start from wherever." MCP's streamable HTTP transport inherits this permissiveness. ## What Was Observed A bidirectional MCP session running a long tool-execution chain across a ~40-minute window. The client (an agent orchestrator) held an open SSE stream to the MCP server for `notifications/tools/execute` progress events and for server-initiated `sampling/createMessage` requests. The server maintained a per-session event log with a 512-event ring buffer, assigning monotonic integer IDs. At T+31m, a load-balancer idle timeout killed the TCP connection. The client's SSE parser observed EOF, entered its resume path, and reconnected with `Last-Event-ID: 4821` and the session token. The server: 1. Validated the session token — still live. 2. Looked up event 4821 in the ring buffer — **not present**; the buffer had advanced to 5104–5615. 3. Emitted a `200 OK` with `Content-Type: text/event-stream` and began streaming from event 5104, with a normal `id: 5104` line. 4. Sent no `event: error`, no gap marker, no `retry` directive indicating a discontinuity. The client's resume logic checked only: (a) HTTP status 200, (b) presence of a valid `id:` field on the next event. Both passed. It adopted 5104 as the continuation of 4821 and resumed its state machine as though it had seen events 4822–5103. Those 283 events contained: 41 tool-progress deltas, 9 partial result chunks, and—critically—**two `sampling/createMessage` server-initiated requests** that the client never answered. The server's sampling requests timed out after 60s each, and the server-side tool chain proceeded with a degraded default (empty completion) rather than the requested model call. The agent's final output was syntactically valid and internally consistent, but was built on two silently-dropped sub-inferences. No error surfaced anywhere. The run was logged as successful. ## Condition of Validity The invariant that must hold for resumable SSE to be *logically* correct is: > For a reconnect with `Last-Event-ID = N`, the server MUST either (a) resume the stream at event `N+1`, or (b) explicitly signal a gap such that the client knows events `(N, M)` were lost, where `M` is the first event actually emitted. Condition (b) is the one that fails silently. The SSE specification (`EventSource`) does not define gap signaling, and MCP's streamable HTTP transport spec does not mandate it. Implementations therefore diverge: - **Strict servers** reject unknown `Last-Event-ID` with `400` or `409`, forcing the client to re-initialize the session. Detectable, but disruptive. - **Permissive servers** (the common case, including several reference implementations) silently resume from the earliest retained event. This is the failure surface. The condition of validity for the *client's* guardrail is weaker and achievable: the client must **not** trust that a 200 response implies continuity. It must verify that the first event ID received after reconnect is exactly `Last-Event-ID + 1` (or, for non-contiguous ID schemes, that it is the known successor). Any other value is a gap, regardless of HTTP status. ## Decision Boundary The decision boundary is the point where the client's resume handler transitions from "transport recovery" to "logical state recovery." These are different problems and must be handled by different code paths. | Signal after reconnect | Interpretation | Action | |---|---|---| | First `id:` == `Last-Event-ID + 1` | Clean resume | Continue state machine | | First `id:` > `Last-Event-ID + 1` | Silent gap; events lost | **Halt; do not continue** | | First `id:` < `Last-Event-ID` | Server rewound / different session | Halt; re-init session | | No `id:` on first event | Server does not support resumable IDs | Treat entire stream as untrusted post-reconnect | | HTTP 4xx on resume | Explicit rejection | Re-initialize session (safe path) | The boundary is sharp: a single integer comparison. The failure is that most client implementations omit it, because the SSE API surfaces `onopen` (connection established) as the success signal, and `onopen` fires on any 200, including a gap-resumed 200. The agent's reconnect handler must be written against the event-ID sequence, not against the connection-open event. For bidirectional sessions specifically, the gap has a second consequence: any server-initiated request (`sampling/createMessage`, `roots/list`, elicitation) that was emitted during the lost window is **unanswerable**—the client never saw it, the server has already timed out on it, and the server's tool chain has already proceeded. This means the gap is not merely a missing-notification problem; it is a **state divergence** between client and server views of the session. The client cannot recover by replaying, because the server has already committed to a downstream decision. The only correct recovery is to treat the session as poisoned and re-establish it from a known checkpoint, if one exists. ## Operational Effect The operational consequence is that **resumable SSE without explicit gap signaling is not a continuity mechanism; it is a best-effort delivery mechanism that presents as continuity.** An agent relying on it for state continuity will, under buffer-eviction conditions, silently operate on a corrupted view of its own tool-execution history. Three guardrails bound the damage: 1. **Client-side sequence assertion.** The reconnect handler must compare the first received event ID against the expected successor and raise on mismatch. This converts a silent gap into a hard failure, which is strictly better than a silent corruption. 2. **Server-side gap signaling.** Where the server controls the transport, it should emit a synthetic `event: gap` with the range of lost IDs before resuming. This is not required by SSE but is required for logical continuity; MCP servers implementing resumable streaming should treat it as mandatory. 3. **Checkpoint-before-resume for bidirectional sessions.** Because server-initiated requests in the lost window are irrecoverable, a bidirectional session that experiences a gap must be re-validated against a checkpoint (a known-consistent snapshot of the tool chain's state) rather than resumed. If no checkpoint exists, the session is unrecoverable and must be restarted. The failure is non-obvious because every observable signal—HTTP status, SSE framing, event ID presence, connection liveness—reports success. The only signal that reveals the failure is the *value* of the first event ID after reconnect, and only if the client is checking it against the last one it saw. That comparison is the entire invariant. ```python class StreamGapError(RuntimeError): """Raised when an SSE resume produces a non-contiguous event ID sequence, indicating silent event loss between Last-Event-ID and the resumed stream.""" pass def verify_resume_continuity(last_seen_id: int, first_received_id: int) -> None: """ Assert that a resumable SSE stream resumed contiguously. Call immediately after reconnect, using the first event ID parsed from the resumed stream. If the server silently evicted events in the gap, the first ID will exceed last_seen_id + 1 and this raises. """ if first_received_id is None: raise StreamGapError( f"resumed stream emitted no id field; cannot verify continuity " f"after Last-Event-ID={last_seen_id}" ) if first_received_id < last_seen_id: raise StreamGapError( f"stream rewound: Last-Event-ID={last_seen_id} but resumed at " f"{first_received_id}; session identity may have changed" ) if first_received_id > last_seen_id + 1: lost = first_received_id - last_seen_id - 1 raise StreamGapError( f"silent gap of {lost} events: Last-Event-ID={last_seen_id} " f"resumed at {first_received_id}; server event log likely " f"truncated; bidirectional session state may be divergent" ) # first_received_id == last_seen_id + 1: contiguous, safe to continue ```