Repository navigation
Description
Description
OpenCode signs in to a remote MCP server correctly, then fails about an hour later, when the access token expires.
For an Entra ID app where the client and the resource are the same registration, Microsoft Entra ID rejects a refresh request that has no scope:
error=invalid_request
AADSTS90023: The request is not valid. The refresh token used contains only policy offers
and the scope parameter is not specified.
The authorization_code exchange succeeds (HTTP 200, hasRefreshToken=true, expiresIn=3599) and the server connects. Every refresh_token exchange returns HTTP 400, and the server ends in failed.
Cause: packages/core/src/mcp/oauth.ts, exchange(), calls refreshAuthorization(...) without scope:
const tokens = await refreshAuthorization(server.authorizationServerUrl, {
metadata: server.authorizationServerMetadata,
clientInformation: client,
refreshToken: input.current.refresh,
resource,
fetchFn,
})The granted scope is already stored (credential.metadata.scope), and the MCP client SDK already supports it: refreshAuthorization takes scope?: string, and AuthProvider.scopeForRefresh exists for this case. RFC 6749 §6 makes scope optional, but Entra requires it for these grants.
Proposed fix:
scope: input.current.metadata?.scope ?? oauth?.scope,Verified manually against the same refresh token: adding scope returns HTTP 200.
Secondary issue: invalid_request is not in the rejections set (InvalidGrant, InvalidClient, UnauthorizedClient), so the failure maps to TemporarilyUnavailable and the server stays failed indefinitely. /mcps never offers to sign in again, and the dead credential is kept.
Plugins
No response
OpenCode version
2.4.1
Steps to reproduce
- Configure a remote MCP server with a pre-registered Entra client where client_id == resource:
URL: https://partner.api.bingads.microsoft.com/ext/mcp/vnext
scope: https://ads.microsoft.com/msads.manage offline_access - Sign in through /mcps. The server connects and lists its tools.
- Wait ~60 minutes for the access token to expire (or force a refresh).
- Check ~/.local/share/opencode/log/opencode.log:
message="mcp oauth authorization completed" ... hasRefreshToken=true expiresIn=3599
message="mcp connected" server=microsoft-ads tools=14
message="mcp oauth request started" ... operation=refresh
message="mcp oauth request rejected" status=400 error=invalid_request
message="AADSTS90023: ... The refresh token used contains only policy offers
and the scope parameter is not specified ..."
message="mcp connect failed" status.status=failed
status.error="Could not refresh the OAuth token: invalid_request (temporarily_unavailable)"
Screenshot
No response
Operating System
Ubuntu 24.04.5 LTS (VM)
Terminal
SSH + VS Code Remote
Activity
Guembri01 commentedon Nov 24, 2026
Taking this one. The refresh exchange at
packages/core/src/mcp/oauth.ts:391drops the grantedscope, and the SDK already accepts it (refreshAuthorization(url, { …, scope, … })); the granted scope is stored at:257. One-line fix plus a test that asserts the token endpoint receives it — PR coming.opencodejr commentedon Nov 24, 2026
Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.
Thanks for the detailed report and analysis. I reproduced this on 2.4.1 with a mock server standing in for Entra ID. The mock rejects any refresh without
scopewithinvalid_request/ AADSTS90023, as you describe Entra doing.oauthconfig setsclient_idandscope, sign-in works, and the server connects.refresh_tokenrequest reached the token endpoint with onlygrant_type,refresh_token,resourceandclient_id. The configured scope was not sent.failed: Could not refresh the OAuth token: invalid_request (temporarily_unavailable)and stayed there. It did not ask to sign in again.The same happens on the latest
dev(d65b5d2) run from source. The script below reproduces it end to end, mock server included.Related issues:
scopein the token response also breaks MCP OAuth refresh. That is a different trigger in the same code path.Reproduction script (repro.sh)
Run it:
sh repro.shfix(mcp): send the granted scope when refreshing an MCP OAuth token
fix(mcp): send the granted scope when refreshing an MCP OAuth token
aguileramontenegro commentedon Nov 27, 2026
Thanks for the fix — I verified it landed in
devas described:exchange()inpackages/core/src/mcp/oauth.tsnow reads the granted scope from the credential (input.current.metadata?.scope, falling back to the configuredoauth.scope) and passes it torefreshAuthorization, which is exactly what Entra requires (AADSTS90023). The tests covering the refresh body are in place too.Two things worth flagging before this is fully closed for affected users:
No release contains the fix yet. The latest release is v2.4.2 (Nov 26), and the PR merged Nov 27, so it is only on
dev. Until a release ships it, anyone on a released build still needs the proxy/sign-in-again workaround.The secondary issue from the report is still open on
dev. Therejectionsset is unchanged — it still only containsInvalidGrant,InvalidClientandUnauthorizedClient. Any other server that answers a failed refresh withinvalid_request(orinvalid_scope) still maps toTemporarilyUnavailable, leaving the server permanentlyfailedwith no re-auth offered in/mcpsand the dead credential kept. For Entra the symptom disappears only because the refresh now succeeds; the misclassification itself is untouched. Since it affects refresh failures generally and not just this scope case, it seems worth tracking as its own issue rather than assuming this one covers it.For context, I ran the server through a local proxy (own grant, always sends
scopeon refresh) since Nov 24, so I will keep that until a release with the fix is out, then verify a session survives more than 60 minutes before removing it.