From 66071f9edd9495f879e023cd15ff2afb99e5435a Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:30:13 -0600 Subject: [PATCH] fix(core): refresh MCP OAuth tokens under the shared credential lock (#62827) --- packages/core/src/mcp/client.ts | 2 +- packages/core/src/mcp/oauth.ts | 102 ++++++++++- packages/core/test/mcp-oauth.test.ts | 243 ++++++++++++++++++++++++++- 3 files changed, 330 insertions(+), 17 deletions(-) diff --git a/packages/core/src/mcp/client.ts b/packages/core/src/mcp/client.ts index c638ddf6eff..e0a7a4b80f0 100644 --- a/packages/core/src/mcp/client.ts +++ b/packages/core/src/mcp/client.ts @@ -339,7 +339,7 @@ export const connect = Effect.fnUntraced(function* (options: ConnectOptions) { return yield* initialize(transport) } const { McpOAuth } = yield* Effect.promise(() => import("./oauth.js")) - const fetch = yield* McpOAuth.loggedFetch({ server, directory: options.directory }) + const fetch = yield* McpOAuth.loggedFetch({ server, directory: options.directory }, options.authProvider) // Servers that bundle their own Code Mode (Cloudflare and others) expose raw tools when asked // with ?codemode=false, which is what our Code Mode wants. The configured URL stays the OAuth identity. const url = new URL(config.url) diff --git a/packages/core/src/mcp/oauth.ts b/packages/core/src/mcp/oauth.ts index 63a005a48af..208c5d4267b 100644 --- a/packages/core/src/mcp/oauth.ts +++ b/packages/core/src/mcp/oauth.ts @@ -15,8 +15,12 @@ import { type StoredOAuthClientInformation, type StoredOAuthTokens, } from "@modelcontextprotocol/client" -import { OAuthMetadataSchema, OpenIdProviderDiscoveryMetadataSchema } from "@modelcontextprotocol/core" -import { Cause, Deferred, Effect } from "effect" +import { + OAuthMetadataSchema, + OAuthTokensSchema, + OpenIdProviderDiscoveryMetadataSchema, +} from "@modelcontextprotocol/core" +import { Cause, Data, Deferred, Effect, Option, Schema } from "effect" import { FetchHttpClient } from "effect/http" import type { ServerResponse } from "node:http" import type { AddressInfo } from "node:net" @@ -32,6 +36,20 @@ export const CLIENT_METADATA_URL = "https://opencode.ai/oauth/opencode/client.js // Refresh tokens rotate, so concurrent refreshes of the same token share one request or the second gets invalid_grant. const refreshes = new Map>() +/** Sends one refresh request whose body carries `token`, in place of the in-process share. */ +type Refresh = (send: FetchLike, url: string | URL, init: RequestInit, token: string) => ReturnType + +// Providers backed by a stored credential refresh under its cross-process lock; see connectProvider. +const lockedRefreshes = new WeakMap() + +const decodeJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown)) + +class RefreshRejected extends Data.TaggedError("McpOAuth.RefreshRejected")<{ + readonly status: number + readonly headers: Headers + readonly body: string +}> {} + const refreshKey = (url: string | URL, init: RequestInit | undefined) => { if (!(init?.body instanceof URLSearchParams) || init.body.get("grant_type") !== "refresh_token") return undefined return [String(url), init.body.get("client_id") ?? "", init.body.get("refresh_token") ?? ""].join("\u0000") @@ -43,12 +61,14 @@ const refreshKey = (url: string | URL, init: RequestInit | undefined) => { const share = (pending: ReturnType) => pending.then((response) => response.clone() as typeof response) // Requests use `FetchHttpClient.Fetch`, which an embedder can replace, rather than the global fetch. -const sender = Effect.gen(function* () { +const sender = Effect.fnUntraced(function* (refresh?: Refresh) { const fetch = yield* FetchHttpClient.Fetch const base: FetchLike = (url, init) => fetch(url, { ...init, timeout: false } as BunFetchRequestInit) const send: FetchLike = (url, init) => { const key = refreshKey(url, init) if (key === undefined) return base(url, init) + if (refresh && init?.body instanceof URLSearchParams) + return refresh(base, url, init, init.body.get("refresh_token") ?? "") const current = refreshes.get(key) if (current) return share(current) const pending = base(url, init).finally(() => { @@ -60,10 +80,14 @@ const sender = Effect.gen(function* () { return send }) -export const loggedFetch = (fields: { readonly server: string; readonly directory?: string }) => +/** Pass the connection's provider so a stored credential's refresh runs under the credential lock. */ +export const loggedFetch = ( + fields: { readonly server: string; readonly directory?: string }, + provider?: OAuthClientProvider, +) => Effect.gen(function* () { const run = Effect.runPromiseWith(yield* Effect.context()) - const send = yield* sender + const send = yield* sender(provider && lockedRefreshes.get(provider)) const request: FetchLike = (url, init) => { const grant = init?.body instanceof URLSearchParams ? init.body.get("grant_type") : undefined const operation = grant === "refresh_token" ? "refresh" : grant === "authorization_code" ? "exchange" : undefined @@ -292,7 +316,7 @@ export const connectProvider = Effect.fnUntraced(function* (input: { }) { const credentials = yield* Credential.Service const run = Effect.runPromiseWith(yield* Effect.context()) - const fetch = yield* sender + const fetch = yield* sender() const found = input.credential if (!found || found.value.type !== "oauth") return provider({ config: input.config, store: memoryStore(), fetch }) const id = found.id @@ -303,7 +327,64 @@ export const connectProvider = Effect.fnUntraced(function* (input: { } // Refresh tokens rotate and the row is shared across connections: only drop it while it still holds ours. let presented = found.value.refresh - return provider({ + // Every process and Location sharing the database refreshes this row under one lock, and the lock is held + // until the rotated tokens are saved. A caller holding a refresh token the row no longer has gets the + // stored tokens instead of spending it: servers that rotate refresh tokens revoke the grant on reuse. + const refresh: Refresh = (send, url, init, token) => + run( + credentials + .lock( + id, + Effect.gen(function* () { + const current = yield* credentials.get(id) + if (current?.value.type !== "oauth") return undefined + if (current.value.refresh !== token) { + yield* Effect.logInfo("mcp oauth refresh skipped", { credentialID: id, reason: "already_rotated" }) + return current.value + } + // Bounds reading the body too, so a server that stalls after its headers cannot hold the lock. + const { response, body } = yield* Effect.tryPromise(async (signal) => { + const response = await send(url, { ...init, signal }) + return { response, body: await response.text() } + }).pipe(Effect.timeout("30 seconds")) + const parsed = OAuthTokensSchema.safeParse( + response.ok ? Option.getOrUndefined(decodeJson(body)) : undefined, + ) + if (!parsed.success) + return yield* new RefreshRejected({ status: response.status, headers: response.headers, body }) + const latest = yield* credentials.get(id) + if (latest?.value.type !== "oauth") return undefined + if (latest.value.refresh !== token) { + yield* Effect.logWarning("mcp oauth refresh discarded", { credentialID: id, reason: "rotated_meanwhile" }) + return latest.value + } + // The SDK ignores an issuer in the token response; keep the one this credential was stamped with. + const issuer = latest.value.metadata?.issuer + const value = toCredential({ + methodID, + serverUrl: input.config.url, + tokens: { refresh_token: token, ...parsed.data, issuer: typeof issuer === "string" ? issuer : undefined }, + client: clientFromCredential(latest.value), + }) + yield* credentials.update(id, { value }) + return value + }), + ) + .pipe( + Effect.map((value) => + value + ? Response.json(toTokens(value)) + : // The credential was signed out meanwhile; the SDK drops it and reports needs_auth. + Response.json({ error: "invalid_grant" }, { status: 400 }), + ), + Effect.catch((error) => + error instanceof RefreshRejected + ? Effect.succeed(new Response(error.body, { status: error.status, headers: error.headers })) + : Effect.fail(error), + ), + ), + ) + const result = provider({ config: input.config, fetch, invalidate: async (scope) => { @@ -322,6 +403,8 @@ export const connectProvider = Effect.fnUntraced(function* (input: { }, saveTokens: async (tokens) => { const previous = await read() + // Another connection rotated past the token this one presented; its tokens win. + if (previous && previous.refresh !== presented && previous.refresh !== (tokens.refresh_token ?? "")) return const value = toCredential({ methodID, serverUrl: input.config.url, @@ -344,6 +427,8 @@ export const connectProvider = Effect.fnUntraced(function* (input: { saveCodeVerifier: async () => {}, }, }) + lockedRefreshes.set(result, refresh) + return result }) export const authorize = (input: { @@ -550,7 +635,8 @@ export const authorize = (input: { submit: (pasted: string) => Effect.gen(function* () { const text = pasted.trim() - if (!URL.canParse(text)) return yield* Effect.fail(new Error("Paste the full URL from the browser's address bar")) + if (!URL.canParse(text)) + return yield* Effect.fail(new Error("Paste the full URL from the browser's address bar")) const url = new URL(text) if (url.searchParams.get("state") !== state) return yield* Effect.fail(new Error("This URL belongs to a different sign-in")) diff --git a/packages/core/test/mcp-oauth.test.ts b/packages/core/test/mcp-oauth.test.ts index e91ff285607..7b029b59ef8 100644 --- a/packages/core/test/mcp-oauth.test.ts +++ b/packages/core/test/mcp-oauth.test.ts @@ -1,12 +1,16 @@ import { afterAll, describe, expect, test } from "bun:test" -import { auth, refreshAuthorization } from "@modelcontextprotocol/client" +import { auth, OAuthError, refreshAuthorization, type FetchLike } from "@modelcontextprotocol/client" +import path from "path" import { ConfigMCP } from "@opencode/schema/config/mcp" import { Credential } from "@opencode/core/credential" +import { Database } from "@opencode/core/database/database" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { Integration } from "@opencode/core/integration" import { McpClient } from "@opencode/core/mcp/client" import { McpOAuth } from "@opencode/core/mcp/oauth" -import { Cause, Effect, Exit } from "effect" +import { Cause, Effect, Exit, ManagedRuntime } from "effect" import { hostEnvironmentLayer } from "./fixture/environment" +import { tmpdir } from "./fixture/tmpdir" const authServer = Bun.serve({ port: 0, fetch: () => new Response(null, { status: 404 }) }) afterAll(() => authServer.stop(true)) @@ -169,7 +173,12 @@ describe("MCP OAuth", () => { async fetch(request) { if (request.method !== "POST" || new URL(request.url).pathname !== "/token") return new Response(null, { status: 404 }) - return Response.json({ access_token: "access", token_type: "Bearer", refresh_token: "refresh", expires_in: 3600 }) + return Response.json({ + access_token: "access", + token_type: "Bearer", + refresh_token: "refresh", + expires_in: 3600, + }) }, }) @@ -202,7 +211,12 @@ describe("MCP OAuth", () => { if (request.method !== "POST" || new URL(request.url).pathname !== "/token") return new Response(null, { status: 404 }) tokenRequests.push(new URLSearchParams(await request.text())) - return Response.json({ access_token: "access", token_type: "Bearer", refresh_token: "refresh", expires_in: 3600 }) + return Response.json({ + access_token: "access", + token_type: "Bearer", + refresh_token: "refresh", + expires_in: 3600, + }) }, }) @@ -235,10 +249,7 @@ describe("MCP OAuth", () => { fetch(request) { if (request.method !== "POST" || new URL(request.url).pathname !== "/token") return new Response(null, { status: 404 }) - return Response.json( - { error: "invalid_client", error_description: "bad_client_secret" }, - { status: 400 }, - ) + return Response.json({ error: "invalid_client", error_description: "bad_client_secret" }, { status: 400 }) }, }) @@ -677,3 +688,219 @@ describe("MCP OAuth", () => { }) }) }) + +// Each instance has its own database connection, in-process lock table and file lock client over one +// database file, the way the TUI, desktop app and background service share credentials. +describe("MCP OAuth refresh across processes", () => { + const id = Credential.ID.make("cred_shared") + + // A token endpoint that rotates refresh tokens and rejects reuse. The test releases each response. + const rotatingServer = () => { + const state = { current: "r1", generation: 1 } + const sent: string[] = [] + const received = Promise.withResolvers() + const release = Promise.withResolvers() + const server = Bun.serve({ + port: 0, + async fetch(request) { + const url = new URL(request.url) + if (request.method !== "POST" || url.pathname !== "/token") return new Response(null, { status: 404 }) + const token = new URLSearchParams(await request.text()).get("refresh_token") ?? "" + sent.push(token) + received.resolve() + await release.promise + if (token !== state.current) return Response.json({ error: "invalid_grant" }, { status: 400 }) + state.generation++ + state.current = `r${state.generation}` + return Response.json({ + access_token: `a${state.generation}`, + token_type: "Bearer", + refresh_token: state.current, + expires_in: 3600, + }) + }, + }) + return { server, sent, received: received.promise, release: () => release.resolve() } + } + + const instance = (file: string) => + ManagedRuntime.make( + AppNodeBuilder.build(Credential.node, [Database.node.replace(Database.configured({ path: file }))]), + ) + + const connection = (runtime: ReturnType, url: string) => + runtime.runPromise( + Effect.gen(function* () { + const credentials = yield* Credential.Service + const provider = yield* McpOAuth.connectProvider({ + config: remote(url), + credential: yield* credentials.get(id), + }) + return { provider, fetchFn: yield* McpOAuth.loggedFetch({ server: "test" }, provider) } + }), + ) + + const stored = async (runtime: ReturnType) => { + const value = (await runtime.runPromise(Credential.Service.use((credentials) => credentials.get(id))))?.value + return value?.type === "oauth" ? { access: value.access, refresh: value.refresh } : undefined + } + + const setup = async (url: string) => { + const tmp = await tmpdir() + const file = path.join(tmp.path, "opencode.db") + const first = instance(file) + const second = instance(file) + await first.runPromise( + Credential.Service.use((credentials) => + credentials.create({ ...credential({ access: "a1", refresh: "r1", url }), id }), + ), + ) + return { + first, + second, + async [Symbol.asyncDispose]() { + await Promise.all([first.dispose(), second.dispose()]) + await tmp[Symbol.asyncDispose]() + }, + } + } + + const refresh = (server: ReturnType, fetchFn: FetchLike, refreshToken: string) => + refreshAuthorization(new URL(server.url.origin), { + metadata: { + issuer: server.url.origin, + authorization_endpoint: `${server.url.origin}/authorize`, + token_endpoint: `${server.url.origin}/token`, + response_types_supported: ["code"], + }, + clientInformation: { client_id: "client" }, + refreshToken, + fetchFn, + }) + + test("sends one refresh when two instances refresh the same credential at once", async () => { + const endpoint = rotatingServer() + await using processes = await setup(endpoint.server.url.href) + const [first, second] = await Promise.all([ + connection(processes.first, endpoint.server.url.href), + connection(processes.second, endpoint.server.url.href), + ]) + + const pending = refresh(endpoint.server, first.fetchFn, "r1") + // The first instance holds the credential lock until its rotated tokens are saved. + await endpoint.received + const waiting = refresh(endpoint.server, second.fetchFn, "r1") + endpoint.release() + const results = await Promise.all([pending, waiting]).finally(() => endpoint.server.stop(true)) + + expect(endpoint.sent).toEqual(["r1"]) + expect(results.map((tokens) => [tokens.access_token, tokens.refresh_token])).toEqual([ + ["a2", "r2"], + ["a2", "r2"], + ]) + expect((await first.provider.tokens())?.access_token).toBe("a2") + expect((await second.provider.tokens())?.access_token).toBe("a2") + expect(await stored(processes.second)).toEqual({ access: "a2", refresh: "r2" }) + }) + + test("uses the stored tokens instead of sending a refresh token another instance already spent", async () => { + const endpoint = rotatingServer() + endpoint.release() + await using processes = await setup(endpoint.server.url.href) + const stale = await connection(processes.first, endpoint.server.url.href) + const other = await connection(processes.second, endpoint.server.url.href) + await refresh(endpoint.server, other.fetchFn, "r1") + + const tokens = await refresh(endpoint.server, stale.fetchFn, "r1").finally(() => endpoint.server.stop(true)) + + expect(endpoint.sent).toEqual(["r1"]) + expect([tokens.access_token, tokens.refresh_token]).toEqual(["a2", "r2"]) + expect(await stored(processes.first)).toEqual({ access: "a2", refresh: "r2" }) + }) + + test("keeps tokens another instance rotated when a slower refresh saves", async () => { + const endpoint = rotatingServer() + endpoint.release() + await using processes = await setup(endpoint.server.url.href) + const slow = await connection(processes.first, endpoint.server.url.href) + expect((await slow.provider.tokens())?.refresh_token).toBe("r1") + const other = await connection(processes.second, endpoint.server.url.href) + await refresh(endpoint.server, other.fetchFn, "r1").finally(() => endpoint.server.stop(true)) + + await slow.provider.saveTokens({ access_token: "late", token_type: "Bearer", refresh_token: "late" }) + + expect(await stored(processes.first)).toEqual({ access: "a2", refresh: "r2" }) + }) + + test("saves rotated tokens through the SDK's refresh under the lock", async () => { + const endpoint = rotatingServer() + endpoint.release() + await using processes = await setup(endpoint.server.url.href) + const current = await connection(processes.first, endpoint.server.url.href) + + const result = await auth(current.provider, { + serverUrl: endpoint.server.url.href, + fetchFn: current.fetchFn, + }).finally(() => endpoint.server.stop(true)) + + expect(result).toBe("AUTHORIZED") + expect(endpoint.sent).toEqual(["r1"]) + const value = (await processes.second.runPromise(Credential.Service.use((credentials) => credentials.get(id)))) + ?.value + expect(value?.type === "oauth" && [value.access, value.refresh, value.metadata?.issuer]).toEqual([ + "a2", + "r2", + endpoint.server.url.href, + ]) + }) + + test("hands a rejected refresh back to the SDK and keeps the credential", async () => { + const server = Bun.serve({ + port: 0, + fetch: () => Response.json({ error: "invalid_grant", error_description: "revoked" }, { status: 400 }), + }) + await using processes = await setup(server.url.href) + const current = await connection(processes.first, server.url.href) + + const error = await refresh(server, current.fetchFn, "r1") + .catch((error: unknown) => error) + .finally(() => server.stop(true)) + + expect(error).toBeInstanceOf(OAuthError) + expect(error instanceof OAuthError && [error.code, error.message]).toEqual(["invalid_grant", "revoked"]) + expect(await stored(processes.second)).toEqual({ access: "a1", refresh: "r1" }) + }) + + test("answers invalid_grant without a request when the credential was signed out", async () => { + const endpoint = rotatingServer() + endpoint.release() + await using processes = await setup(endpoint.server.url.href) + const current = await connection(processes.first, endpoint.server.url.href) + await processes.second.runPromise(Credential.Service.use((credentials) => credentials.remove(id))) + + const error = await refresh(endpoint.server, current.fetchFn, "r1") + .catch((error: unknown) => error) + .finally(() => endpoint.server.stop(true)) + + expect(error instanceof OAuthError && error.code).toBe("invalid_grant") + expect(endpoint.sent).toEqual([]) + }) + + test("releases the lock when the token endpoint is unreachable", async () => { + const endpoint = rotatingServer() + endpoint.release() + const unreachable = Bun.serve({ port: 0, fetch: () => new Response(null, { status: 404 }) }) + unreachable.stop(true) + await using processes = await setup(endpoint.server.url.href) + const current = await connection(processes.first, endpoint.server.url.href) + + const error = await refresh(unreachable, current.fetchFn, "r1").catch((error: unknown) => error) + expect(error).toBeInstanceOf(Error) + expect(error).not.toBeInstanceOf(OAuthError) + expect(await stored(processes.first)).toEqual({ access: "a1", refresh: "r1" }) + + const tokens = await refresh(endpoint.server, current.fetchFn, "r1").finally(() => endpoint.server.stop(true)) + expect([tokens.access_token, tokens.refresh_token]).toEqual(["a2", "r2"]) + expect(await stored(processes.second)).toEqual({ access: "a2", refresh: "r2" }) + }) +})