fix(core): use the localhost redirect when an MCP OAuth server rewrites 127.0.0.1
Merged
TA
21d ago+45+45+01234567890123456789-6-6-0123456789
Issue for this PR
Closes #63408
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
McpOAuth.authorize registers one redirect, http://127.0.0.1:PORT/callback, through DCR. Clerk-backed servers such as Octolens accept it at /oauth/authorize. Their consent page then rewrites it to http://localhost:PORT/callback, so Allow fails with redirect_uri ... does not match any of the OAuth 2.0 Client's pre-registered redirect urls. Clerk also binds the code to the localhost URI: redeeming it with 127.0.0.1 returns invalid_grant.
Now DCR registers both the 127.0.0.1 and localhost callbacks. When the browser comes back with Host: localhost:PORT, the token exchange sends the localhost redirect.
- The authorize request still sends
127.0.0.1, so servers that don't rewrite see the same requests as before. - The listener still binds only
127.0.0.1, whichlocalhostreaches. - A configured
oauth.redirect_uriis unchanged. The CIMD document already lists both hosts. - The pasted-URL fallback still redeems with
127.0.0.1, so a pastedlocalhostURL from a rewriting server isn't handled yet.
How did you verify your code works?
- Before: reproduced against Octolens's Clerk server by registering a client with only
127.0.0.1. The consent page carriedredirect_uri=http://localhost:PORT/callback, and with both hosts registered, a token exchange using127.0.0.1returnedinvalid_grant. - After: ran
authorizeagainst the real Octolens server with nooauthconfig. The callback arrived onlocalhost, and the token exchange returned access and refresh tokens. - Added a test to
packages/core/test/mcp-oauth.test.tsthat sends the callback with alocalhostHost header. It checks that DCR registers both URIs and that the token request uses thelocalhostone. It fails when the fix is reverted. bun test test/mcp-oauth.test.ts test/oauthandbun typecheckinpackages/core;bun run checkfrom the root.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Added by the opencode bot (AI-generated).