mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 15:33:45 +00:00
🛄 fix: Clear MCP Connection State Before Catalog Refresh (#15741)
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
This commit is contained in:
parent
ab6cde0c79
commit
2577324343
2 changed files with 128 additions and 4 deletions
123
client/src/hooks/MCP/__tests__/useMCPServerManager.spec.tsx
Normal file
123
client/src/hooks/MCP/__tests__/useMCPServerManager.spec.tsx
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
import React from 'react';
|
||||
import { Provider } from 'jotai';
|
||||
import { QueryKeys } from 'librechat-data-provider';
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider, QueryObserver } from '@tanstack/react-query';
|
||||
import type { MCPReinitializeResponse } from 'librechat-data-provider';
|
||||
import { useMCPServerManager } from '../useMCPServerManager';
|
||||
|
||||
const mockShowToast = jest.fn();
|
||||
const mockSetMCPValues = jest.fn();
|
||||
const mockReinitialize = jest.fn();
|
||||
|
||||
jest.mock('@librechat/client', () => ({
|
||||
useToastContext: () => ({ showToast: mockShowToast }),
|
||||
}));
|
||||
|
||||
jest.mock('~/hooks', () => ({
|
||||
useLocalize: () => (key: string) => key,
|
||||
useHasAccess: () => true,
|
||||
useCatalogReady: () => true,
|
||||
useMCPSelect: () => ({ mcpValues: [], setMCPValues: mockSetMCPValues }),
|
||||
useMCPConnectionStatus: () => ({ connectionStatus: {} }),
|
||||
}));
|
||||
|
||||
jest.mock('~/data-provider', () => ({
|
||||
useGetStartupConfig: () => ({ data: {} }),
|
||||
useMCPServersQuery: () => ({ data: {}, isLoading: false }),
|
||||
}));
|
||||
|
||||
jest.mock('librechat-data-provider/react-query', () => ({
|
||||
useReinitializeMCPServerMutation: () => ({ mutateAsync: mockReinitialize }),
|
||||
useCancelMCPOAuthMutation: () => ({}),
|
||||
useUpdateUserPluginsMutation: () => ({}),
|
||||
useGetAllEffectivePermissionsQuery: () => ({ data: {} }),
|
||||
}));
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
let reject!: (reason: Error) => void;
|
||||
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
|
||||
resolve = resolvePromise;
|
||||
reject = rejectPromise;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
describe('useMCPServerManager initialization', () => {
|
||||
it.each(['success', 'error'])(
|
||||
'finishes before a slow catalog refetch ends in %s',
|
||||
async (outcome) => {
|
||||
const connection = deferred<MCPReinitializeResponse>();
|
||||
const catalog = deferred<string[]>();
|
||||
mockReinitialize.mockReturnValue(connection.promise);
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false, cacheTime: 0 } },
|
||||
logger: { log: console.log, warn: console.warn, error: jest.fn() },
|
||||
});
|
||||
const catalogQuery = new QueryObserver(queryClient, {
|
||||
queryKey: [QueryKeys.mcpTools],
|
||||
queryFn: () => catalog.promise,
|
||||
initialData: [],
|
||||
staleTime: Infinity,
|
||||
});
|
||||
const unsubscribe = catalogQuery.subscribe(() => undefined);
|
||||
const invalidate = jest.spyOn(queryClient, 'invalidateQueries');
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<Provider>
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
</Provider>
|
||||
);
|
||||
const { result, unmount } = renderHook(() => useMCPServerManager(), { wrapper });
|
||||
const initialized = jest.fn();
|
||||
|
||||
try {
|
||||
act(() => {
|
||||
void result.current.initializeServer('test-server').then(initialized);
|
||||
});
|
||||
expect(result.current.isInitializing('test-server')).toBe(true);
|
||||
expect(mockShowToast).not.toHaveBeenCalled();
|
||||
|
||||
const response = { success: true, message: 'Connected', serverName: 'test-server' };
|
||||
await act(async () => {
|
||||
connection.resolve(response);
|
||||
});
|
||||
|
||||
expect(queryClient.getQueryState([QueryKeys.mcpTools])?.fetchStatus).toBe('fetching');
|
||||
expect(result.current.isInitializing('test-server')).toBe(false);
|
||||
expect(initialized).toHaveBeenCalledWith(response);
|
||||
expect(mockSetMCPValues).toHaveBeenCalledWith(['test-server']);
|
||||
expect(mockShowToast).toHaveBeenCalledWith({
|
||||
message: 'com_ui_mcp_initialized_success',
|
||||
status: 'success',
|
||||
});
|
||||
for (const key of [
|
||||
QueryKeys.mcpServers,
|
||||
QueryKeys.mcpTools,
|
||||
QueryKeys.mcpAuthValues,
|
||||
QueryKeys.mcpConnectionStatus,
|
||||
]) {
|
||||
expect(invalidate).toHaveBeenCalledWith([key]);
|
||||
}
|
||||
|
||||
await act(async () => {
|
||||
if (outcome === 'error') {
|
||||
catalog.reject(new Error('Catalog unavailable'));
|
||||
} else {
|
||||
catalog.resolve(['test-tool']);
|
||||
}
|
||||
});
|
||||
await waitFor(() => {
|
||||
expect(queryClient.getQueryState([QueryKeys.mcpTools])?.fetchStatus).toBe('idle');
|
||||
});
|
||||
expect(result.current.isInitializing('test-server')).toBe(false);
|
||||
expect(mockShowToast).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
catalog.resolve([]);
|
||||
unmount();
|
||||
unsubscribe();
|
||||
queryClient.clear();
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
|
@ -506,12 +506,15 @@ export function useMCPServerManager({
|
|||
|
||||
startServerPolling(serverName, response.flowId, response.oauthTimeout);
|
||||
} else {
|
||||
await Promise.all([
|
||||
cleanupServerState(serverName);
|
||||
void Promise.all([
|
||||
queryClient.invalidateQueries([QueryKeys.mcpServers]),
|
||||
queryClient.invalidateQueries([QueryKeys.mcpTools]),
|
||||
queryClient.invalidateQueries([QueryKeys.mcpAuthValues]),
|
||||
queryClient.invalidateQueries([QueryKeys.mcpConnectionStatus]),
|
||||
]);
|
||||
]).catch((error) => {
|
||||
console.error(`[MCP Manager] Failed to refresh queries for ${serverName}:`, error);
|
||||
});
|
||||
|
||||
showToast({
|
||||
message: localize('com_ui_mcp_initialized_success', { 0: serverName }),
|
||||
|
|
@ -522,8 +525,6 @@ export function useMCPServerManager({
|
|||
if (!currentValues.includes(serverName)) {
|
||||
setMCPValues([...currentValues, serverName]);
|
||||
}
|
||||
|
||||
cleanupServerState(serverName);
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue