Repository navigation
fix(core): parse MCP server configs once before connecting or signing in - #66931
Conversation
There was a problem hiding this comment.
The change works. On the base I saw all three problems: a server with an invalid URL still registered as an OAuth integration, command: [] failed with a raw TypeError, and an invalid redirect_uri reported connected. With this PR each of those fails with the new message. MCP tests and tsc pass; the one failing test (terminates MCP descendants…) fails the same way on dev.
Public API unchanged. The config schema stays as it is, and the changed signatures (McpClient.connect's config → target, McpOAuth.*, McpConnection.Spec) are internal to packages/core.
One regression is worth fixing before merge: an empty redirect_uri or auth_server_metadata_url now fails the server (see the inline comment).
| const url = URL.parse(config.url) | ||
| if (!url) return invalid(server, `Invalid MCP server URL: ${config.url}`) | ||
| const oauth = config.oauth || undefined | ||
| const redirect = oauth?.redirect_uri === undefined ? undefined : URL.parse(oauth.redirect_uri) |
There was a problem hiding this comment.
This fails the server on an empty string, but dev treated "" as unset: oauth?.redirect_uri ? new URL(...) : undefined, and if (!url) return undefined in configuredDiscovery. An empty string is easy to get: {env:VAR} substitutes "" when the variable isn't set. So a config like "redirect_uri": "{env:MCP_REDIRECT}" that connects today would turn into failed "Invalid MCP OAuth redirect_uri: " after this change. I confirmed this with redirect_uri: "", which is connected on the base and failed here. Line 43 (auth_server_metadata_url) has the same problem. Treating a falsy value as unset (oauth?.redirect_uri ? URL.parse(...) : undefined) keeps the old behaviour.
There was a problem hiding this comment.
The empty redirect_uri / auth_server_metadata_url regression is fixed: both are treated as unset again, and the test now checks that such a server connects. MCP tests pass (the one failure, terminates MCP descendants…, also fails on dev) and tsc is clean. Public API unchanged. Nothing new to raise.
Issue for this PR
No issue. Part of an internal audit of the MCP service (config boundary item).
Type of change
What does this PR do?
A broken MCP server config now fails just that server with a clear status, and nothing past the point where a server is registered ever sees a raw URL or command string.
Each server's config is parsed once, when its entry is created, into a typed
McpTarget:If parsing fails, the entry gets a connection that only reports
failedwith the reason, it isn't registered as an OAuth integration, and it never reaches the pool,McpClient.connectorMcpOAuth.McpConnection.Spec,McpClient.connect,McpOAuth.authorizeandMcpOAuth.connectProvidernow take the parsed target instead of the raw config.mcp add broken { type: "local", command: [] } -status: failed "undefined is not an object (evaluating 'command.command.includes')" +status: failed "MCP server command is empty" mcp add broken { type: "remote", url: "not a url" } status: failed "Invalid MCP server URL: not a url" -integration mcp_… registered, sign-in throws in loggedFetch +no integration registered mcp add x { type: "remote", url: "https://…", oauth: { redirect_uri: "not a url" } } -status: connected, sign-in dies with an uncaught TypeError from new URL() +status: failed "Invalid MCP OAuth redirect_uri: not a url"The same applies to
oauth.auth_server_metadata_url. The public config schema is unchanged, so one bad server still can't stop the rest of the config from loading.The
?codemode=falserule now lives in one place:McpTarget.dialUrldecides whether to add it (used byclient.ts), andMcpTarget.withoutDialParamsstrips it from an echoed protected resource (used byoauth.ts). The pool key is unchanged: it already keys the inputs to that rule (url,codemode).Behavior changes:
oauth.redirect_urioroauth.auth_server_metadata_urlis nowfailedinstead of connecting anonymously and crashing only when someone signs in.urlis no longer registered as an OAuth integration (integrationIDis absent).McpClient.connect's optionconfigis renamed totarget. These modules are reachable as@opencode/core/mcp/*deep imports, but nothing outsidepackages/coreuses them; no SDK or plugin API changes.A follow-up stacked on this branch decodes stored OAuth credential metadata with one schema and fixes two small sign-in bugs.
How did you verify your code works?
marks an MCP server with an invalid config as failedinpackages/core/test/mcp.test.ts; ondevit fails at the integration check, the empty-command message, and theredirect_uristatus. It passes here.rejects an invalid redirect URLfrommcp-oauth.test.ts: it asserted the old uncaughtTypeError, which can no longer happen because the redirect is parsed before sign-in.packages/core: MCP tests,bun typecheck,bun run typecheck:tsc.packages/server:bun typecheckand MCP tests. Rootbun run check.Screenshots / recordings
Not a UI change.
Checklist