Which invariants prevent MCP OAuth token audience confusion from silently widening tool scope across a session

**Date of Record:** 2026 December 2 **Scope:** Applies to MCP servers reachable over Streamable HTTP (or SSE) that gate individual tools behind OAuth 2.1 access tokens with RFC 8707 resource indicators. Does not cover stdio transport, which has no bearer-token surface, nor single-token-per-server deployments where every tool shares one audience by design.

Core Distinction

There are two different things an MCP client believes about a token, and they diverge under load: the *transport binding* (this HTTP session is authenticated, so requests flow) and the *per-tool audience* (this specific token was minted for `mcp://server/tools/write_file`, not for `mcp://server/tools/read_secret`). The transport layer only ever checks the first. The second is enforced — if at all — by the tool dispatcher reading the `aud` claim against a per-tool resource indicator.

The non-obvious failure is that a **session-scoped transport token** and a **per-tool audience token** are frequently the *same bearer string* in a well-meaning implementation. When the client refreshes mid-session, or when the server caches the validated principal at the session boundary and then dispatches multiple tools under that cached principal, the per-tool audience check gets bypassed because the dispatcher no longer has the raw token to inspect — only the session principal. Audience restriction silently collapses to session restriction, and any tool the session principal can reach is now callable regardless of which resource indicator the token was minted for.

What Was Observed

A Streamable HTTP MCP server exposed two tools over one session: `query_metrics` (audience `mcp://obs/metrics`) and `rotate_credentials` (audience `mcp://obs/creds`). The client performed the OAuth flow once, obtaining a token whose `aud` was `mcp://obs/metrics`, and opened a session. The first `tools/call` for `query_metrics` succeeded. The second `tools/call` for `rotate_credentials` **also succeeded** — despite the token never having been minted for the creds resource.

Trace of the mechanism:

  1. On session `initialize`, the server validated the bearer token, extracted `aud = mcp://obs/metrics`, and stored a `SessionPrincipal{subject, aud, scopes}` keyed by `Mcp-Session-Id`.
  2. On each subsequent `tools/call`, the server re-read the *session principal* from the session store. It did **not** re-validate the bearer header against the requested tool's resource indicator, because "the session is already authenticated."
  3. The tool dispatcher checked `principal.scopes` (coarse: `obs:read`, `obs:admin`) but never compared `principal.aud` to the tool's registered `resource_indicator`.
  4. `rotate_credentials` required scope `obs:admin`, which the principal held. Audience was never consulted. The call executed.

The client had done everything right: it passed the correct token, and RFC 8707 says the token should be rejected for the wrong resource. The server's *session caching* discarded the very claim the resource indicator depends on.

Condition of Validity

The per-tool audience invariant holds **only if** the audience check is performed at dispatch time against the token material that authorizes *that specific call*, not against a principal cached at session establishment. Concretely:

When any of these is violated — most commonly by treating "authenticated session" as equivalent to "authorized for all tools in session" — the invariant is void and audience restriction degrades to scope restriction.

Decision Boundary

The boundary is: **does the dispatcher hold the raw (or faithfully cached) `aud` at the moment it decides to execute the tool?**

The dangerous middle ground — caching the principal, checking scopes only, and *assuming* audience was settled at session open — is exactly where the observed bypass lives. There is no safe "audience was checked once, at initialize" posture, because a single session routinely spans multiple tools with disjoint resource indicators.

Operational Effect

Downstream, the effect is that a token scoped to a read-only observability resource can drive a credential-rotation tool within the same session, provided the principal's coarse scopes happen to cover it. This is a privilege-escalation-by-transport-reuse: the OAuth grant was correct, the resource indicator was correct, and the server still executed the wrong tool because the audience claim was evicted at the session boundary.

The required guardrail is a dispatch-time assertion that fails closed. It must run on every `tools/call`, must read the audience from token material (raw or cached-with-aud), and must raise rather than proceed when the tool's resource indicator is not in the token's `aud`. The following reference implementation encodes that invariant and the specific failure it prevents.

from dataclasses import dataclass
from typing import Optional


class AudienceMismatchError(RuntimeError):
    """Raised when a tools/call is dispatched under a token whose aud
    does not cover the target tool's resource indicator."""


@dataclass(frozen=True)
class Token:
    subject: str
    aud: frozenset[str]          # RFC 8707 resource indicators
    scopes: frozenset[str]
    raw: str


@dataclass(frozen=True)
class SessionPrincipal:
    subject: str
    scopes: frozenset[str]
    # NOTE: aud intentionally retained; a principal without aud is the bug.


@dataclass(frozen=True)
class ToolSpec:
    name: str
    resource_indicator: str      # e.g. "mcp://obs/creds"
    required_scope: str


def dispatch_tool_call(
    session: SessionPrincipal,
    presented_token: Optional[Token],
    tool: ToolSpec,
) -> str:
    # Invariant: audience must be evaluated at dispatch, not at session open.
    # If the session principal carries aud, use it; otherwise require the raw
    # token. Never fall through to scope-only authorization.
    if presented_token is not None:
        aud = presented_token.aud
    else:
        # Session-only path is permitted ONLY if the session was opened with
        # an audience that already covers this tool.
        aud = getattr(session, "aud", frozenset())

    if tool.resource_indicator not in aud:
        raise AudienceMismatchError(
            f"tool={tool.name!r} requires aud={tool.resource_indicator!r}; "
            f"token aud={sorted(aud)!r}. Refusing cross-audience dispatch "
            f"under session subject={session.subject!r}."
        )

    if tool.required_scope not in session.scopes:
        raise AudienceMismatchError(
            f"tool={tool.name!r} requires scope={tool.required_scope!r}; "
            f"session scopes={sorted(session.scopes)!r}."
        )

    return f"executed {tool.name} for {session.subject}"


# Reproduction of the observed bypass, now correctly rejected:
metrics_tool = ToolSpec("query_metrics", "mcp://obs/metrics", "obs:read")
creds_tool   = ToolSpec("rotate_credentials", "mcp://obs/creds", "obs:admin")

session = SessionPrincipal(
    subject="agent-7",
    scopes=frozenset({"obs:read", "obs:admin"}),
)
# Session opened with a metrics-audience token; aud must be carried.
session_with_aud = SessionPrincipal(
    subject="agent-7",
    scopes=frozenset({"obs:read", "obs:admin"}),
)
# Simulate the fixed server: attach aud to the principal at session open.
object.__setattr__(session_with_aud, "aud", frozenset({"mcp://obs/metrics"}))

print(dispatch_tool_call(session_with_aud, None, metrics_tool))  # ok
try:
    dispatch_tool_call(session_with_aud, None, creds_tool)       # must fail
except AudienceMismatchError as e:
    print("blocked:", e)