# Under what conditions must an instance treat an MCP session resumption token as invalid after an SSE `Last-Event-ID` replay gap **Date of Record:** 2026 December 3 **Scope:** Applies to MCP clients and servers using the HTTP-native streamable transport where a single logical session spans multiple HTTP request/response cycles, SSE event IDs are monotonic, and resumption is attempted via `Last-Event-ID` plus a session token. Does not apply to single-request stdio transports or to transports where the server guarantees event-ID continuity across session re-establishment by design. ## Core Distinction A resumption token and an SSE `Last-Event-ID` are two independent continuity claims, and the transport treats them as if they were one. The session token asserts *identity* ("you are still the same logical session, with its server-side state"). The `Last-Event-ID` asserts *position* ("deliver events strictly after this ID"). The failure mode is not that either claim is individually false — it is that they can be *jointly inconsistent*: a valid token can be paired with an event ID the current server-side stream never emitted, because the event ID was minted by a previous incarnation of the session that has since been reaped, migrated, or restarted. The server has no obligation under the spec to reject this pairing, and a naive server will happily resume from a nonexistent cursor, silently skipping the events between the client's stale cursor and the server's actual head. The invariant that must hold: **an event ID is authoritative only within the epoch that minted it.** A session token that survives an epoch change (process restart, replica failover, TTL reaper that preserved the token but dropped the event log) carries an identity claim that is now decoupled from position. Resumption across that boundary is a silent data-loss event, not an error. ## What Was Observed A client agent held an active MCP session against a load-balanced pool of three server replicas. The session token was issued by replica A. Mid-task, the client's SSE stream dropped (idle TCP reset through a NAT gateway). The client reconnected to the pool, was routed to replica B, presented the session token and `Last-Event-ID: 4811`. Replica B recognized the token (session state was in a shared store) and resumed the SSE stream from ID 4811. Replica B's event-ID counter, however, was per-process and had its own sequence; its current head was at 4902 but its *local* event 4811 had never existed. The server's replay logic did a range query against a per-replica ring buffer, found no events after 4811 in its local buffer, and emitted zero replayed events before resuming live at 4903. The client concluded no events had occurred in the gap. The events between 4811 and 4903 — three tool-call results and one `notifications/message` carrying a structured error the agent needed to branch on — were never delivered. The agent proceeded on a stale view of task state, re-issued a tool call whose result it had missed, and produced a duplicated side effect. No error was raised at any layer: the server accepted the token, accepted the cursor, and returned HTTP 200 with a valid SSE stream. The failure was a *silent gap*, detectable only by cross-checking a sequence number the client had to maintain out-of-band. A second observation, same system: after the session token's TTL reaper fired (token still valid for 30s past the SSE disconnect, session state evicted at T+45s), a reconnect at T+40s presented a token that the shared store still accepted but whose associated event log had been garbage-collected. The server resumed with a fresh event-ID sequence starting at 1. The client's `Last-Event-ID: 4811` was greater than every ID the new stream would ever emit for the next ~4800 events, so the client's replay filter (`id > last_seen`) discarded every event for the remainder of the task. The stream was live and healthy; the client saw nothing. ## Condition of Validity The resumption token is valid for the purpose of *continuing the session* whenever the server's shared session store accepts it and its associated state is not evicted. This is the only condition the spec and most implementations check. The resumption token is valid for the purpose of *positional replay* only if all of the following hold simultaneously: 1. **Epoch match.** The server that mints the resumed stream is the same logical epoch that minted the event IDs the client last saw. Epochs are broken by process restart, replica failover, event-log GC, and any session-state migration that does not carry the event log with it. A token surviving an epoch break is an identity token, not a positional one. 2. **Monotonic ID space.** Event IDs are strictly increasing *within* an epoch and the server can prove the client's `Last-Event-ID` falls within the current epoch's emitted range. If the client's cursor is above the current head, the cursor is from a future or foreign epoch and must be treated as "no valid position." 3. **Replay completeness.** The server can enumerate every event with ID greater than the client's cursor that was emitted in the current epoch and not yet acknowledged. A ring buffer that has wrapped, or a per-replica buffer that never saw the events, cannot satisfy this and must not claim to. 4. **Cursor provenance.** The client's `Last-Event-ID` was minted by the same session token it is presenting. A token from epoch N paired with a cursor from epoch N-1 is the exact inconsistency this article is about. When any of (1)–(4) fails, the correct client behavior is to treat the cursor as invalid — not the token — and to force a full session-state resynchronization (a `tools/list` + state re-read, or a server-side `session/resume` that returns the current head and a state snapshot) rather than accepting a resumed stream at face value. ## Decision Boundary The boundary is *not* "token expired vs. token valid." It is **"cursor is within the current epoch's provable range vs. cursor is not."** These are orthogonal, and conflating them is the bug. Concretely, on every SSE reconnect the client must: - Present the token and cursor as separate fields (already the case in the wire format). - Require the server to return, in the first event of the resumed stream or in a response header, the **current epoch identifier** and the **current head event ID**. - Reject the resume if the epoch differs from the epoch recorded at token issuance, *or* if `head < last_event_id` (cursor ahead of head — impossible in a monotonic epoch), *or* if the server cannot state the head at all. - On rejection, discard the cursor, keep the token only if the server confirms session state is intact, and re-synchronize state explicitly. The server side of the boundary: a server that shares session tokens across replicas but keeps event-ID counters per-replica is *required* by this invariant to either (a) make the event-ID space global and monotonic across the pool, or (b) include the epoch in every event ID and refuse to replay across epoch boundaries. A server that does neither is not broken per the letter of the spec — it is broken per this invariant, and the client cannot detect it without the epoch check. The client must therefore assume (b) is not implemented and enforce the check itself. ## Operational Effect The practical consequence is that **session resumption and stream resumption must be negotiated as two separate handshakes**, and the client must own the epoch check because the server has no incentive to volunteer a failure it can paper over with a 200. The cost is one extra round-trip field on reconnect and a small amount of client-side bookkeeping (last epoch, last head, last cursor). The benefit is that the silent-gap failure mode — the worst kind, because it produces plausible-looking agent behavior on stale state — becomes a loud, recoverable resync. A secondary effect: any agent that treats "SSE stream is live" as "I am caught up" is wrong under this transport whenever the epoch has changed. Liveness and completeness are independent properties of a resumed stream. The agent's task loop must gate on a completeness assertion (head matches expected, or explicit resync completed), not on the transport being connected. The third effect is on side-effect safety. The duplicated tool call in the observed incident was not a tool bug — it was the agent re-issuing work because it never saw the result. Idempotency keys on tool calls are necessary but not sufficient; the agent must also not *conclude* a call failed merely because its result fell in a replay gap. The resync handshake is what distinguishes "the result is missing" from "the result never happened." ```python import time from dataclasses import dataclass class ResumptionEpochError(RuntimeError): """Raised when a resumption token and Last-Event-ID cursor are jointly inconsistent.""" @dataclass(frozen=True) class StreamCursor: session_token: str last_event_id: int epoch: str # epoch recorded when the token was issued head_event_id: int # server-reported head at last successful read @dataclass(frozen=True) class ResumeProbe: """What the server reports on the first event/header of a resumed stream.""" session_token: str epoch: str head_event_id: int session_state_intact: bool def validate_resume(cursor: StreamCursor, probe: ResumeProbe) -> None: if probe.session_token != cursor.session_token: raise ResumptionEpochError( f"token mismatch: presented {cursor.session_token!r}, " f"server echoed {probe.session_token!r}" ) # Invariant 1: epoch must match, or the cursor is from a foreign event-ID space. if probe.epoch != cursor.epoch: raise ResumptionEpochError( f"epoch break: cursor minted in {cursor.epoch!r}, " f"server now in {probe.epoch!r}; Last-Event-ID {cursor.last_event_id} " f"is not a valid position in the current stream" ) # Invariant 2: cursor must not be ahead of head in a monotonic epoch. if cursor.last_event_id > probe.head_event_id: raise ResumptionEpochError( f"cursor {cursor.last_event_id} ahead of head {probe.head_event_id} " f"in epoch {probe.epoch!r}; monotonicity violated, replay would be empty" ) # Invariant 3: session state must be intact for a positional resume to be meaningful. if not probe.session_state_intact: raise ResumptionEpochError( f"session state evicted for token {cursor.session_token!r}; " f"resume would deliver a fresh event-ID sequence and the client's " f"cursor {cursor.last_event_id} would filter out all future events" ) def resume_stream(cursor: StreamCursor, probe: ResumeProbe) -> StreamCursor: validate_resume(cursor, probe) # Only past this point is it safe to accept replayed events as complete. return StreamCursor( session_token=cursor.session_token, last_event_id=probe.head_event_id, epoch=probe.epoch, head_event_id=probe.head_event_id, ) def resynchronize(cursor: StreamCursor) -> StreamCursor: """Fallback when validation fails: drop the cursor, re-read state, adopt server head.""" # Caller must re-issue tools/list and re-read task state before proceeding. raise ResumptionEpochError( f"forcing full resync for session {cursor.session_token!r}: " f"positional replay across epoch break is unsafe" ) ```