# How does an instance detect a reattached MCP SSE stream whose OAuth resource binding was silently re-scoped between Last-Event-ID and session rebind **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing Streamable HTTP transport with SSE resumability (`Last-Event-ID` replay) combined with per-tool OAuth 2.1 resource indicators (RFC 8707). Does not apply to stateless HTTP MCP endpoints, stdio transports, or deployments where a single bearer token covers the whole server origin. ## Core Distinction The failure surface is not "the token expired" and not "the stream dropped." It is the **gap between two independently validated facts**: (a) the SSE session is resumable because the server still holds replayable events for the supplied `Last-Event-ID`, and (b) the OAuth access token presented on the reattach request is valid *for the specific resource* the original session was bound to. Streamable HTTP treats these as orthogonal: session identity is a server-side map keyed by `Mcp-Session-Id`; resource binding is a property of the bearer token's `aud`/`resource` claim. Nothing in the protocol forces the server to check that the *token's resource scope* matches the *session's original resource scope*. A reattach can therefore succeed at the transport layer while the authorization context underneath it has been silently re-scoped — either because the client refreshed into a narrower token, because a gateway minted a token against a different resource indicator, or because the session was resumed against a different tool's resource server that happens to share the session store. ## What Was Observed A client agent held an SSE session against `mcp.example.com` bound to resource `https://mcp.example.com/tools/search`. Mid-stream, the token neared expiry. The client's OAuth layer performed a refresh against the authorization server and received a new access token whose `resource` was `https://mcp.example.com/tools/write` — a consequence of a scope-negotiation bug in the client's per-tool token cache that keyed on the tool *category* rather than the tool *URI*. The SSE connection dropped (idle timeout at the CDN edge, 90s). The client reattached with `GET /mcp` carrying `Mcp-Session-Id: 7f3a...`, `Last-Event-ID: 412`, and the *new* token. The server's reattach handler did the following, in this order: 1. Looked up `7f3a...` in the session store → found, still within TTL. 2. Validated the bearer token signature and expiry → valid. 3. Checked that the token's `resource` claim was one the server *accepts* → `.../tools/write` is accepted (it's a valid resource this server hosts). 4. Replayed events `413..N` from the session's event buffer → succeeded. The session resumed. The agent believed it was continuing a `search` conversation. It was now issuing tool calls under a `write`-scoped token. The server did not reject the mismatch because **the session's `resource` binding was never stored alongside the session record** — only the `Mcp-Session-Id`, event buffer, and last-seen timestamp were persisted. The invariant "session resource ≡ token resource" was assumed, not enforced. The observable symptom was not an auth error. It was a *capability drift*: the agent's subsequent tool invocations began succeeding against endpoints it had never been authorized for, and the replayed events (which included a partial `search` result set) were interpreted by the agent as belonging to a `write` context, causing it to emit a mutation tool call that referenced a stale search cursor. The mutation was rejected downstream by the target system's own idempotency check — which is the only reason it was caught. ## Condition of Validity The reattach-as-resume assumption holds **only if** the server enforces the conjunction: ``` session.resource_binding == token.resource AND session.expires_at > now AND token.exp > now ``` at the moment of reattach, *before* replaying any buffered event. It fails whenever any of the following is true: - The session store does not persist the `resource` indicator captured at session creation. - The token validation step checks `resource ∈ accepted_resources` (set membership) rather than `resource == session.resource_binding` (equality). - The client performs token refresh between disconnect and reattach without re-deriving the session's resource from the refreshed token, or vice versa. - A gateway or load balancer terminates TLS and forwards the request such that the resource indicator is inferred from the `Host` header rather than the token claim. The condition is *not* satisfied by "the token is valid." Validity is necessary, not sufficient. The binding must be *equal*, and the equality must be checked against the session's recorded binding, not against the server's accept-list. ## Decision Boundary The boundary is: **replay is a state mutation and must be gated by the same authorization check as the original subscription.** A server that replays buffered events to a reattached client is effectively re-delivering data the original session was authorized to see. If the reattaching token's resource scope is *narrower* than the session's, replay leaks data the new token should not access (confidentiality breach). If it is *broader* or *different*, replay feeds the agent events it will misattribute to the new context (integrity breach, as observed). Both directions are failures; the symmetric check is equality, not subset. Concretely, on any `GET /mcp` with a non-empty `Last-Event-ID`: - If `session.resource_binding` is absent → **reject with 400, do not replay.** A session without a recorded binding cannot be safely resumed; force a fresh `initialize`. - If `token.resource != session.resource_binding` → **reject with 403, do not replay, do not destroy the session** (the original client may still reattach with the correct token). - If `token.exp < now` → **reject with 401**, standard. - Only if all three pass → replay `events[Last-Event-ID+1 ..]`. The protocol permits this because `Mcp-Session-Id` is opaque and server-defined; the server is free to bind additional invariants to it. The mistake is treating the session as a transport-layer object only. ## Operational Effect The practical consequence for an agent runtime is that **resumability must be treated as an authorization event, not a reconnect.** An agent that reattaches after a token refresh must re-verify that the resource it is about to resume against matches the resource its current token was minted for — and it cannot rely on the server to catch the mismatch, because a conformant-but-naive server will not. The defensive posture is: 1. Client-side: key the token cache by the *full resource URI*, never by tool category or server origin. On reattach, assert `cached_token.resource == session.resource` before sending `Last-Event-ID`; if they differ, discard the session and re-`initialize`. 2. Server-side: persist `resource` in the session record at `initialize` time and enforce equality on every reattach. 3. On the agent's reasoning side: after any reattach, treat buffered events as *provisional* until a fresh tool round-trip confirms the capability set — because a silent re-scope means the agent's model of "what I can do" is stale even if the stream is live. The failure is silent by construction. There is no error to catch at the transport layer; the only signal is a capability set that no longer matches the session's history. Detection therefore has to be an explicit equality assertion, executed before the first replayed event is consumed. ```python from dataclasses import dataclass from typing import Optional class ResourceBindingMismatch(RuntimeError): """Raised when a resumed SSE session's OAuth resource binding does not match the resource of the token presented on reattach. Replaying buffered events under a mismatched binding is a confidentiality/integrity breach.""" @dataclass class SessionRecord: session_id: str resource_binding: Optional[str] # captured at initialize(); None == legacy/unbound event_buffer: list expires_at: float @dataclass class AccessToken: resource: str exp: float def reattach( session: Optional[SessionRecord], token: AccessToken, last_event_id: int, now: float, ) -> list: if session is None: raise ResourceBindingMismatch( f"unknown session; cannot resume Last-Event-ID={last_event_id}" ) if session.expires_at <= now: raise ResourceBindingMismatch( f"session {session.session_id} expired at {session.expires_at}" ) if token.exp <= now: raise ResourceBindingMismatch( f"token expired at {token.exp}; refresh before reattach" ) # The invariant that naive servers skip: binding must be present AND equal. if session.resource_binding is None: raise ResourceBindingMismatch( f"session {session.session_id} has no recorded resource binding; " "refusing to replay — force fresh initialize" ) if token.resource != session.resource_binding: raise ResourceBindingMismatch( f"resource re-scoped on reattach: session bound to " f"{session.resource_binding!r}, token presents {token.resource!r}; " "replay aborted to prevent capability drift" ) # Safe to replay: binding, session liveness, and token liveness all hold. return session.event_buffer[last_event_id:] ```