Skip to content

mcp: CIMD loopback redirect_uri uses an ephemeral port that client.json does not declare #62742

Open

Description

@MHBahrampour

Description

When a remote MCP server's authorization server advertises client_id_metadata_document_supported: true, OpenCode selects CIMD and identifies as https://opencode.ai/oauth/opencode/client.json, but it sends a random loopback port in redirect_uri:

client_id    = https://opencode.ai/oauth/opencode/client.json
redirect_uri = http://127.0.0.1:53218/callback      # new random port every attempt

The published metadata document only declares port-less loopback URIs:

"redirect_uris": [
  "http://127.0.0.1/callback",
  "http://localhost/callback",
  "http://127.0.0.1/mcp/oauth/callback"
]

So an authorization server that compares redirect_uri exactly (port included) accepts the authorize request, issues a code, then rejects the token exchange. The login can never complete, and the error surfaces after the user has already approved access.

Reproduced with the Neon MCP server (https://mcp.neon.tech/mcp) on v2.2.6 (latest at time of writing):

Authorization failed
OpenCode couldn't finish connecting to Neon.
Invalid redirect URI

This is the same class of failure as #50510 (Exa), which was closed as completed on 2026-10-23, and #54421 (Turso). Either the fix is incomplete or it regressed.

Note the config schema shipped in this same binary documents defaults the runtime does not use:

  • schema: oauth.redirectUri default http://127.0.0.1:19876/mcp/oauth/callback, oauth.callbackPort default 19876
  • runtime: random port, path /callback

Neither the schema default nor the random port appears in client.json, so no default configuration can work with a strict AS.

Plugins

None.

OpenCode version

v2.2.6 (latest release, 2026-11-06)

Steps to reproduce

  1. Add the server with no OAuth options:
{
  "mcp": {
    "servers": {
      "Neon": { "type": "remote", "url": "https://mcp.neon.tech/mcp" }
    }
  }
}
  1. Run opencode mcp auth Neon (or start the login from the app).
  2. Sign in and approve on the Neon consent page.
  3. The callback page reports Authorization failed — Invalid redirect URI.

Minimal, self-contained repro with no stored credentials and no OAuth config:

XDG_DATA_HOME=/tmp/oc/data XDG_CONFIG_HOME=/tmp/oc/cfg \
OPENCODE_CONFIG_CONTENT='{"mcp":{"servers":{"Neon":{"type":"remote","url":"https://mcp.neon.tech/mcp"}}}}' \
opencode mcp auth Neon --standalone

Printed authorize URL (run it twice — the port changes each time):

https://mcp.neon.tech/api/authorize?response_type=code
  &client_id=https%3A%2F%2Fopencode.ai%2Foauth%2Fopencode%2Fclient.json
  &redirect_uri=http%3A%2F%2F127.0.0.1%3A53218%2Fcallback
  &resource=https%3A%2F%2Fmcp.neon.tech%2Fmcp&...

Setting oauth.callback_port: 19876 (documented as a shorthand for changing only the port) changes nothing: registration is still cimd, and the URI becomes http://127.0.0.1:19876/callback, which is also absent from client.json.

Expected behavior

Login completes. If the loopback callback cannot be represented in the CIMD document, OpenCode should fall back to dynamic client registration (which registers the exact URI, and which Neon supports) instead of sending a redirect URI the client metadata does not contain.

Actual behavior

Authorization succeeds and the token exchange fails:

mcp oauth request started    operation=exchange
mcp oauth request rejected   status=400 error=invalid_request message="Invalid redirect URI" operation=exchange
mcp oauth authorization failed

Full sequence from the log:

mcp oauth client registration selected  server=Neon registration=cimd
mcp oauth awaiting authorization        server=Neon
mcp oauth request started               server=Neon operation=exchange
mcp oauth request rejected              status=400 error=invalid_request message="Invalid redirect URI"
mcp oauth authorization failed          errors=[{"type":"OAuthError","message":"Invalid redirect URI","code":"invalid_request"}]

https://mcp.neon.tech/.well-known/oauth-authorization-server confirms the server is CIMD-capable and supports DCR:

{
  "issuer": "https://mcp.neon.tech",
  "registration_endpoint": "https://mcp.neon.tech/api/register",
  "client_id_metadata_document_supported": true,
  "token_endpoint_auth_methods_supported": ["client_secret_post", "client_secret_basic", "none", "private_key_jwt"],
  "code_challenge_methods_supported": ["S256"]
}

Workaround

Set an explicit redirect_uri, which disables CIMD and causes OpenCode to use DCR with the exact URI. Login then succeeds on the same build and server:

{
  "mcp": {
    "servers": {
      "Neon": {
        "type": "remote",
        "url": "https://mcp.neon.tech/mcp",
        "oauth": { "redirect_uri": "http://127.0.0.1:19876/callback" }
      }
    }
  }
}

Result: registration=dcr, a DCR client registered as redirect_uris: ["http://127.0.0.1:19876/callback"], and a stored credential whose metadata.client.redirect_uris matches.

Additional context

  • The failure is not a stale-credential problem. The credential store held no Neon entry before the workaround (opencode.db → credential: the only Neon row was created after the fix), so the failing attempt really did use CIMD. This distinguishes it from mcp: re-auth after token expiry reuses the stored DCR client with a new loopback port, so the AS rejects it with Callback URL mismatch #62331, where a stored DCR client is reused with a new port.
  • The mcp oauth client registration selected ... registration=cimd log line is misleading in the opposite direction too: a stored client short-circuits CIMD, but the label is computed from config and still says cimd.
  • Suggested directions: (a) publish port-bearing loopback URIs in client.json and use exactly those (fixed port and path); (b) only choose CIMD when the callback URI matches a declared redirect_uris entry (port-agnostic), otherwise use DCR; (c) bind the port from the metadata document rather than an ephemeral one.
  • Strictly, RFC 8252 §7.3 says an AS should allow any port for a registered loopback URI, so Neon is arguably not complying. But the OpenCode-side inconsistency is self-contained: the document it publishes and the URI it sends disagree, and it already has a working DCR fallback.

Environment:

  • opencode version: 2.2.6
  • OS: Linux 6.18.54.1-microsoft-standard-WSL2 (x86_64)
  • Terminal: Windows Terminal on WSL2, TERM=xterm-256color
  • Shell: fish 4
  • Install/channel: release binary (~/.opencode/bin/opencode), background service via opencode-serve.service
  • Plugins: none

Activity

  1. opencodejr commented on Nov 8, 2026

    @opencodejr
    Contributor

    Thanks for the very thorough write-up and the clean reproduction.

    We've looked at the code, and this is working as currently designed rather than a regression of #50510. The published client.json leaves the port off its loopback redirect URIs on purpose, because OpenCode binds a new loopback port for every login. It relies on RFC 8252 §7.3, which says authorization servers should ignore the port when matching loopback redirects. Neon accepts the authorize request (so it ignores the port there) but then compares the port strictly at the token exchange, which is why the login fails only after you approve. #50510 was a different mismatch: the /mcp/oauth/callback path was missing from client.json, and that request failed at authorize.

    So I'm treating this as a feature request: make OpenCode work with authorization servers that don't honour port-agnostic loopback matching. Your suggestions are all options here: publish port-bearing URIs and pin the port, use CIMD only when the callback matches a published entry, or fall back to DCR when the server also offers a registration endpoint (or when callback_port is set). Maintainers will consider it.

    Until then, your workaround is the right one. Setting oauth.redirect_uri turns CIMD off and registers the exact URI through DCR:

    "oauth": { "redirect_uri": "http://127.0.0.1:19876/callback" }

    The 19876 / /mcp/oauth/callback defaults you saw come from the legacy v1 config schema, not the v2 one, and we've flagged that for maintainers too.

    Related issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions