mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
* Refactor: MCPServersRegistry Singleton Pattern with Dependency Injection for DB methods consumption * refactor: error handling in MCP initialization and improve logging for MCPServersRegistry instance creation. - Added checks for mongoose instance in ServerConfigsDB constructor and refined error messages for clarity. - Reorder and use type imports --------- Co-authored-by: Atef Bellaaj <slalom.bellaaj@external.daimlertruck.com> Co-authored-by: Danny Avila <danny@librechat.ai>
38 lines
1 KiB
JavaScript
38 lines
1 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const { mergeAppTools, getAppConfig } = require('./Config');
|
|
const { createMCPServersRegistry, createMCPManager } = require('~/config');
|
|
|
|
/**
|
|
* Initialize MCP servers
|
|
*/
|
|
async function initializeMCPs() {
|
|
const appConfig = await getAppConfig();
|
|
const mcpServers = appConfig.mcpConfig;
|
|
if (!mcpServers) {
|
|
return;
|
|
}
|
|
|
|
// Initialize MCPServersRegistry first (required for MCPManager)
|
|
try {
|
|
createMCPServersRegistry(mongoose);
|
|
} catch (error) {
|
|
logger.error('[MCP] Failed to initialize MCPServersRegistry:', error);
|
|
throw error;
|
|
}
|
|
|
|
const mcpManager = await createMCPManager(mcpServers);
|
|
|
|
try {
|
|
const mcpTools = (await mcpManager.getAppToolFunctions()) || {};
|
|
await mergeAppTools(mcpTools);
|
|
|
|
logger.info(
|
|
`MCP servers initialized successfully. Added ${Object.keys(mcpTools).length} MCP tools.`,
|
|
);
|
|
} catch (error) {
|
|
logger.error('Failed to initialize MCP servers:', error);
|
|
}
|
|
}
|
|
|
|
module.exports = initializeMCPs;
|