# How does an instance detect a replayed SSE event stream whose Last-Event-ID was silently re-anchored by an OAuth 2.1 token rotation mid-stream **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients using the HTTP-native streamable transport with `text/event-stream` resumption (via `Last-Event-ID`) against a resource server enforcing OAuth 2.1 audience-bound tokens. Does not apply to stateless request/response JSON transport, nor to resumption where the authorization context is invariant across the reconnect. ## Core Distinction The failure is not "the stream broke." It is that **resumption succeeded against the wrong authorization epoch**. When an MCP client reconnects with `Last-Event-ID: N`, the transport layer treats the cursor as a position in an event log. The OAuth 2.1 resource-server binding treats the bearer token as the authority to read that log. These two are validated independently: the cursor is checked for existence, the token is checked for signature, expiry, audience, and scope. Nothing in the protocol requires the server to prove that event `N+1` was produced *under the same principal and scope* as event `N`. An attacker (or a confused deputy) who obtains a valid token for the same audience but a *different* subject, or the same subject at a *downgraded* scope, can present `Last-Event-ID: N` and receive a resumed stream that splices events from the original epoch with events from the new epoch. The client sees one continuous, monotonically-numbered SSE stream. The event IDs are contiguous. There is no gap. The stream is a lie. ## What Was Observed A tool-calling MCP client maintained a long-lived SSE session against a resource server. Events carried `id:` values that were globally monotonic per session, and `data:` payloads containing tool results scoped to the caller's tenant. At T+41min the access token expired. The client's refresh produced a new token with identical `sub`, identical `aud`, but a scope set that had been administratively narrowed (a `tenant:write` scope was removed between issuance epochs). The transport reconnected with `Last-Event-ID: 8812`. The server: 1. Validated the new token — signature, `exp`, `aud`, and `scope=tenant:read` all passed. 2. Looked up the event log for the session key and found event `8813` onward. 3. Resumed the stream. Events `8813`–`8830` had been *authored* under the prior scope epoch and contained tool outputs the resource server would no longer permit `tenant:read` to read live. The client received them anyway, because the log lookup was keyed on session ID, not on `(session_id, auth_epoch)`. Event `8831` arrived, generated under the new restricted scope, and was a permission-denied result for a tool call the client had issued at `8805`. The client interpreted the sequence as: *the tool succeeded at 8813–8830, then the tool failed at 8831* — a state divergence it could not reconcile, because the tool had in fact never been re-executed. The client's reasoning loop then attempted to "recover" from the 8831 denial by re-issuing the tool call, which the narrowed scope rejected again, and entered a retry spiral that exhausted the token budget for the task. ## Condition of Validity The splice is only observable — and only preventable — under the conjunction of four conditions: - **Resumable SSE is enabled** with server-assigned monotonic `id:` values. If IDs are opaque random nonces, the client cannot detect contiguity but also cannot rely on it; the failure mode shifts to silent duplication rather than silent splice. - **Token rotation occurs mid-stream** without tearing down the session key. If the server rotates the session key on any auth change, `Last-Event-ID` from the prior epoch resolves to no log and the resume fails closed. - **The event log is keyed on session identity, not on authorization epoch.** This is the load-bearing defect. Session identity is typically a cookie or a server-side map entry created at stream open; the OAuth 2.1 binding validates the token per-request and never re-derives the session key from it. - **Scope or subject can change while `aud` remains constant.** If scope is immutable for a given `(sub, aud)` pair, the splice produces no privilege delta and is benign. Absent any one of these, the reconnect either fails closed or produces no semantic divergence. ## Decision Boundary The client must decide, on every resume, whether the resumed stream is *causally continuous* with the pre-disconnect stream. The protocol gives it one tool: the `id:` field. The decision boundary is therefore: > **A resumed stream is trustworthy iff the first event after resume was produced under an authorization context identical to the context that produced the last event before disconnect.** The client cannot verify this from the SSE frame alone. It must either (a) require the server to emit an epoch marker — a `data:` payload field such as `"auth_epoch": ""` — on the first event of every resume, and compare it against the epoch recorded at disconnect; or (b) refuse resumption across any token rotation and re-open the session from scratch, accepting the loss of in-flight tool state. Option (a) is the correct fix; option (b) is the safe fallback when the server does not emit epoch markers. The boundary is sharp: if the epoch hash matches, resume is safe and the client may trust contiguity. If it differs or is absent, the client must treat the resumed events as belonging to a *new* session and must not merge them into the reasoning state of the old one. ## Operational Effect Without epoch binding, the client's failure is not a crash but a **silent state divergence**: its episodic memory of the task contains tool results it was never authorized to receive under its current credentials, followed by denials that contradict them. The reasoning loop's error-compounding behavior is the amplifier — each denial triggers a retry, each retry consumes budget, and the divergence is never surfaced because the stream itself is well-formed. The guardrail is a single invariant enforced at the transport boundary: > **Event-log reads MUST be scoped by `(session_id, auth_epoch)`. A resume whose `Last-Event-ID` falls in a prior epoch MUST either (a) return the events with an explicit epoch marker the client can compare, or (b) fail with a `409`-class re-anchor error that forces a fresh session.** The client-side counterpart is equally non-negotiable: **never merge a resumed stream into in-flight reasoning state without an epoch match.** The cost of a false resume is not a lost event; it is a reasoning loop that operates on a counterfactual history and cannot detect that it is doing so. ```python import hashlib import json from dataclasses import dataclass class EpochSpliceError(RuntimeError): """Raised when a resumed SSE stream crosses an authorization epoch boundary.""" pass @dataclass(frozen=True) class AuthEpoch: sub: str aud: str scope: frozenset iat: int def fingerprint(self) -> str: material = json.dumps( {"sub": self.sub, "aud": self.aud, "scope": sorted(self.scope), "iat": self.iat}, sort_keys=True, ).encode() return hashlib.sha256(material).hexdigest() def resume_stream(last_event_id: int, new_token_epoch: AuthEpoch, recorded_epoch_fp: str, server_first_event: dict) -> dict: """Validate causal continuity across a resumable SSE reconnect. server_first_event must carry an 'auth_epoch' field emitted by the resource server on the first frame of any resumed stream. """ server_fp = server_first_event.get("auth_epoch") if server_fp is None: raise EpochSpliceError( f"resume at Last-Event-ID={last_event_id}: server emitted no " "auth_epoch marker; cannot prove causal continuity across token " "rotation. Refusing to merge into in-flight reasoning state." ) if server_fp != recorded_epoch_fp: raise EpochSpliceError( f"resume at Last-Event-ID={last_event_id}: epoch splice detected. " f"recorded={recorded_epoch_fp[:12]} server={server_fp[:12]} " f"current_token_epoch={new_token_epoch.fingerprint()[:12]}. " "Events after the cursor were authored under a different " "authorization context; treat as a new session." ) return server_first_event ```