Skip to content

Commit d45a77e

Browse files
committed
feat: add OAuth 2.0/OpenID Connect metadata discovery fallback
- Add support for /.well-known/openid-configuration endpoint as fallback to oauth standard .well-known/oauth-authorization-server - Addresses GitHub discussion modelcontextprotocol#563
1 parent aa6a98a commit d45a77e

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎client/src/lib/oauth-state-machine.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ export interface StateTransition {
2525
execute: (context: StateMachineContext) => Promise<void>;
2626
}
2727

28+
/**
29+
* Discover OAuth metadata for all industry standard endpoints
30+
*/
31+
async function discoverOAuthMetadataWithFallback(authServerUrl: URL): ReturnType<typeof discoverOAuthMetadata> {
32+
let metadata = await discoverOAuthMetadata(authServerUrl);
33+
if (metadata) return metadata;
34+
35+
// Fallback to OpenID Connect Discovery endpoint
36+
// Include both standard OIDC appending to issuer and path, as well as RFC 8414 compatible of inserting between base and existing path
37+
const openidConfigUrls = [
38+
new URL(`${authServerUrl.origin}${authServerUrl.pathname}/.well-known/openid-configuration`), // OIDC standard
39+
new URL(`${authServerUrl.origin}/.well-known/openid-configuration${authServerUrl.pathname}`), // RFC 8414 compatible
40+
];
41+
42+
for (const url of openidConfigUrls) {
43+
try {
44+
const response = await fetch(url);
45+
metadata = await response.json();
46+
if (metadata) return metadata;
47+
} catch (e) {
48+
console.error(e)
49+
}
50+
}
51+
}
52+
2853
// State machine transitions
2954
export const oauthTransitions: Record<OAuthStep, StateTransition> = {
3055
metadata_discovery: {
@@ -56,7 +81,7 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
5681
resourceMetadata ?? undefined,
5782
);
5883

59-
const metadata = await discoverOAuthMetadata(authServerUrl);
84+
const metadata = await discoverOAuthMetadataWithFallback(authServerUrl);
6085
if (!metadata) {
6186
throw new Error("Failed to discover OAuth metadata");
6287
}

0 commit comments

Comments
 (0)