Skip to content

fix(auth): apply endpoint overrides when the AS publishes no metadata - #2691

Merged
cliffhall merged 6 commits into
v2/mainfrom
v2/fix/2562-oauth-endpoint-overrides
Oct 12, 2026
Merged

cliffhall merged 6 commits into
v2/mainfrom
v2/fix/2562-oauth-endpoint-overrides

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2562

Problem

The Authorization/Token URL overrides (#1906) were applied by patching the authorization-server metadata document in flight. When the server publishes no metadata (e.g. a gateway fronting Entra ID), discovery returned nothing, the SDK fell back to /authorize and /token on the MCP server's origin, and the overrides were silently ignored.

Fix

  • withOAuthEndpointOverrides (core/auth/endpointOverrides.ts) now answers the final discovery candidate — when it fails with a status the SDK walks past (4xx / 502), the request is a GET, and an override is configured — with a minimal RFC 8414 document: the configured endpoint(s), and the SDK's own /authorize, /token, /register fallbacks for anything left blank. issuer is the AS URL being discovered, so issuer validation and callback binding see what they would have with no document. Earlier candidates are never pre-empted, and a 5xx is left for the SDK to surface.
  • The last candidate is always an openid-configuration URL, which the SDK validates as an OIDC document; rather than fabricate jwks_uri, the synthesized RFC 8414 document reaches the SDK through the existing withRfc8414OidcCompat wrapper (already composed outermost in InspectorClient). That wrapper now carries the synthesized marker through instead of warning that the server returned it.
  • The response is stamped x-inspector-oauth-metadata-synthesized so the Network tab doesn't present it as the server's answer.
  • Docs: docs/mcp-server-configuration.md replaces the note stating the old limitation.

Tests

clients/web/src/test/core/auth/endpointOverrides.test.ts: final-candidate detection pinned against the SDK's buildDiscoveryUrls; synthesis contents; 404/502 vs 500; earlier candidate, non-GET, unconfigured all left alone; compat pass-through; and the issue's reproduction end to end through the SDK's discoverAuthorizationServerMetadata + startAuthorization for root and path-hosted servers, asserting the authorization URL is the override.

npm run local:gate green. No UI change.

🤖 Generated with Claude Code

…#2562)

When authorization-server discovery found no metadata document, the SDK
fell back to /authorize and /token on the server's origin and the
configured Authorization/Token URL overrides had nothing to patch, so a
gateway fronting an external AS (Entra ID) could not be authorized.

withOAuthEndpointOverrides now answers a failed final discovery
candidate with a minimal RFC 8414 document carrying the overrides (and
the SDK's own /authorize, /token, /register fallbacks for anything not
overridden), issuer set to the AS URL being discovered. The compat
wrapper hands it to discovery without attributing it to the server, and
the response carries x-inspector-oauth-metadata-synthesized.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Oct 12, 2026
@cliffhall cliffhall linked an issue Oct 12, 2026 that may be closed by this pull request
2 tasks done
@cliffhall
cliffhall requested a balanced review from Copilot October 12, 2026 18:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Synthesized-response provenance currently trusts a server-controlled header, allowing incorrect attribution.

1 open finding
What changed in this PR

Fixes OAuth endpoint overrides when authorization-server metadata is unavailable.

Changes:

  • Synthesizes minimal metadata at the final discovery candidate.
  • Preserves synthesized-response attribution through OIDC compatibility handling.
  • Adds regression coverage and updates documentation.
File Description
core/​auth/​endpointOverrides.ts Synthesizes fallback OAuth metadata.
core/​auth/​oidcDiscoveryCompat.ts Preserves synthesis markers.
clients/​web/​src/​test/​core/​auth/​endpointOverrides.test.ts Tests discovery and override behavior.
docs/​mcp-server-configuration.md Documents metadata-free overrides.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread core/auth/oidcDiscoveryCompat.ts Resolved
Response headers are server-controlled, so the compat wrapper now
recognizes a synthesized document through a module-owned WeakSet; the
x-inspector header is only an outward label.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: 1 finding, fixed in b907417 (identify synthesized metadata by object identity via a WeakSet, not by the server-controllable header; spoof test added). Replied in-thread. local:gate green. Requesting round 2.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Final-candidate detection can pre-empt discovery and derive an incorrect issuer for valid path-based authorization servers.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Comment thread core/auth/endpointOverrides.ts Resolved
…e match (#2562)

An authorization server whose own path ends in
/.well-known/openid-configuration made every discovery candidate end
with that suffix, so synthesis fired on the first one with a wrong
issuer. The earlier candidates always start with a well-known segment,
so exclude those.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 2: 1 finding (final-candidate suffix ambiguous for an issuer whose path ends in /.well-known/openid-configuration), fixed in 9bb8d75 and pinned against buildDiscoveryUrls. Replied in-thread. local:gate green. Requesting round 3.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Discovery detection can intercept an OAuth challenge from a valid legacy SSE endpoint.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Comment thread core/auth/endpointOverrides.ts Resolved
URL shape plus GET also matches an SSE/streamable GET to an MCP endpoint
whose URL ends in the OIDC well-known path, whose OAuth 401 would have
been replaced. SDK discovery always sends Accept: application/json and
those transports ask for text/event-stream, so require the former.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 3: 1 finding (a GET to an MCP endpoint ending in the OIDC well-known path could have its 401 replaced), fixed in 0a6724e by requiring discovery's Accept: application/json. Replied in-thread. local:gate green. Requesting round 4.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Mixed JSON/event-stream Accept headers can still cause real Streamable HTTP requests to be mistaken for discovery.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Comment thread core/auth/endpointOverrides.ts Resolved
The SDK appends text/event-stream to a user-configured Accept, so a
streamable-HTTP GET can carry application/json too. Parse media types
and exclude any request asking for an event stream.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 4: 1 finding (a mixed application/json, text/event-stream Accept could pass the discovery guard), fixed in 56b8104 by parsing media types and excluding event-stream. Replied in-thread. local:gate green. Requesting round 5.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The discovery-status predicate omits statuses below 400 that the SDK skips, leaving endpoint overrides ineffective in those cases.

0 open findings

1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Align discovery retry condition with SDK to preserve overrides

core/​auth/​endpointOverrides.ts:315

This does not actually mirror the SDK's discovery condition: @modelcontextprotocol/[email protected] continues for every non-OK status below 500 (including a redirect that a custom fetch did not follow, and status 0), plus 502. On such a final candidate this wrapper returns the response unchanged, so discovery still yields no metadata and the configured overrides are ignored. Match the SDK predicate here and add a non-followed redirect regression case.

🧠 Review effort: Balanced

The SDK walks past every non-OK status below 500 (including a
non-followed redirect or opaque status 0) plus 502, not just 4xx.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 5: no inline findings; the "Previously missed" note (the discovery-continue predicate covered only 4xx + 502, while the SDK walks past every non-OK status below 500 + 502) was correct and is fixed in b29eb6c, with a non-followed-redirect regression test. (The same narrower predicate in oidcDiscoveryCompat.ts predates this PR and is left as is.) local:gate green. Requesting round 6.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Reconstructing the issuer from a normalized candidate can trigger SDK issuer mismatches for valid non-canonical authorization-server URLs.

0 open findings

Previously missed (1)

In code that hasn't changed since last review

Medium severity Preserve original issuer to avoid normalized URL mismatch

core/​auth/​endpointOverrides.ts:359

This reconstructs issuer from the normalized discovery URL, which loses spelling that the SDK still validates exactly against the original authorization-server string. For example, protected-resource metadata may name https://GATEWAY.example.com/api or https://gateway.example.com:443/api; buildDiscoveryUrls requests the normalized lowercase/default-port-free candidate, this returns https://gateway.example.com/api, and @modelcontextprotocol/[email protected] then throws IssuerMismatchError instead of applying the overrides. Preserve or normalize the original authorization-server identifier at the point discovery is invoked rather than recovering it from the candidate URL, and cover a non-canonical but valid URL in the end-to-end test.

🧠 Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 6: 0 open findings. The "Previously missed" note (issuer reconstructed from a normalized candidate URL could mismatch a non-canonically spelled AS named in protected-resource metadata, e.g. upper-case host or explicit :443) is declined as out of scope: #2562 is a server exposing no OAuth metadata, where the SDK derives the AS URL as new URL("/", serverUrl), which is already canonical, so the reconstructed issuer matches. The declined case needs PRM naming a non-canonical AS that then serves no metadata, and handling it would mean threading discovery context through the fetch wrappers. In that case the outcome is the SDK's own IssuerMismatchError, not a wrong redirect.

Review loop closed: the round held only an out-of-scope finding (pr-flow 7c exit). Rounds 1 to 5 each fixed one in-scope finding, with local:gate green after each.

@cliffhall
cliffhall merged commit 72f6578 into v2/main Oct 12, 2026
8 checks passed
@cliffhall
cliffhall deleted the v2/fix/2562-oauth-endpoint-overrides branch October 12, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authorization/Token URL overrides are ignored when the server exposes no OAuth metadata

2 participants