LibreChat/client/src/data-provider/MCP/queries.ts
Danny Avila 3bf715e05e
♻️ refactor: On-demand MCP connections: remove proactive reconnect, default to available (#11839)
* feat: Implement reconnection staggering and backoff jitter for MCP connections

- Enhanced the reconnection logic in OAuthReconnectionManager to stagger reconnection attempts for multiple servers, reducing the risk of connection storms.
- Introduced a backoff delay with random jitter in MCPConnection to improve reconnection behavior during network issues.
- Updated the ConnectionsRepository to handle multiple server connections concurrently with a defined concurrency limit.

Added tests to ensure the new reconnection strategy works as intended.

* refactor: Update MCP server query configuration for improved data freshness

- Reduced stale time from 5 minutes to 30 seconds to ensure quicker updates on server initialization.
- Enabled refetching on window focus and mount to enhance data accuracy during user interactions.

* ♻️  refactor: On-demand MCP connections; remove proactive reconnection, default to available

  - Remove reconnectServers() from refresh controller (connection storm root cause)
  - Stop gating server selection on connection status; add to selection immediately
  - Render agent panel tools from DB cache, not live connection status
  - Proceed to cached tools on init failure (only gate on OAuth)
  - Remove unused batchToggleServers()
  - Reduce useMCPServersQuery staleTime from 5min to 30s, enable refetchOnMount/WindowFocus

* refactor: Optimize MCP tool initialization and server connection logic

- Adjusted tool initialization to only occur if no cached tools are available, improving efficiency.
- Updated comments for clarity on server connection and tool fetching processes.
- Removed unnecessary connection status checks during server selection to streamline the user experience.
2026-02-17 22:33:57 -05:00

44 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions, QueryObserverResult } from '@tanstack/react-query';
import { QueryKeys, dataService } from 'librechat-data-provider';
import type * as t from 'librechat-data-provider';
/**
* Hook for fetching all accessible MCP servers with permission metadata
*/
export const useMCPServersQuery = <TData = t.MCPServersListResponse>(
config?: UseQueryOptions<t.MCPServersListResponse, unknown, TData>,
): QueryObserverResult<TData> => {
return useQuery<t.MCPServersListResponse, unknown, TData>(
[QueryKeys.mcpServers],
() => dataService.getMCPServers(),
{
staleTime: 30 * 1000, // 30 seconds — short enough to pick up servers that finish initializing after first load
refetchOnWindowFocus: true,
refetchOnReconnect: false,
refetchOnMount: true,
retry: false,
...config,
},
);
};
/**
* Hook for fetching MCP-specific tools
* @param config - React Query configuration
* @returns MCP servers with their tools
*/
export const useMCPToolsQuery = <TData = t.MCPServersResponse>(
config?: UseQueryOptions<t.MCPServersResponse, unknown, TData>,
): QueryObserverResult<TData> => {
return useQuery<t.MCPServersResponse, unknown, TData>(
[QueryKeys.mcpTools],
() => dataService.getMCPTools(),
{
refetchOnWindowFocus: false,
refetchOnReconnect: false,
refetchOnMount: false,
staleTime: 5 * 60 * 1000, // 5 minutes
...config,
},
);
};