Skip to content

Commit d6fed2a

Browse files
committed
pick resource, and store in state
1 parent 32cca1d commit d6fed2a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

client/src/lib/auth-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface AuthDebuggerState {
3030
oauthStep: OAuthStep;
3131
resourceMetadata: OAuthProtectedResourceMetadata | null;
3232
resourceMetadataError: Error | null;
33+
resource: string | null;
3334
authServerUrl: URL | null;
3435
oauthMetadata: OAuthMetadata | null;
3536
oauthClientInfo: OAuthClientInformationFull | OAuthClientInformation | null;

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
exchangeAuthorization,
88
discoverOAuthProtectedResourceMetadata,
99
} from "@modelcontextprotocol/sdk/client/auth.js";
10+
import {
11+
resourceUrlFromServerUrl
12+
} from "@modelcontextprotocol/sdk/shared/auth-utils.js";
1013
import {
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

Comments
 (0)