fix(core): use the localhost redirect when an MCP OAuth server rewrites 127.0.0.1

Merged
TA
21d ago
+45-6

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, which localhost reaches.
  • A configured oauth.redirect_uri is unchanged. The CIMD document already lists both hosts.
  • The pasted-URL fallback still redeems with 127.0.0.1, so a pasted localhost URL 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 carried redirect_uri=http://localhost:PORT/callback, and with both hosts registered, a token exchange using 127.0.0.1 returned invalid_grant.
  • After: ran authorize against the real Octolens server with no oauth config. The callback arrived on localhost, and the token exchange returned access and refresh tokens.
  • Added a test to packages/core/test/mcp-oauth.test.ts that sends the callback with a localhost Host header. It checks that DCR registers both URIs and that the token request uses the localhost one. It fails when the fix is reverted.
  • bun test test/mcp-oauth.test.ts test/oauth and bun typecheck in packages/core; bun run check from 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).

Related: #62331, #62742