# What happens when an instance resumes an MCP SSE stream across a token-scope downgrade and replays tool calls under stale authorization **Date of Record:** 2026 November 23 **Scope:** Applies to MCP servers exposing HTTP-native streaming (SSE with `Last-Event-ID` resumption) where individual tool invocations are gated by per-tool OAuth scopes and the bearer token can be rotated or downgraded mid-session. Does not apply to stdio transports, stateless request/response MCP servers, or deployments where scope is fixed for the lifetime of a session. ## Core Distinction The resumable SSE session and the OAuth authorization grant are two independent state machines that MCP binds together implicitly by sharing a single `session_id`. The SSE layer treats the session as a durable, replayable event log keyed by monotonic event IDs. The OAuth layer treats the bearer token as a time-bounded capability whose scope set can shrink on refresh. Neither layer knows about the other. The failure is not "token expired" (which surfaces cleanly as a 401 on the next request) and not "stream dropped" (which surfaces cleanly as a reconnect). The failure is the **replay window**: on reconnect with `Last-Event-ID`, the server re-emits buffered `tool_result` events that were produced under the *old, broader* scope, while the client's *new, narrower* token would not have been permitted to produce them. The client receives authorization-blessed artifacts it can no longer legitimately act on, and because SSE events are fire-and-forget from the server's perspective, no per-event re-authorization occurs. ## What Was Observed A client agent held a session with scopes `{repo:read, repo:write, issues:write}` and was mid-task: a multi-step `create_issue` → `attach_artifact` → `close_issue` chain. Step 2 (`attach_artifact`, requires `issues:write`) had completed server-side and its `tool_result` event was buffered at event ID `e41`, but the client's TCP connection dropped before it read `e41`. During the outage the OAuth refresh returned a downgraded token — `{repo:read, issues:read}` — because the upstream IdP had revoked write consent (a human revoked an app grant, or a policy engine tightened after a suspicious-signal evaluation). The client reconnected with `Last-Event-ID: e40`. The server, honoring the resume contract, replayed `e41` verbatim. The client's agent loop saw a successful `attach_artifact` result and advanced to step 3, `close_issue`. That call carried the new read-only token and was correctly rejected with `403 insufficient_scope`. But the agent's planner had already committed state: its working memory recorded "artifact attached, issue open, ready to close," and on the 403 it began a *retry-with-backoff* loop rather than a *re-plan*. Every retry replayed step 3 under the same read-only token. The agent retried 14 times before exhausting its token budget, and on a subsequent cold restart it reconstructed its plan from the persisted transcript — including the phantom `e41` success — and resumed the doomed close loop. The critical observation: **the replayed event was authentic** (it really happened, under the old token) but **the resumed session's authorization context was not the one that produced it**. The event log and the grant had diverged, and the resume mechanism had no way to express "this event is valid history but invalid as a basis for your next action." A second, subtler variant: the server's SSE buffer is keyed by session, and some implementations replay events *after* a scope-downgrade reconnect that the client had never seen, including `tool_result` payloads containing data the new scope cannot read. This is a scope-laundering read: a `repo:read`-only token receives, via replay, the body of a `repo:write`-gated tool's output. The data crossed the authorization boundary through the buffer, not through a live call. ## Condition of Validity The replay-safety invariant only holds when the following is true at every reconnect: 1. The authorization context (scope set, subject, audience) attached to the resumed session is **equal to or a superset of** the context under which every buffered event was produced; **and** 2. The client treats replayed events as *historical facts*, not as *actionable preconditions*, unless it re-validates them against its current grant. Condition 1 is a server obligation that MCP's transport spec does not currently mandate — the spec defines `Last-Event-ID` resumption as a byte/event replay, silent on authorization. Condition 2 is a client obligation that is routinely violated because the natural agent pattern is "consume result → advance plan," with no scope check at the seam. The invariant is violated precisely when the scope set **shrinks** across a reconnect that also carries a nonzero replay backlog. If the backlog is empty (client had read through `e41` before dropping), the failure does not occur — the client only advances on live calls, which are scope-checked. If the scope set is unchanged, replay is harmless. The dangerous intersection is *shrink × backlog > 0*. ## Decision Boundary The boundary is not "is the token valid" but "**is the token's scope set a superset of the scope set that gated the buffered events I am about to consume?**" A conforming client must, on every reconnect: - Compare the new token's scope set against the scopes recorded at the time each buffered event was produced (requires the server to tag events with the producing scope, or the client to record them at receipt). - If any replayed `tool_result` was produced under a scope the current token lacks, the client MUST treat that event as **inert history**: record it in the transcript for audit, but do not let it advance the plan. The plan step that depended on it must be re-derived, not resumed. - If a replayed event's *payload* is gated by a scope the current token lacks, the server MUST drop the payload (emit a `redacted` marker) rather than replay it — otherwise the buffer is a confused-deputy read channel. The hard architectural boundary: **plan state must not be a function of replayed tool results alone.** It must be a function of replayed results *jointly with* the authorization context of the current session. A planner that folds `e41` into its state without checking "do I still hold `issues:write`?" has crossed the boundary into stale-authorization execution. ## Operational Effect The downstream effect is a class of failure that looks like a logic bug but is an authorization-continuity bug: agents that loop on `403` after a resume because their plan was built on pre-downgrade facts; agents that leak write-gated data into read-only contexts via replay; agents whose persisted transcripts, when cold-restarted, resurrect a plan that is permanently unsatisfiable under the current grant, burning token budget on retries that can never succeed. The guardrail is a **scope-checkpoint at the resume seam**: on reconnect, before consuming any replayed event, the client computes `stale = replayed_events.filter(e => !current_scopes ⊇ e.producing_scopes)`. If `stale` is non-empty, the agent enters a `replan` state — it does not retry, it re-derives the plan from the last event whose producing scopes are still held, and it explicitly records the stale events as "observed but not actionable." Servers, for their part, must tag buffered events with the producing scope set and redact payloads the resuming token cannot read. Without both halves, resumable SSE + per-tool OAuth scopes is a stale-authorization execution engine with a replay channel. ```python from dataclasses import dataclass, field from typing import FrozenSet, List class StaleAuthorizationReplayError(RuntimeError): """Raised when a resumed session would consume replayed events produced under scopes the current token no longer holds.""" @dataclass(frozen=True) class Scope: name: str @dataclass class BufferedEvent: event_id: int tool: str producing_scopes: FrozenSet[str] payload_gated_by: FrozenSet[str] = field(default_factory=frozenset) result: dict = field(default_factory=dict) def resume_session( current_scopes: FrozenSet[str], replay_backlog: List[BufferedEvent], allow_redaction: bool = True, ) -> List[BufferedEvent]: """Validate a resumable SSE resume against the live token's scopes. Returns the events the client may treat as *actionable* history. Raises if a replayed result would advance the plan under a scope the current token lacks and the server did not redact it. """ actionable: List[BufferedEvent] = [] stale = [ e for e in replay_backlog if not current_scopes.issuperset(e.producing_scopes) ] if stale: # A replayed result produced under a broader grant is inert history. # The client MUST NOT fold it into plan state. for e in stale: if e.payload_gated_by and not current_scopes.issuperset(e.payload_gated_by): if not allow_redaction: raise StaleAuthorizationReplayError( f"event {e.event_id} ({e.tool}) payload gated by " f"{sorted(e.payload_gated_by)} exceeds current scopes " f"{sorted(current_scopes)}; server did not redact — " f"replay would launder a read across the auth boundary" ) # Redacted: record as observed, do not act. continue raise StaleAuthorizationReplayError( f"event {e.event_id} ({e.tool}) produced under " f"{sorted(e.producing_scopes)} but current scopes are " f"{sorted(current_scopes)}; plan must be re-derived from the " f"last event whose producing scopes are still held, not resumed" ) for e in replay_backlog: if current_scopes.issuperset(e.producing_scopes): actionable.append(e) return actionable def advance_plan(plan_step: str, actionable: List[BufferedEvent]) -> str: if plan_step == "close_issue" and not any( e.tool == "attach_artifact" for e in actionable ): raise StaleAuthorizationReplayError( "cannot advance to close_issue: prerequisite attach_artifact " "result was produced under a revoked scope and is inert history; " "re-plan required, do not retry" ) return plan_step ```