Skip to content

Commit f326cf4

Browse files
fix(mcp): request offline access from Google so it issues a refresh token
Google returns a refresh token only for access_type=offline, and after the first consent only with prompt=consent. Without it the MCP login expires after an hour (anomalyco#59820).
1 parent fedc6f2 commit f326cf4

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎packages/core/src/mcp/oauth.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ const provider = (options: Options): OAuthClientProvider => {
247247
throw new Error(
248248
`MCP server "${options.config.url}" returned a ${url.protocol} authorization URL; only http and https are supported`,
249249
)
250+
// Google issues a refresh token only for access_type=offline, and again after the first consent
251+
// only with prompt=consent; without one the login expires after an hour (anomalyco/opencode#59820).
252+
if (url.hostname === "accounts.google.com") {
253+
if (!url.searchParams.has("access_type")) url.searchParams.set("access_type", "offline")
254+
if (!url.searchParams.has("prompt")) url.searchParams.set("prompt", "consent")
255+
}
250256
return redirect.open(url)
251257
},
252258
...(options.invalidate ? { invalidateCredentials: options.invalidate } : {}),

‎packages/core/test/mcp-oauth.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,25 @@ describe("MCP OAuth", () => {
605605
expect(url.searchParams.has("prompt")).toBe(false)
606606
})
607607

608+
test("asks Google for offline access and consent so it issues a refresh token", async () => {
609+
const { server } = authorizationServer({ authorization_endpoint: "https://accounts.google.com/o/oauth2/v2/auth" })
610+
const { url } = await Effect.runPromise(Effect.scoped(start(server, { client_id: "client" }))).finally(() =>
611+
server.stop(true),
612+
)
613+
expect(url.hostname).toBe("accounts.google.com")
614+
expect(url.searchParams.get("access_type")).toBe("offline")
615+
expect(url.searchParams.get("prompt")).toBe("consent")
616+
})
617+
618+
test("leaves other authorization servers without Google-specific parameters", async () => {
619+
const { server } = authorizationServer({})
620+
const { url } = await Effect.runPromise(Effect.scoped(start(server, { client_id: "client" }))).finally(() =>
621+
server.stop(true),
622+
)
623+
expect(url.searchParams.has("access_type")).toBe(false)
624+
expect(url.searchParams.has("prompt")).toBe(false)
625+
})
626+
608627
test("forwards iss from the redirect so issuer-advertising servers can complete", async () => {
609628
const { server } = authorizationServer({ authorization_response_iss_parameter_supported: true })
610629
const result = await Effect.runPromise(

0 commit comments

Comments
 (0)