From 8523074e87813950fce36ddd69bcaf8ae6fc4bb0 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 10 Jul 2025 20:32:38 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix:=20Invalidate=20Tool=20Cachi?= =?UTF-8?q?ng=20after=20MCP=20Initialization=20(#8384)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added Constants import in PluginController for better organization. - Renamed cachedTools to cachedToolsArray for clarity in PluginController. - Ensured getCachedTools returns an empty object if no tools are found. - Cleared tools array cache after MCP initialization in initializeMCP for consistency. --- api/server/controllers/PluginController.js | 11 +++++------ api/server/services/initializeMCP.js | 3 +++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/api/server/controllers/PluginController.js b/api/server/controllers/PluginController.js index f7aad84aeb..a0af142938 100644 --- a/api/server/controllers/PluginController.js +++ b/api/server/controllers/PluginController.js @@ -1,11 +1,10 @@ const { logger } = require('@librechat/data-schemas'); -const { CacheKeys, AuthType } = require('librechat-data-provider'); +const { CacheKeys, AuthType, Constants } = require('librechat-data-provider'); const { getCustomConfig, getCachedTools } = require('~/server/services/Config'); const { getToolkitKey } = require('~/server/services/ToolService'); const { getMCPManager, getFlowStateManager } = require('~/config'); const { availableTools } = require('~/app/clients/tools'); const { getLogStores } = require('~/cache'); -const { Constants } = require('librechat-data-provider'); /** * Filters out duplicate plugins from the list of plugins. @@ -140,9 +139,9 @@ function createGetServerTools() { const getAvailableTools = async (req, res) => { try { const cache = getLogStores(CacheKeys.CONFIG_STORE); - const cachedTools = await cache.get(CacheKeys.TOOLS); - if (cachedTools) { - res.status(200).json(cachedTools); + const cachedToolsArray = await cache.get(CacheKeys.TOOLS); + if (cachedToolsArray) { + res.status(200).json(cachedToolsArray); return; } @@ -173,7 +172,7 @@ const getAvailableTools = async (req, res) => { } }); - const toolDefinitions = await getCachedTools({ includeGlobal: true }); + const toolDefinitions = (await getCachedTools({ includeGlobal: true })) || {}; const toolsOutput = []; for (const plugin of authenticatedPlugins) { diff --git a/api/server/services/initializeMCP.js b/api/server/services/initializeMCP.js index 98b87d156e..0ed82e6ea1 100644 --- a/api/server/services/initializeMCP.js +++ b/api/server/services/initializeMCP.js @@ -44,6 +44,9 @@ async function initializeMCP(app) { await mcpManager.mapAvailableTools(toolsCopy, flowManager); await setCachedTools(toolsCopy, { isGlobal: true }); + const cache = getLogStores(CacheKeys.CONFIG_STORE); + await cache.delete(CacheKeys.TOOLS); + logger.debug('Cleared tools array cache after MCP initialization'); logger.info('MCP servers initialized successfully'); } catch (error) { logger.error('Failed to initialize MCP servers:', error);