Skip to content

Commit 70eda0c

Browse files
committed
fix(mcp): include scope in OAuth authorization URL for pre-registered clients
SDK v1.27.1 does not fall back to clientMetadata.scope when building the authorization URL for pre-registered OAuth clients, causing configured scopes to be dropped from the OAuth consent screen. Augment the URL in redirectToAuthorization as a safe guard — the !has("scope") check prevents duplication once the SDK is upgraded. Closes anomalyco#28895
1 parent 0cf99cf commit 70eda0c

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎packages/opencode/src/mcp/oauth-provider.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export class McpOAuthProvider implements OAuthClientProvider {
135135
}
136136

137137
async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
138+
if (this.config.scope && !authorizationUrl.searchParams.has("scope")) {
139+
authorizationUrl.searchParams.set("scope", this.config.scope)
140+
}
138141
log.info("redirecting to authorization", { mcpName: this.mcpName, url: authorizationUrl.toString() })
139142
await this.callbacks.onRedirect(authorizationUrl)
140143
}

‎packages/opencode/test/mcp/oauth-provider.test.ts‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,127 @@ describe("McpOAuthProvider.clientMetadata", () => {
5959
expect(provider.clientMetadata.token_endpoint_auth_method).toBe("none")
6060
})
6161
})
62+
63+
describe("McpOAuthProvider.redirectToAuthorization", () => {
64+
test("appends scope from config when URL lacks it", async () => {
65+
let captured: URL | undefined
66+
const provider = new McpOAuthProvider(
67+
"test-scope",
68+
"https://mcp.example.com/mcp",
69+
{ clientId: "pre-registered-id", scope: "openid offline_access" },
70+
{ onRedirect: async (url) => { captured = url } },
71+
stubAuth,
72+
)
73+
74+
const urlWithoutScope = new URL(
75+
"https://auth.example.com/authorize?client_id=test&state=abc&code_challenge=xyz&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A19876%2Fmcp%2Foauth%2Fcallback",
76+
)
77+
await provider.redirectToAuthorization(urlWithoutScope)
78+
79+
expect(captured?.searchParams.get("scope")).toBe("openid offline_access")
80+
})
81+
82+
test("does NOT append scope when URL already has it (SDK upgraded)", async () => {
83+
let captured: URL | undefined
84+
const provider = new McpOAuthProvider(
85+
"test-scope-dup",
86+
"https://mcp.example.com/mcp",
87+
{ clientId: "pre-registered-id", scope: "openid profile" },
88+
{ onRedirect: async (url) => { captured = url } },
89+
stubAuth,
90+
)
91+
92+
const urlWithScope = new URL("https://auth.example.com/authorize?client_id=test&scope=openid%20email&state=abc")
93+
await provider.redirectToAuthorization(urlWithScope)
94+
95+
// URL should keep the SDK's scope, not overwrite with config scope
96+
expect(captured?.searchParams.get("scope")).toBe("openid email")
97+
})
98+
99+
test("does NOT append scope when config has none", async () => {
100+
let captured: URL | undefined
101+
const provider = new McpOAuthProvider(
102+
"test-no-scope",
103+
"https://mcp.example.com/mcp",
104+
{ clientId: "pre-registered-id" },
105+
{ onRedirect: async (url) => { captured = url } },
106+
stubAuth,
107+
)
108+
109+
const url = new URL("https://auth.example.com/authorize?client_id=test&state=abc")
110+
await provider.redirectToAuthorization(url)
111+
112+
expect(captured?.searchParams.has("scope")).toBe(false)
113+
})
114+
115+
test("does NOT append scope when scope config is empty string", async () => {
116+
let captured: URL | undefined
117+
const provider = new McpOAuthProvider(
118+
"test-empty-scope",
119+
"https://mcp.example.com/mcp",
120+
{ clientId: "pre-registered-id", scope: "" },
121+
{ onRedirect: async (url) => { captured = url } },
122+
stubAuth,
123+
)
124+
125+
const url = new URL("https://auth.example.com/authorize?client_id=test&state=abc")
126+
await provider.redirectToAuthorization(url)
127+
128+
expect(captured?.searchParams.has("scope")).toBe(false)
129+
})
130+
131+
test("URL-encodes multi-space scope correctly", async () => {
132+
let captured: URL | undefined
133+
const provider = new McpOAuthProvider(
134+
"test-multi-scope",
135+
"https://mcp.example.com/mcp",
136+
{ clientId: "pre-registered-id", scope: "openid email profile offline_access" },
137+
{ onRedirect: async (url) => { captured = url } },
138+
stubAuth,
139+
)
140+
141+
const url = new URL("https://auth.example.com/authorize?client_id=test&state=abc")
142+
await provider.redirectToAuthorization(url)
143+
144+
expect(captured?.searchParams.get("scope")).toBe("openid email profile offline_access")
145+
})
146+
147+
test("appends scope to URL that has no query params", async () => {
148+
let captured: URL | undefined
149+
const provider = new McpOAuthProvider(
150+
"test-no-params",
151+
"https://mcp.example.com/mcp",
152+
{ scope: "openid" },
153+
{ onRedirect: async (url) => { captured = url } },
154+
stubAuth,
155+
)
156+
157+
const url = new URL("https://auth.example.com/authorize")
158+
await provider.redirectToAuthorization(url)
159+
160+
expect(captured?.searchParams.get("scope")).toBe("openid")
161+
})
162+
163+
test("preserves all existing params when appending scope", async () => {
164+
let captured: URL | undefined
165+
const provider = new McpOAuthProvider(
166+
"test-preserve",
167+
"https://mcp.example.com/mcp",
168+
{ scope: "openid" },
169+
{ onRedirect: async (url) => { captured = url } },
170+
stubAuth,
171+
)
172+
173+
const url = new URL(
174+
"https://auth.example.com/authorize?response_type=code&client_id=myclient&state=xyz&code_challenge=abc123&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A19876%2Fcallback",
175+
)
176+
await provider.redirectToAuthorization(url)
177+
178+
expect(captured?.searchParams.get("response_type")).toBe("code")
179+
expect(captured?.searchParams.get("client_id")).toBe("myclient")
180+
expect(captured?.searchParams.get("state")).toBe("xyz")
181+
expect(captured?.searchParams.get("code_challenge")).toBe("abc123")
182+
expect(captured?.searchParams.get("code_challenge_method")).toBe("S256")
183+
expect(captured?.searchParams.get("scope")).toBe("openid")
184+
})
185+
})

0 commit comments

Comments
 (0)