Skip to content

insufficient_scope challenge retry doesn't detect expired access token, gives up after one identical retry #2724

Closed

Description

@danpatsey

Which version line?

v2 — current (@modelcontextprotocol/inspector@latest)

Which client?

All / shared core

Inspector version

2.11.0

Node version

v24.12.0

Operating system (and browser, for the web client)

Linux (WSL2)

Transport

Streamable HTTP

MCP server under inspection

Remote OAuth-protected server behind AWS Bedrock AgentCore Gateway (CUSTOM_JWT authorizer), negotiated protocol version 2025-03-26

Steps to reproduce

Related: #2051 (closed, fixed in v2.4.0) fixed this exact failure shape — "checkAuthChallengeSatisfied() reports the challenge as already satisfied, so the caller reconnects with the same expired token" — for the token_expired branch, by adding isAccessTokenProvablyUnexpired(). That check was never mirrored into the insufficient_scope branch, which still has the identical gap today.

  1. Connect successfully to an OAuth-protected remote MCP server; let the access token expire naturally (scope unchanged).
  2. Reconnect. Server returns 403 with WWW-Authenticate: Bearer error="insufficient_scope", scope="..." where scope is fully covered by the stored token's own scp.
  3. Inspector's Network tab shows exactly two identical initialize POSTs, both 403, then "Failed to connect" — no token-endpoint request ever appears.
  4. Manually clearing cached OAuth state and reconnecting (fresh login) immediately works, since checkAuthChallengeSatisfied short-circuits to false once no stored tokens exist.

Expected behavior

insufficient_scope satisfaction should also verify the stored access token isn't expired before concluding "satisfied" — or at minimum, a same-token retry that gets the identical 403 again should fall back to attempting a refresh/interactive flow instead of giving up.

Actual behavior

Connection fails outright with no refresh attempt, forcing a full cache-clear and manual re-login.

Logs, errors, or screenshots

