77 exchangeAuthorization ,
88 discoverOAuthProtectedResourceMetadata ,
99} from "@modelcontextprotocol/sdk/client/auth.js" ;
10+ import {
11+ resourceUrlFromServerUrl
12+ } from "@modelcontextprotocol/sdk/shared/auth-utils.js" ;
1013import {
1114 OAuthMetadataSchema ,
1215 OAuthProtectedResourceMetadata ,
@@ -37,7 +40,7 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
3740 resourceMetadata = await discoverOAuthProtectedResourceMetadata (
3841 context . serverUrl ,
3942 ) ;
40- if ( resourceMetadata . authorization_servers ?. length ) {
43+ if ( resourceMetadata ? .authorization_servers ?. length ) {
4144 authServerUrl = new URL ( resourceMetadata . authorization_servers [ 0 ] ) ;
4245 }
4346 } catch ( e ) {
@@ -48,11 +51,15 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
4851 }
4952 }
5053
51- // TODO: use SDK function selectResourceURL here once new version bump lands
52- if ( resourceMetadata && resourceMetadata . resource !== context . serverUrl ) {
53- resourceMetadataError = new Error (
54- `Warning: metadata resource ${ resourceMetadata . resource } does not match serverUrl ${ context . serverUrl } ` ,
55- ) ;
54+ let resource : string | undefined ;
55+ if ( resourceMetadata ) {
56+ resource = resourceUrlFromServerUrl ( context . serverUrl ) ;
57+ // TODO: use SDK function selectResourceURL once version bump lands to be consistent
58+ if ( resource !== resourceMetadata . resource )
59+ resourceMetadataError = new Error (
60+ `Warning: metadata resource ${ resourceMetadata . resource } does not match serverUrl ${ context . serverUrl } ` ,
61+ ) ;
62+ }
5663 }
5764
5865 const metadata = await discoverOAuthMetadata ( authServerUrl ) ;
@@ -63,6 +70,7 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
6370 context . provider . saveServerMetadata ( parsedMetadata ) ;
6471 context . updateState ( {
6572 resourceMetadata,
73+ resource,
6674 resourceMetadataError,
6775 authServerUrl,
6876 oauthMetadata : parsedMetadata ,
@@ -118,7 +126,7 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
118126 clientInformation,
119127 redirectUrl : context . provider . redirectUrl ,
120128 scope,
121- resource : new URL ( context . serverUrl ) ,
129+ resource : context . state . resource ,
122130 } ,
123131 ) ;
124132
@@ -169,7 +177,7 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
169177 authorizationCode : context . state . authorizationCode ,
170178 codeVerifier,
171179 redirectUri : context . provider . redirectUrl ,
172- resource : new URL ( context . serverUrl ) ,
180+ resource : context . state . resource ,
173181 } ) ;
174182
175183 context . provider . saveTokens ( tokens ) ;
0 commit comments