You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mcp: replayed OAuth error rejects a valid sign-in #64846
Signing in to an MCP server whose authorization server is Ory Network intermittently fails with Safari on macOS: "The resource owner or authorization server denied the request. The consent verifier has already been used."
In one attempt, the browser reached http://127.0.0.1:19876/callback?code=...&scope=...&state=..., but OpenCode had already logged these lines in the same second:
mcp oauth callback rejected reason=authorization_error
mcp oauth authorization failed ... The consent verifier has already been used.
A macOS program can replay Ory's single-use consent URL without browser cookies, producing an error callback with the same state; Ory documents the link-preview case in ory/cli#466.
The callback handler in packages/core/src/mcp/oauth.ts ends sign-in on that error before checking state, so it rejects the browser's later valid code.
The valid code should complete sign-in; Chrome or Firefox is a workaround.
Plugins
One local plugin, opencode-tasks (unrelated)
OpenCode version
2.2.1 (latest channel)
Steps to reproduce
Configure an MCP server whose authorization server is Ory Network, without remembered consent.
Run /mcps, select the server, sign in with Safari on macOS, and click Allow.
Intermittently, sign-in fails with the consent-verifier error even though the browser reached the callback with a code.
Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.
Thanks for the detailed report. I reproduced this on 2.2.1, and it still happens on the latest dev (d33b578). I didn't use Ory or Safari: a local mock stood in for the MCP server and its authorization server, and curl played the browser and the replaying program.
What I did:
Ran opencode mcp auth <server> against the mock.
While it waited, sent an error callback with the same state (error_description=The consent verifier has already been used.) to the loopback listener.
Then sent the browser's real callback with a valid code and the same state.
Result: the error callback ended the sign-in straight away (■ The consent verifier has already been used. / Failed), and the valid code callback got no response because the listener was already closed. Without the error callback, the same code callback signs in fine (Connected to ory).
An error callback with a wrongstate also ends the sign-in. So, as you said, the error branch runs before the state check in packages/core/src/mcp/oauth.ts.
One thing to note: in your case the replayed error carries the right state. Checking state first would block forged errors but not that one. Fully fixing your case probably also means keeping the wait open for a valid code after an error callback. Until then, Chrome or Firefox (as you found) avoids it.
The script below reproduces it: it starts the mock itself and needs bun, curl and python3.
Reproduction script (repro.sh)
#!/usr/bin/env bash# Reproduces #64846: an OAuth error callback carrying the right state, arriving before the# browser's real code callback, ends MCP sign-in and the valid code is rejected.# Usage: ./repro.sh (released CLI on PATH; needs bun, curl, python3)# SKIP_ERROR=1 ./repro.sh control: no error callback, sign-in succeeds# ERROR_STATE=wrong ./repro.sh error callback with a wrong state also ends sign-in# OC_SERVE=1 OPENCODE=<cmd> ./repro.sh run against a source checkout (starts `serve` itself)set -u
OPENCODE=${OPENCODE:-opencode}
WORK=$(mktemp -d); mkdir -p /tmp/opencode
# Mock OAuth-protected MCP server + authorization server on 127.0.0.1:18080 (stands in for Ory).
cat >"$WORK/mock-as.ts"<<'MOCK'// Mock OAuth-protected MCP server + authorization server (stands in for Ory Network).const PORT = 18080const base = `http://127.0.0.1:${PORT}`Bun.serve({ port: PORT, async fetch(req) { const url = new URL(req.url) console.log("[mock]", req.method, url.pathname + url.search) const json = (b: unknown, s = 200) => Response.json(b, { status: s }) if (url.pathname.startsWith("/.well-known/oauth-protected-resource")) return json({ resource: `${base}/mcp`, authorization_servers: [base] }) if (url.pathname.startsWith("/.well-known/oauth-authorization-server") || url.pathname.startsWith("/.well-known/openid-configuration")) return json({ issuer: base, authorization_endpoint: `${base}/authorize`, token_endpoint: `${base}/token`, registration_endpoint: `${base}/register`, response_types_supported: ["code"], grant_types_supported: ["authorization_code", "refresh_token"], code_challenge_methods_supported: ["S256"], token_endpoint_auth_methods_supported: ["none"], }) if (url.pathname === "/register") { const body = await req.json() return json({ ...body, client_id: "mock-client" }, 201) } if (url.pathname === "/authorize") { const redirect = new URL(url.searchParams.get("redirect_uri")!) redirect.searchParams.set("code", "valid-code") redirect.searchParams.set("scope", "openid") redirect.searchParams.set("state", url.searchParams.get("state")!) return new Response(null, { status: 302, headers: { location: redirect.toString() } }) } if (url.pathname === "/token") return json({ access_token: "tok", token_type: "Bearer", expires_in: 3600, refresh_token: "r" }) if (url.pathname === "/mcp") { const auth = req.headers.get("authorization") if (auth !== "Bearer tok") return new Response("unauthorized", { status: 401, headers: { "WWW-Authenticate": `Bearer resource_metadata="${base}/.well-known/oauth-protected-resource/mcp"` }, }) return new Response("method not allowed", { status: 405 }) } return new Response("nf", { status: 404 }) },})console.log("mock listening", base)MOCK
curl -s -o /dev/null http://127.0.0.1:18080/ || { bun "$WORK/mock-as.ts">"$WORK/mock.log"2>&1& MOCK_PID=$!; sleep 1; }
mkdir -p "$WORK/proj";cd"$WORK/proj"echo'{ "mcp": { "servers": { "ory": { "type": "remote", "url": "http://127.0.0.1:18080/mcp" } } } }'> opencode.json
export XDG_DATA_HOME=$(mktemp -d) XDG_CONFIG_HOME=$(mktemp -d) XDG_STATE_HOME=$(mktemp -d) XDG_CACHE_HOME=$(mktemp -d)
LOG=$(mktemp)# From a source checkout the CLI cannot spawn its background service, so start one ourselves.if [ -n"${OC_SERVE:-}" ];then$OPENCODE serve --port 4999 >/tmp/opencode/serve.log 2>&1&
SERVE_PID=$!foriin$(seq 1 60);do curl -s -o /dev/null http://127.0.0.1:4999/ &&break; sleep 1;doneexport OPENCODE_PASSWORD=$(grep -o "server password .*" /tmp/opencode/serve.log | head -1 | cut -d"" -f3)
OC_FLAGS="--server http://127.0.0.1:4999"fi$OPENCODE mcp auth ory ${OC_FLAGS:-}</dev/null >"$LOG"2>&1&
PID=$!foriin$(seq 1 60);do
AUTH=$(grep -o 'http://127.0.0.1:18080/authorize[^ ]*'"$LOG"| head -1 | sed 's/\x1b\[[0-9;]*m//g')
[ -n"$AUTH" ] &&break; sleep 0.5
doneecho"authorize URL: $AUTH"# What the browser would be redirected to after clicking Allow (code + same state)
CALLBACK=$(curl -s -o /dev/null -w '%{redirect_url}'"$AUTH")echo"browser callback: $CALLBACK"
STATE=$(python3 -c 'import sys,urllib.parse as u;print(u.parse_qs(u.urlparse(sys.argv[1]).query)["state"][0])'"$CALLBACK")
CB_BASE=${CALLBACK%%\?*}# 1) the replayed, cookie-less consent request: the AS answers with an error redirect (same state)echo"--- replayed error callback:"
[ "${SKIP_ERROR:-}" ] || curl -s -o /dev/null -w "HTTP %{http_code}\n""$CB_BASE?error=access_denied&error_description=The+consent+verifier+has+already+been+used.&state=${ERROR_STATE:-$STATE}"
sleep 0.3
# 2) the browser's real, valid callbackecho"--- valid code callback:"
curl -s -o /dev/null -w 'HTTP %{http_code}\n'"$CALLBACK"
sleep 2
kill$PID2>/dev/null;wait$PID2>/dev/null
[ -n"${MOCK_PID:-}" ] &&kill$MOCK_PID2>/dev/null
[ -n"${SERVE_PID:-}" ] &&kill$SERVE_PID2>/dev/null
# stop the background service this run started
pkill -f -- "[s]erve --service"2>/dev/null; pkill -f -- "[i]ndex.ts serve"2>/dev/null; sleep 1
echo"--- opencode mcp auth output:"
sed 's/\x1b\[[0-9;]*m//g'"$LOG"
Root cause: The OAuth callback handler in packages/core/src/mcp/oauth.ts checked error before state and called Deferred.fail on any error, permanently settling the Deferred. When an authorization server replays a single-use consent URL (Ory Network via Safari link preview), the error callback arrives first with the correct state, fails the Deferred, and the browser's later valid code callback is rejected with 409.
Fix: Validate state first. An error callback with the correct state ends the browser's view with an error page but deliberately does not settle the Deferred, so a subsequent valid code callback can still complete the login. Wrong state still ends the login, and a second code callback is 409.
Changes:
packages/core/src/mcp/oauth.ts — extracted handleOauthCallback from authorize; state checked before error; error with correct state no longer settles the Deferred
packages/core/test/mcp/oauth-callback.test.ts — 6 new tests
All MCP OAuth tests pass. TypeScript checked clean.
#65818 fixes this and passes the repro script: the replayed error gets HTTP 400, the valid code gets HTTP 200, and auth ends with "Connected to ory". @DEV-S-SHAH, never settling on an error with the correct state would leave a real denial or server error waiting forever, because sign-in has no timeout. #65818 holds the error for 10 seconds, then fails with it if no valid code arrives. @opencodejr, one note for anyone running repro.sh: I removed the final pkill -f "serve --service" line because it can also stop your own running OpenCode service.
Activity
opencodejr commentedon Nov 19, 2026
Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.
Thanks for the detailed report. I reproduced this on 2.2.1, and it still happens on the latest
dev(d33b578). I didn't use Ory or Safari: a local mock stood in for the MCP server and its authorization server, andcurlplayed the browser and the replaying program.What I did:
opencode mcp auth <server>against the mock.state(error_description=The consent verifier has already been used.) to the loopback listener.codeand the samestate.Result: the error callback ended the sign-in straight away (
■ The consent verifier has already been used./Failed), and the valid code callback got no response because the listener was already closed. Without the error callback, the same code callback signs in fine (Connected to ory).An error callback with a wrong
statealso ends the sign-in. So, as you said, the error branch runs before thestatecheck inpackages/core/src/mcp/oauth.ts.One thing to note: in your case the replayed error carries the right
state. Checkingstatefirst would block forged errors but not that one. Fully fixing your case probably also means keeping the wait open for a valid code after an error callback. Until then, Chrome or Firefox (as you found) avoids it.The script below reproduces it: it starts the mock itself and needs
bun,curlandpython3.Reproduction script (repro.sh)
Run it:
bash repro.shDEV-S-SHAH commentedon Nov 19, 2026
I've implemented a fix for this.
Root cause: The OAuth callback handler in packages/core/src/mcp/oauth.ts checked error before state and called Deferred.fail on any error, permanently settling the Deferred. When an authorization server replays a single-use consent URL (Ory Network via Safari link preview), the error callback arrives first with the correct state, fails the Deferred, and the browser's later valid code callback is rejected with 409.
Fix: Validate state first. An error callback with the correct state ends the browser's view with an error page but deliberately does not settle the Deferred, so a subsequent valid code callback can still complete the login. Wrong state still ends the login, and a second code callback is 409.
Changes:
All MCP OAuth tests pass. TypeScript checked clean.
Refs: #64846
sitole commentedon Nov 23, 2026
#65818 fixes this and passes the repro script: the replayed error gets HTTP 400, the valid code gets HTTP 200, and auth ends with "Connected to ory". @DEV-S-SHAH, never settling on an error with the correct state would leave a real denial or server error waiting forever, because sign-in has no timeout. #65818 holds the error for 10 seconds, then fails with it if no valid code arrives. @opencodejr, one note for anyone running
repro.sh: I removed the finalpkill -f "serve --service"line because it can also stop your own running OpenCode service.