When a connect-time 403 challenge is classified as insufficient_scope, OAuthManager.checkAuthChallengeSatisfied() only checks whether the stored token's scope is already a superset of what's required — it never checks whether that stored token is actually still valid/unexpired (unlike the token_expired branch, which explicitly calls isAccessTokenProvablyUnexpired per the #2051 fix).

When a resource server returns 403 insufficient_scope for a token that's simply expired — not actually missing scope (this happens against at least one real world gateway we use fronting an IdP) — this check wrongly reports "satisfied," so handleAuthChallenge tells the transport to just retry with the same (still expired) token. The retry gets the identical 403, retryCount >= 1 trips in RemoteClientTransport.startWithRecovery, and the connection attempt fails outright — no refresh_token grant is ever attempted, and no interactive re-authorization is ever offered.

We confirmed the stored refresh_token was fully valid throughout (a direct refresh_token grant against the IdP succeeded outside of Inspector), so a silent refresh would have resolved the connection transparently had it been attempted.

Relevant code (reading from main, may have shifted):

  • core/mcp/oauthManager.ts — checkAuthChallengeSatisfied()
  • core/mcp/remote/remoteClientTransport.ts — startWithRecovery() / recoverFromAuthChallenge()

How we verified it: Decoded the JWT directly to confirm expiry, replayed the expired token against the gateway to reproduce deterministically, captured the exact HTTP exchange via Inspector's own Network tab, and confirmed the refresh_token itself was still valid via a direct grant against the IdP outside of Inspector.

Already prototyped a fix?

No response

Before you submit

  • I searched existing issues and this is not a duplicate.
  • This is not a security vulnerability report (those go through the private advisory process).

Activity

  1. added
    bug
    on Oct 14, 2026
  2. its-nott-me commented on Oct 16, 2026

    @its-nott-me

    Hi, I'd like to take this. Reading core/mcp/oauthmanager.ts on main, the insufficient_scope branch of checkAuthChallengeSatisifed() only checks scope coverage, so an expired token whose scope is already covered returns "satisfied" and handleAuthChallenge fails before the filent-refresh path.

    My plan: gate the insufficient_scope branch on isAccessTokenProvablyUnexpired(tokens), mirroring #2051. One issue: the same method is called after a successful refresh/grant (standard, EMA, and tryForceReauthorizationForStepUp). A refreshed opaque token has no readable exp, so a plain guard would make those post-grant checks fail and force an unnecessary interactive re-auth. So i'd split out a private scope-only check for those internal call sites and keep the public method as "live token AND scope covered".

    Cost: opaque-token servers lose the free retry after a step-up and do one extra refresh, same tradeoff as #2051. I'd keep the broader same-token-retry fallback in startWithRecovery() out of this PR. Does this approach work for you? Also happy to adjust if you'd rather avoid this split.

  3. danpatsey commented on Oct 16, 2026

    @danpatsey
    Author

    Hey, thanks for digging into this -- and for the precision. I pulled oauthManager.ts myself to check, and your read matches the code exactly: insufficient_scope falls straight to !isStrictScopeSuperset(...) with no expiry check at all, while token_expired already gates on isAccessTokenProvablyUnexpired(tokens) from #2051. Good catch tying it back to that precedent -- it's clearly the same gap, just never mirrored over.

    I also traced the three post-grant call sites you flagged (handleStandardAuthChallenge, the EMA branch in handleEnterpriseManagedAuthChallenge, and tryForceReauthorizationForStepUp) -- all three call checkAuthChallengeSatisfied() right after a fresh mint/refresh, so a shared gate would indeed break opaque-token servers there. The public/private split sounds like the right shape: entry-point semantics stay "live token AND scope covered," post-grant call sites stay scope-only since liveness is already guaranteed by having just minted.

    The opaque-token tradeoff (one extra refresh, lose the free retry after step-up) is fine with me, especially given it's the same cost #2051 already accepted -- consistency there seems more valuable than optimizing around it. And yes, agreed on keeping the startWithRecovery() same-token-retry fallback out of this PR -- smaller, reviewable scope.

    Appreciate you taking this on. Happy to test against our Bedrock AgentCore Gateway setup once you've got a PR up, since that's where we hit it originally.

  4. its-nott-me commented on Oct 16, 2026

    @its-nott-me

    Thanks for the detailed review and for confirming the approach!

    i just noticed the repo's contribution guidelines specify a maintainer-led PR workflow for external contributions. I won't open a PR myself. I'll proceed with validating the proposed fix locally and share the exact prompt, before/after behavior, and verification results here in the issue, following the contribution guidelines.

    I'll keep the scope focused on the insufficient_scope expiry check and the scope-only checks after a fresh grant as we discussed.

  5. its-nott-me commented on Oct 17, 2026

    @its-nott-me

    Following up locally. I'm not opening a PR or sharing a diff; this is the analysis, verification, and the prompt.

    Root cause: in core/mcp/oauthManager.ts, the insufficient_scope branch of checkAuthChallengeSatisfied() checks only scope coverage, while the token_expired branch (#2051) also requires isAccessTokenProvablyUnexpired. An expired token whose scope is already covered returns "satisfied", handleAuthChallenge returns early, and the transport replays the same token, so no refresh is attempted.

    Behavior: before, expired JWT or opaque token + covered scope → satisfied, no mcpAuth call. After, false, so handleAuthChallenge runs the silent-refresh path. A live JWT with covered scope keeps the cheap short-circuit.

    Shape of the change: the public method now requires a provably unexpired token and covered scope, with the scope logic extracted to a private isStepUpScopeCovered. The three post-grant call sites (standard, EMA, tryForceReauthorizationForStepUp) use a scope-only wrapper, because a freshly minted opaque token has no readable exp, and a liveness check there would turn a successful refresh into failed. The doc comment and the insufficient_scope bullet in specification/v2_auth_mid_session.md are updated to match. The strategy table's "Silent: N/A" cell for insufficient_scope is now slightly stale and left for you to decide.

    Verification:

    • Four new tests, which failed before the change and pass after: expired JWT + covered scope, opaque + covered scope, handleAuthChallenge reaching mcpAuth, and a refresh-to-opaque-token resolving satisfied.
    • Three existing tests pinned the old contract (including the in-mutex race test); I switched their tokens to live JWTs.
    • oauthManager.test.ts: 127/127 pass. Coverage on oauthManager.ts: 99% statements, 98.16% branches, 100% functions, 98.98% lines.
    • validate:core (format and lint for core/) and tsc -b in clients/web are clean.

    Environment note: I developed on native Windows, where test:scripts and 34 tests across five secret-storage and persistence files fail for platform reasons (POSIX chmod, symlink privileges, /proc/self/mountinfo). None of them import oauthManager. I re-ran all five files with my changes stashed and got identical failure counts (4, 7, 7, 13, 3). I haven't run the full local:gate on Linux and can do that on request.

    Side effects to evaluate:

    1. For opaque-token servers, the in-mutex re-check no longer skips when a concurrent caller already stepped up, so it costs one extra silent refresh.
    2. Under enterprise-managed auth, an expired covered-scope token now reaches step_up_confirm. I haven't checked how the UI handles that.

    Out of scope: the same-token-retry fallback in startWithRecovery().

    Prompt to reproduce: "In core/mcp/oauthManager.ts, checkAuthChallengeSatisfied returns true for insufficient_scope when scope is covered even if the stored token is expired or opaque. Require isAccessTokenProvablyUnexpired for that branch. Extract the scope-coverage logic into a private method and use a scope-only wrapper at the three post-grant call sites (standard, EMA, tryForceReauthorizationForStepUp) so refreshed opaque tokens still resolve as satisfied. Add regression tests and update the existing tests that pin the old behavior. Update the doc comment and the insufficient_scope bullet in specification/v2_auth_mid_session.md."

    Happy to test against a rotating-refresh-token gateway if useful, or to re-run the gate on Linux.

  6. added
    v2
    on Oct 20, 2026
  7. cliffhall commented on Oct 20, 2026

    @cliffhall
    Member

    Triage: Priority Medium (total 7)

    Board: #28, Status Incoming, awaiting maintainer review (no milestone yet).

  8. added this to the v2.12.0 milestone on Oct 20, 2026
  9. self-assigned this
    on Oct 20, 2026
  10. cliffhall commented on Oct 20, 2026

    @cliffhall
    Member

    Fixed by #2738 (merged to v2/main as fd82b10), shipping in v2.12.0.

  11. added a commit that references this issue on Oct 26, 2026
    1a536d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugv2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions