feat(coding-agent): support OAuth discovery for path-prefixed auth servers behind gateways

- Extract resource_metadata URL from WWW-Authenticate and follow RFC 9728 chain
- Add buildWellKnownUrls with path-prefixed well-known fallback for gateways
- Fix resolveRegistrationEndpoint to try path-prefixed well-known (was missing await)
- Support relative Mcp-Auth-Server URL resolution against server URL
- Pass resourceMetadataUrl through all discoverOAuthEndpoints call sites
- Add comprehensive tests for path-prefixed, resource_metadata, and relative URL flows
This commit is contained in:
Mohd Faiz Hasim
2026-05-26 22:50:32 +08:00
parent 8da054f4cc
commit 5be0f01a91
6 changed files with 283 additions and 47 deletions
@@ -401,12 +401,16 @@ export class MCPCommandController {
);
return;
}
const authResult = analyzeAuthError(error as Error);
const authResult = analyzeAuthError(error as Error, finalConfig.url);
if (authResult.requiresAuth) {
let oauth = authResult.authType === "oauth" ? (authResult.oauth ?? null) : null;
if (!oauth && finalConfig.url) {
try {
oauth = await discoverOAuthEndpoints(finalConfig.url, authResult.authServerUrl);
oauth = await discoverOAuthEndpoints(
finalConfig.url,
authResult.authServerUrl,
authResult.resourceMetadataUrl,
);
} catch {
// Ignore discovery error and handle below.
}
@@ -742,11 +746,11 @@ export class MCPCommandController {
}
// Analyze the connection error to extract OAuth endpoints
const authResult = analyzeAuthError(connectionError!);
const authResult = analyzeAuthError(connectionError!, "url" in config ? config.url : undefined);
let oauth = authResult.authType === "oauth" ? (authResult.oauth ?? null) : null;
if (!oauth && (config.type === "http" || config.type === "sse") && config.url) {
oauth = await discoverOAuthEndpoints(config.url, authResult.authServerUrl);
oauth = await discoverOAuthEndpoints(config.url, authResult.authServerUrl, authResult.resourceMetadataUrl);
}
if (!oauth) {