Skip to content

OAuth login fails after a successful browser approval: scan restart or quarantine expiry retires the pending flow #1722

Closed

Description

@james-crowley

What happened

Adding a remote OAuth upstream (Linear, https://mcp.linear.app/mcp, Streamable HTTP, dynamic client registration, oauth.redirect_uri pinned to a loopback port) and approving it in the browser failed twice with the consent step succeeding. The callback reached mcpproxy both times ("Authorization Successful" page, OAuth callback parameters delivered to the waiting flow), and then the code exchange failed:

ERROR core/connection_oauth.go ❌ Failed to exchange authorization code
  error: failed to send token request: Post "https://mcp.linear.app/token": ... instance retired (removed, replaced or quarantined)

The third attempt worked (token stored with a refresh token, 76 tools discovered, a call_tool_read of list_teams returned data), so the flow itself is fine. Both failures were the in-flight OAuth flow being torn down by something else.

Build: main at 14cb288 (v0.1.0 personal), Linux amd64, fresh data dir, require_mcp_auth: true, enable_socket: false.

Cause 1: the quarantine inspection exemption expired during the login

A new upstream starts quarantined. About 10 minutes after the login started the log shows Inspection exemption expired, triggering disconnect and then Skipping quarantined client every minute. The login was started with POST /api/v1/servers/linear/login, and I approved in the browser about 18 minutes later. The exchange then failed as above. Nothing told the person approving that the login had been invalidated.

Cause 2: the informational scan restarts a pending-auth server and retires the flow

After POST /api/v1/servers/linear/unquarantine and a fresh login, I approved within about a minute and it failed again. The log shows why:

WARN  scanner/service.go  Timed out waiting for server to connect for scan  {"server":"linear","timeout":"30s"}
WARN  scanner/service.go  Could not export live tool definitions for scanning  {"error":"server linear is not connected"}
INFO  server/scan_informational.go  Tool export returned 0, retrying after EnsureConnected
INFO  server/server.go  Replacing the server's client to recreate it with fresh secret resolution
INFO  runtime/lifecycle.go  Replaced upstream server instance {"generation":8,"old_instance":5,"new_instance":6}

A server in Pending Auth can never be "connected", so the scan waits 30 s, then replaces the client. Replacing the client retires the instance that owns the pending OAuth state (PKCE verifier and callback waiter), so the code that arrives afterwards cannot be exchanged. The scan had been started by the unquarantine, and the replacement landed after I had already requested a new login URL.

Expected

  • A scan (or any background restart) should skip a server whose state is Pending Auth or has an OAuth flow in progress, or it should keep the in-flight flow alive across the replacement.
  • If a login is invalidated, the callback page should say so instead of "Authorization Successful", and GET /api/v1/servers should show a reason.
  • The inspection exemption should not expire while an OAuth flow for that server is waiting.

Workaround

Unquarantine, wait until the scan has finished (it times out once), request the login URL again, and approve within a couple of minutes.

Side note

The stored token is readable as plaintext in config.db (already tracked in #1697).

Activity

  1. Dumbris commented on Oct 22, 2026

    @Dumbris
    Member

    Thanks for the precise diagnosis, both causes were exactly as you described. Fix in #1739:

    1. Scan no longer restarts a server that is waiting on your login. Every scan path (admission, the informational rescan after a tool change, manual) now asks whether the server has a manual OAuth flow in flight or is parked in Pending Auth, and if so scans without calling the reconnect that replaced the instance (and logs Server is waiting on an OAuth login; scanning without reconnecting it). No more 30 s wait followed by "Replacing the server's client".
    2. The inspection exemption is held while a login is in flight. The add's one-hour exemption was being overwritten by the scan's 15-minute grant; a grant now never shortens one in force, and an expired exemption is extended in 5-minute steps for as long as a login for that server is active, so the expiry-driven disconnect cannot land under the sign-in.
    3. The failure is visible when it does happen (an operator removes, restarts or quarantines the server mid-login): the waiting flow notices its instance was retired, and the callback page shows "Authorization Failed: this sign-in was invalidated before it could finish: the server's connection was retired … while the sign-in was in progress" instead of "Authorization Successful" plus a silent exchange error.

    GET /api/v1/servers does not carry a per-server reason for this case: the retired instance's failure is deliberately not recorded on the server status (a same-name replacement would inherit it), so the page and the log are the surfaces. Nothing in the change approves or unquarantines anything.

  2. added a commit that references this issue on Oct 22, 2026
    a983081
  3. james-crowley commented on Oct 23, 2026

    @james-crowley
    ContributorAuthor

    Confirmed fixed on main (a983081, #1739). I used the repo's fake OAuth server (tests/oauthserver), with the proxy on 127.0.0.1, and compared main against the parent commit (2146860):

    • Scan while a login is pending: I added the server (quarantined), started the login, ran a manual scan while it was pending, approved, then completed the callback. On main the login succeeds and the server connects with its tools. On the parent the page says "Authorization Successful", but the token exchange fails with "instance retired" and the server stays pending auth; the log shows the upstream instance being replaced 1→5.
    • Unquarantine, then login, then scan: same result. Main connects; the parent fails the exchange.
    • Operator restart mid-login: main now answers the callback with HTTP 400 and a clear "invalidated before it could finish" page. The parent showed "Authorization Successful" and then failed silently.
    • Exemption: on main a scan's 15-minute grant no longer shortens the 1-hour exemption from add. On the parent it did. I could only check the hold past expiry through the new unit tests.

    The new tests pass with -race on main. The scanner test fails on the parent (it reconnects the server awaiting login). Thanks for the quick fix.

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