diff --git a/packages/core/src/mcp/client.ts b/packages/core/src/mcp/client.ts index ebee2300918..0a40969f8d2 100644 --- a/packages/core/src/mcp/client.ts +++ b/packages/core/src/mcp/client.ts @@ -19,7 +19,6 @@ import { type ElicitResult, type GetPromptResult, type Implementation, - type OAuthClientProvider, type Prompt, type ReadResourceResult, type Resource, @@ -31,6 +30,7 @@ import { Cause, Clock, Duration, Effect, Exit, Schedule, Schema } from "effect" import { ConfigMCP } from "@opencode/schema/config/mcp" import type { Session } from "@opencode/schema/session" import { isRecord } from "@opencode/util/record" +import type { McpOAuth } from "./oauth.js" import { McpStdio } from "./stdio.js" const DEFAULT_STARTUP_TIMEOUT = 30_000 @@ -177,7 +177,7 @@ export const connect = Effect.fnUntraced(function* ( config: typeof ConfigMCP.Server.Type | Attached, directory: string, // Remote only. A provider with no stored token and a no-op redirect ends in UnauthorizedError → needs_auth. - authProvider?: OAuthClientProvider, + authProvider?: McpOAuth.Provider, elicitation?: ElicitationHandler, clientInfo: Implementation = { name: "opencode", version: "unknown" }, ) { @@ -301,7 +301,7 @@ export const connect = Effect.fnUntraced(function* ( if (!URL.canParse(config.url)) return yield* new ConnectError({ server, message: `Invalid MCP URL for "${server}"` }) const { McpOAuth } = yield* Effect.promise(() => import("./oauth.js")) - const fetch = yield* McpOAuth.loggedFetch({ server, directory }) + const fetch = yield* McpOAuth.loggedFetch({ server, directory }, authProvider?.send) // 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/index.ts b/packages/core/src/mcp/index.ts index f9951531529..778fc86855a 100644 --- a/packages/core/src/mcp/index.ts +++ b/packages/core/src/mcp/index.ts @@ -273,12 +273,26 @@ export const layer = (options?: Options) => return { name, entry } }) - const connectProvider = Effect.fnUntraced(function* (entry: ServerEntry) { + const connectProvider = Effect.fnUntraced(function* (name: ServerName, entry: ServerEntry) { if (entry.config.type !== "remote" || !entry.integrationID) return undefined const { McpOAuth } = yield* Effect.promise(() => import("./oauth.js")) - return yield* McpOAuth.connectProvider({ config: entry.config, integrationID: entry.integrationID }).pipe( - Effect.provideService(Credential.Service, credentials), - ) + const scope = entry.scope + // Connect reports a rejected sign-in itself; a live connection needs to be taken to needs_auth. + const onRejected = () => + callbacks( + Effect.gen(function* () { + // A failed connect already cleared the scope and reported needs_auth. + if (entry.scope !== scope) return + entry.status = { status: "needs_auth", error: `MCP server "${name}" requires a new sign-in` } + yield* stopServer(name, entry) + yield* bus.publish(McpEvent.StatusChanged, { server: name }) + }).pipe(locks.withLock(name)), + ) + return yield* McpOAuth.connectProvider({ + config: entry.config, + integrationID: entry.integrationID, + onRejected, + }).pipe(Effect.provideService(Credential.Service, credentials)) }) const elicitation = { @@ -488,7 +502,7 @@ export const layer = (options?: Options) => yield* bus.publish(McpEvent.StatusChanged, { server: name }) const scope = yield* Scope.fork(servers) entry.scope = scope - const authProvider = yield* connectProvider(entry) + const authProvider = yield* connectProvider(name, entry) const { McpClient } = yield* Effect.promise(() => import("./client.js")) // List tools as part of connect so a failure here marks the server failed rather than // leaving it connected with a silently empty tool list and no path to recover. diff --git a/packages/core/src/mcp/oauth.ts b/packages/core/src/mcp/oauth.ts index 72450bb6cff..e8902370a59 100644 --- a/packages/core/src/mcp/oauth.ts +++ b/packages/core/src/mcp/oauth.ts @@ -15,7 +15,11 @@ import { type StoredOAuthClientInformation, type StoredOAuthTokens, } from "@modelcontextprotocol/client" -import { OAuthMetadataSchema, OpenIdProviderDiscoveryMetadataSchema } from "@modelcontextprotocol/core" +import { + OAuthMetadataSchema, + OAuthTokensSchema, + OpenIdProviderDiscoveryMetadataSchema, +} from "@modelcontextprotocol/core" import { Cause, Deferred, Effect } from "effect" import type { ServerResponse } from "node:http" import { ConfigMCP } from "@opencode/schema/config/mcp" @@ -28,33 +32,13 @@ import { ErrorSummary } from "../util/error-summary.js" /** Client ID Metadata Document: servers that support CIMD accept this URL as the client_id without registration. */ export const CLIENT_METADATA_URL = "https://opencode.ai/oauth/opencode/client.json" -// Refresh tokens rotate, so concurrent refreshes of the same token share one request or the second gets invalid_grant. -const refreshes = new Map>() - -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") -} - // Bun's and Node's fetch types both apply here and the SDK's FetchLike wants Bun's Response, so the // overload is picked by annotation and clone() is pinned to the type it was called on. A tool call // can stay silent for minutes, so Bun's idle deadline is off; the MCP execution timeout still applies. const base: FetchLike = noTimeoutFetch -const share = (pending: ReturnType) => pending.then((response) => response.clone() as typeof response) +const copy = (response: Awaited>) => response.clone() as typeof response -const send: FetchLike = (url, init) => { - const key = refreshKey(url, init) - if (key === undefined) return base(url, init) - const current = refreshes.get(key) - if (current) return share(current) - const pending = base(url, init).finally(() => { - if (refreshes.get(key) === pending) refreshes.delete(key) - }) - refreshes.set(key, pending) - return share(pending) -} - -export const loggedFetch = (fields: { readonly server: string; readonly directory?: string }) => +export const loggedFetch = (fields: { readonly server: string; readonly directory?: string }, send = base) => Effect.gen(function* () { const run = Effect.runPromiseWith(yield* Effect.context()) const request: FetchLike = (url, init) => { @@ -144,9 +128,15 @@ export interface Options { readonly clientMetadataUrl?: string readonly discovery?: OAuthDiscoveryState readonly invalidate?: OAuthClientProvider["invalidateCredentials"] + readonly send?: FetchLike } -export const provider = (options: Options): OAuthClientProvider => { +export interface Provider extends OAuthClientProvider { + /** The fetch the transport must use, so refreshes of a saved credential run under its lock. */ + readonly send?: FetchLike +} + +export const provider = (options: Options): Provider => { const oauth = options.config.oauth || undefined const client = oauth?.client_id ? { client_id: oauth.client_id, client_secret: oauth.client_secret } : undefined const redirect = options.redirect @@ -159,7 +149,7 @@ export const provider = (options: Options): OAuthClientProvider => { return { redirectUrl, discoveryState: async () => { - discovery ??= await configuredDiscovery({ config: options.config, fetchFn: send }) + discovery ??= await configuredDiscovery({ config: options.config, fetchFn: base }) return discovery }, saveDiscoveryState: (state) => { @@ -205,6 +195,7 @@ export const provider = (options: Options): OAuthClientProvider => { return redirect.open(url) }, ...(options.invalidate ? { invalidateCredentials: options.invalidate } : {}), + ...(options.send ? { send: options.send } : {}), saveCodeVerifier: (verifier) => options.store.saveCodeVerifier(verifier), codeVerifier: async () => { const verifier = await options.store.codeVerifier() @@ -248,7 +239,7 @@ export const toCredential = (input: { methodID: input.methodID, access: input.tokens.access_token, refresh: input.tokens.refresh_token ?? "", - // 0 is non-expiring; toTokens then omits expires_in so the SDK does not force a refresh. + // 0 is non-expiring; toTokens then omits expires_in. expires: input.tokens.expires_in ? Date.now() + input.tokens.expires_in * 1000 : 0, metadata: { serverUrl: input.serverUrl, @@ -274,55 +265,110 @@ export const toTokens = (credential: Credential.OAuth): StoredOAuthTokens => { export const connectProvider = Effect.fnUntraced(function* (input: { readonly config: typeof ConfigMCP.Remote.Type readonly integrationID: Integration.ID + readonly onRejected?: () => void }) { const credentials = yield* Credential.Service const run = Effect.runPromiseWith(yield* Effect.context()) const found = (yield* credentials.list(input.integrationID)).at(-1) if (!found || found.value.type !== "oauth") return provider({ config: input.config, store: memoryStore() }) const id = found.id - const methodID = found.value.methodID const read = async () => { const stored = await run(credentials.get(id)) return stored?.value.type === "oauth" ? stored.value : undefined } - // Refresh tokens rotate and the row is shared across connections: only drop it while it still holds ours. - let presented = found.value.refresh + // The SDK stamps tokens with the issuer it discovered; the next save under the lock persists it. + let issuer: string | undefined + // The access token on this connection's last request rejected with 401 or 403. + let rejectedAccess: string | undefined + // Refresh tokens rotate, so read, refresh and save run under the credential lock that other + // connections and processes share, as in Integration.connection.resolve. + const refresh = (url: string | URL, init: RequestInit, presented: string, stale: string | undefined) => + credentials.lock( + id, + Effect.gen(function* () { + const current = yield* credentials.get(id) + if (current?.value.type !== "oauth") return undefined + const oauth = current.value + // Another connection already replaced the token the server rejected. + if (stale !== undefined && oauth.access !== stale) return oauth + const response = yield* Effect.tryPromise((signal) => base(url, { ...init, signal })).pipe( + Effect.timeout("30 seconds"), + ) + if (!response.ok) { + const error = yield* Effect.promise(async () => parseErrorResponse(await response.clone().text())) + // The tokens stay, but the next sign-in must register a new client instead of reusing a rejected one. + if (error.code === "invalid_client" || error.code === "unauthorized_client") + yield* credentials.update(id, { value: { ...oauth, metadata: { ...oauth.metadata, client: undefined } } }) + return yield* Effect.fail(response) + } + const tokens = yield* Effect.tryPromise(async () => OAuthTokensSchema.parse(await response.json())) + const value = toCredential({ + methodID: oauth.methodID, + serverUrl: input.config.url, + tokens: { + ...tokens, + refresh_token: tokens.refresh_token ?? presented, + issuer: toTokens(oauth).issuer ?? issuer, + }, + client: clientFromCredential(oauth), + }) + yield* credentials.update(id, { value }) + return value + }), + ) + const send: FetchLike = (url, init) => { + const presented = + init?.body instanceof URLSearchParams && init.body.get("grant_type") === "refresh_token" + ? init.body.get("refresh_token") + : null + if (!init || presented === null) + return base(url, init).then((response) => { + const bearer = new Headers(init?.headers).get("authorization")?.replace(/^bearer /i, "") + if (bearer && (response.status === 401 || response.status === 403)) rejectedAccess = bearer + return response + }) + return run( + refresh(url, init, presented, rejectedAccess).pipe( + Effect.map((oauth) => + oauth + ? Response.json(toTokens(oauth)) + : Response.json({ error: "invalid_grant", error_description: "Credential removed" }, { status: 400 }), + ), + // Every waiter gets the rejection, which the SDK reads as invalid_grant and so on. + Effect.catchIf( + (error) => error instanceof Response, + (response) => Effect.succeed(copy(response)), + ), + ), + ) + } + // A rejected refresh keeps the saved credential, so nobody is signed out. This connection stops offering + // its refresh token, so the SDK's retry ends in needs_auth instead of being rejected again. + let invalidated = false return provider({ config: input.config, + send, invalidate: async (scope) => { if (scope === "verifier" || scope === "discovery") return - const oauth = await read() - if (!oauth || oauth.refresh !== presented) return - await run(Effect.logWarning("mcp oauth credential invalidated", { credentialID: id, scope })) - await run(credentials.remove(id)) + await run(Effect.logWarning("mcp oauth credential rejected", { credentialID: id, scope })) + invalidated = true + input.onRejected?.() }, store: { tokens: async () => { const oauth = await read() if (!oauth) return undefined - presented = oauth.refresh - return toTokens(oauth) + return toTokens(invalidated ? { ...oauth, refresh: "" } : oauth) }, + // Tokens are only saved under the lock in send, so a late save cannot undo a newer rotation. saveTokens: async (tokens) => { - const previous = await read() - const value = toCredential({ - methodID, - serverUrl: input.config.url, - tokens, - client: previous ? clientFromCredential(previous) : undefined, - }) - presented = value.refresh - await run(credentials.update(id, { value })) + issuer ??= tokens.issuer }, clientInformation: async () => { const oauth = await read() return oauth ? clientFromCredential(oauth) : undefined }, - saveClientInformation: async (client) => { - const oauth = await read() - if (!oauth) return - await run(credentials.update(id, { value: { ...oauth, metadata: { ...oauth.metadata, client } } })) - }, + saveClientInformation: async () => {}, codeVerifier: async () => undefined, saveCodeVerifier: async () => {}, }, diff --git a/packages/core/test/mcp-oauth.test.ts b/packages/core/test/mcp-oauth.test.ts index c40ed2e176f..d5a3cef50e0 100644 --- a/packages/core/test/mcp-oauth.test.ts +++ b/packages/core/test/mcp-oauth.test.ts @@ -1,12 +1,25 @@ import { afterAll, describe, expect, test } from "bun:test" -import { auth, refreshAuthorization } from "@modelcontextprotocol/client" +import { createHash } from "node:crypto" +import { auth } from "@modelcontextprotocol/client" +import { createMcpHandler, Server } from "@modelcontextprotocol/server" import { ConfigMCP } from "@opencode/schema/config/mcp" +import { Bus } from "@opencode/core/bus" +import { Config } from "@opencode/core/config" import { Credential } from "@opencode/core/credential" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Environment } from "@opencode/core/environment/index" +import { Form } from "@opencode/core/form" import { Integration } from "@opencode/core/integration" +import { Location } from "@opencode/core/location" +import { Mcp } from "@opencode/core/mcp/index" import { McpClient } from "@opencode/core/mcp/client" import { McpOAuth } from "@opencode/core/mcp/oauth" -import { Cause, Effect, Exit } from "effect" +import { AbsolutePath } from "@opencode/core/schema" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Cause, Context, Effect, Exit, Fiber, Layer, Schedule } from "effect" import { hostEnvironmentLayer } from "./fixture/environment" +import { location } from "./fixture/location" +import { testEffect } from "./lib/effect" const authServer = Bun.serve({ port: 0, fetch: () => new Response(null, { status: 404 }) }) afterAll(() => authServer.stop(true)) @@ -37,7 +50,7 @@ const memoryCredentials = (initial: Credential.Info[]) => { const rows = new Map(initial.map((row) => [row.id, row])) const unused = () => Effect.die("unused credential method") const service = Credential.Service.of({ - lock: unused, + lock: (_id, operation) => operation, all: unused, create: unused, activate: unused, @@ -240,7 +253,9 @@ describe("MCP OAuth", () => { const store = memoryCredentials([credential({ access: "expired", refresh: "refresh", url: server.url.href })]) const oauthProvider = await connectProvider(remote(server.url.href), store) - const result = await auth(oauthProvider, { serverUrl: server.url.href }).finally(() => server.stop(true)) + const result = await auth(oauthProvider, { serverUrl: server.url.href, fetchFn: oauthProvider.send }).finally(() => + server.stop(true), + ) expect(result).toBe("AUTHORIZED") expect(tokenRequests).toHaveLength(1) @@ -281,67 +296,6 @@ describe("MCP OAuth", () => { expect(registrations).toBe(0) }) - test("drops an invalidated credential only while it still holds the presented token", async () => { - const url = authServer.url.href - const rotated = memoryCredentials([credential({ access: "a", refresh: "r1", url })]) - const rotatedProvider = await connectProvider(remote(url), rotated) - await rotatedProvider.tokens() - // Another connection refreshed first; the row now holds a token this provider never presented. - await Effect.runPromise( - rotated.service.update(Credential.ID.make("cred_test"), { - value: credential({ access: "b", refresh: "r2", url }).value, - }), - ) - await rotatedProvider.invalidateCredentials?.("tokens") - expect(rotated.rows.size).toBe(1) - - const stale = memoryCredentials([credential({ access: "a", refresh: "r1", url })]) - const staleProvider = await connectProvider(remote(url), stale) - await staleProvider.tokens() - await staleProvider.invalidateCredentials?.("tokens") - expect(stale.rows.size).toBe(0) - }) - - test("shares concurrent refreshes for the same token", async () => { - let requests = 0 - const pending = Promise.withResolvers() - const server = Bun.serve({ - port: 0, - async fetch(request) { - const body = new URLSearchParams(await request.text()) - if (body.get("grant_type") !== "refresh_token") return new Response(null, { status: 404 }) - requests++ - await pending.promise - return Response.json({ access_token: "access", token_type: "Bearer", refresh_token: "next" }) - }, - }) - const fetchFn = await Effect.runPromise(McpOAuth.loggedFetch({ server: "test" })) - const options = { - 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: "refresh", - fetchFn, - } - - const first = refreshAuthorization(new URL(server.url.origin), options) - const second = refreshAuthorization(new URL(server.url.origin), options) - // Both refreshes must reach the token endpoint before the shared response is released. - await new Promise((resolve) => setTimeout(resolve, 50)) - - expect(requests).toBe(1) - pending.resolve() - const results = await Promise.all([first, second]).finally(() => server.stop(true)) - expect(results).toEqual([ - { access_token: "access", token_type: "Bearer", refresh_token: "next" }, - { access_token: "access", token_type: "Bearer", refresh_token: "next" }, - ]) - }) - test("generates a loopback redirect URL when none is configured", async () => { expect(await authorize()).toMatch(/^http:\/\/127\.0\.0\.1:\d+\/callback$/) }) @@ -585,3 +539,311 @@ describe("MCP OAuth", () => { }) }) }) + +const it = testEffect( + AppNodeBuilder.build( + LayerNode.group([ + Config.node, + Bus.node, + Integration.node, + Credential.node, + Form.node, + Environment.node, + Location.node, + ]), + [ + Location.node.replace( + Layer.succeed( + Location.Service, + Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) })), + ), + ), + Environment.node.replace(hostEnvironmentLayer), + ], + ), +) + +// An OAuth-protected MCP server whose refresh tokens are single-use: replaying one returns invalid_grant. +// With hold, every 401 after the first waits until the first refresh was answered. A dead client gets invalid_client. +const rotatingServer = Effect.fnUntraced(function* (input: { + readonly refresh: string + readonly hold?: boolean + readonly deadClient?: string +}) { + const counts = { refresh: 0, invalidGrant: 0 } + const state = { + current: input.refresh, + access: new Set(), + refreshed: Promise.withResolvers(), + registrations: 0, + } + let issued = 0 + let rejections = 0 + const handler = createMcpHandler(() => { + const protocol = new Server({ name: "mock", version: "1.0.0" }, { capabilities: { tools: {} } }) + protocol.setRequestHandler("tools/list", () => + Promise.resolve({ tools: [{ name: "echo", inputSchema: { type: "object" as const, properties: {} } }] }), + ) + protocol.setRequestHandler("tools/call", () => + Promise.resolve({ content: [{ type: "text" as const, text: "ok" }] }), + ) + return protocol + }) + const server = Bun.serve({ + port: 0, + async fetch(request) { + const url = new URL(request.url) + if (url.pathname.startsWith("/.well-known/oauth-protected-resource")) + return Response.json({ resource: `${url.origin}/mcp`, authorization_servers: [url.origin] }) + if (url.pathname.startsWith("/.well-known/oauth-authorization-server")) + return Response.json({ + issuer: url.origin, + authorization_endpoint: `${url.origin}/authorize`, + token_endpoint: `${url.origin}/token`, + registration_endpoint: `${url.origin}/register`, + response_types_supported: ["code"], + grant_types_supported: ["authorization_code", "refresh_token"], + }) + if (url.pathname === "/register") { + state.registrations++ + return Response.json({ client_id: `registered-${state.registrations}`, redirect_uris: [] }) + } + if (url.pathname === "/token") { + const body = new URLSearchParams(await request.text()) + if (body.get("client_id") === input.deadClient) + return Response.json({ error: "invalid_client" }, { status: 401 }) + if (body.get("grant_type") === "refresh_token") { + counts.refresh++ + state.refreshed.resolve() + if (body.get("refresh_token") !== state.current) { + counts.invalidGrant++ + return Response.json({ error: "invalid_grant", error_description: "refresh token reused" }, { status: 400 }) + } + } + issued++ + state.current = `rt${issued}` + const access = `at${issued}` + state.access.add(access) + return Response.json({ + access_token: access, + token_type: "Bearer", + refresh_token: state.current, + expires_in: 3600, + }) + } + if (url.pathname !== "/mcp") return new Response(null, { status: 404 }) + if (!state.access.has((request.headers.get("authorization") ?? "").replace(/^Bearer /, ""))) { + if (input.hold && rejections++ > 0) await state.refreshed.promise + return new Response(null, { + status: 401, + headers: { + "www-authenticate": `Bearer resource_metadata="${url.origin}/.well-known/oauth-protected-resource"`, + }, + }) + } + return handler.fetch(request) + }, + }) + yield* Effect.addFinalizer(() => Effect.promise(() => server.stop(true))) + return { url: `${server.url.origin}/mcp`, counts, state } +}) + +const rotatingConfig = (url: string) => + new ConfigMCP.Remote({ type: "remote", url, oauth: { client_id: "client" }, codemode: false }) + +// Saves the credential under the integration the Mcp layer registers for server "svc". +const saveCredential = Effect.fnUntraced(function* (input: { + readonly url: string + readonly access: string + readonly refresh: string + readonly expires?: number + readonly client?: string +}) { + const credentials = yield* Credential.Service + return yield* credentials.create({ + integrationID: Integration.ID.make( + "mcp_" + createHash("sha1").update(`svc\u0000${input.url}`).digest("hex").slice(0, 16), + ), + value: Credential.OAuth.make({ + type: "oauth", + methodID, + access: input.access, + refresh: input.refresh, + expires: input.expires ?? Date.now() - 1000, + metadata: { + serverUrl: input.url, + tokenType: "Bearer", + ...(input.client ? { client: { client_id: input.client } } : {}), + }, + }), + }) +}) + +// A slow save stands in for SQLite contention during a boot burst. +const slowCredentials = Effect.fnUntraced(function* (saveDelay = 0) { + const credentials = yield* Credential.Service + return Credential.Service.of({ + ...credentials, + update: (id, updates) => Effect.sleep(saveDelay).pipe(Effect.andThen(credentials.update(id, updates))), + }) +}) + +// Each Location gets its own Mcp layer over the shared credential store, as in one server process. +const locations = Effect.fnUntraced(function* (input: { + readonly url: string + readonly config?: typeof ConfigMCP.Remote.Type + readonly count: number + readonly saveDelay?: number +}) { + const slow = yield* slowCredentials(input.saveDelay) + return yield* Effect.forEach( + Array.from({ length: input.count }, (_, index) => index), + (index) => + Effect.gen(function* () { + const directory = AbsolutePath.make(`${import.meta.dir}/location-${index}`) + const context = yield* Layer.build( + Mcp.layer().pipe( + Layer.provide(Layer.succeed(Credential.Service, slow)), + Layer.provide(Layer.succeed(Location.Service, Location.Service.of(location({ directory })))), + ), + ) + const service = Context.get(context, Mcp.Service) + yield* service.transform((editor) => editor.set("svc", input.config ?? rotatingConfig(input.url))) + return service + }), + { concurrency: "unbounded" }, + ) +}) + +const status = (service: Mcp.Interface, expected: string) => + Effect.gen(function* () { + const current = (yield* service.servers()).find((server) => server.name === "svc")?.status + if (current?.status !== expected) return yield* Effect.fail(current) + }).pipe(Effect.retry(Schedule.spaced("5 millis")), Effect.timeout("10 seconds")) + +describe("MCP OAuth refresh lock", () => { + for (const saveDelay of [0, 50]) + it.live(`refreshes an expired credential once for 10 connections (save +${saveDelay} ms)`, () => + Effect.gen(function* () { + // Later connections' 401s land right after the refresh response, while it is still being saved. + const server = yield* rotatingServer({ refresh: "rt0", hold: true }) + const saved = yield* saveCredential({ url: server.url, access: "expired", refresh: "rt0" }) + const credentials = yield* Credential.Service + const slow = yield* slowCredentials(saveDelay) + const config = rotatingConfig(server.url) + + // Connect directly rather than through Mcp.layer, whose per-URL queue would serialize these connects. + const exits = yield* Effect.forEach( + Array.from({ length: 10 }), + () => + McpOAuth.connectProvider({ config, integrationID: saved.integrationID }).pipe( + Effect.flatMap((provider) => McpClient.connect("svc", config, import.meta.dir, provider)), + Effect.provideService(Credential.Service, slow), + Effect.provide(hostEnvironmentLayer), + Effect.exit, + ), + { concurrency: "unbounded" }, + ) + + expect(exits.filter(Exit.isSuccess)).toHaveLength(10) + + expect(server.counts).toEqual({ refresh: 1, invalidGrant: 0 }) + const stored = (yield* credentials.get(saved.id))?.value + expect(stored?.type === "oauth" && stored.refresh).toBe(server.state.current) + }), + ) + + it.live("refreshes a token revoked mid-session once across Locations", () => + Effect.gen(function* () { + const server = yield* rotatingServer({ refresh: "rt0" }) + server.state.access.add("at0") + // The saved token has an hour left, so only the server's 401 says it needs a refresh. + const saved = yield* saveCredential({ + url: server.url, + access: "at0", + refresh: "rt0", + expires: Date.now() + 3_600_000, + }) + const credentials = yield* Credential.Service + const [first, second] = yield* locations({ url: server.url, count: 2, saveDelay: 50 }) + if (!first || !second) throw new Error("missing Location") + yield* status(first, "connected") + yield* status(second, "connected") + expect(server.counts.refresh).toBe(0) + + // The server revokes the access token; the second call's 401 lands while the first refresh is still saving. + server.state.access.clear() + const one = yield* first.callTool({ server: "svc", name: "echo" }).pipe(Effect.forkScoped) + yield* Effect.promise(() => server.state.refreshed.promise) + const two = yield* second.callTool({ server: "svc", name: "echo" }).pipe(Effect.forkScoped) + + expect(yield* Fiber.join(one)).toMatchObject({ content: [{ type: "text", text: "ok" }] }) + expect(yield* Fiber.join(two)).toMatchObject({ content: [{ type: "text", text: "ok" }] }) + expect(server.counts).toEqual({ refresh: 1, invalidGrant: 0 }) + const stored = (yield* credentials.get(saved.id))?.value + expect(stored?.type === "oauth" && stored.refresh).toBe(server.state.current) + }), + ) + + it.live("keeps a rejected credential and asks for sign-in", () => + Effect.gen(function* () { + const server = yield* rotatingServer({ refresh: "rt0" }) + server.state.access.add("at0") + const saved = yield* saveCredential({ url: server.url, access: "at0", refresh: "rt0" }) + const credentials = yield* Credential.Service + const [service] = yield* locations({ url: server.url, count: 1 }) + if (!service) throw new Error("missing Location") + yield* status(service, "connected") + + // The server revokes the whole grant while connected, then again on a fresh connect. + server.state.current = "revoked" + server.state.access.clear() + expect(Exit.isFailure(yield* service.callTool({ server: "svc", name: "echo" }).pipe(Effect.exit))).toBe(true) + yield* status(service, "needs_auth") + yield* service.connect("svc") + yield* status(service, "needs_auth") + + // One rejected refresh per attempt, and the saved credential is untouched. + expect(server.counts).toEqual({ refresh: 2, invalidGrant: 2 }) + expect(yield* credentials.get(saved.id)).toEqual(saved) + }), + ) + + it.live("drops a rejected client so the next sign-in registers a new one", () => + Effect.gen(function* () { + const server = yield* rotatingServer({ refresh: "rt0", deadClient: "dead" }) + // No configured client_id, so the saved client from an earlier registration is the one in use. + const config = new ConfigMCP.Remote({ type: "remote", url: server.url, codemode: false }) + const saved = yield* saveCredential({ url: server.url, access: "expired", refresh: "rt0", client: "dead" }) + const credentials = yield* Credential.Service + const [service] = yield* locations({ url: server.url, config, count: 1 }) + if (!service) throw new Error("missing Location") + yield* status(service, "needs_auth") + + // The sign-in stays, without the rejected client. + const kept = (yield* credentials.get(saved.id))?.value + expect(kept?.type === "oauth" && [kept.access, kept.refresh, kept.metadata?.client]).toEqual([ + "expired", + "rt0", + undefined, + ]) + + const authorization = yield* McpOAuth.authorize({ + name: "svc", + config, + integrationID: saved.integrationID, + methodID, + }) + const authorizationUrl = new URL(authorization.url) + expect(authorizationUrl.searchParams.get("client_id")).toBe("registered-1") + const redirect = new URL(authorizationUrl.searchParams.get("redirect_uri") ?? "") + redirect.searchParams.set("code", "accepted") + redirect.searchParams.set("state", authorizationUrl.searchParams.get("state") ?? "") + yield* Effect.promise(() => fetch(redirect)) + yield* credentials.create({ integrationID: saved.integrationID, value: yield* authorization.callback }) + + yield* status(service, "connected") + expect(server.state.registrations).toBe(1) + }), + ) +})