Skip to content

fix(core): allow valid MCP OAuth callbacks after replay errors - #65818

Open
sitole wants to merge 1 commit into
anomalyco:devfrom
sitole:mcp-oauth-callback
Open

sitole wants to merge 1 commit into
anomalyco:devfrom
sitole:mcp-oauth-callback

Conversation

@sitole

@sitole sitole commented Nov 23, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #64846

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Updates packages/core/src/mcp/oauth.ts to check state before error. Wrong-state callbacks show an error without ending sign-in. Errors with matching or missing state wait DENIAL_GRACE (10 seconds) before failing with the server's error. A valid code received during that time wins.

Matching-state access_denied still shows the "Authorization cancelled" page from #67456. Only the OpenCode result is delayed.

This handles replayed Ory consent errors. Unlike ory/cli#466's /start + cookie approach, it preserves the authorization URL and pasting it into a browser on another machine.

Rebased on current dev and squashed into one commit. Adapted to input.target.redirect and synchronous Deferred.doneUnsafe (#66495).

How did you verify your code works?

  • Updated the signIns table in packages/core/test/mcp/oauth.test.ts. A new row checks a replayed error, another sign-in's code, then the valid code: HTTP 400, 400, 200. Only the valid code is exchanged. This row fails on dev.
  • Updated fix(core): show a neutral page when OAuth sign-in is cancelled #67456's denial row with TestClock. Matching-state and missing-state denials fail after the grace period. Wrong-state denials do not end sign-in. The page checks remain.
  • test/mcp/oauth.test.ts and test/oauth-page.test.ts: 40 pass, 0 fail. The OAuth suite passed 5 consecutive runs.
  • bun run check passes lint and typecheck.
  • Ran the issue's repro.sh with OC_SERVE=1 against a source build. The replayed error gets HTTP 400; the valid code gets HTTP 200; auth ends with "Connected to ory". On dev, auth ends with "The consent verifier has already been used." / "Failed", and the code callback gets no response.
  • Not yet tested with real Safari and Ory Network.

Screenshots / recordings

N/A (no UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks sound to me. Your new error-then-code test fails on the base with the consent-verifier error from #64846 and passes with this PR. The whole mcp-oauth.test.ts suite passes (43/43) and the type check is clean. I didn't run the CLI script from the issue end to end, and nothing was tested with real Safari and Ory Network.

I checked how the race behaves. A code that arrives during the 10-second grace period wins, and the grace timer is cancelled. Repeated error callbacks are harmless. An error with no state already ended sign-in immediately on the base, so treating it as this sign-in's is not a regression. Pasted redirect URLs behave as before.

One trade-off: a real denial now reaches the terminal 10 seconds late, though the browser page shows the error straight away. That seems acceptable for this case.

The public API is unchanged.

No blocking concerns.

@sitole

sitole commented Nov 24, 2026

Copy link
Copy Markdown
Author

@rekram1-node, could you review #65818 when you have time? You wrote #58342 (“keep MCP OAuth login waiting after stray callback requests”), and this PR extends that callback handling in packages/core/src/mcp/oauth.ts: an error callback with the matching state waits 10 seconds for a valid code before failing with the error, instead of ending sign-in immediately. CI checks (typecheck and tests) have not run yet on this fork PR and need approval to run.

Ignore callbacks with a wrong state and wait 10 seconds after a
matching-state error for a valid code. This prevents replayed requests
from ending sign-in before the browser returns. Keep pasted URLs working.

Let errors with missing or matching state end login after DENIAL_GRACE.
Ignore wrong non-null state and test denial with and without state.
@sitole
sitole force-pushed the mcp-oauth-callback branch from 2e0c283 to 50a4d9a Compare December 1, 2026 13:46

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: replayed OAuth error rejects a valid sign-in

1 participant