Skip to content

Commit 3589f5f

Browse files
fix(mcp): persist OAuth request constraints (Zoo-Code-Org#2096)
1 parent cfdd03d commit 3589f5f

4 files changed

Lines changed: 300 additions & 22 deletions

File tree

‎src/services/mcp/McpOAuthClientProvider.ts‎

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
104104
private readonly _tokenEndpointAuthMethod: string,
105105
private readonly _grantTypes: McpOAuthGrantType[],
106106
private readonly _scopes: string[],
107+
private readonly _scopeConfigured: boolean,
107108
private readonly _state: string,
108109
private readonly _authServerMeta: Record<string, any> | null,
109110
private readonly _resourceIndicator: string | null,
@@ -154,10 +155,18 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
154155
const authMethods: string[] = authServerMeta?.token_endpoint_auth_methods_supported ?? []
155156
const tokenEndpointAuthMethod = authMethods.includes("none") ? "none" : "client_secret_post"
156157
const grantTypes = selectMcpOAuthGrantTypes(authServerMeta?.grant_types_supported)
157-
const scopes =
158-
options?.scope !== undefined
159-
? options.scope.split(/\s+/).filter((scope) => scope.length > 0)
160-
: (authServerMeta?.scopes_supported ?? [])
158+
const configuredScope = options?.scope
159+
const scopeConfigured = configuredScope !== undefined
160+
const scopes = scopeConfigured
161+
? configuredScope.split(/\s+/).filter((scope) => scope.length > 0)
162+
: (authServerMeta?.scopes_supported ?? [])
163+
164+
const storedData = await secretStorage.getOAuthData(serverUrl, options?.clientId)
165+
const issuerMatches = typeof storedData?.issuer === "string" && storedData.issuer === authServerMeta?.issuer
166+
const storedResourceIndicator =
167+
!resourceIndicator && options?.resource !== false && issuerMatches
168+
? storedData?.resource_indicator
169+
: undefined
161170

162171
// Generate a CSRF state token for the OAuth flow.
163172
const state = Array.from(crypto.getRandomValues(new Uint8Array(16)))
@@ -177,6 +186,7 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
177186
tokenEndpointAuthMethod,
178187
grantTypes,
179188
scopes,
189+
scopeConfigured,
180190
state,
181191
authServerMeta,
182192
resourceIndicator,
@@ -185,6 +195,9 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
185195
options?.redirectUri,
186196
options?.clientId,
187197
)
198+
if (storedResourceIndicator) {
199+
provider._effectiveResourceIndicator = storedResourceIndicator
200+
}
188201

189202
if (options?.clientId) {
190203
if (options.redirectUri) {
@@ -335,6 +348,12 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
335348
await this._secretStorage.deleteOAuthData(this._serverUrl, this._configuredClientId)
336349
return undefined
337350
}
351+
352+
// Restore the authorization resource after a restart only when the current
353+
// configuration or discovery did not already select one.
354+
if (!this._effectiveResourceIndicator && !this._resourceDisabled) {
355+
this._effectiveResourceIndicator = data.resource_indicator ?? null
356+
}
338357
if (this._refreshPromise) {
339358
return this._refreshPromise
340359
}
@@ -376,6 +395,7 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
376395
tokens,
377396
expires_at,
378397
...(this._authServerMeta?.issuer ? { issuer: this._authServerMeta.issuer as string } : {}),
398+
...(this._effectiveResourceIndicator ? { resource_indicator: this._effectiveResourceIndicator } : {}),
379399
...(clientInfo ? { client_info: clientInfo } : {}),
380400
},
381401
this._configuredClientId,
@@ -395,8 +415,8 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
395415
// with path components, causing it to fall back to a default "/authorize" path.
396416
// We correct the URL using our pre-fetched metadata:
397417
// 1. Replace the origin+pathname with the real authorization_endpoint.
398-
// 2. Preserve all SDK-generated query params (client_id, code_challenge, etc.)
399-
// 3. Add `scope` when the server advertises scopes but the SDK omitted it.
418+
// 2. Preserve all SDK-generated query params (client_id, code_challenge, etc.).
419+
// 3. Override SDK scopes when the configuration explicitly supplies a scope.
400420
// 4. Set an explicit or discovered RFC 8707 `resource` parameter. Preserve
401421
// an SDK-generated value unless the configuration disables it.
402422
if (this._resourceIndicator) {
@@ -406,7 +426,12 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
406426
authorizationUrl.searchParams.delete("resource")
407427
this._effectiveResourceIndicator = null
408428
} else {
409-
this._effectiveResourceIndicator = authorizationUrl.searchParams.get("resource")
429+
const sdkResourceIndicator = authorizationUrl.searchParams.get("resource")
430+
if (sdkResourceIndicator) {
431+
this._effectiveResourceIndicator = sdkResourceIndicator
432+
} else if (this._effectiveResourceIndicator) {
433+
authorizationUrl.searchParams.set("resource", this._effectiveResourceIndicator)
434+
}
410435
}
411436
let correctedUrl = authorizationUrl
412437
if (this._authServerMeta?.authorization_endpoint) {
@@ -427,16 +452,22 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
427452
authorizationUrl.searchParams.forEach((value, key) => {
428453
fixed.searchParams.set(key, value)
429454
})
430-
// Ensure the scope param is present — the SDK sometimes omits it
431-
if (!fixed.searchParams.has("scope") && this._scopes.length > 0) {
432-
fixed.searchParams.set("scope", this._scopes.join(" "))
433-
}
434455
correctedUrl = fixed
435456
} catch {
436457
// Fall through and use the original URL if correction fails
437458
}
438459
}
439460

461+
if (this._scopeConfigured) {
462+
if (this._scopes.length > 0) {
463+
correctedUrl.searchParams.set("scope", this._scopes.join(" "))
464+
} else {
465+
correctedUrl.searchParams.delete("scope")
466+
}
467+
} else if (!correctedUrl.searchParams.has("scope") && this._scopes.length > 0) {
468+
correctedUrl.searchParams.set("scope", this._scopes.join(" "))
469+
}
470+
440471
// Store the (possibly corrected) URL; it will be opened by openBrowser()
441472
// once the user confirms the "Authenticate" toast in McpHub.
442473
this._pendingAuthorizationUrl = correctedUrl

‎src/services/mcp/SecretStorageService.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export interface StoredMcpOAuthData {
1010
* allowed only against the same issuer. Missing on data saved by older versions.
1111
*/
1212
issuer?: string
13+
/**
14+
* RFC 8707 resource used during authorization and token requests. This keeps
15+
* refresh requests consistent after the extension restarts.
16+
*/
17+
resource_indicator?: string
1318
/**
1419
* Full DCR response from the auth server, persisted so that fields like
1520
* client_secret, grant_types, and token_endpoint_auth_method survive restarts.

0 commit comments

Comments
 (0)