fix(mcp): port upstream accept OAuth callbacks rewritten to localhost (anomalyco/opencode#63403)

DE
1h ago
+124-6

Issue for this PR

Closes #1878

Tracking: #1478. Upstream: https://github.com/anomalyco/opencode/pull/63403

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

User-visible failure: some authorization servers rewrite a loopback redirect from 127.0.0.1 to localhost. MCP OAuth registered and sent only http://127.0.0.1:<port>/mcp/oauth/callback. After the rewrite, the browser reached the callback server on localhost, but the token exchange still sent the 127.0.0.1 redirect URI. The server rejected it as a redirect_uri mismatch, so mcp auth failed after the user had already approved access.

Fix: this follows the upstream fix, adapted to the fork's v1 MCP code:

  • McpOAuthProvider.clientMetadata registers both http://127.0.0.1:<port>/… and http://localhost:<port>/… for the default callback. An explicit redirectUri in config is still registered alone and used unchanged.
  • The callback server records which loopback host the successful callback arrived on, from its Host header, using noteCallbackHost. Only 127.0.0.1 and localhost are accepted. McpOAuthProvider.redirectUrl then returns that host, so the SDK's token exchange sends the redirect URI the server actually used.
  • waitForCallback resets the recorded host with resetCallbackHost, so each new flow starts from 127.0.0.1.

Upstream passes the host through its callback result. The fork's waitForCallback resolves only the code and the provider is created elsewhere, so the host is kept per callback port in oauth-provider.ts. All changes are inside altimate_change … upstream_fix markers.

Existing clients registered through dynamic client registration before this change only have the 127.0.0.1 URI. They still behave as before until they register again.

How did you verify your code works?

  • test/mcp/oauth-callback.test.ts: a real callback sent to http://localhost:<port> switches the provider's redirectUrl to localhost.
  • test/mcp/oauth-provider.test.ts: both loopback URIs are registered, an explicit redirectUri is registered alone, non-loopback Host values are ignored, and a reset starts from 127.0.0.1 again.
  • bun test test/mcp/: 284 pass, 0 fail.
  • bun run typecheck in packages/opencode passes.
  • bun run script/upstream/analyze.ts --markers --base origin/main --strict passes.

Cost side effects

None.

Screenshots / recordings

N/A. There is no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR
  • I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"

Summary by cubic

Fixes mcp auth failing after the user approves access when an authorization server rewrites the loopback redirect from 127.0.0.1 to localhost.

  • The provider registers both http://127.0.0.1:<port>/mcp/oauth/callback and http://localhost:<port>/mcp/oauth/callback, and the token exchange sends whichever host the callback actually arrived on.
  • An explicit redirectUri in config is still registered alone and used unchanged.
  • The callback host comes from the request Host header and only accepts 127.0.0.1 and localhost; each new flow resets to 127.0.0.1.
  • Clients registered before this change keep the old behavior until they register again.

Devin Review