claude-code - 💡(How to fix) Fix [BUG] MCP OAuth flow fails: listener port doesn't match redirectUri, causing ERR_CONNECTION_REFUSED and "invalid code verifier" [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#53066Fetched 2026-04-25 06:13:16
View on GitHub
Comments
0
Participants
1
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
labeled ×5

Error Message

Error Messages/Logs

Root Cause

  1. The browser is redirected to the registered redirect_uri (e.g. http://localhost:8205/callback) but nothing is listening there → ERR_CONNECTION_REFUSED
  2. If the callback is manually forwarded to the actual listener port, the token exchange fails with "invalid code verifier" because Claude Code sends the ephemeral port as redirect_uri in the token exchange request, which doesn't match what was sent in the authorization request

Fix Action

Fix / Workaround

Workaround

RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

Describe the bug

When using a remote MCP server with OAuth configured in .mcp.json, Claude Code sends a fixed redirect_uri in the authorization request but binds its callback listener on a random ephemeral port. This causes two failures:

  1. The browser is redirected to the registered redirect_uri (e.g. http://localhost:8205/callback) but nothing is listening there → ERR_CONNECTION_REFUSED
  2. If the callback is manually forwarded to the actual listener port, the token exchange fails with "invalid code verifier" because Claude Code sends the ephemeral port as redirect_uri in the token exchange request, which doesn't match what was sent in the authorization request

To reproduce

  1. Configure a remote MCP server with OAuth in .mcp.json: { "type": "http", "url": "https://api.salesforce.com/platform/mcp/v1/analytics/tableau-next", "oauth": { "clientId": "<client_id>", "redirectUri": "http://localhost:8205/callback" } }
  2. Register http://localhost:8205/callback as the callback URL in the OAuth provider (Salesforce Connected App)
  3. Trigger MCP auth in Claude Code
  4. Observe the authorization URL contains redirect_uri=http%3A%2F%2Flocalhost%3A8205%2Fcallback
  5. Complete login — browser is redirected to http://localhost:8205/callback but gets ERR_CONNECTION_REFUSED
  6. Running netstat confirms nothing is listening on 8205 during the flow

Expected behavior

Claude Code should bind its callback listener on the port specified in redirectUri, so the browser redirect lands on an active listener.

Actual behavior

Claude Code binds the listener on a random ephemeral port (observed: 40236, 40442, 45943 across attempts) while sending the configured redirectUri to the auth server. The two are never consistent.

Workaround

A standalone PKCE script that owns the full flow and writes the token directly to ~/.claude/.credentials.json. Available for both PowerShell (Windows) and bash (Mac/Linux).

Environment

  • Claude Code version: Version 2.1.120
  • OS: Windows 11 (confirmed), likely affects Mac/Linux equally as the OAuth logic is platform-agnostic
  • MCP OAuth provider: Salesforce (login.salesforce.com)

What Should Happen?

Expected behavior

Claude Code should bind its callback listener on the port specified in redirectUri, so the browser redirect lands on an active listener.

Error Messages/Logs

Steps to Reproduce

To reproduce

  1. Configure a remote MCP server with OAuth in .mcp.json: { "type": "http", "url": "https://api.salesforce.com/platform/mcp/v1/analytics/tableau-next", "oauth": { "clientId": "<client_id>", "redirectUri": "http://localhost:8205/callback" } }
  2. Register http://localhost:8205/callback as the callback URL in the OAuth provider (Salesforce Connected App)
  3. Trigger MCP auth in Claude Code
  4. Observe the authorization URL contains redirect_uri=http%3A%2F%2Flocalhost%3A8205%2Fcallback
  5. Complete login — browser is redirected to http://localhost:8205/callback but gets ERR_CONNECTION_REFUSED
  6. Running netstat confirms nothing is listening on 8205 during the flow

Claude Model

Sonnet (default)

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.1.120

Platform

Anthropic API

Operating System

Windows

Terminal/Shell

PowerShell

Additional Information

No response

extent analysis

TL;DR

The issue can be resolved by ensuring Claude Code binds its callback listener to the port specified in the redirectUri configuration.

Guidance

  • Verify that the redirectUri in .mcp.json matches the port where the callback listener is intended to be bound.
  • Check the OAuth configuration in .mcp.json to ensure it is correctly set up with the intended redirectUri.
  • Consider using the provided standalone PKCE script as a temporary workaround to own the full OAuth flow and write the token directly to ~/.claude/.credentials.json.
  • Review the Claude Code documentation to see if there are any specific settings or configurations that need to be adjusted to ensure the callback listener is bound to the correct port.

Example

No code example is provided as the issue does not require a specific code change, but rather a configuration adjustment.

Notes

The issue seems to be related to the OAuth configuration and the binding of the callback listener in Claude Code. The provided workaround using a standalone PKCE script may be a temporary solution until the issue is resolved.

Recommendation

Apply the workaround using the standalone PKCE script until the issue is resolved in a future version of Claude Code, as it allows for a consistent and reliable OAuth flow.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [BUG] MCP OAuth flow fails: listener port doesn't match redirectUri, causing ERR_CONNECTION_REFUSED and "invalid code verifier" [1 participants]