Skip to content

fix: use discoverOAuthFromWWWAuthenticate for reactive OAuth flow (#18760)#18798

Closed
echoVic wants to merge 1 commit intogoogle-gemini:mainfrom
echoVic:fix/oauth-resource-metadata-discovery
Closed

fix: use discoverOAuthFromWWWAuthenticate for reactive OAuth flow (#18760)#18798
echoVic wants to merge 1 commit intogoogle-gemini:mainfrom
echoVic:fix/oauth-resource-metadata-discovery

Conversation

@echoVic
Copy link

@echoVic echoVic commented Feb 11, 2026

Problem

handleAutomaticOAuth() passes the resource_metadata URI (already a .well-known URL extracted from the WWW-Authenticate header) to discoverOAuthConfig(), which expects a server URL and internally constructs .well-known paths from it. This causes:

  1. Double-nested .well-known paths → 404 on resource metadata fetch
  2. Fallback to base URL → fetches metadata without path context
  3. ResourceMismatchError → the resource parameter from fallback metadata doesn't match the path-based server URL

This affects all OAuth-protected MCP servers that serve resources under paths (gateways, multi-tenant setups).

Root Cause

// BEFORE (broken): resourceMetadataUri is already a .well-known URL
const resourceMetadataUri = OAuthUtils.parseWWWAuthenticateHeader(wwwAuthenticate);
if (resourceMetadataUri) {
  // discoverOAuthConfig() will try to build .well-known URLs from this,
  // resulting in double-nested paths like:
  // /.well-known/oauth-protected-resource/.well-known/oauth-protected-resource/path
  oauthConfig = await OAuthUtils.discoverOAuthConfig(resourceMetadataUri);
}

Fix

Use the existing discoverOAuthFromWWWAuthenticate() utility which correctly:

  1. Parses the resource_metadata URI from the WWW-Authenticate header
  2. Directly fetches the resource metadata (no double .well-known construction)
  3. Validates the resource parameter against the actual server URL (RFC 9728 §7.3)
  4. Discovers the authorization server from the metadata
// AFTER (fixed): use the correct utility that handles the full flow
const serverUrl = mcpServerConfig.httpUrl || mcpServerConfig.url;
let oauthConfig = await OAuthUtils.discoverOAuthFromWWWAuthenticate(
  wwwAuthenticate,
  serverUrl,
);

Fallback to base URL discovery via discoverOAuthConfig() is preserved for servers that don't include resource_metadata in the WWW-Authenticate header.

Fixes #18760

The handleAutomaticOAuth() function was passing the resource_metadata URI
(already a .well-known URL) to discoverOAuthConfig() which expects a server
URL and constructs .well-known paths from it. This caused double-nested
.well-known paths (404) and subsequent ResourceMismatchError.

Fix: Use the existing discoverOAuthFromWWWAuthenticate() which directly
fetches the resource_metadata URI and validates against the actual server
URL, with fallback to base URL discovery.

Fixes google-gemini#18760
@echoVic echoVic requested a review from a team as a code owner February 11, 2026 06:06
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @echoVic, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the automatic OAuth handling for MCP servers, particularly those with path-based resources. The previous implementation caused failures in OAuth configuration discovery due to incorrect URI handling, resulting in authentication issues. The fix refactors the discovery process to use a more robust utility, ensuring proper resource metadata fetching and validation, thereby improving the reliability of OAuth authentication for affected server configurations.

Highlights

  • OAuth Discovery Fix: Resolved an issue where handleAutomaticOAuth() incorrectly passed an already .well-known URI to discoverOAuthConfig(), leading to double-nested .well-known paths, 404 errors, and ResourceMismatchError for OAuth-protected MCP servers.
  • Corrected Utility Usage: Switched to using OAuthUtils.discoverOAuthFromWWWAuthenticate() which correctly parses the resource_metadata URI, directly fetches metadata, validates the resource parameter against the server URL, and discovers the authorization server.
  • Fallback Preservation: Ensured that the fallback mechanism to discover OAuth configuration from the base URL via discoverOAuthConfig() is preserved for servers that do not include resource_metadata in the WWW-Authenticate header.
Changelog
  • packages/core/src/tools/mcp-client.ts
    • Refactored handleAutomaticOAuth to use discoverOAuthFromWWWAuthenticate for initial OAuth configuration discovery.
    • Adjusted the declaration of serverUrl to be available earlier in the handleAutomaticOAuth function.
    • Modified the fallback logic for OAuth discovery to correctly use discoverOAuthConfig when discoverOAuthFromWWWAuthenticate does not yield a configuration.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@google-cla
Copy link

google-cla bot commented Feb 11, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-cli gemini-cli bot added the area/security Issues related to security label Feb 11, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes a bug in the automatic OAuth flow where a resource_metadata URI was incorrectly passed to a function expecting a server URL. The change to use discoverOAuthFromWWWAuthenticate correctly handles the discovery process, making the implementation more robust by preserving fallback logic and removing redundant serverUrl variable declarations. The security analysis did not find any vulnerabilities, although it noted that the content of several files, such as packages/core/src/tools/mcp-client.ts and packages/core/src/mcp/oauth-utils.ts, could not be read, which prevented SAST reconnaissance passes on them.

@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 16, 2026
@scidomino scidomino closed this Feb 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality area/security Issues related to security help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(oauth): 401-triggered OAuth discovery fails for path-based MCP servers due to misrouted resource_metadata URI

2 participants