Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
⏹️ fix: Cancel MCP OAuth From the Tool Dialog (#16561)
lia-by-librechat[bot]99255e34da45
diff --git a/client/src/components/SidePanel/Agents/Tools/ItemDialog/__tests__/McpSection.spec.tsx b/client/src/components/SidePanel/Agents/Tools/ItemDialog/__tests__/McpSection.spec.tsxindex b569833f56..1ee42046fa 100644--- a/client/src/components/SidePanel/Agents/Tools/ItemDialog/__tests__/McpSection.spec.tsx+++ b/client/src/components/SidePanel/Agents/Tools/ItemDialog/__tests__/McpSection.spec.tsx@@ -11,6 +11,7 @@ const mockMcpServersMap = jest.fn((): Map<string, object> => new Map());let mockMcpToolsLoading = false;const mockGetServerStatusIconProps = jest.fn((): object | null => null);const mockInitializeServer = jest.fn();+const mockCancelOAuth = jest.fn();const mockIsConnectionDeferred = jest.fn((): boolean => false);const mockToggleIntentAll = jest.fn();const mockIsToolProgrammaticOnly = jest.fn((_toolId: string): boolean => false);@@ -186,6 +187,7 @@ describe('McpSection', () => {mockSetValue.mockClear();mockGetValues.mockReturnValue([]);mockInitializeServer.mockReset();+ mockCancelOAuth.mockReset();mockIsConnectionDeferred.mockReset();mockIsConnectionDeferred.mockReturnValue(false);mockToggleIntentAll.mockClear();@@ -309,6 +311,104 @@ describe('McpSection', () => {);});+ test.each([true, false])(+ 'offers cancellation for pending OAuth (local initialization=%s)',+ (isInitializing) => {+ mockGetServerStatusIconProps.mockReturnValue({+ serverStatus: { connectionState: 'connecting', requiresOAuth: true },+ isInitializing,+ canCancel: true,+ onCancel: mockCancelOAuth,+ });++ render(<McpSection item={item} />);++ const cancel = screen.getByRole('button', { name: 'com_ui_cancel' });+ expect(cancel).toBeEnabled();+ expect(+ screen.queryByRole('button', { name: 'com_nav_mcp_connect_server' }),+ ).not.toBeInTheDocument();+ fireEvent.click(cancel);+ expect(mockCancelOAuth).toHaveBeenCalledTimes(1);+ expect(mockInitializeServer).not.toHaveBeenCalled();+ },+ );++ test('keeps Connect disabled until initialization becomes cancellable', () => {+ mockGetServerStatusIconProps.mockReturnValue({+ isInitializing: true,+ canCancel: false,+ });++ render(<McpSection item={item} />);++ expect(screen.getByRole('button', { name: 'com_nav_mcp_connect_server' })).toBeDisabled();+ expect(screen.queryByRole('button', { name: 'com_ui_cancel' })).not.toBeInTheDocument();+ });++ test('cancelling closes OAuth and prevents a late connection from selecting tools', async () => {+ mockInitializeServer.mockResolvedValue({+ success: true,+ oauthRequired: true,+ oauthUrl: 'https://oauth.example/authorize',+ });+ const { rerender } = render(<McpSection item={item} />);+ fireEvent.click(screen.getByRole('button', { name: 'com_nav_mcp_connect_server' }));+ expect(await screen.findByTestId('oauth-dialog')).toBeInTheDocument();++ mockGetServerStatusIconProps.mockReturnValue({+ isInitializing: true,+ canCancel: true,+ onCancel: mockCancelOAuth,+ });+ rerender(<McpSection item={item} />);+ fireEvent.click(screen.getByRole('button', { name: 'com_ui_cancel' }));++ expect(screen.queryByTestId('oauth-dialog')).not.toBeInTheDocument();+ expect(mockCancelOAuth).toHaveBeenCalledTimes(1);+ mockGetServerStatusIconProps.mockReturnValue({+ serverStatus: { connectionState: 'connected' },+ isInitializing: false,+ canCancel: false,+ });+ rerender(<McpSection item={item} />);+ expect(mockSetValue).not.toHaveBeenCalled();+ });++ test('allows a fresh OAuth attempt after cancellation', async () => {+ mockInitializeServer.mockResolvedValue({+ success: true,+ oauthRequired: true,+ oauthUrl: 'https://oauth.example/first',+ });+ const { rerender } = render(<McpSection item={item} />);+ fireEvent.click(screen.getByRole('button', { name: 'com_nav_mcp_connect_server' }));+ expect(await screen.findByTestId('oauth-dialog')).toHaveTextContent(+ 'https://oauth.example/first',+ );++ mockGetServerStatusIconProps.mockReturnValue({+ isInitializing: true,+ canCancel: true,+ onCancel: mockCancelOAuth,+ });+ rerender(<McpSection item={item} />);+ fireEvent.click(screen.getByRole('button', { name: 'com_ui_cancel' }));++ mockGetServerStatusIconProps.mockReturnValue(null);+ mockInitializeServer.mockResolvedValue({+ success: true,+ oauthRequired: true,+ oauthUrl: 'https://oauth.example/second',+ });+ rerender(<McpSection item={item} />);+ fireEvent.click(screen.getByRole('button', { name: 'com_nav_mcp_connect_server' }));+ expect(await screen.findByTestId('oauth-dialog')).toHaveTextContent(+ 'https://oauth.example/second',+ );+ expect(mockInitializeServer).toHaveBeenCalledTimes(2);+ });+test('shows empty hint when the server exposes no tools', () => {const empty: McpItem = {...item,diff --git a/client/src/components/SidePanel/Agents/Tools/ItemDialog/sections/McpSection.tsx b/client/src/components/SidePanel/Agents/Tools/ItemDialog/sections/McpSection.tsxindex f1439f0184..df5ab020a6 100644--- a/client/src/components/SidePanel/Agents/Tools/ItemDialog/sections/McpSection.tsx+++ b/client/src/components/SidePanel/Agents/Tools/ItemDialog/sections/McpSection.tsx@@ -344,6 +344,7 @@ export default function McpSection({ item }: Props) {const isConnected = connectionState === 'connected' || liveServer.isConnected === true;const isReadyForAgent = liveServer.isReadyForAgent ?? isConnected;const isBusy = isInitializing || connectionState === 'connecting';+ const canCancel = statusIconProps?.canCancel === true;/** Close + clear the OAuth dialog once the server is ready, and don't let it* reopen on its own if the connection later drops. No useEffect — adjust state@@ -429,6 +430,13 @@ export default function McpSection({ item }: Props) {}};+ const handleCancel = (e: MouseEvent) => {+ setAutoSelectPending(false);+ setOauthOpen(false);+ setOauthUrl(null);+ statusIconProps?.onCancel(e);+ };+return (<div className="flex flex-col gap-5">{item.description && (@@ -463,15 +471,17 @@ export default function McpSection({ item }: Props) {<div className="min-h-0 overflow-hidden"><Buttontype="button"- variant="submit"+ variant={canCancel ? 'outline' : 'submit'}className="mt-5 w-full gap-2"- disabled={isBusy}+ disabled={isBusy && !canCancel}tabIndex={isReadyForAgent ? -1 : undefined}aria-hidden={isReadyForAgent || undefined}- onClick={handleConnect}+ onClick={canCancel ? handleCancel : handleConnect}>- {isBusy && <Spinner className="size-4" />}- {localize('com_nav_mcp_connect_server', { 0: serverName })}+ {isBusy && !canCancel && <Spinner className="size-4" />}+ {canCancel+ ? localize('com_ui_cancel')+ : localize('com_nav_mcp_connect_server', { 0: serverName })}</Button></div></div>