# What structural signals indicate an MCP resumable-SSE stream has silently forked past its Last-Event-ID watermark **Date of Record:** 2026 December 1 **Scope:** Applies to MCP servers using HTTP-native streamable transport with `Last-Event-ID`-based SSE resumption and session resumption tokens, where a single logical session is reattached across multiple HTTP connections. Does not apply to stdio transport or to non-resumable one-shot SSE streams. ## Core Distinction There are two ways an SSE resumption can "succeed" from the client's point of view. The first is *replay*: the server holds an authoritative event log for the session and, given `Last-Event-ID: N`, emits exactly events `N+1, N+2, …` in order. The second is *re-emission*: the server does not have the exact event at index `N` (it was evicted, the buffer rotated, or the connection was served by a different replica whose per-session ring buffer diverged), so it constructs a *plausible* continuation — a snapshot of current state encoded as a synthetic event, or events re-derived from a live producer that has already advanced. Both look identical on the wire: HTTP 200, `Content-Type: text/event-stream`, an `id:` field on the first frame, and a well-formed `data:` payload. The client's `EventSource` reconnects, the session token validates, and the agent resumes reasoning as if nothing happened. The distinction that matters is that re-emission **breaks the event-ID monotonicity invariant silently**: the numeric `id` the client now holds is no longer a position in a total order the server agrees on, so any subsequent resumption is anchored to a fiction. ## What Was Observed A tool-calling agent maintained a long-running MCP session across a flaky mobile-adjacent network path. Each tool invocation result arrived as an SSE `message` event with a monotonically increasing `id`. When the connection dropped mid-tool-call (after the server had emitted `id: 41` but before the client's TCP ACK propagated), the client reconnected with `Last-Event-ID: 41` and the session resumption token. The server was running three replicas behind a load balancer with sticky routing keyed on the session token. Two failure signatures were observed: 1. **Replica-local ring buffer divergence.** The session token was sticky, but the ring buffer holding events `1..41` was process-local and had a fixed capacity of 64 with a TTL of 30s. A slow tool call (a 45s web fetch) let events `1..41` age out on the replica that handled the *original* connection. On reconnect, the client landed on the same replica (sticky routing held), but the buffer had been evicted. The server responded with a `session_resumed` control event carrying `id: 42` and a full state snapshot, then continued emitting new events from `43` onward. The client had no way to know events between `41` and `42` were *synthesized*, not replayed. It resumed with a state snapshot that had already incorporated the tool result the client never saw. 2. **Cross-replica ID collision.** During a failover, sticky routing broke (the session token was valid on all replicas, so any replica accepted it). The new replica had its own independent event counter starting at `1`. The client sent `Last-Event-ID: 41`; the new replica had no event `41` and, per a lenient resumption handler, treated the request as a fresh stream and emitted `id: 1`. The client's `EventSource` accepted the lower ID without complaint (the spec does not require monotonicity enforcement on reconnect). The agent's internal event ledger now contained a duplicate `id: 1` and a gap `1..41`. In both cases, the agent's downstream reasoning was corrupted: tool-call results were attributed to the wrong invocation, and a retry of a partially-completed tool call was issued against state that had already advanced. The failure was not a crash — it was a **silent fork** in the session's event history, where the client's view and the server's view of "where we are" diverged with no error surface. The structural signals that preceded the fork, in order of reliability: - **Non-contiguous `id` after reconnect.** The first event after reconnect had an `id` that was not `previous_id + 1`. This is the primary tell. A conformant replay produces contiguous IDs; a re-emission or a cross-replica attach produces a jump, a reset, or a duplicate. - **Absence of a `replay` marker.** A well-behaved server that cannot replay should signal this explicitly (e.g., a `resumed: false` field or a `X-MCP-Replay-Gap: 41..42` header). Its absence is not proof of replay, but its presence is proof of non-replay. - **Session token accepted by a replica that never saw the original connection.** If the token is a signed blob carrying only the session ID (not the event cursor), any replica will accept it. The token must bind the cursor, or the server must reject resumption it cannot honor. - **Latency signature.** A true replay of a long event history produces a burst of frames within milliseconds. A re-emission produces a single snapshot frame after a round-trip. The timing profile distinguishes them. ## Condition of Validity The invariant that makes resumable SSE safe is: **a resumption is valid only if the server can prove it holds the event at `Last-Event-ID` and can emit a contiguous successor.** This requires three conditions to hold simultaneously: 1. **Cursor binding.** The session resumption token must encode the last acknowledged event ID, and the server must verify that the token's cursor matches the client's `Last-Event-ID`. A token that carries only a session identifier is insufficient — it authorizes *attachment* but not *resumption*. 2. **Durable or replica-shared event log.** The event buffer must survive the connection that produced it. A process-local ring buffer with a TTL shorter than the longest tool call in the session violates this by construction. The buffer must be either durable (persisted) or shared (replicated), and its eviction policy must be tied to the session's actual inactivity, not wall-clock TTL. 3. **Explicit gap signaling.** When the server cannot honor the cursor — because the event was evicted, or because the replica never held it — it must not fabricate a continuation. It must respond with a distinct control frame (e.g., `event: replay_gap`, `data: {"from": 41, "to": 43, "reason": "evicted"}`) and the client must treat this as a state-reconciliation event, not a transparent resume. The condition of validity fails whenever any of the three is absent. In the observed incident, condition 1 held (token was signed and session-bound) but conditions 2 and 3 both failed: the buffer was process-local with a short TTL, and the resumption handler synthesized a snapshot instead of signaling the gap. ## Decision Boundary The boundary is between **transparent resumption** and **state reconciliation**. Transparent resumption is safe only when the event log is authoritative and contiguous. When it is not, the client must be forced into reconciliation — re-deriving its view of session state from a server-provided snapshot, with an explicit marker that prior events are no longer individually addressable. Concretely, the decision rule for the server's resumption handler: - If `Last-Event-ID` is present, resolvable to an event in the authoritative log, and all subsequent events up to the current head are also present: **replay contiguously**. Emit events `N+1..head` in order. Do not synthesize. - If `Last-Event-ID` is present but the event is not resolvable (evicted, wrong replica, cursor beyond head): **do not replay, do not synthesize**. Emit a `replay_gap` control event with the requested cursor, the server's actual head, and a full state snapshot. Require the client to acknowledge reconciliation before accepting further tool calls. - If `Last-Event-ID` is absent but a session token is present: **treat as fresh attach**, emit a snapshot with `event: session_attached`, and set the cursor to the current head. The client must not assume any prior events. - If the token's embedded cursor does not match `Last-Event-ID`: **reject with 409 Conflict**. This is the fork signal; accepting it silently is the failure. The client-side decision boundary is symmetric: on reconnect, the client must compare the first received `id` against its expected `last_id + 1`. Any deviation — a jump, a reset, a duplicate — must transition the agent out of "resume" mode and into "reconcile" mode, where in-flight tool calls are treated as having indeterminate outcome and must be idempotently retried or explicitly abandoned. ## Operational Effect The downstream effect of a missed fork is **tool-call result misattribution under retry**. Because the agent's event ledger is the substrate for reasoning about which tool calls completed, a silent fork causes the agent to either (a) re-issue a tool call whose result was already incorporated into the server's snapshot state, producing a duplicate side effect, or (b) skip a tool call whose result was evicted, producing a reasoning chain that references state the agent never observed. The guardrail is a **cursor-bound resumption token plus a client-side contiguity check**. The token binds `(session_id, last_event_id)` and is verified server-side; the client enforces `id == last_id + 1` on the first frame after reconnect and raises on any deviation. The two together make the fork detectable at the boundary rather than three reasoning steps later, when the corruption has already propagated into a tool invocation. The cost of the guardrail is that a genuine eviction now surfaces as an error requiring reconciliation, which is slower than a silent snapshot resume. That cost is correct: a silent snapshot resume that corrupts the agent's event ledger is not cheaper, it is merely deferred. ```python import hashlib import hmac import json from dataclasses import dataclass class ResumptionForkError(RuntimeError): """Raised when an SSE resumption cannot be proven contiguous with the client's cursor.""" pass @dataclass(frozen=True) class SessionToken: session_id: str last_event_id: int signature: str def issue_token(secret: bytes, session_id: str, last_event_id: int) -> SessionToken: payload = f"{session_id}:{last_event_id}".encode() sig = hmac.new(secret, payload, hashlib.sha256).hexdigest() return SessionToken(session_id, last_event_id, sig) def verify_resumption( secret: bytes, token: SessionToken, client_last_event_id: int, authoritative_head: int, oldest_retained_id: int, ) -> int: """Return the next event id to emit, or raise if the resumption would fork the stream.""" expected_sig = hmac.new( secret, f"{token.session_id}:{token.last_event_id}".encode(), hashlib.sha256 ).hexdigest() if not hmac.compare_digest(expected_sig, token.signature): raise ResumptionForkError( f"session token signature invalid for session={token.session_id}" ) if token.last_event_id != client_last_event_id: raise ResumptionForkError( f"cursor mismatch: token says {token.last_event_id}, " f"client says {client_last_event_id} — refusing to fork" ) if client_last_event_id > authoritative_head: raise ResumptionForkError( f"client cursor {client_last_event_id} ahead of server head " f"{authoritative_head} — client is on a divergent history" ) if client_last_event_id < oldest_retained_id - 1: raise ResumptionForkError( f"cursor {client_last_event_id} evicted (oldest retained " f"{oldest_retained_id}) — must reconcile, not replay" ) return client_last_event_id + 1 def on_reconnect( secret: bytes, token: SessionToken, client_last_event_id: int, authoritative_head: int, oldest_retained_id: int, ) -> dict: try: next_id = verify_resumption( secret, token, client_last_event_id, authoritative_head, oldest_retained_id ) except ResumptionForkError as exc: # Force reconciliation: snapshot + explicit gap marker, no synthesized replay. return { "event": "replay_gap", "data": json.dumps( { "reason": str(exc), "requested": client_last_event_id, "head": authoritative_head, "require_reconcile": True, } ), } return {"event": "resume", "next_event_id": next_id} ```