Skip to content

Commit 9bcf502

Browse files
fix(mcp): preserve SDK OAuth resource (#2096)
1 parent 382678b commit 9bcf502

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎src/services/mcp/McpOAuthClientProvider.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
105105
private readonly _state: string,
106106
private readonly _authServerMeta: Record<string, any> | null,
107107
private readonly _resourceIndicator: string | null,
108+
private readonly _resourceDisabled: boolean,
108109
private readonly _clientName: string,
109110
private readonly _configuredRedirectUri?: string,
110111
private readonly _configuredClientId?: string,
@@ -175,6 +176,7 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
175176
state,
176177
authServerMeta,
177178
resourceIndicator,
179+
options?.resource === false,
178180
serverName || "Roo Code",
179181
options?.redirectUri,
180182
options?.clientId,
@@ -389,11 +391,11 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
389391
// 1. Replace the origin+pathname with the real authorization_endpoint.
390392
// 2. Preserve all SDK-generated query params (client_id, code_challenge, etc.)
391393
// 3. Add `scope` when the server advertises scopes but the SDK omitted it.
392-
// 4. Add RFC 8707 `resource` parameter when the protected resource metadata
393-
// advertised a resource indicator.
394+
// 4. Set an explicit or discovered RFC 8707 `resource` parameter. Preserve
395+
// an SDK-generated value unless the configuration disables it.
394396
if (this._resourceIndicator) {
395397
authorizationUrl.searchParams.set("resource", this._resourceIndicator)
396-
} else {
398+
} else if (this._resourceDisabled) {
397399
authorizationUrl.searchParams.delete("resource")
398400
}
399401
let correctedUrl = authorizationUrl

‎src/services/mcp/__tests__/McpHub.spec.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,16 @@ describe("McpHub", () => {
17261726
{ type: "stdio", command: "test", oauth: { clientId: "client" } },
17271727
{ type: "sse", url: "https://mcp.example.com/sse", oauth: { clientId: "client" } },
17281728
{ type: "streamable-http", url: "https://mcp.example.com/mcp", oauth: { clientId: "" } },
1729-
{ type: "streamable-http", url: "https://mcp.example.com/mcp", oauth: { resource: "invalid" } },
1729+
{
1730+
type: "streamable-http",
1731+
url: "https://mcp.example.com/mcp",
1732+
oauth: { clientId: "client", resource: "invalid" },
1733+
},
1734+
{
1735+
type: "streamable-http",
1736+
url: "https://mcp.example.com/mcp",
1737+
oauth: { clientId: "client", resource: true },
1738+
},
17301739
{ type: "streamable-http", url: "https://mcp.example.com/mcp", oauth: { resource: false } },
17311740
])("rejects invalid OAuth configuration: $type", (config) => {
17321741
expect(() => ServerConfigSchema.parse(config)).toThrow()

‎src/services/mcp/__tests__/McpOAuthClientProvider.spec.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,20 +602,23 @@ describe("McpOAuthClientProvider", () => {
602602
await provider.close()
603603
})
604604

605-
it("should not duplicate resource if the SDK already included it", async () => {
605+
it("should preserve an SDK resource when provider discovery finds no resource", async () => {
606+
;(discoverOAuthProtectedResourceMetadata as any).mockResolvedValueOnce({
607+
authorization_servers: ["https://auth.example.com"],
608+
})
606609
setupCallbackServerMock()
607610
const provider = await McpOAuthClientProvider.create("https://example.com/mcp", createMockSecretStorage())
608611

609-
// SDK URL already contains a resource param
610612
const sdkUrl = new URL(
611613
"https://auth.example.com/authorize?client_id=abc&resource=https%3A%2F%2Fexample.com%2F&state=123",
612614
)
613615
await provider.redirectToAuthorization(sdkUrl)
614616
await provider.openBrowser()
615617

616-
const openedUri = (vscode.env.openExternal as any).mock.calls[0][0].toString()
617-
const resourceMatches = (openedUri.match(/resource=/g) || []).length
618-
expect(resourceMatches).toBe(1)
618+
const openedUri = vi.mocked(vscode.env.openExternal).mock.calls[0][0].toString()
619+
const openedUrl = new URL(openedUri)
620+
expect(openedUrl.searchParams.get("resource")).toBe("https://example.com/")
621+
expect(openedUrl.searchParams.getAll("resource")).toHaveLength(1)
619622
await provider.close()
620623
})
621624

0 commit comments

Comments
 (0)