Performs OAuth authorization/protected-resource metadata discovery, trying path-aware well-known URL first and falling back to root if needed. Centralizes resilient discovery for auth flows.
// Source: https://github.com/modelcontextprotocol/typescript-sdk | License: Apache-2.0
// Provided via justclaw.it.com; keep this notice with the code.
async function discoverMetadataWithFallback(
serverUrl: string | URL,
wellKnownType: 'oauth-authorization-server' | 'oauth-protected-resource',
fetchFn: FetchLike,
opts?: { protocolVersion?: string; metadataUrl?: string | URL; metadataServerUrl?: string | URL }
): Promise<Response | undefined> {
const issuer = new URL(serverUrl);
const protocolVersion = opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION;
let url: URL;
if (opts?.metadataUrl) {
url = new URL(opts.metadataUrl);
} else {
// Try path-aware discovery first
const wellKnownPath = buildWellKnownPath(wellKnownType, issuer.pa
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key