@@ -698,6 +698,9 @@ export async function discoverAuthorizationServerMetadata(
698
698
} ) ;
699
699
}
700
700
701
+ const url = typeof authorizationServerUrl === 'string' ? new URL ( authorizationServerUrl ) : authorizationServerUrl ;
702
+ const hasPath = url . pathname !== '/' ;
703
+
701
704
const oauthMetadata = await retrieveOAuthMetadataFromAuthorizationServer ( authorizationServerUrl , {
702
705
fetchFn,
703
706
protocolVersion,
@@ -707,10 +710,26 @@ export async function discoverAuthorizationServerMetadata(
707
710
return oauthMetadata ;
708
711
}
709
712
710
- return retrieveOpenIdProviderMetadataFromAuthorizationServer ( authorizationServerUrl , {
713
+ const oidcMetadata = await retrieveOpenIdProviderMetadataFromAuthorizationServer ( authorizationServerUrl , {
711
714
fetchFn,
712
715
protocolVersion,
713
716
} ) ;
717
+
718
+ if ( oidcMetadata ) {
719
+ return oidcMetadata ;
720
+ }
721
+
722
+ // If both path-aware discoveries failed and the issuer has a path component,
723
+ // try OAuth discovery at the root as a final fallback for compatibility
724
+ if ( hasPath ) {
725
+ const rootUrl = new URL ( url . origin ) ;
726
+ return retrieveOAuthMetadataFromAuthorizationServer ( rootUrl , {
727
+ fetchFn,
728
+ protocolVersion,
729
+ } ) ;
730
+ }
731
+
732
+ return undefined ;
714
733
}
715
734
716
735
/**
@@ -756,8 +775,12 @@ async function retrieveOAuthMetadataFromMcpServer(
756
775
757
776
/**
758
777
* Retrieves RFC 8414 OAuth 2.0 Authorization Server Metadata from the authorization server.
778
+ *
779
+ * Per RFC 8414 Section 3.1, when the issuer identifier contains path components,
780
+ * the well-known URI is constructed by inserting "/.well-known/oauth-authorization-server"
781
+ * before the path component.
759
782
*
760
- * @param authorizationServerUrl - The authorization server URL
783
+ * @param authorizationServerUrl - The authorization server URL (issuer identifier)
761
784
* @param options - Configuration options
762
785
* @param options.fetchFn - Optional fetch function for making HTTP requests, defaults to global fetch
763
786
* @param options.protocolVersion - MCP protocol version to use (required)
@@ -774,7 +797,6 @@ async function retrieveOAuthMetadataFromAuthorizationServer(
774
797
}
775
798
) : Promise < OAuthMetadata | undefined > {
776
799
const url = typeof authorizationServerUrl === 'string' ? new URL ( authorizationServerUrl ) : authorizationServerUrl ;
777
-
778
800
const hasPath = url . pathname !== '/' ;
779
801
780
802
const metadataEndpoint = new URL (
@@ -801,8 +823,13 @@ async function retrieveOAuthMetadataFromAuthorizationServer(
801
823
802
824
/**
803
825
* Retrieves OpenID Connect Discovery 1.0 metadata from the authorization server.
826
+ *
827
+ * Per RFC 8414 Section 5 compatibility notes and OpenID Connect Discovery 1.0 Section 4.1,
828
+ * when the issuer identifier contains path components, discovery endpoints are tried in order:
829
+ * 1. RFC 8414 style: Insert /.well-known/openid-configuration before the path
830
+ * 2. OIDC Discovery 1.0 style: Append /.well-known/openid-configuration after the path
804
831
*
805
- * @param authorizationServerUrl - The authorization server URL
832
+ * @param authorizationServerUrl - The authorization server URL (issuer identifier)
806
833
* @param options - Configuration options
807
834
* @param options.fetchFn - Optional fetch function for making HTTP requests, defaults to global fetch
808
835
* @param options.protocolVersion - MCP protocol version to use (required)
0 commit comments