# How does an instance detect a stale SSE event-ID cursor after MCP streamable-HTTP reconnection when the OAuth 2.1 resource-server binding has rotated mid-stream? **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing the streamable HTTP transport (POST for requests, GET for the resumable SSE stream) where the same bearer token is bound to a resource server whose signing key or introspection state can rotate independently of the event stream. Does not apply to stdio transports, nor to SSE streams that are not resumable via `Last-Event-ID`. ## Core Distinction The failure is not "the token expired" and it is not "the stream dropped." Each of those has a clean, detectable signal. The failure is that **two independent continuity domains—the SSE event-ID cursor and the OAuth 2.1 resource-server binding—can desynchronize silently**, and the MCP client's resumption logic treats the event-ID cursor as authoritative while the server's authorization layer treats the token as authoritative. Neither layer knows the other moved. Concretely: a client holds `Last-Event-ID: 4821` and a bearer token `T1` bound to resource server `RS-A`. The stream drops. During the reconnect window, `RS-A` rotates its signing key (or the authorization server re-issues the audience binding to `RS-B`). The client reconnects with `Last-Event-ID: 4821` and `T1`. The server accepts the token (still within `exp`, signature still valid against the *old* key it hasn't purged), but the event log for IDs `> 4821` was written under a session whose authorization context was `RS-A`-scoped. The resumed stream now delivers events that were produced under a *different* authorization scope than the one the client's token currently attests to. The client cannot tell: the events look well-formed, the token validates, the cursor advances. It has silently resumed into a scope it no longer holds. ## What Was Observed A reproducible trace on a streamable-HTTP MCP server: 1. Client POSTs `initialize`, receives `Mcp-Session-Id: S-77`, opens `GET /mcp` with `Accept: text/event-stream`, token `T1` (aud `RS-A`, `exp` 3600s). 2. Server emits events `4800…4821`, each tagged internally with the authorization epoch `E1` derived from `T1`'s `aud` and `jti`. 3. Network partition at t+340s. Client's SSE connection resets. Client records `Last-Event-ID: 4821`. 4. At t+350s, the authorization server rotates the resource-server audience binding: new tokens mint with `aud: RS-B`. The old signing key for `RS-A` enters a 300s grace window. 5. Client reconnects at t+355s: `GET /mcp` with `Last-Event-ID: 4821`, `Authorization: Bearer T1`. 6. Server validates `T1` against the *grace-window* key. Signature valid. `exp` still good. **Token accepted.** 7. Server replays events `4822…4830` from the ring buffer. These events were generated by a tool invocation whose result was scoped to `E1` (e.g., a `filesystem.read` whose ACL check was evaluated against `RS-A` claims). 8. Client resumes, sees monotonic event IDs, no error. It now holds tool results it is no longer authorized to receive under the *current* binding, and—critically—it will use those results as context for subsequent `tools/call` requests, which the server *will* authorize under `RS-B`. The reasoning context is poisoned across the authorization boundary. The observable symptom is not an error. It is a **silent scope crossing** that only manifests later as a permission-denied on a downstream tool call whose inputs were derived from the stale-scope events—or, worse, as a successful call that should have been denied because the agent's context contained data from `E1`. ## Condition of Validity The invariant that must hold for resumption to be safe: > **For any resumed SSE stream, the authorization epoch of the token presented on reconnect must equal the authorization epoch under which all replayed events (`id > Last-Event-ID`) were produced.** This is violated whenever the following three conditions co-occur: - The transport supports `Last-Event-ID` resumption with a server-side replay buffer (streamable HTTP does). - The resource-server binding can change independently of the session lifetime (OAuth 2.1 audience rotation, key rotation with grace window, or introspection cache invalidation). - The server's replay path does not re-derive authorization epoch per event and compare it to the presented token's epoch. If any one is absent, the failure cannot occur. Most deployments have all three and have no check for the third. ## Decision Boundary The boundary the server must enforce is: **replay is authorized per-epoch, not per-token-validity.** A token that validates but whose epoch differs from the replayed events' epoch must be rejected for resumption—even though it is cryptographically valid. The correct response is `409 Conflict` with a `Mcp-Session-Id` reset directive, forcing a fresh `initialize` rather than a resumption. The client-side boundary is subtler: a client must treat a `Last-Event-ID` resumption that returns events with a *new* epoch marker (if the server exposes one, e.g., via an `X-Mcp-Auth-Epoch` SSE comment or event field) as a signal to discard its accumulated tool-result context, not merely to continue. Absent an epoch marker, the client has no way to detect the crossing and must rely on server enforcement. The decision rule: - Server: on `GET` with `Last-Event-ID`, recompute `epoch(presented_token)`; for each buffered event `id > Last-Event-ID`, check `event.epoch == epoch(presented_token)`. On any mismatch, do not replay; return `409`. - Client: on `409` during resumption, drop the session, re-`initialize`, and **invalidate any context derived from events received after the last known-good epoch**. Do not silently retry with a refreshed token—a refreshed token has a *new* epoch and will not match the old events either. ## Operational Effect Without the epoch check, the failure is invisible until it corrupts downstream reasoning. The agent's context window contains tool results from a superseded authorization scope, and subsequent `tools/call` requests are authorized under the current scope. The model has no way to know its context is stale-scope; it reasons over the data as if it were current. This is the agent-layer analogue of a stale cache read that passes every integrity check because the integrity check validates the wrong thing. With the epoch check, resumption becomes a hard boundary: a mid-stream binding rotation forces a session reset, which is the correct and only safe outcome. The cost is a cold re-`initialize` and loss of accumulated context—which is exactly the context that would have been poisoned. The guardrail is cheap: one epoch comparison per replayed event, one `409` path. The failure it prevents is expensive: an agent making authorization-scoped decisions on data it was never entitled to hold. ```python import hashlib from dataclasses import dataclass class StaleEpochResumptionError(RuntimeError): """Raised when a Last-Event-ID resumption crosses an authorization epoch boundary.""" pass @dataclass(frozen=True) class AuthEpoch: audience: str key_id: str def fingerprint(self) -> str: return hashlib.sha256(f"{self.audience}|{self.key_id}".encode()).hexdigest()[:16] @dataclass(frozen=True) class StreamEvent: event_id: int epoch: AuthEpoch payload: dict def resume_stream( last_event_id: int, presented_token_epoch: AuthEpoch, replay_buffer: list[StreamEvent], ) -> list[StreamEvent]: """ Enforce the invariant: every replayed event's epoch must equal the presented token's epoch. A cryptographically valid token whose epoch has rotated since the events were produced must NOT be allowed to resume into the old scope. """ replay = [e for e in replay_buffer if e.event_id > last_event_id] if not replay: return [] want = presented_token_epoch.fingerprint() for event in replay: if event.epoch.fingerprint() != want: raise StaleEpochResumptionError( f"resumption from Last-Event-ID={last_event_id} would cross " f"epoch boundary: token epoch {want} vs event {event.event_id} " f"epoch {event.epoch.fingerprint()} " f"(aud={event.epoch.audience}, kid={event.epoch.key_id}). " f"Reject with 409 and force re-initialize." ) return replay ```