MCPcopy Create free account
hub / github.com/modelcontextprotocol/typescript-sdk / discoverMetadataWithFallback

Function discoverMetadataWithFallback

src/client/auth.ts:828–856  ·  view source on GitHub ↗

* Generic function for discovering OAuth metadata with fallback support

(
    serverUrl: string | URL,
    wellKnownType: 'oauth-authorization-server' | 'oauth-protected-resource',
    fetchFn: FetchLike,
    opts?: { protocolVersion?: string; metadataUrl?: string | URL; metadataServerUrl?: string | URL }
)

Source from the content-addressed store, hash-verified

826 * Generic function for discovering OAuth metadata with fallback support
827 */
828async function discoverMetadataWithFallback(
829 serverUrl: string | URL,
830 wellKnownType: 'oauth-authorization-server' | 'oauth-protected-resource',
831 fetchFn: FetchLike,
832 opts?: { protocolVersion?: string; metadataUrl?: string | URL; metadataServerUrl?: string | URL }
833): Promise<Response | undefined> {
834 const issuer = new URL(serverUrl);
835 const protocolVersion = opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION;
836
837 let url: URL;
838 if (opts?.metadataUrl) {
839 url = new URL(opts.metadataUrl);
840 } else {
841 // Try path-aware discovery first
842 const wellKnownPath = buildWellKnownPath(wellKnownType, issuer.pathname);
843 url = new URL(wellKnownPath, opts?.metadataServerUrl ?? issuer);
844 url.search = issuer.search;
845 }
846
847 let response = await tryMetadataDiscovery(url, protocolVersion, fetchFn);
848
849 // If path-aware discovery fails with 404 and we're not already at root, try fallback to root discovery
850 if (!opts?.metadataUrl && shouldAttemptFallback(response, issuer.pathname)) {
851 const rootUrl = new URL(`/.well-known/${wellKnownType}`, issuer);
852 response = await tryMetadataDiscovery(rootUrl, protocolVersion, fetchFn);
853 }
854
855 return response;
856}
857
858/**
859 * Looks up RFC 8414 OAuth 2.0 Authorization Server Metadata.

Callers 2

discoverOAuthMetadataFunction · 0.85

Calls 3

buildWellKnownPathFunction · 0.85
tryMetadataDiscoveryFunction · 0.85
shouldAttemptFallbackFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…