Skip to content

Conversation

dustinhealy
Copy link
Collaborator

@dustinhealy dustinhealy commented Jul 29, 2025

Summary

This pull request fixes issues with MCP OAuth flows concerning cancellation handling, polling cleanup, UI state management during server initialization, and most critically resolving a bug introduced in #8718 which broke initial OAuth flows.

Backend Changes:

  • api/server/routes/mcp.js:

    • OAuth Cancellation: Changed completeFlow to failFlow for proper cancellation state management, preventing stale client IDs and "Client ID mismatch" errors
    • Reinitialize Success Logic: Fixed success condition to properly handle OAuth-required servers (success: (userConnection && !oauthRequired) || (oauthRequired && oauthUrl))
  • api/server/services/MCP.js:

    • Cancellation vs Error Detection: Added logic to differentiate between user-cancelled flows and actual OAuth failures
    • UI Error Prevention: Returns hasFailedFlow: false for cancelled flows to prevent error triangle display in UI

Frontend Changes:

  • client/src/hooks/MCP/useMCPServerManager.ts:

    • Polling Cleanup: Added clearInterval(pollInterval) in error conditions and catch blocks to stop infinite polling
    • OAuth Cancellation Flow: Reordered cancellation to call backend first, then cleanup frontend state only on success
    • Server Interaction Guards: Added isInitializing checks to prevent server selection/toggling during initialization
    • Auto-OAuth Parameter: Added autoOpenOAuth parameter to initializeServer for better OAuth URL handling control
  • client/src/components/Chat/Input/MCPSelect.tsx:

    • Visual Feedback: Added opacity styling and disabled state for initializing servers
    • Interaction Prevention: Disabled button clicks during server initialization
  • client/src/components/Chat/Input/MCPSubMenu.tsx:

    • Consistent Disabled State: Applied same disabled/opacity styling as MCPSelect for initializing servers
  • client/src/components/MCP/ServerInitializationSection.tsx:

    • OAuth Parameter Fix: Updated initializeServer call to pass autoOpenOAuth: false for manual initialization

Change Type

  • Bug fix (non-breaking change which fixes an issue)

Testing

Manual verification of MCP OAuth flow scenarios:

  • OAuth flow initiation and completion
  • OAuth flow cancellation at various stages
  • OAuth flow reinitialization and completion after cancellation
  • Error handling during OAuth failures
  • Server selection/deselection during initialization attempts
  • Polling behavior after errors and cancellations
  • UI state consistency across different cancellation scenarios
  • Multiple server initialization attempts

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • My changes do not introduce new warnings
  • A pull request for updating the documentation has been submitted.

@dustinhealy dustinhealy marked this pull request as ready for review July 29, 2025 17:48
@danny-avila danny-avila changed the title 🪳fix: MCP Initialization Flows ⚒️ fix: MCP Initialization Flows Jul 29, 2025
@danny-avila danny-avila merged commit 6fd3b56 into dev Jul 29, 2025
7 checks passed
@danny-avila danny-avila deleted the fix/mcp-oauth-bug branch July 29, 2025 18:54
jhrozek pushed a commit to StacklokLabs/LibreChat that referenced this pull request Jul 31, 2025
* fix: add OAuth flow back in to success state

* feat: disable server clicks during initialization to prevent spam

* fix: correct new tab behavior for OAuth between one-click and normal initialization flows

* fix: stop polling on error during oauth (was infinite popping toasts because we didn't clear interval)

* fix: cleanupServerState should be called after successful cancelOauth, not before

* fix: change from completeFlow to failFlow to avoid stale client IDs on OAuth after cancellation

* fix: add logic to differentiate between cancelled and failed flows when checking status for indicators (so error triangle indicator doesn't show up on cancellaiton)
xycjscs pushed a commit to xycjscs/LibreChat that referenced this pull request Aug 9, 2025
* fix: add OAuth flow back in to success state

* feat: disable server clicks during initialization to prevent spam

* fix: correct new tab behavior for OAuth between one-click and normal initialization flows

* fix: stop polling on error during oauth (was infinite popping toasts because we didn't clear interval)

* fix: cleanupServerState should be called after successful cancelOauth, not before

* fix: change from completeFlow to failFlow to avoid stale client IDs on OAuth after cancellation

* fix: add logic to differentiate between cancelled and failed flows when checking status for indicators (so error triangle indicator doesn't show up on cancellaiton)
miguelwon pushed a commit to miguelwon/LibreChat that referenced this pull request Aug 10, 2025
* fix: add OAuth flow back in to success state

* feat: disable server clicks during initialization to prevent spam

* fix: correct new tab behavior for OAuth between one-click and normal initialization flows

* fix: stop polling on error during oauth (was infinite popping toasts because we didn't clear interval)

* fix: cleanupServerState should be called after successful cancelOauth, not before

* fix: change from completeFlow to failFlow to avoid stale client IDs on OAuth after cancellation

* fix: add logic to differentiate between cancelled and failed flows when checking status for indicators (so error triangle indicator doesn't show up on cancellaiton)
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