Skip to content

fix: throw error on auth fallback for non-root AS paths#1724

Open
guoyangzhen wants to merge 2 commits intomodelcontextprotocol:mainfrom
guoyangzhen:fix/auth-fallback-non-root-path
Open

fix: throw error on auth fallback for non-root AS paths#1724
guoyangzhen wants to merge 2 commits intomodelcontextprotocol:mainfrom
guoyangzhen:fix/auth-fallback-non-root-path

Conversation

@guoyangzhen
Copy link

Summary

Fixes #1716

When authorization server metadata discovery fails and the server URL has a non-root path (e.g., https://example.com/admin), the fallback to /authorize, /token, and /register endpoints uses absolute paths that silently lose the URL path prefix.

For example:

  • new URL('/authorize', 'https://example.com/admin')https://example.com/authorize (wrong!)
  • Correct would be: https://example.com/admin/authorize

Problem

Three locations in packages/client/src/client/auth.ts construct fallback URLs using absolute paths:

  1. startAuthorization() line ~1204: new URL('/authorize', authorizationServerUrl)
  2. executeTokenRequest() line ~1286: new URL('/token', authorizationServerUrl)
  3. registerClient() line ~1533: new URL('/register', authorizationServerUrl)

Per RFC 6749, the /.well-known defaults only apply to authorization servers at the domain root. For non-root paths, metadata discovery is required.

Fix

Added checks for non-root paths. When metadata discovery fails and the AS URL has a non-root pathname, a descriptive error is thrown instead of silently constructing a wrong URL.

Impact

n- Before: Silent redirect to wrong URL, broken auth flow with no diagnostic info

  • After: Clear error message indicating metadata discovery failed for a non-root AS

Fixes modelcontextprotocol#1716

When authorization server metadata discovery fails and the server URL has a non-root path, the fallback to /authorize, /token, and /register endpoints silently constructs wrong URLs (losing the path prefix). This fix throws a descriptive error instead of silently redirecting to nonexistent endpoints.

Affected locations:
- startAuthorization: /authorize fallback
- executeTokenRequest: /token fallback  
- registerClient: /register fallback
@guoyangzhen guoyangzhen requested a review from a team as a code owner March 20, 2026 15:13
@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2026

🦋 Changeset detected

Latest commit: ea8ea36

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startAuthorization silently redirects to wrong URL when AS metadata discovery fails for non-root paths

1 participant