Skip to content

Commit 18527bd

Browse files
committed
fix(mcp): tolerate optional OAuth discovery failures
1 parent 3a48c08 commit 18527bd

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,11 @@ const layer = Layer.effect(
901901
try: async () => {
902902
const { auth, discoverOAuthServerInfo } = await import("@modelcontextprotocol/sdk/client/auth.js")
903903
const fetchFn = createFetchWithInit(undefined, mcpConfig.headers ? { headers: mcpConfig.headers } : undefined)
904-
const discovery = await discoverOAuthServerInfo(url, { fetchFn })
904+
// Optional discovery must not block public or static-header MCP connections.
905+
const discovery = await discoverOAuthServerInfo(url, { fetchFn }).catch(() => undefined)
905906
// Public discovery does not complete an explicit sign-in to an OAuth-enabled server.
906907
if (
907-
discovery.authorizationServerMetadata &&
908+
discovery?.authorizationServerMetadata &&
908909
(await auth(authProvider, { serverUrl: url, scope: oauthConfig?.scope, fetchFn })) !== "AUTHORIZED"
909910
) {
910911
throw new UnauthorizedError()

‎packages/opencode/test/mcp/oauth-auto-connect.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const mcpTest = testEffect(
2323
interface OAuthMcpOptions {
2424
capabilities?: "tools" | "resources"
2525
oauthMetadata?: boolean
26+
metadataFailure?: "oauth" | "openid"
2627
lazyDiscovery?: boolean
2728
protectedCalls?: boolean
2829
backgroundChallenge?: boolean
@@ -42,6 +43,8 @@ function serveOAuthMcp(options: OAuthMcpOptions = {}) {
4243
})
4344
let listToolsCalls = 0
4445
let protectedCalls = 0
46+
let authorizeCalls = 0
47+
const metadataRequests: string[] = []
4548
let releaseGet: () => void = () => {}
4649
const getGate = new Promise<void>((resolve) => {
4750
releaseGet = resolve
@@ -68,6 +71,14 @@ function serveOAuthMcp(options: OAuthMcpOptions = {}) {
6871
const origin = url.origin
6972
const mcpUrl = `${origin}/mcp`
7073

74+
if (url.pathname === "/authorize") authorizeCalls++
75+
if (url.pathname.startsWith("/.well-known/")) metadataRequests.push(url.pathname)
76+
if (
77+
(options.metadataFailure === "oauth" && url.pathname === "/.well-known/oauth-authorization-server") ||
78+
(options.metadataFailure === "openid" && url.pathname === "/.well-known/openid-configuration")
79+
) {
80+
return new Response("Unavailable", { status: 503 })
81+
}
7182
if (options.oauthMetadata === false && url.pathname.startsWith("/.well-known/")) {
7283
return new Response("Not found", { status: 404 })
7384
}
@@ -181,6 +192,8 @@ function serveOAuthMcp(options: OAuthMcpOptions = {}) {
181192
},
182193
listToolsCalls: () => listToolsCalls,
183194
protectedCalls: () => protectedCalls,
195+
authorizeCalls: () => authorizeCalls,
196+
metadataRequests: () => metadataRequests,
184197
releaseGet: () => releaseGet(),
185198
close: async () => {
186199
await http.stop(true)
@@ -344,6 +357,29 @@ mcpTest.instance("authenticate() connects a public server without OAuth metadata
344357
}),
345358
)
346359

360+
for (const metadataFailure of ["oauth", "openid"] as const) {
361+
mcpTest.instance(`authenticate() connects without OAuth when ${metadataFailure} discovery returns 503`, () =>
362+
Effect.gen(function* () {
363+
yield* stopOAuthCallback
364+
const server = yield* serveOAuthMcp({ oauthMetadata: false, metadataFailure, lazyDiscovery: true })
365+
server.allowAnonymous()
366+
const mcp = yield* MCP.Service
367+
const name = `test-oauth-discovery-${metadataFailure}-503`
368+
const added = yield* mcp.add(name, {
369+
...remote(server.url),
370+
...(metadataFailure === "openid" ? { headers: { Authorization: "Bearer static-test-token" } } : {}),
371+
})
372+
expect((added.status as Record<string, { status: string }>)[name]?.status).toBe("connected")
373+
expect((yield* mcp.authenticate(name)).status).toBe("connected")
374+
expect((yield* mcp.status())[name]?.status).toBe("connected")
375+
expect(server.metadataRequests()).toContain(
376+
metadataFailure === "oauth" ? "/.well-known/oauth-authorization-server" : "/.well-known/openid-configuration",
377+
)
378+
expect(server.authorizeCalls()).toBe(0)
379+
}),
380+
)
381+
}
382+
347383
mcpTest.instance("authenticate() connects a public resource-only server without listing tools", () =>
348384
Effect.gen(function* () {
349385
yield* stopOAuthCallback

0 commit comments

Comments
 (0)