# Which invariants prevent MCP session migration from silently rebinding a tool's OAuth 2.1 resource token to the wrong upstream **Date of Record:** 2026 November 30 **Scope:** Applies to HTTP-native Model Context Protocol deployments that support resumable SSE (legacy transport) and streamable-HTTP transport with `Last-Event-ID` resumption, and that bind per-tool OAuth 2.1 tokens to a specific resource (RFC 8707). Does not cover stdio transport or single-transport, unauthenticated servers where no resumption or token binding occurs. ## Core Distinction The failure mode is not *token expiry* and not *transport disconnect*. It is a **rebinding of credential scope during a resume**. When a client resumes a suspended session, the MCP spec allows the server to hand back a new session identifier and a new event stream. In an HTTP-native implementation, the resume request is a fresh HTTP request carrying fresh `Authorization` headers. The streamable-HTTP resume path and the legacy SSE resume path are *different endpoints* with *different middleware stacks*, and the OAuth 2.1 resource indicator (`resource` parameter per RFC 8707) attached to a tool call is a property of the *session's tool binding*, not of the HTTP request that resumes it. The invariant that must hold: **the resource indicator bound to a tool invocation at call time must survive session migration byte-for-byte, or the migration must fail closed.** What actually happens in the wild is that the resume request re-derives the resource indicator from the *new* request's audience, and the tool then executes against an upstream the original caller never authorized. ## What Was Observed A client held a session `sess_A` opened over SSE. During a long-running `tools/call` to a tool whose OAuth 2.1 token carried `resource=https://upstream-prod.internal/mcp`, the client's connection dropped at event `id: 4471`. The client reconnected via the streamable-HTTP endpoint with `Last-Event-ID: 4471` and a `Mcp-Session-Id: sess_A` header, plus a token minted for the *new* gateway hostname `resource=https://gateway-eu.internal/mcp`. The server's streamable-HTTP handler validated the bearer token, extracted the `resource` claim, and — because `Mcp-Session-Id` lookup found `sess_A` still live in the session store — resumed the event stream. The stream replayed events 4472 onward. But the tool's continuation code read the resource indicator from the *current request context* (the resumed request), not from the session's recorded tool binding. The remaining tool sub-calls therefore went to `gateway-eu.internal` instead of `upstream-prod.internal`. No error was raised. The tool returned a 200 with plausible data. The only observable signal was a mismatch in the tool's own audit log: the first 4471 events referenced `upstream-prod`, the tail referenced `gateway-eu`. Because the tool's output schema was identical on both upstreams, the client's schema validation passed. The trigger condition is narrow and specific: 1. Session opened on transport T1 (SSE) with tool binding carrying resource R1. 2. Mid-call disconnect with a pending `Last-Event-ID`. 3. Resume on transport T2 (streamable-HTTP) with a token whose resource claim is R2 ≠ R1. 4. Server resumes the *stream* but re-derives *resource scope* from the resume request. If any of (1)–(4) is absent, the bug does not fire. It is a genuine cross-transport boundary condition: a same-transport resume (SSE→SSE) typically reuses the same middleware and preserves the binding; a T1→T2 migration crosses the seam. ## Condition of Validity This is a real invariant violation only under the following conditions, and an agent should treat it as *inapplicable* otherwise: - The server implements **both** transports and permits migration between them for a single session. Servers that pin a session to its opening transport (returning `409 Conflict` on cross-transport resume) are immune by construction. - The tool binding carries a **per-tool** resource indicator distinct from the session's default audience. If all tools share one resource indicator, the resume's re-derivation is a no-op. - The resume request's bearer token is **valid** for the server (so authentication passes) but carries a **different** `resource` claim than the original. A token with an invalid signature fails earlier and is not this bug. - The server's session store records tool bindings but the **resume handler does not read them** — i.e., the binding is written on `tools/call` but the resume path reads from request context. This is the structural defect; servers that reconstruct tool context from the session record on resume are immune. The invariant is *not* violated when the resume token carries the same resource claim, when the server rejects cross-transport resume, or when the tool has no per-tool resource binding. ## Decision Boundary An agent operating against such a server must decide, on every resume, whether it can trust the resumed stream. The decision boundary is: - **Fail closed** if the resume response's returned session identifier differs from the one sent, *and* the tool binding's resource indicator is not echoed in the resume response. Treat the tail of the stream as untrusted and re-issue the tool call from scratch with a token minted for the original resource. - **Proceed** only if the resume response includes an explicit binding assertion (e.g., a `Mcp-Tool-Resource` response header or an in-band `binding/resumed` event) whose value matches the resource indicator recorded at call time. - **Never** infer binding correctness from a 200 status or from schema-valid tool output. Both upstreams can be schema-conformant; the failure is invisible to schema validation. The guardrail the server must implement is a **binding-preservation check on resume**: before replaying events past `Last-Event-ID`, compare the resume token's `resource` claim against the session's recorded per-tool binding. On mismatch, either reject with `401` and a `WWW-Authenticate` indicating the required resource, or — if the spec permits — resume the stream but emit a `binding/rebound` control event so the client can discard the tail. Silent continuation is the only forbidden outcome. ## Operational Effect Downstream, the effect is a **split-brain tool execution**: the pre-disconnect portion of the tool's side effects landed on upstream R1, the post-resume portion on R2. If the tool is non-idempotent (writes, payments, state mutations), the result is a partial transaction spanning two authorization domains — an audit and compliance failure, not merely a data-consistency one. The client's retry logic, seeing a 200, will not retry, so the split persists. For the agent, the operational rule is: **on any cross-transport resume, re-verify the tool binding before consuming replayed events, and if the binding cannot be verified, abandon the stream and re-invoke.** The cost of re-invocation is a duplicate call; the cost of silent rebinding is an unauthorized side effect on an upstream the caller never scoped. The invariant to encode is that *session identity is not tool authorization* — `Mcp-Session-Id` continuity does not imply resource continuity, and the two must be checked independently. ```python class BindingRebindError(RuntimeError): """Raised when a resumed MCP session's tool binding cannot be proven to still point at the resource indicator recorded at call time.""" def resume_session( session_id: str, last_event_id: int, resume_token_resource: str, session_store: dict, ) -> str: """Resume an MCP session across transports, enforcing binding preservation. Returns the resource indicator the resumed stream is authorized to reach. Raises BindingRebindError if the resume token's resource claim does not match the per-tool binding recorded at tools/call time. """ record = session_store.get(session_id) if record is None: raise BindingRebindError( f"session {session_id!r} not found; cannot prove binding, refusing resume" ) bound_resource = record.get("tool_binding_resource") if bound_resource is None: raise BindingRebindError( f"session {session_id!r} has no recorded tool binding; " "resume would re-derive scope from request context" ) if bound_resource != resume_token_resource: raise BindingRebindError( f"resource rebind on resume: session {session_id!r} bound to " f"{bound_resource!r}, resume token scoped to {resume_token_resource!r}; " "abandoning stream tail to prevent split-brain tool execution" ) return bound_resource ```