Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions packages/core/src/tools/mcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,18 +666,25 @@ async function handleAutomaticOAuth(
try {
debugLogger.log(`🔐 '${mcpServerName}' requires OAuth authentication`);

// Always try to parse the resource metadata URI from the www-authenticate header
let oauthConfig;
const resourceMetadataUri =
OAuthUtils.parseWWWAuthenticateHeader(wwwAuthenticate);
if (resourceMetadataUri) {
oauthConfig = await OAuthUtils.discoverOAuthConfig(resourceMetadataUri);
} else if (hasNetworkTransport(mcpServerConfig)) {
// Use the actual server URL for OAuth discovery and resource validation
const serverUrl = mcpServerConfig.httpUrl || mcpServerConfig.url;

// Try to discover OAuth config from the WWW-Authenticate header first.
// discoverOAuthFromWWWAuthenticate() directly fetches the resource_metadata URI
// from the header and validates the resource parameter against the server URL,
// avoiding the double .well-known path issue that occurs when passing a
// resource_metadata URI to discoverOAuthConfig() (which expects a server URL).
let oauthConfig = await OAuthUtils.discoverOAuthFromWWWAuthenticate(
wwwAuthenticate,
serverUrl,
);

if (!oauthConfig && hasNetworkTransport(mcpServerConfig)) {
// Fallback: try to discover OAuth config from the base URL
const serverUrl = new URL(
const serverUrlObj = new URL(
mcpServerConfig.httpUrl || mcpServerConfig.url!,
);
const baseUrl = `${serverUrl.protocol}//${serverUrl.host}`;
const baseUrl = `${serverUrlObj.protocol}//${serverUrlObj.host}`;
oauthConfig = await OAuthUtils.discoverOAuthConfig(baseUrl);
}

Expand All @@ -701,7 +708,6 @@ async function handleAutomaticOAuth(

// Perform OAuth authentication
// Pass the server URL for proper discovery
const serverUrl = mcpServerConfig.httpUrl || mcpServerConfig.url;
debugLogger.log(
`Starting OAuth authentication for server '${mcpServerName}'...`,
);
Expand Down