Skip to content

feat: add NeedsReauth MCP connection state for dead OAuth2 credentials - #5712

Merged
Pratham-Mishra04 merged 1 commit into
devfrom
07-29-feat_add_mcpconnectionstateneedsreauth_for_shared_mcp_clients
Aug 8, 2026
Merged

Pratham-Mishra04 merged 1 commit into
devfrom
07-29-feat_add_mcpconnectionstateneedsreauth_for_shared_mcp_clients

Conversation

@Pratham-Mishra04

Copy link
Copy Markdown
Collaborator

Summary

When a shared-connection MCP client's OAuth2 credential permanently dies (refresh token rejected or expired), the client was being left in the generic Disconnected state. This is misleading because no amount of automatic reconnection will fix it — only a human reauthorizing the client will. This PR introduces a new NeedsReauth connection state that is set when connectToMCPClient detects an ErrOAuth2TokenExpired-wrapped failure, and ensures the health monitor does not silently overwrite it with Connected or Disconnected during subsequent ping cycles.

Changes

  • Added MCPConnectionStateNeedsReauth to the MCPConnectionState enum in schemas/mcp.go, with documentation distinguishing it from PendingVerification and noting it only applies to shared-connection auth types.
  • In connectToMCPClient, after a gate error is returned, the error string is checked against ErrOAuth2TokenExpired using the new isOAuth2TokenExpiredErrorText helper. If matched, the client state is flipped to NeedsReauth under the manager lock, mirroring how the success path sets Connected.
  • In EnableClient, the post-connect failure state update was changed from an if/else to a switch so that NeedsReauth (already written by connectToMCPClient under its own lock) is preserved rather than overwritten with Disconnected.
  • In updateClientState on the health monitor, the existing Disabled guard was extended to also bail out early for NeedsReauth, preventing health-check ticks from clobbering the more specific signal.
  • Added isOAuth2TokenExpiredErrorText in utils.go, using the same substring-matching technique as isTransientError because runConnectWithPluginPipeline flattens the underlying Go error to a string on the BifrostError it returns, making errors.Is/errors.As unusable at the call site.
  • Added reauth_state_test.go covering: the isOAuth2TokenExpiredErrorText helper, the connectToMCPClient path that sets NeedsReauth on OAuth expiry, the control case that generic failures stay Disconnected, the health monitor guard that preserves NeedsReauth against both failed and successful ping ticks, and a regression guard confirming the pre-existing Disabled preservation behavior is unchanged.

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (React)
  • Docs

How to test

go version
go test ./core/mcp/...

The new reauth_state_test.go file covers the primary scenarios:

  1. A client whose OAuth credential store returns an ErrOAuth2TokenExpired-wrapped error on ConnectionHeaders should land in MCPConnectionStateNeedsReauth after connectToMCPClient returns.
  2. A client whose credential store returns a plain connectivity error should remain in MCPConnectionStateDisconnected.
  3. A client already in NeedsReauth should not be moved to Connected or Disconnected by health monitor ticks.
  4. A client in Disabled should still be unaffected by health monitor ticks (regression guard).

Breaking changes

  • No

The new needs_reauth state string is additive. Existing clients in Disconnected, Disabled, or other states are unaffected.

Security considerations

This change improves visibility into dead OAuth2 credentials by surfacing them as a distinct state rather than silently retrying forever. No secrets or PII are exposed; the state transition is based solely on error text matching against a known sentinel error string.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I added/updated tests where appropriate
  • I updated documentation where needed
  • I verified builds succeed (Go and UI)
  • I verified the CI pipeline passes locally if applicable

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Pratham-Mishra04 commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

This was referenced Jul 30, 2026
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added a distinct connection status for shared integrations that require OAuth reauthorization.
    • Expired OAuth credentials are now identified and surfaced as requiring reauthorization.
  • Bug Fixes

    • Prevented health checks and reconnection attempts from overwriting “Needs reauthorization” or “Disabled” statuses.
    • Preserved the reauthorization status when reconnection fails.
    • Kept unrelated connection failures marked as disconnected.
    • Ignored stale connection events for disabled or reauthorization-required integrations.

Walkthrough

Shared MCP connections now expose needs_reauth when OAuth credentials permanently expire. Connection failures and health checks preserve this state. Generic failures remain Disconnected, and disabled clients remain Disabled.

Changes

MCP reauthentication lifecycle

Layer / File(s) Summary
Reauthentication state and expiration detection
core/schemas/mcp.go, core/mcp/utils.go, core/mcp/reauth_state_test.go
Adds the needs_reauth state and detects OAuth token-expiration errors in flattened connection errors.
Connection failure state transitions
core/mcp/clientmanager.go, core/mcp/reauth_state_test.go
Marks expired OAuth connections as NeedsReauth, preserves disabled clients, ignores stale SSE callbacks, and keeps generic failures disconnected.
Health-monitor state preservation
core/mcp/healthmonitor.go, core/mcp/reauth_state_test.go
Prevents health checks and reconnect attempts from overwriting NeedsReauth or Disabled. Tests cover both states.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant OAuthCredentialStore
  participant connectToMCPClient
  participant MCPClientManager
  OAuthCredentialStore->>connectToMCPClient: return expired OAuth credential error
  connectToMCPClient->>MCPClientManager: report connection failure
  MCPClientManager->>MCPClientManager: set NeedsReauth
Loading

Possibly related PRs

Suggested reviewers: akshaydeo, danpiths, roroghost17

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the main change: adding the NeedsReauth MCP state for expired OAuth2 credentials.
Description check ✅ Passed The description explains the problem, implementation, tests, affected area, security impact, and breaking-change status; optional checklist items remain unchecked.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 07-29-feat_add_mcpconnectionstateneedsreauth_for_shared_mcp_clients

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 07-29-feat_generalize_tokenrefreshworker_s_auth-mode_scope branch from b00a267 to e214e46 Compare August 8, 2026 08:43
@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 07-29-feat_add_mcpconnectionstateneedsreauth_for_shared_mcp_clients branch from fe261ef to bd1ded9 Compare August 8, 2026 08:43

Pratham-Mishra04 commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

Merge activity

  • Aug 8, 8:47 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Aug 8, 9:12 AM UTC: Graphite rebased this pull request as part of a merge.
  • Aug 8, 9:13 AM UTC: @Pratham-Mishra04 merged this pull request with Graphite.

@Pratham-Mishra04
Pratham-Mishra04 changed the base branch from 07-29-feat_generalize_tokenrefreshworker_s_auth-mode_scope to graphite-base/5712 August 8, 2026 09:08
@Pratham-Mishra04
Pratham-Mishra04 changed the base branch from graphite-base/5712 to dev August 8, 2026 09:10
@Pratham-Mishra04
Pratham-Mishra04 dismissed coderabbitai[bot]’s stale review August 8, 2026 09:10

The base branch was changed.

@Pratham-Mishra04
Pratham-Mishra04 requested a review from a team as a code owner August 8, 2026 09:10
@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 07-29-feat_add_mcpconnectionstateneedsreauth_for_shared_mcp_clients branch from bd1ded9 to 3953567 Compare August 8, 2026 09:11
@Pratham-Mishra04
Pratham-Mishra04 merged commit b691a45 into dev Aug 8, 2026
14 of 15 checks passed
@Pratham-Mishra04
Pratham-Mishra04 deleted the 07-29-feat_add_mcpconnectionstateneedsreauth_for_shared_mcp_clients branch August 8, 2026 09:13
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.

2 participants