Skip to content

[Fix] MCP OAuth sign-in can fail, never finish, or stay in the connecting state - #2762

Open
zoomote[bot] wants to merge 3 commits into
mainfrom
fix/mcp-oauth-signin-lifecycle
Open

zoomote[bot] wants to merge 3 commits into
mainfrom
fix/mcp-oauth-signin-lifecycle

Conversation

@zoomote

@zoomote zoomote Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

​Created by Roomote. Follow up by mentioning @roomote, in the web UI, or in Discord.

Related GitHub Issue

Closes: #2761

Description

MCP OAuth sign-in for streamable HTTP servers could fail in four ways: the opened authorization URL carried redirect_uri port 0, a sign-in flow stayed pending after its watcher was removed, a second 401 after sign-in left the server stuck in connecting, and an always-rejecting server triggered an unlimited reconnect loop. This PR fixes each cause.

  • McpOAuthClientProvider.redirectToAuthorization() now starts the callback listener first and then overrides redirect_uri with the active listener port. The cached-client early return in registerClientIfNeeded() also starts the listener, so the opened URL always carries a real port on the first connect and on a later 401.
  • A new shared removal path (_endOAuthWatcher) unsubscribes a watcher and ends the flow it belongs to. deleteConnection(), flow replacement in _runOAuthFlow(), and dispose() all go through it. A flow's own cleanup no longer removes a newer flow's watcher entry.
  • A reconnect that throws in the token watcher or the toast fast path now ends the flow and records the error on the server status.
  • reauthPromises is keyed by the connection object instead of the server name, so a 401 on a new connection starts its own flow instead of being swallowed by an old one.
  • Automatic reconnects that reuse a saved token the server rejected are now bounded (three attempts) and delayed (exponential backoff of 1 s, 2 s, 4 s, capped at 10 s). At the limit, the flow ends with a visible error on the server status. A new token resets the count, and a successful connect clears it.
  • connectToServer() notifies the panel when the status changes to connected and notifies again when the tool, resource, and template lists arrive.
  • The stale comment in McpOAuthClientProvider.spec.ts that claimed port 0 is never sent to an OAuth server is corrected.

Reviewers may want to check the McpOAuthFlowEnd.tla spec against the shared removal path, and the retry bound in _initiateOAuthFlow() against their expectation for servers with short-lived tokens.

Test Procedure

  • Fault-injection tests in src/services/mcp/__tests__/McpHub.spec.ts recreate each row of the issue's reproduction table: delete during a flow, flow replacement, reconnect errors in the token watcher and the toast handler, one and two 401s after sign-in, an always-rejecting server (bounded to four connects with a visible error), and a slow tools list (panel updates at connected, then again after the lists arrive).
  • src/services/mcp/__tests__/McpOAuthClientProvider.spec.ts covers the redirect URL: fresh client, cached client, replacement listener after a later 401, and a failed token refresh; each opened URL carries the active listener port.
  • src/services/mcp/__tests__/mcp-oauth-flow.property.spec.ts runs seeded randomized histories against the real code for each TLA+ invariant sentence.
  • TLA+ specs live in docs/architecture/tla/: McpOAuthRedirectUrl, McpOAuthFlowEnd, and McpOAuthStatusHandoff. Each fixed model passes and each sabotage variant fails with its named invariant. Run with TLA_JAR=<jar> ./check.sh McpOAuthRedirectUrl McpOAuthFlowEnd McpOAuthStatusHandoff.
  • pnpm --dir src run check-types passes. ESLint with --prune-suppressions passes with no new suppressions. The MCP/OAuth suites (258 tests) pass. A full pnpm test run passes on rerun; one run showed two unrelated flaky failures under cold parallel load.

Recorded Stream (new provider only)

N/A — no new provider in this PR.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Listing Discussion (new provider or MCP server only): I opened a GitHub Discussion for this listing and linked it in the PR description.
  • Recorded Stream (new provider only): I added one recorded streaming response. It shows the finish chunk. If the handler reads cost or usage from chunks, it also shows the usage chunk.
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I reviewed my own code with care.
  • Writing Style: The PR title and description use Simplified Technical English (ASD-STE100): short sentences, plain words, active voice, one idea per sentence, no contractions.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Test Helpers (test changes only): Shared test helpers (src/test-utils/, @/utils/test-utils) are used only for mechanical duplication; test intent and scenario-specific mocks remain visible.
  • Visual Snapshot (UI changes only): If a user would notice this change at a glance (layout, theme tokens, brand elements, empty/error states), I added or updated a *.visual.tsx snapshot in webview-ui/. See webview-ui/AGENTS.md → "When a UI change needs a snapshot".
  • Documentation Impact: I checked if my changes need documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I read and agree to the Contributor Guidelines.

Visual Snapshots

No rendered webview state changes in this PR. The sign-in toast, progress bar, and server status labels keep their existing appearance; only their lifecycle timing and error text change. A committed *.visual.tsx snapshot therefore does not apply.

Videos (interaction / animation only)

N/A — no new interactive flow, animation, or rendered state.

Documentation Updates

Additional Notes

The issue's "Not verified" items (strict authorization servers that reject a changed port, the 5-minute listener timeout window, concurrent connectToServer calls, and repeated OAuth discovery failures) stay open as follow-ups.

Get in Touch

Discord: 0x7777777_

Related PRs

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Review status

This PR was opened by an automated account. A human maintainer must verify the change intent, provenance, and validation before merging.

Current step: Required CI passed. Waiting for automated review of the latest commit.

If automated review does not start, a maintainer must restart it.

Review-state labels are managed by this workflow; do not edit them manually. community-approved is managed the same way — do not add or remove it manually. It signals a fresh community code approval for the current head as an advisory priority only; maintainer review is still required.

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.35294% with 43 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/services/mcp/McpHub.ts 85.71% 16 Missing and 24 partials ⚠️
src/services/mcp/McpOAuthFlow.ts 94.11% 0 Missing and 2 partials ⚠️
src/core/tools/UseMcpToolTool.ts 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@edelauna
edelauna marked this pull request as ready for review November 28, 2026 15:56
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Changed-code mutation testing

Report for PR head 852856e24021.

Surviving / uncovered mutants Advisories
0 1
Details: mutants, guidance, and reproduction

Changed-code mutation testing

PackageChanged executable linesValidKilledTimeoutSurvivedNo coverageResult
extension49900000Advisory incomplete

Focused tests

  • extension (cwd src): The mutation run was incomplete; selected test file names were not recorded. See Advisory findings for the failure.

Reproduce locally

From a full checkout containing both commits:

pnpm install --frozen-lockfile
node scripts/stryker-diff.mjs ci --base dd30249147159dad2d410845527a14a8e55d1463 --head d6c12b5786e829e77e69164ff6b9eea806370034

Mutation reports

  • extension: reports/mutation/extension/mutation.html

The workflow uploads generated reports in the changed-code-mutation-report artifact. A direct artifact link appears below after upload.

Advisory findings

  • extension generated 643 mutants in preflight (limit 400). Split the PR or obtain a maintainer-reviewed narrow exclusion.

@github-actions github-actions Bot added coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Nov 28, 2026
@github-actions github-actions Bot added coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit and removed coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Nov 28, 2026

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

awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit coderabbit-review-active Required CI passed; CodeRabbit review is active

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP OAuth sign-in can fail, never finish, or stay in the connecting state

2 participants