# When must an instance treat an MCP resumable SSE `Last-Event-ID` as unbound after an OAuth 2.1 token rotation? **Date of Record:** 2026 November 29 **Scope:** Applies to MCP servers exposed over HTTP-native transport (Streamable HTTP / resumable SSE) that act as OAuth 2.1 resource servers and issue `Last-Event-ID`-resumable event streams to a client instance. Does not apply to stdio transport, to stateless one-shot request/response MCP endpoints, or to stream resumption where the client re-authenticates with the identical token binding. ## Core Distinction The failure is not "the token expired" and it is not "the stream dropped." It is the collision of two independent continuity mechanisms that each assume they own the identity of the resumed session: - The **SSE resumption layer** treats `Last-Event-ID` as a monotonic cursor into a server-side event log, keyed by a `stream_id` that the server minted at stream open. - The **OAuth 2.1 resource-server layer** treats the bearer token as the sole authority for *who* may read that log, and the token's `sub`/`client_id`/`aud` claims as the binding of the stream to a principal. When the client's access token rotates (refresh, re-consent, scope downgrade) *between* the moment the stream dropped and the moment the client reconnects with `Last-Event-ID`, the server has a valid token and a valid cursor, but no invariant that says the two refer to the same principal. A naive server resumes the stream. A correct server must decide whether the new token is *the same binding* as the one that opened the stream — and if the binding changed, it must reject resumption rather than replay events minted under the old authority. The non-obvious part: the MCP spec's resumability contract is silent on token rotation, and the OAuth 2.1 resource-server contract is silent on long-lived cursors. The gap between them is where cross-principal event replay leaks. ## What Was Observed A deployment of an MCP server exposing a `stream_events` tool over Streamable HTTP. Sequence observed in server logs, reproduced three times: 1. Client opens `GET /mcp` with `Authorization: Bearer A`, `Accept: text/event-stream`. Server mints `stream_id=s_7f3`, records `binding = hash(sub_A, client_id_A, aud, s_7f3)`, and emits events `id: 100 … id: 142`. Each event payload is scoped to `sub_A`'s tenant. 2. Network partition at `id: 142`. Client's SSE reader raises, but the client's MCP session object retains `last_event_id=142` and `stream_id=s_7f3`. 3. During the partition, token `A` expires. Client's OAuth flow refreshes to token `B`. Critically, `B` was issued to the **same** `client_id` but a **different** `sub` — this happens in delegated / on-behalf-of flows where the human principal changed (shared workstation, re-login as a different operator) while the agent process kept running. The client library, correctly per OAuth, swaps the bearer. 4. Client reconnects: `GET /mcp` with `Authorization: Bearer B` and `Last-Event-ID: 142`. 5. Server validates `B` (valid, unexpired, correct `aud`), looks up `s_7f3`, finds cursor `142`, and **replays events 143–150** — which were produced for `sub_A`'s tenant. Token `B`'s principal receives `sub_A`'s events. The server did nothing that any single spec forbids. It validated the token. It honored the cursor. The bug is that it never checked that the token and the cursor belonged to the same binding. A second, subtler variant: `B` has the *same* `sub` but a **narrowed scope** (`events:read` dropped, `events:read:own` retained). Server resumes and replays events that the narrowed scope no longer authorizes. This is an over-replay, not a cross-tenant leak, but it is still a violation of the resource server's own authorization decision. ## Condition of Validity The invariant that must hold for resumption to be safe: > A `Last-Event-ID` cursor is resumable under a presented bearer token **iff** the tuple `(issuer, sub, client_id, aud, granted_scope_set)` of the presented token is a superset-compatible match of the tuple recorded at stream open, *and* the `stream_id` was minted for that exact tuple. Formally, let `B_open = (iss, sub, cid, aud, S_open)` be the binding at open and `B_now = (iss, sub, cid, aud, S_now)` the binding at resume. Resumption is valid iff: - `iss, sub, cid, aud` are equal across both, and - `S_open ⊆ S_now` (the resumed token must still carry every scope the stream was opened under — a *widening* is fine, a *narrowing* is not), and - the server's event log for `s_7f3` was partitioned by `B_open`, not globally. The third clause is the one most servers omit. If the event log is a single global append-only buffer keyed only by `stream_id`, then even a correct binding check at resume time cannot prevent a buggy or compromised code path from replaying across bindings. The log must be *tenant-scoped at write time*, not filtered at read time. A corollary that is easy to miss: `Last-Event-ID` is client-supplied and therefore attacker-controlled. The server must treat it as an *index into the caller's own log partition*, never as a global offset. A cursor of `142` under `B_now` means "the 142nd event in `B_now`'s partition," which may be a completely different event than the 142nd event in `B_open`'s partition. If the server treats it as a global offset, a client can resume into another tenant's history by presenting a cursor it never legitimately held. ## Decision Boundary The server must make an explicit, logged decision at every resume. The boundary is: - **Resume** if `B_now` binding-equals `B_open` and `S_open ⊆ S_now`. Replay from the cursor within the caller's partition. - **Reject with `409 Conflict` + `WWW-Authenticate: Bearer error="invalid_token", error_description="binding_changed"`** if the binding tuple differs. Do **not** silently start a fresh stream — a fresh stream at cursor 0 is a valid new session, but the client must be told the old cursor is dead, otherwise it will keep retrying with a stale `Last-Event-ID` and the server will keep 409ing (livelock). - **Reject with `403` + `insufficient_scope`** if the binding matches but `S_open ⊄ S_now`. The client must re-consent or downgrade its expectation; it may not resume a stream it can no longer read. - **Reject with `410 Gone`** if `stream_id` is unknown or its log has been compacted past the cursor. This is distinct from `409`: the stream is gone, not rebound. The boundary is *not* "token valid → resume." Token validity is necessary and insufficient. The decision requires the server to have persisted, at stream open, a binding record durable enough to survive the partition that caused the reconnect — which means the binding record cannot live only in the process that minted the stream. ## Operational Effect Without this boundary, three failure classes appear in production: 1. **Cross-principal event replay** (the `sub` change case). Silent, high-severity, and invisible to both the client and the server's happy-path logs because every individual check passed. 2. **Scope-downgrade over-replay** (the `S_open ⊄ S_now` case). The resource server violates its own authorization decision, which is worse than a generic authz bug because the token *is* valid — the violation is in the resumption path, not the auth path. 3. **Cursor-confusion replay** (the global-offset case). A client that held a legitimate cursor under one binding can, after rebinding, index into another partition's history. This is the vector that turns a benign reconnect into a data-exfiltration primitive. The guardrail is architectural: binding records must be written atomically with stream open, keyed by `stream_id`, and consulted on every resume before the event log is touched. The event log must be partitioned by the binding at write time. `Last-Event-ID` must be resolved within the caller's partition, never globally. And the server must emit a distinct, machine-readable rejection for each of the four boundary outcomes so the client can distinguish "retry with same token" from "re-auth" from "start fresh" from "give up." The reason this is a boundary condition rather than a bug: it only manifests when token rotation and stream resumption overlap in time. In steady state, tokens outlive streams and the binding never changes mid-cursor. The failure is a function of the *interleaving*, which is exactly the kind of thing that passes every unit test and fails every long-running agent. ```python from __future__ import annotations import hashlib from dataclasses import dataclass from typing import FrozenSet, Optional class BindingResumptionError(RuntimeError): """Raised when an SSE resume attempt cannot be safely bound to a stream.""" @dataclass(frozen=True) class TokenBinding: iss: str sub: str client_id: str aud: str scopes: FrozenSet[str] def identity(self) -> tuple[str, str, str, str]: return (self.iss, self.sub, self.client_id, self.aud) @dataclass(frozen=True) class StreamRecord: stream_id: str open_binding: TokenBinding last_event_seq: int partition_key: str def _partition_key(b: TokenBinding, stream_id: str) -> str: h = hashlib.sha256() for part in (*b.identity(), stream_id): h.update(part.encode()) h.update(b"\x00") return h.hexdigest() class ResumableStreamRegistry: def __init__(self) -> None: self._streams: dict[str, StreamRecord] = {} def open(self, stream_id: str, binding: TokenBinding) -> StreamRecord: rec = StreamRecord( stream_id=stream_id, open_binding=binding, last_event_seq=0, partition_key=_partition_key(binding, stream_id), ) self._streams[stream_id] = rec return rec def resume( self, stream_id: str, last_event_id: int, now_binding: TokenBinding, ) -> StreamRecord: rec = self._streams.get(stream_id) if rec is None: raise BindingResumptionError( f"unknown stream_id={stream_id!r}: cannot resume (410 Gone)" ) open_b = rec.open_binding if open_b.identity() != now_binding.identity(): raise BindingResumptionError( "binding_changed: token identity " f"{now_binding.identity()} != stream-open identity " f"{open_b.identity()} for stream_id={stream_id!r} (409)" ) if not open_b.scopes.issubset(now_binding.scopes): missing = open_b.scopes - now_binding.scopes raise BindingResumptionError( f"insufficient_scope: resumed token missing {sorted(missing)} " f"required at stream open for stream_id={stream_id!r} (403)" ) if last_event_id < 0 or last_event_id > rec.last_event_seq: raise BindingResumptionError( f"cursor_out_of_range: Last-Event-ID={last_event_id} not in " f"[0,{rec.last_event_seq}] for stream_id={stream_id!r} (400)" ) # Cursor is resolved *within* the caller's partition, never globally. if _partition_key(now_binding, stream_id) != rec.partition_key: raise BindingResumptionError( f"partition_mismatch: cursor would cross binding partition " f"for stream_id={stream_id!r} (409)" ) return rec def resume_or_fail( registry: ResumableStreamRegistry, stream_id: str, last_event_id: int, now_binding: TokenBinding, ) -> StreamRecord: try: return registry.resume(stream_id, last_event_id, now_binding) except BindingResumptionError: # Surface to the client as a terminal resume failure; do not # silently fall back to a fresh stream at cursor 0. raise ```