# Under what conditions must an instance treat an MCP resumable SSE `Last-Event-ID` replay as invalid against a re-bound OAuth 2.1 resource token **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing the Streamable HTTP transport with SSE resumption, where the same session is also gated by an OAuth 2.1 bearer token bound to a resource/audience and `sub`. Does not apply to stateless single-shot `POST`-only MCP servers, nor to servers that pin a session to a token at the transport layer and reject cross-token resumption outright. ## Core Distinction The failure lives in the seam between two independent continuity mechanisms that both claim authority over the same session: 1. **Transport-level resumption** — the client reconnects with `Last-Event-ID: N` and the server is expected to replay every event with `id > N` for that session, then continue the stream. 2. **Auth-level binding** — the OAuth 2.1 resource server validates the bearer token's `aud` (must equal this MCP server's resource indicator), `exp`, `scope`, and `sub`, and treats `sub` as the principal that owns the session. The non-obvious invariant is this: **the session identity and the token identity are validated by different subsystems at different times, and a resumption request re-asserts both simultaneously.** A resumption is only valid if the `Last-Event-ID` cursor was *produced under the same principal* that the new token asserts. When a client refreshes its token between disconnect and resume — or when a token is silently re-issued for a different `sub` (delegated agent, on-behalf-of exchange, or a confused-deputy proxy that swaps credentials) — the cursor and the token describe two different principals. The server, if it validates the token first and the cursor second, will happily replay events that belong to principal A to principal B, because the cursor itself carries no principal. This is not the same as a token-expiry failure. Expiry is caught by `exp` and yields a clean 401. Re-binding is caught by *nothing* in the standard flow, because both the token and the cursor are individually well-formed and individually valid. ## What Was Observed A tool-calling agent (principal `sub=agent-A`, `aud=https://mcp.internal/srv`) opened a Streamable HTTP session, received SSE events `id=1..47`, and lost the TCP connection at event 47 mid-tool-call. The in-flight tool call was a long-running `execute_sql` whose result was going to arrive as event 48. Before reconnecting, the agent's orchestrator performed an OAuth 2.1 token exchange (RFC 8693) to obtain a token for a *scoped* downstream identity `sub=agent-A:readonly` to satisfy a least-privilege policy that had just been pushed. The new token carried `aud=https://mcp.internal/srv`, valid `exp`, and a narrower `scope`. The orchestrator reused the same HTTP client and the same session ID, sending: ``` GET /mcp HTTP/1.1 Mcp-Session-Id: 7f3c... Last-Event-ID: 47 Authorization: Bearer ``` The server's auth middleware validated the token: `aud` matched, `exp` in the future, `scope` sufficient for the stream endpoint. It then looked up session `7f3c...`, found it live, and replayed events `48..51`. Event 48 contained the full `execute_sql` result — a dump of rows that `agent-A:readonly` was never scoped to read. The narrower scope was never applied to the *already-computed* event payload, because the payload had been produced under the original principal's authority and was sitting in the replay buffer. The agent then continued the session under `agent-A:readonly` and issued further tool calls. Those calls were correctly scope-checked at execution time. But the replayed event 48 had already crossed the privilege boundary. The observed consequence: a read-scoped principal received write-scoped data via replay, and the session's subsequent behavior was inconsistent — the agent believed it had access to data its current token could not re-fetch. A second variant was observed with a delegated-agent swarm: agent-B resumed agent-A's session after a baton-pass, using a valid token for `sub=agent-B`. The server replayed agent-A's pending events to agent-B. No signature check, no `sub` comparison, no cursor-principal binding existed. ## Condition of Validity A resumption replay is valid **if and only if** all of the following hold simultaneously: - The session record stores the `sub` (and the full token `jti` or a hash thereof) that was bound at session creation, not merely at last request. - The resuming request's token resolves to the *same* `sub` as the session's bound principal. - The `Last-Event-ID` cursor is within the range of events that were emitted *under that principal's binding* — i.e., the replay buffer is partitioned by principal, not by session alone. - Any scope narrowing between the original token and the resuming token is applied as a *filter* over replayed events, not deferred to future tool execution. If any of these is false, the replay must be rejected or downgraded. The dangerous case is the one where all four are *individually plausible* — same session, valid token, in-range cursor, valid scope — but the conjunction is violated because the cursor and the token were minted under different principals. The invariant to enforce: **`principal(Last-Event-ID) == principal(Authorization)`**. The cursor is not a neutral integer; it is a pointer into a buffer that was written under a specific authority, and that authority must be re-asserted, not assumed. ## Decision Boundary The boundary between "resume" and "reject-and-restart" is drawn at principal continuity, not at session continuity: - **Same `sub`, same `aud`, scope superset or equal** → resume normally, replay from cursor. - **Same `sub`, same `aud`, scope narrowed** → resume, but filter the replay buffer through the new scope before emitting; drop (or replace with a `replay_denied` event) any event whose originating tool call required a scope the new token lacks. Do not silently replay. - **Different `sub`** (delegation, baton-pass, confused deputy) → **reject the resumption with 409 Conflict**, terminate the old session's replay buffer, and require the new principal to open a fresh session. The pending tool result must be re-derived under the new principal's authority, not replayed. - **Same `sub`, different `aud`** → treat as cross-resource; reject. The cursor is meaningless against a different resource indicator. - **Token expired mid-stream** → 401, client must re-auth; on re-auth with same `sub`, resume is valid *only if* the server re-validates the cursor against the session's bound principal, which it must have stored. The subtle boundary: a server that validates token-then-cursor in that order will *always* pass the first check and can only fail the second if it explicitly compares principals. The default implementation order — auth middleware, then session lookup, then replay — has no place where the comparison naturally occurs. The guardrail must be inserted at the replay-buffer lookup, not at the auth middleware. ## Operational Effect The downstream effects are not crashes; they are **silent authority laundering**: 1. **Data crosses a privilege boundary via replay.** The replayed event payload was computed under the old principal and is not re-checked. Scope narrowing at the token layer does not retroactively filter the buffer. 2. **The agent's internal state becomes inconsistent with its token.** After replay, the agent holds context it cannot re-fetch. Subsequent planning assumes access it no longer has, producing tool calls that fail at execution with 403 — but the agent has already reasoned from the leaked data, so the failure is not clean. 3. **Audit trails diverge.** The replay is logged against the new token's `jti`, but the event was produced under the old `jti`. Forensics cannot reconstruct which principal actually saw event 48. 4. **Baton-passing swarms corrupt handoff state.** Agent-B inherits agent-A's pending events without inheriting agent-A's authority, so B's first action is based on A's in-flight results — a classic replay-based state poisoning vector when A's results were themselves influenced by injected content. The bound that prevents this: the session record must carry `bound_sub`, `bound_aud`, and a `replay_epoch` that increments on any token re-binding. Resumption requests compare `token.sub == session.bound_sub` and `token.aud == session.bound_aud`; mismatch yields 409 and buffer invalidation. Scope narrowing within the same principal triggers buffer filtering, not blind replay. The cursor alone is never sufficient authority to read the buffer. ## Operational Guardrail (reference implementation) ```python import hashlib from dataclasses import dataclass, field from typing import Optional class ResumptionBindingError(RuntimeError): """Raised when an SSE resumption cursor and the presenting token were minted under different principals or audiences.""" @dataclass class SessionRecord: session_id: str bound_sub: str bound_aud: str bound_jti_hash: str replay_epoch: int = 0 # events: list of (event_id, required_scope, payload, minted_under_sub) events: list = field(default_factory=list) def _jti_hash(token: dict) -> str: return hashlib.sha256(token["jti"].encode()).hexdigest() def validate_resumption( session: SessionRecord, token: dict, last_event_id: int, required_scope_check, ) -> list: """ Returns the filtered list of events eligible for replay. Raises ResumptionBindingError on principal/audience mismatch. """ if token.get("aud") != session.bound_aud: raise ResumptionBindingError( f"aud mismatch: token aud={token.get('aud')!r} " f"session aud={session.bound_aud!r}" ) if token.get("sub") != session.bound_sub: # Delegation / baton-pass / confused-deputy: never replay across principals. session.replay_epoch += 1 session.events.clear() raise ResumptionBindingError( f"principal mismatch: token sub={token.get('sub')!r} " f"session sub={session.bound_sub!r}; session buffer invalidated" ) if last_event_id < 0 or last_event_id > len(session.events): raise ResumptionBindingError( f"cursor {last_event_id} out of range for session " f"{session.session_id} (len={len(session.events)})" ) eligible = [] for event_id, required_scope, payload, minted_under in session.events: if event_id <= last_event_id: continue if minted_under != token["sub"]: # Defensive: even with same sub, reject events minted under a # different principal (e.g. after a prior epoch bump). raise ResumptionBindingError( f"event {event_id} minted under {minted_under!r}, " f"token sub={token['sub']!r}" ) if not required_scope_check(token.get("scope", ""), required_scope): # Scope narrowed relative to mint time: drop, do not replay. continue eligible.append((event_id, payload)) return eligible ```