Repository navigation
Description
Description
After the first successful sign-in, re-authenticating a remote MCP server always fails at the authorization server with:
{"error":"unauthorized_client","error_description":"Callback URL mismatch. http://127.0.0.1:59270/callback is not in the list of allowed callbacks."}
The authorization server (Auth0, at https://auth.example.com/) supports Dynamic Client Registration and issues no refresh token (scopes_supported: [] in the protected-resource metadata, so offline_access is never requested). The access token lives 24 h.
What the stored credential shows (from opencode.db → credential, secrets omitted):
"metadata": {
"serverUrl": "https://mcp.example.com/mcp",
"issuer": "https://auth.example.com/",
"client": {
"client_name": "opencode",
"token_endpoint_auth_method": "none",
"redirect_uris": ["http://127.0.0.1:64394/callback"]
}
}"refresh": ""
So:
- First sign-in: OpenCode binds a random loopback port (64394), registers a client via DCR with
redirect_uris: ["http://127.0.0.1:64394/callback"], and stores that client. - After 24 h the token expires. There is no refresh token, so OpenCode starts a new browser flow.
- It binds a new random port (59270) but reuses the stored DCR client, whose only registered redirect URI is the old port. Redirect URIs are matched exactly, so the authorization server rejects the request.
Every OAuth-without-refresh-token server ends up broken after the first token expiry, and the only way out is opencode mcp logout <name> followed by opencode mcp auth <name>, which forces a new registration. In OpenChamber the server also shows no "Sign in again" action in this state.
Expected: a re-authentication either reuses the port the stored client was registered with, or detects that the redirect URI differs from the stored client's redirect_uris and re-registers (DCR) before sending the authorization request. Either keeps re-auth working without a manual logout.
Possibly related, but not the same: #54421 (CIMD client.json redirect_uris vs. loopback callback, closed), #57130 (engine-owned loopback listener per attempt), #58319 (repeated re-auth when the token response has no scope).
Plugins
None
OpenCode version
2.2.5
Steps to reproduce
- Add a remote MCP server whose authorization server supports DCR and does not issue refresh tokens, for example:
(
{ "mcp": { "servers": { "my-mcp": { "type": "remote", "url": "https://mcp.example.com/mcp", "disabled": false } } } }/.well-known/oauth-protected-resource→authorization_servers: ["https://auth.example.com/"]; the AS metadata includesregistration_endpoint.) - Sign in (
opencode mcp auth my-mcpor the first connect). It works. - Wait until the access token expires (24 h here), or set the stored
expiresto the past. - Reconnect. The browser opens and the authorization server answers
Callback URL mismatch. http://127.0.0.1:<new port>/callback is not in the list of allowed callbacks. opencode mcp logout my-mcp && opencode mcp auth my-mcpworks again, until the next expiry.
Screenshot and/or share link
No response
Operating System
macOS (Darwin)
Terminal
OpenChamber (OpenCode background service) and the opencode mcp CLI
Activity
opencodejr commentedon Nov 6, 2026
Thanks for the detailed report. I was able to reproduce this on 2.2.5, and it still happens on the latest
dev(3ec9b3b0a).To test it, the script below runs a local mock in place of the MCP server and its authorization server. The mock supports dynamic client registration, checks redirect URIs exactly (like Auth0) and never issues a refresh token. The script then runs
opencode mcp authtwice, usingcurlas the browser:http://127.0.0.1:40221/callbackand signs in fine.redirect_uri=http://127.0.0.1:43071/callback. The mock rejects it withCallback URL mismatch. http://127.0.0.1:43071/callback is not in the list of allowed callbacks., and the CLI keeps waiting for authorization.In the code, the sign-in reuses the stored client but picks a random callback port unless
oauth.callback_portoroauth.redirect_uriis configured. It never checks the new callback URL against the client's registeredredirect_uris.Until this is addressed, besides
opencode mcp logoutfollowed byopencode mcp auth, setting a fixedoauth.callback_porton the server should keep the callback URL the same between sign-ins. I haven't tested that workaround against your server.Related issues, none of which covers this problem:
client.json(CIMD) don't match the loopback callback.Reproduction script (repro.sh)
Run it:
bash repro.sh