Skip to content

Commit 3eaf23c

Browse files
committed
fix: store resource in session storage
1 parent 1ea8e9a commit 3eaf23c

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

client/src/lib/auth.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,41 @@ export class DebugInspectorOAuthClientProvider extends InspectorOAuthClientProvi
131131
return JSON.parse(metadata);
132132
}
133133

134+
/**
135+
* Saves the resource URL to session storage to persist it across the redirect.
136+
*/
137+
saveResource(resource: URL): void {
138+
const key = getServerSpecificKey(
139+
SESSION_KEYS.RESOURCE_URL,
140+
this.serverUrl,
141+
);
142+
sessionStorage.setItem(key, resource.toString());
143+
}
144+
145+
/**
146+
* Retrieves the persisted resource URL from session storage.
147+
*/
148+
getResource(): URL | undefined {
149+
const key = getServerSpecificKey(
150+
SESSION_KEYS.RESOURCE_URL,
151+
this.serverUrl,
152+
);
153+
const urlString = sessionStorage.getItem(key);
154+
if (!urlString) {
155+
return undefined;
156+
}
157+
return new URL(urlString);
158+
}
159+
134160
clear() {
135161
super.clear();
136162
sessionStorage.removeItem(
137163
getServerSpecificKey(SESSION_KEYS.SERVER_METADATA, this.serverUrl),
138164
);
165+
166+
// Also clear the resource URL
167+
sessionStorage.removeItem(
168+
getServerSpecificKey(SESSION_KEYS.RESOURCE_URL, this.serverUrl),
169+
);
139170
}
140171
}

client/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const SESSION_KEYS = {
88
CLIENT_INFORMATION: "mcp_client_information",
99
SERVER_METADATA: "mcp_server_metadata",
1010
AUTH_DEBUGGER_STATE: "mcp_auth_debugger_state",
11+
RESOURCE_URL: "mcp_resource_url",
1112
} as const;
1213

1314
// Generate server-specific session storage keys

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
5656
resourceMetadata ?? undefined,
5757
);
5858

59+
// Persist the resource URL so it survives the redirect.
60+
if (resource) {
61+
context.provider.saveResource(resource);
62+
}
63+
5964
const metadata = await discoverOAuthMetadata(authServerUrl);
6065
if (!metadata) {
6166
throw new Error("Failed to discover OAuth metadata");
@@ -165,13 +170,16 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
165170
const metadata = context.provider.getServerMetadata()!;
166171
const clientInformation = (await context.provider.clientInformation())!;
167172

173+
// Retrieve the resource from persistent storage, not volatile state.
174+
const resource = context.provider.getResource();
175+
168176
const tokens = await exchangeAuthorization(context.serverUrl, {
169177
metadata,
170178
clientInformation,
171179
authorizationCode: context.state.authorizationCode,
172180
codeVerifier,
173181
redirectUri: context.provider.redirectUrl,
174-
resource: context.state.resource ?? undefined,
182+
resource: resource ?? undefined,
175183
});
176184

177185
context.provider.saveTokens(tokens);

0 commit comments

Comments
 (0)