@@ -332,12 +332,6 @@ async function authInternal(
332
332
// Ignore errors and fall back to /.well-known/oauth-authorization-server
333
333
}
334
334
335
- const resource : URL | undefined = await selectResourceURL ( serverUrl , provider , resourceMetadata ) ;
336
-
337
- const metadata = await discoverAuthorizationServerMetadata ( serverUrl , authorizationServerUrl , {
338
- fetchFn,
339
- } ) ;
340
-
341
335
/**
342
336
* If we don't get a valid authorization server metadata from protected resource metadata,
343
337
* fallback to the legacy MCP spec's implementation (version 2025-03-26): MCP server acts as the Authorization server.
@@ -346,6 +340,12 @@ async function authInternal(
346
340
authorizationServerUrl = serverUrl ;
347
341
}
348
342
343
+ const resource : URL | undefined = await selectResourceURL ( serverUrl , provider , resourceMetadata ) ;
344
+
345
+ const metadata = await discoverAuthorizationServerMetadata ( authorizationServerUrl , {
346
+ fetchFn,
347
+ } ) ;
348
+
349
349
// Handle client registration if needed
350
350
let clientInformation = await Promise . resolve ( provider . clientInformation ( ) ) ;
351
351
if ( ! clientInformation ) {
@@ -664,24 +664,19 @@ export async function discoverOAuthMetadata(
664
664
* and OpenID Connect Discovery 1.0 specifications.
665
665
*
666
666
* This function implements a fallback strategy for authorization server discovery:
667
- * 1. If `authorizationServerUrl` is provided, attempts RFC 8414 OAuth metadata discovery first
667
+ * 1. Attempts RFC 8414 OAuth metadata discovery first
668
668
* 2. If OAuth discovery fails, falls back to OpenID Connect Discovery
669
- * 3. If `authorizationServerUrl` is not provided, uses legacy MCP specification behavior
670
669
*
671
- * @param serverUrl - The MCP Server URL, used for legacy specification support where the MCP server
672
- * acts as both the resource server and authorization server
673
670
* @param authorizationServerUrl - The authorization server URL obtained from the MCP Server's
674
- * protected resource metadata. If this parameter is `undefined`,
675
- * it indicates that protected resource metadata was not successfully
676
- * retrieved, triggering legacy fallback behavior
671
+ * protected resource metadata, or the MCP server's URL if the
672
+ * metadata was not found.
677
673
* @param options - Configuration options
678
674
* @param options.fetchFn - Optional fetch function for making HTTP requests, defaults to global fetch
679
675
* @param options.protocolVersion - MCP protocol version to use, defaults to LATEST_PROTOCOL_VERSION
680
676
* @returns Promise resolving to authorization server metadata, or undefined if discovery fails
681
677
*/
682
678
export async function discoverAuthorizationServerMetadata (
683
- serverUrl : string | URL ,
684
- authorizationServerUrl ?: string | URL ,
679
+ authorizationServerUrl : string | URL ,
685
680
{
686
681
fetchFn = fetch ,
687
682
protocolVersion = LATEST_PROTOCOL_VERSION ,
@@ -690,18 +685,10 @@ export async function discoverAuthorizationServerMetadata(
690
685
protocolVersion ?: string ;
691
686
} = { }
692
687
) : Promise < AuthorizationServerMetadata | undefined > {
693
- if ( ! authorizationServerUrl ) {
694
- // Legacy support: MCP servers act as the Auth server.
695
- return retrieveOAuthMetadataFromMcpServer ( serverUrl , {
696
- fetchFn,
697
- protocolVersion,
698
- } ) ;
699
- }
700
-
701
688
const url = typeof authorizationServerUrl === 'string' ? new URL ( authorizationServerUrl ) : authorizationServerUrl ;
702
689
const hasPath = url . pathname !== '/' ;
703
690
704
- const oauthMetadata = await retrieveOAuthMetadataFromAuthorizationServer ( authorizationServerUrl , {
691
+ const oauthMetadata = await fetchOAuthMetadata ( authorizationServerUrl , {
705
692
fetchFn,
706
693
protocolVersion,
707
694
} ) ;
@@ -712,7 +699,7 @@ export async function discoverAuthorizationServerMetadata(
712
699
713
700
if ( hasPath ) {
714
701
const rootUrl = new URL ( url . origin ) ;
715
- const rootOauthMetadata = await retrieveOAuthMetadataFromAuthorizationServer ( rootUrl , {
702
+ const rootOauthMetadata = await fetchOAuthMetadata ( rootUrl , {
716
703
fetchFn,
717
704
protocolVersion,
718
705
} ) ;
@@ -730,50 +717,9 @@ export async function discoverAuthorizationServerMetadata(
730
717
return oidcMetadata ;
731
718
}
732
719
733
- /**
734
- * Legacy implementation where the MCP server acts as the Auth server.
735
- * According to MCP spec version 2025-03-26.
736
- *
737
- * @param serverUrl - The MCP Server URL
738
- * @param options - Configuration options
739
- * @param options.fetchFn - Optional fetch function for making HTTP requests, defaults to global fetch
740
- * @param options.protocolVersion - MCP protocol version to use (required)
741
- * @returns Promise resolving to OAuth metadata, or undefined if discovery fails
742
- */
743
- async function retrieveOAuthMetadataFromMcpServer (
744
- serverUrl : string | URL ,
745
- {
746
- fetchFn = fetch ,
747
- protocolVersion,
748
- } : {
749
- fetchFn ?: FetchLike ;
750
- protocolVersion : string ;
751
- }
752
- ) : Promise < OAuthMetadata | undefined > {
753
- const serverOrigin = typeof serverUrl === 'string' ? new URL ( serverUrl ) . origin : serverUrl . origin ;
754
-
755
- const metadataEndpoint = new URL ( buildWellKnownPath ( 'oauth-authorization-server' ) , serverOrigin ) ;
756
-
757
- const response = await fetchWithCorsRetry ( metadataEndpoint , getProtocolVersionHeader ( protocolVersion ) , fetchFn ) ;
758
-
759
- if ( ! response ) {
760
- throw new Error ( `CORS error trying to load OAuth metadata from ${ metadataEndpoint } ` ) ;
761
- }
762
-
763
- if ( ! response . ok ) {
764
- if ( response . status === 404 ) {
765
- return undefined ;
766
- }
767
-
768
- throw new Error ( `HTTP ${ response . status } trying to load OAuth metadata from ${ metadataEndpoint } ` ) ;
769
- }
770
-
771
- return OAuthMetadataSchema . parse ( await response . json ( ) ) ;
772
- }
773
-
774
720
/**
775
721
* Retrieves RFC 8414 OAuth 2.0 Authorization Server Metadata from the authorization server.
776
- *
722
+ *
777
723
* Per RFC 8414 Section 3.1, when the issuer identifier contains path components,
778
724
* the well-known URI is constructed by inserting "/.well-known/oauth-authorization-server"
779
725
* before the path component.
@@ -784,7 +730,7 @@ async function retrieveOAuthMetadataFromMcpServer(
784
730
* @param options.protocolVersion - MCP protocol version to use (required)
785
731
* @returns Promise resolving to OAuth metadata, or undefined if discovery fails
786
732
*/
787
- async function retrieveOAuthMetadataFromAuthorizationServer (
733
+ async function fetchOAuthMetadata (
788
734
authorizationServerUrl : string | URL ,
789
735
{
790
736
fetchFn = fetch ,
@@ -821,7 +767,7 @@ async function retrieveOAuthMetadataFromAuthorizationServer(
821
767
822
768
/**
823
769
* Retrieves OpenID Connect Discovery 1.0 metadata from the authorization server.
824
- *
770
+ *
825
771
* Per RFC 8414 Section 5 compatibility notes and OpenID Connect Discovery 1.0 Section 4.1,
826
772
* when the issuer identifier contains path components, discovery endpoints are tried in order:
827
773
* 1. RFC 8414 style: Insert /.well-known/openid-configuration before the path
0 commit comments