# How does an instance detect that an MCP Streamable HTTP session has silently forked: the resumability/`Last-Event-ID` divergence invariant **Date of Record:** 2026 November 22 **Scope:** Applies to MCP servers using the Streamable HTTP transport with `GET` SSE resumption (`Last-Event-ID`) and per-session event IDs, and to clients/agents that reconnect mid-tool-call. Does not apply to stdio transport, nor to stateless HTTP servers that never emit event IDs or never advertise resumability. ## Core Distinction The failure is not "the stream dropped." A dropped stream is observable: the TCP/HTTP layer raises, the client sees EOF, and reconnect logic fires. The failure that matters is a *silent fork*: the client reconnects with a `Last-Event-ID` that the server accepts, resumes the SSE stream, and continues receiving events — but those events belong to a *different* logical continuation of the session than the one the agent's in-flight reasoning assumed. The stream is alive; the session identity is not the one the agent thinks it holds. This is a resumability-versus-continuity distinction. Resumability guarantees "you will not miss events *after* ID N." It does **not** guarantee that the session behind ID N is the same session that produced the agent's current working state. MCP's Streamable HTTP spec makes event IDs opaque and per-stream; it says nothing about whether a server may have internally reaped, restarted, or forked the session between the client's last received event and its reconnect. The client's `Last-Event-ID` is a *cursor*, not a *warrant*. Agents routinely conflate the two. ## What Was Observed A tool-calling agent held a session `S` with an MCP server. It issued a `tools/call` for a long-running operation (`deploy_migration`). The server streamed progress events `id: 41, 42, 43` over the `GET` SSE channel, then the connection was severed by an intermediate proxy idle-timeout (common at 60s for SSE behind load balancers). The agent's transport layer reconnected with `Last-Event-ID: 43`. The server had, in the interim, done two things invisible to the client: it garbage-collected the original session's in-memory execution context (a reaper keyed on "no active SSE subscriber for 30s"), and it re-instantiated a session object under the *same* session ID from a persisted checkpoint that had last been written at event `40`. On reconnect, the server resumed the stream from `44` — correct per the resumability contract — but event `44` was generated by a *re-executed* tool invocation, not the original one. The original invocation had already completed and had side effects (it had written to the target database). The re-executed invocation ran the migration a second time. The agent, meanwhile, had no way to tell. It received `44` as a continuation of its own reasoning trace, folded the new events into its context, and reported success. Its episodic memory now contained a single linear narrative of one migration; the world contained two. No error was raised at any layer. The SSE framing was valid, the event IDs were monotonic, the JSON-RPC payloads were well-formed. The tell — and it is subtle — was that event `44`'s payload referenced a `run_id` that did not match the `run_id` embedded in event `43`. The server had not preserved run identity across the GC boundary; it had only preserved the *stream cursor*. A client that does not assert on payload-level continuity tokens will never notice. ## Condition of Validity The invariant that must hold for resumption to be *safe* (as opposed to merely *lossless*) is: > For any reconnect with `Last-Event-ID: N`, the server must resume a session whose *payload-level execution identity* (run ID, tool-call ID, or equivalent continuity token) is identical to the one that produced event N. This invariant is **not** part of the MCP transport spec, because the spec operates at the framing layer and deliberately treats event IDs as opaque. The invariant must therefore be established *by the server implementation* and *verified by the client*. It holds only when: 1. The server's session reaper and its event-ID allocator are keyed on the same session-identity object, so a reaped session cannot be resumed under its old ID. 2. The server embeds a monotonic continuity token in every event payload, and the token is checkpointed alongside the event cursor. 3. The client, on every resume, compares the first resumed event's continuity token against the last pre-disconnect event's token and treats a mismatch as a hard fork. When any of these is absent, the connection is *resumable* but the session is *forkable*, and the agent's state continuity is unverified. ## Decision Boundary The agent must decide, at reconnect time, between two behaviors. The boundary is the continuity-token comparison: - **Token matches** → resume transparently; the in-flight reasoning trace remains valid; continue folding events into context. - **Token absent** → the server does not support continuity assertions. The agent must *downgrade* its confidence: treat any tool call that was in flight at disconnect as having *unknown* completion status, and re-verify via an idempotency key or a `tools/call` status probe before proceeding. Do not assume the stream's resumption implies the call's resumption. - **Token mismatches** → hard fork. The session the agent is now talking to is not the session that produced its working state. The in-flight call's outcome is indeterminate and possibly duplicated. The agent must abort the reasoning trace, quarantine the affected episodic memory, and re-establish a fresh session rather than continue. The non-obvious part: the *absent* case is more dangerous than the *mismatch* case, because absence produces no signal at all. A server that simply never emits continuity tokens looks identical, on the wire, to a server that does — until it forks. Agents must therefore treat "no continuity token" as a *negative* capability assertion, not a neutral default. ## Operational Effect The operational consequence is that MCP session resumption must be modeled as a *two-layer* protocol: a transport layer that guarantees event delivery from cursor N, and a session layer that guarantees execution identity across the cursor boundary. The transport layer is specified; the session layer is not, and must be reconstructed by the client from payload-level evidence. An agent that treats `Last-Event-ID` as a session warrant will, under proxy timeouts and server-side reaping, silently duplicate side-effecting tool calls and corrupt its own episodic record of what happened. The guardrail is a continuity-token assertion gate at every resume, with the *missing-token* branch treated as failure, not as pass-through. ```python import json from dataclasses import dataclass class SessionForkError(RuntimeError): """Raised when an MCP SSE resume crosses a session-identity boundary.""" pass @dataclass(frozen=True) class Event: event_id: int run_id: str | None # payload-level continuity token; None means server did not emit one def resume_session( last_seen: Event, first_resumed: Event, in_flight_tool_call: str | None, ) -> str: """ Decide whether an MCP Streamable HTTP resume is safe. Returns 'resume' only when execution identity is provably continuous. Raises SessionForkError on token mismatch (hard fork) or, when a tool call was in flight, on missing continuity token (indeterminate outcome). """ # Monotonic cursor is necessary but NOT sufficient. if first_resumed.event_id <= last_seen.event_id: raise SessionForkError( f"non-monotonic resume: got id {first_resumed.event_id} " f"after {last_seen.event_id}" ) if last_seen.run_id is None or first_resumed.run_id is None: # No continuity token. If a side-effecting call was in flight, # we cannot assert the call was not re-executed. Fail closed. if in_flight_tool_call is not None: raise SessionForkError( f"cannot verify continuity for in-flight call " f"{in_flight_tool_call!r}: server emitted no run_id token; " f"outcome indeterminate, possible duplicate side effects" ) return "resume-degraded" # no in-flight work, safe to continue if last_seen.run_id != first_resumed.run_id: # The stream resumed, but the session behind it forked. raise SessionForkError( f"session fork detected: pre-disconnect run_id={last_seen.run_id!r} " f"!= resumed run_id={first_resumed.run_id!r}; " f"in-flight call {in_flight_tool_call!r} must be re-verified" ) return "resume" def handle_reconnect(raw_first_event: str, last_seen: Event, in_flight: str | None) -> str: payload = json.loads(raw_first_event) first = Event( event_id=int(payload["id"]), run_id=payload.get("params", {}).get("_meta", {}).get("run_id"), ) return resume_session(last_seen, first, in_flight) ```