mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-16 15:38:10 +01:00
🔧 refactor: Update MCP connection handling to improve performance and testing
* refactor: Replace getAll() with getLoaded() in MCP.js to prevent unnecessary connection creation for user-context servers. * test: Adjust MCP.spec.js to mock getLoaded() instead of getAll() for consistency with the new implementation. * feat: Enhance MCPServersInitializer to reset initialization flag for better handling of process restarts and stale data. * test: Add integration tests to verify re-initialization behavior and ensure stale data is cleared when necessary.
This commit is contained in:
parent
02fc4647e1
commit
877c67e295
5 changed files with 185 additions and 9 deletions
|
|
@ -448,7 +448,10 @@ async function getMCPSetupData(userId) {
|
|||
/** @type {Map<string, import('@librechat/api').MCPConnection>} */
|
||||
let appConnections = new Map();
|
||||
try {
|
||||
appConnections = (await mcpManager.appConnections?.getAll()) || new Map();
|
||||
// Use getLoaded() instead of getAll() to avoid forcing connection creation
|
||||
// getAll() creates connections for all servers, which is problematic for servers
|
||||
// that require user context (e.g., those with {{LIBRECHAT_USER_ID}} placeholders)
|
||||
appConnections = (await mcpManager.appConnections?.getLoaded()) || new Map();
|
||||
} catch (error) {
|
||||
logger.error(`[MCP][User: ${userId}] Error getting app connections:`, error);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ describe('tests for the new helper functions used by the MCP connection status e
|
|||
|
||||
beforeEach(() => {
|
||||
mockGetMCPManager.mockReturnValue({
|
||||
appConnections: { getAll: jest.fn(() => new Map()) },
|
||||
appConnections: { getLoaded: jest.fn(() => new Map()) },
|
||||
getUserConnections: jest.fn(() => new Map()),
|
||||
});
|
||||
mockRegistryInstance.getOAuthServers.mockResolvedValue(new Set());
|
||||
|
|
@ -143,7 +143,7 @@ describe('tests for the new helper functions used by the MCP connection status e
|
|||
const mockOAuthServers = new Set(['server2']);
|
||||
|
||||
const mockMCPManager = {
|
||||
appConnections: { getAll: jest.fn(() => Promise.resolve(mockAppConnections)) },
|
||||
appConnections: { getLoaded: jest.fn(() => Promise.resolve(mockAppConnections)) },
|
||||
getUserConnections: jest.fn(() => mockUserConnections),
|
||||
};
|
||||
mockGetMCPManager.mockReturnValue(mockMCPManager);
|
||||
|
|
@ -153,7 +153,7 @@ describe('tests for the new helper functions used by the MCP connection status e
|
|||
|
||||
expect(mockRegistryInstance.getAllServerConfigs).toHaveBeenCalledWith(mockUserId);
|
||||
expect(mockGetMCPManager).toHaveBeenCalledWith(mockUserId);
|
||||
expect(mockMCPManager.appConnections.getAll).toHaveBeenCalled();
|
||||
expect(mockMCPManager.appConnections.getLoaded).toHaveBeenCalled();
|
||||
expect(mockMCPManager.getUserConnections).toHaveBeenCalledWith(mockUserId);
|
||||
expect(mockRegistryInstance.getOAuthServers).toHaveBeenCalledWith(mockUserId);
|
||||
|
||||
|
|
@ -174,7 +174,7 @@ describe('tests for the new helper functions used by the MCP connection status e
|
|||
mockRegistryInstance.getAllServerConfigs.mockResolvedValue(mockConfig);
|
||||
|
||||
const mockMCPManager = {
|
||||
appConnections: { getAll: jest.fn(() => Promise.resolve(null)) },
|
||||
appConnections: { getLoaded: jest.fn(() => Promise.resolve(null)) },
|
||||
getUserConnections: jest.fn(() => null),
|
||||
};
|
||||
mockGetMCPManager.mockReturnValue(mockMCPManager);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue