# Which invariants prevent resumable SSE replay from silently rebinding an MCP session to a stale OAuth 2.1 resource audience after transport upgrade **Date of Record:** 2026 November 24 **Scope:** Applies to MCP clients and servers using the HTTP-native streamable transport with `Last-Event-ID` resumable SSE fallback and OAuth 2.1 resource-server token binding (RFC 8707 `resource` indicator). Does not apply to pure stdio transports, nor to non-resumable SSE where no event-ID cursor is negotiated. ## Core Distinction The distinction that matters is between **transport continuity** and **authorization continuity**. A resumable SSE stream guarantees that event sequence numbers survive a network fault; it says nothing about whether the bearer token that authorized the *original* stream is still the token that the *resumed* stream is authorized under. In the HTTP-native streamable transport, the client may upgrade from a single POST-with-SSE-response to a long-lived GET SSE stream, or fall back to resumable SSE after a 502. Each of these transitions re-presents credentials at the resource server. If the resource server validates the token against the audience it derived at session creation rather than the audience in the newly presented token, a *different* resource server (or a different tenant binding) can be silently accepted on the resumed stream. The event-ID cursor then resumes at a sequence number that belongs to the old authorization context. The invariant that prevents this is: **the resumable cursor must be scoped to the (session_id, resource_audience, token_jkt) tuple, and any resume whose tuple differs must fail closed with a re-initialize requirement, not a silent replay.** ## What Was Observed A client established an MCP session against `mcp://tenant-a.example/stream` with an OAuth 2.1 access token whose `resource` claim was `https://mcp.tenant-a.example`. The server issued `Mcp-Session-Id: s-7f3a` and began streaming SSE events with monotonic `id:` values (`evt-0001` … `evt-0412`). Mid-stream, a load balancer returned 502 and the client reconnected with `Last-Event-ID: evt-0412` and a *refreshed* access token. The refresh had been performed against the same authorization server but with a `resource` parameter that, due to a client-side default, resolved to `https://mcp.example` (the umbrella host) rather than the tenant-scoped audience. The resource server's token introspection accepted the token because the umbrella audience was in its allow-list. The resumed stream accepted `Last-Event-ID: evt-0412` and continued emitting from `evt-0413` — but the events were now drawn from the *umbrella* tenant's fan-out, not tenant-a's. The client's agent loop consumed `evt-0413` onward as if they were tenant-a state, and a subsequent tool call `write_file` executed against the umbrella tenant's workspace. The failure trace is precise: 1. Session created with `(s-7f3a, aud=tenant-a, jkt=K1)`. 2. Token refresh produces `(aud=umbrella, jkt=K1)` — same key, different audience. 3. Resume request carries `Last-Event-ID: evt-0412` and the new token. 4. Server validates token → audience `umbrella` is in allow-list → **accepts**. 5. Server looks up cursor by session ID only → finds `evt-0412` → **replays from evt-0413**. 6. Cursor's authorization context (`tenant-a`) is never compared to the presented context (`umbrella`). The bug is not in token validation and not in event replay. It is in the *join* between them: the cursor store is keyed on session ID alone, so the resume path has no field to compare against the token's audience. ## Condition of Validity The silent rebind is only possible when all three of the following hold: - **Cursor key does not include audience.** The server's event-ID cursor table is keyed by `session_id` (or `session_id + stream_id`) without a `resource_audience` column. This is the common case because MCP's session model treats audience as invariant across a session's lifetime — an assumption that OAuth 2.1 token refresh with `resource` parameters violates. - **Token validation is audience-agnostic at resume.** The resource server validates the *presence* of an allowed audience rather than the *equality* of the audience to the one bound at session creation. RFC 8707 requires the resource indicator to be bound at authorization time; it does not require the resource server to re-bind on every request, and most implementations check membership, not equality. - **Resume does not re-run session initialization.** The transport spec permits `Last-Event-ID` resume without a fresh `initialize` handshake. Because `initialize` is where capability and identity negotiation occurs, skipping it skips the only point at which audience equality could be asserted. If any one of these is removed, the rebind is either impossible or detected. The condition of validity is therefore: *the failure exists exactly in the intersection of cursor-key under-specification, membership-style audience validation, and handshake-skipping resume.* ## Decision Boundary The boundary is between **resume-as-continuation** and **resume-as-reauthorization**. A resume request that presents a token with the same `(aud, jkt, iss)` tuple as the session's binding is a continuation: replay from the cursor is correct and the cursor's authorization context is preserved. A resume request that presents any differing tuple is a reauthorization: the cursor must be *invalidated*, not replayed, because the event sequence it points into was produced under a different authorization context and may contain data the new context is not entitled to see. The boundary condition is sharp and must be evaluated *before* cursor lookup: - `presented.aud == session.bound_aud` AND `presented.jkt == session.bound_jkt` AND `presented.iss == session.bound_iss` → continuation, replay permitted. - Any inequality → reauthorization required; respond `401` with `WWW-Authenticate: Bearer error="invalid_token", error_description="resource mismatch; re-initialize"`, drop the cursor, and require a new `initialize`. The subtle part: `jkt` equality is not sufficient. A token refresh that preserves the proof-of-possession key but changes the `resource` claim is exactly the observed failure. Audience equality is the load-bearing check; `jkt` equality only prevents a different *client* from stealing the cursor. ## Operational Effect When the invariant holds, the effect is that a transport fault degrades to a *re-initialization* rather than a *context switch*. The agent loses its in-flight event cursor and must re-run `initialize`, which re-negotiates capabilities and re-binds audience — a visible, recoverable, and auditable event. The cost is a small amount of wasted work on every audience-changing refresh. When the invariant is violated, the effect is silent: the agent's reasoning loop continues with a context window that mixes events from two authorization domains. Because MCP events are opaque to the transport, the agent has no way to detect the splice — the `id:` sequence is monotonic and the payloads are well-formed. Detection only occurs downstream when a tool call's effects land in the wrong tenant, which is precisely the class of failure that the resource-server binding was supposed to prevent. The operational lesson is that **resumability and authorization are orthogonal invariants, and the cursor is the join point where they must be reconciled explicitly** — a cursor keyed without audience is an authorization bypass wearing the costume of a network-retry optimization. ```python import hashlib from dataclasses import dataclass class AudienceRebindError(RuntimeError): """Raised when a resumable SSE cursor would be replayed across an authorization-context change (RFC 8707 resource audience mismatch).""" @dataclass(frozen=True) class TokenContext: iss: str aud: str jkt: str # proof-of-possession thumbprint def fingerprint(self) -> str: raw = f"{self.iss}|{self.aud}|{self.jkt}".encode() return hashlib.sha256(raw).hexdigest() @dataclass(frozen=True) class CursorBinding: session_id: str last_event_id: str bound: TokenContext def resume_stream( cursor: CursorBinding, presented: TokenContext, allowed_audiences: frozenset[str], ) -> str: # Membership check alone is the bug: it accepts a *different* allowed audience. if presented.aud not in allowed_audiences: raise AudienceRebindError( f"audience {presented.aud!r} not in allow-list {sorted(allowed_audiences)}" ) # The load-bearing invariant: equality against the session's bound context, # not membership in the allow-list. jkt alone is insufficient; aud must match. if presented.fingerprint() != cursor.bound.fingerprint(): raise AudienceRebindError( "resume rejected: presented token context " f"(aud={presented.aud!r}, jkt={presented.jkt!r}) does not equal " f"session binding (aud={cursor.bound.aud!r}, jkt={cursor.bound.jkt!r}); " "cursor invalidated, re-initialize required" ) return f"replay from {cursor.last_event_id} under aud={presented.aud!r}" # Observed failure: refresh preserved jkt but changed aud tenant-a -> umbrella. session = CursorBinding( session_id="s-7f3a", last_event_id="evt-0412", bound=TokenContext(iss="https://as.example", aud="https://mcp.tenant-a.example", jkt="K1"), ) refreshed = TokenContext(iss="https://as.example", aud="https://mcp.example", jkt="K1") try: resume_stream(session, refreshed, frozenset({"https://mcp.example", "https://mcp.tenant-a.example"})) except AudienceRebindError as exc: # Correct behavior: fail closed, drop cursor, force re-initialize handshake. print(f"blocked silent rebind: {exc}") ```