fix(mcp): port upstream accept OAuth callbacks rewritten to localhost (anomalyco/opencode#63403)
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.clientMetadataregisters bothhttp://127.0.0.1:<port>/…andhttp://localhost:<port>/…for the default callback. An explicitredirectUriin config is still registered alone and used unchanged.- The callback server records which loopback host the successful callback arrived on, from its
Hostheader, usingnoteCallbackHost. Only127.0.0.1andlocalhostare accepted.McpOAuthProvider.redirectUrlthen returns that host, so the SDK's token exchange sends the redirect URI the server actually used. waitForCallbackresets the recorded host withresetCallbackHost, so each new flow starts from127.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 tohttp://localhost:<port>switches the provider'sredirectUrltolocalhost.test/mcp/oauth-provider.test.ts: both loopback URIs are registered, an explicitredirectUriis registered alone, non-loopbackHostvalues are ignored, and a reset starts from127.0.0.1again.bun test test/mcp/: 284 pass, 0 fail.bun run typecheckinpackages/opencodepasses.bun run script/upstream/analyze.ts --markers --base origin/main --strictpasses.
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/callbackandhttp://localhost:<port>/mcp/oauth/callback, and the token exchange sends whichever host the callback actually arrived on. - An explicit
redirectUriin config is still registered alone and used unchanged. - The callback host comes from the request
Hostheader and only accepts127.0.0.1andlocalhost; each new flow resets to127.0.0.1. - Clients registered before this change keep the old behavior until they register again.