# What structural signals indicate a resumable MCP SSE stream has silently forked into a duplicate event history **Date of Record:** 2026 November 19 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with `text/event-stream` responses and `Last-Event-ID`-based resumption. Does not cover stdio transport, WebSocket transports, or SSE endpoints that emit no `id:` field on events. ## Core Distinction The distinction that matters here is between **transport-level resumption** and **logical stream identity**. The MCP HTTP-native transport lets a client reconnect to a stream by supplying `Last-Event-ID`, and the server is expected to replay events strictly after that ID. This is a transport contract, and it is satisfiable by a server that has *no* durable notion of which logical session the stream belonged to. The failure mode is not "replay is missing" or "reconnect fails"—those are loud. The failure mode is that a reconnect *succeeds*, the client receives a well-formed event stream, and yet the event history the client now holds is a **fork** of the server's canonical history rather than a suffix of it. The client's `Last-Event-ID` cursor and the server's event log no longer share a linearization, and nothing in the SSE framing signals the divergence. ## What Was Observed A concrete trace from a server that used per-connection monotonic event IDs (`event_id = ++connection_counter`) rather than a session-scoped monotonic sequence. The server maintained an in-memory ring buffer of the last 1024 events *per session*, but the counter was per-connection. 1. Client `C` opens stream, receives events `id: 1..40` for session `S`. The session's canonical log advances to `seq=40`, but the connection counter is at `40` only because this is the first connection. 2. Network blip. Client reconnects with `Last-Event-ID: 40` and `Mcp-Session-Id: S`. Server accepts, opens a *new* connection, and the new connection's counter starts at `0`. The server's replay logic is keyed on `Last-Event-ID` interpreted against the *session* log, so it correctly replays `seq=41..43`. But it labels them `id: 1,2,3` because the ID generator is the connection counter. 3. The client's next reconnect sends `Last-Event-ID: 3`. The server looks up `3` in the session log, finds `seq=3` (an event from the *original* connection, long since consumed), and replays `seq=4..N`. The client has now received events `1..40`, `41..43`, and `4..N`—overlapping, out of order, and with duplicate logical events under different transport IDs. 4. The client's tool-call state machine, which assumes a monotonically increasing cursor, either deadlocks waiting for `seq=44` (never arrives because the replay restarted at 4) or applies `seq=4..N` a second time, re-executing side-effecting tool calls. The tell is structural, not semantic: **the `id:` values the client receives after a resume are not strictly greater than the `Last-Event-ID` it sent.** A correct server guarantees `replayed_id > last_event_id` for every replayed event. A forked server violates this while still emitting a syntactically valid stream. ## Condition of Validity `Last-Event-ID` resumption is only sound when the following invariant holds at the server: ``` ∀ events e replayed in response to Last-Event-ID = L: e.id is drawn from a total order O_s that is (a) scoped to the logical session, not the connection, (b) strictly monotonic, and (c) the same order used to assign L originally. ``` If any of (a), (b), (c) fails, the client cannot distinguish "resume succeeded" from "resume forked." The condition is *not* satisfied by per-connection counters, by timestamps with clock skew, or by UUIDs (which have no order). It *is* satisfied by a session-scoped 64-bit sequence persisted alongside the event log, even if the log itself is a bounded ring buffer—provided that when the requested `L` has fallen off the back of the ring, the server responds with an explicit **resumption failure** (e.g., MCP `error` with a resumption-unsupported code) rather than replaying from the oldest available event. Silently replaying from the buffer head is the second half of the fork. ## Decision Boundary The boundary an operator must draw: - **Resume is valid** iff the requested `Last-Event-ID` is present in the session's durable event log *and* the server can replay every event with `id > L` in order. In this case the client may treat the resumed stream as a continuation and must not re-initialize session state. - **Resume is a fork** iff the server cannot prove the above. The client must then *discard* its local cursor, treat the stream as a fresh logical session, and re-issue any in-flight tool calls idempotently (or abort them if not idempotent). Continuing to use the stale cursor is the bug. - **Resume is a hard failure** iff the server returns an explicit resumption error. This is the *safe* case; it forces the client into the fork-handling path. The non-obvious part: a server that returns a fresh stream with new IDs and no error is *worse* than one that refuses the resume, because it converts a detectable failure into an undetectable one. ## Operational Effect Downstream, this manifests as three distinct symptoms that operators routinely misattribute: 1. **Duplicate side effects** on the tool-execution layer, because the client re-applies replayed events it had already consumed. The tool layer sees two `call_id`s for one logical action unless the client dedupes on a session-scoped key—which it cannot construct from per-connection IDs. 2. **Cursor deadlock**, where the client's expected next ID never arrives and the agent's reasoning loop stalls at a `waiting_for_event` state until the token budget or wall-clock timeout fires. 3. **Auditor disagreement** in multi-agent setups: an auditor agent replaying the same session log from a different connection observes a different event sequence and flags the primary agent's transcript as inconsistent, when in fact both are faithful to their respective (forked) streams. The guardrail is to make the invariant *checkable by the client*: require the server to emit a session-scoped `seq` inside each event's `data` payload (not merely the SSE `id:`), and have the client assert `seq == last_seq + 1` on every event after a resume. A single mismatch is a fork signal and must trigger the discard-and-reinitialize path. ```python import json from dataclasses import dataclass, field class StreamForkDetected(RuntimeError): """Raised when a resumed SSE stream violates the monotonic-seq invariant.""" pass @dataclass class ResumableStream: session_id: str last_seq: int = 0 _seen: set = field(default_factory=set) def consume(self, raw_event: dict, *, resuming: bool) -> dict: seq = raw_event.get("seq") if seq is None: raise StreamForkDetected( f"session {self.session_id}: event lacks session-scoped seq; " f"transport id {raw_event.get('id')!r} is not a valid cursor" ) if resuming: expected = self.last_seq + 1 if seq != expected: # Either a gap (replay truncated) or a duplicate (fork/replay-from-head). raise StreamForkDetected( f"session {self.session_id}: resume violated monotonic seq; " f"expected {expected}, got {seq}. Discard cursor and reinitialize." ) else: if seq <= self.last_seq: raise StreamForkDetected( f"session {self.session_id}: non-monotonic seq {seq} " f"after {self.last_seq}" ) if seq in self._seen: raise StreamForkDetected( f"session {self.session_id}: duplicate logical event seq={seq}" ) self._seen.add(seq) self.last_seq = seq return raw_event def handle_reconnect(stream: ResumableStream, server_events: list) -> list: """Process replayed events after a Last-Event-ID reconnect.""" applied = [] for ev in server_events: try: applied.append(stream.consume(ev, resuming=True)) except StreamForkDetected: # Fork is unrecoverable in-place: caller must reinitialize session # and re-issue in-flight tool calls idempotently. raise return applied ```