mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-08-27 04:07:05 +00:00
🛡️ fix: Handle MCP Tool Cache Lookup Failures (#12910)
* Handle MCP tool cache lookup failures * Harden MCP cached tool lookup * Cover full MCP tool cache outage * Guard MCP tool cache store lookup
This commit is contained in:
parent
74307e6dcc
commit
5b5e2b0286
4 changed files with 150 additions and 12 deletions
|
|
@ -1,6 +1,12 @@
|
|||
const { CacheKeys } = require('librechat-data-provider');
|
||||
|
||||
jest.mock('@librechat/data-schemas', () => ({
|
||||
logger: {
|
||||
error: jest.fn(),
|
||||
},
|
||||
}));
|
||||
jest.mock('~/cache/getLogStores');
|
||||
const { logger } = require('@librechat/data-schemas');
|
||||
const getLogStores = require('~/cache/getLogStores');
|
||||
|
||||
const mockCache = { get: jest.fn(), set: jest.fn(), delete: jest.fn() };
|
||||
|
|
@ -75,6 +81,30 @@ describe('getCachedTools', () => {
|
|||
expect(mockCache.get).toHaveBeenCalledWith(ToolCacheKeys.MCP_SERVER('user1', 'github'));
|
||||
});
|
||||
|
||||
it('getMCPServerTools should return null when the cache lookup fails', async () => {
|
||||
const error = new Error('cache unavailable');
|
||||
mockCache.get.mockRejectedValue(error);
|
||||
|
||||
await expect(getMCPServerTools('user1', 'github')).resolves.toBeNull();
|
||||
expect(logger.error).toHaveBeenCalledWith(
|
||||
'[getMCPServerTools] Error fetching cached tools for github:',
|
||||
error,
|
||||
);
|
||||
});
|
||||
|
||||
it('getMCPServerTools should return null when the cache store is unavailable', async () => {
|
||||
const error = new Error('cache store unavailable');
|
||||
getLogStores.mockImplementationOnce(() => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
await expect(getMCPServerTools('user1', 'github')).resolves.toBeNull();
|
||||
expect(logger.error).toHaveBeenCalledWith(
|
||||
'[getMCPServerTools] Error fetching cached tools for github:',
|
||||
error,
|
||||
);
|
||||
});
|
||||
|
||||
it('should NOT use CONFIG_STORE namespace', async () => {
|
||||
mockCache.get.mockResolvedValue(null);
|
||||
await getCachedTools();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
const { CacheKeys, Time } = require('librechat-data-provider');
|
||||
const { logger } = require('@librechat/data-schemas');
|
||||
const getLogStores = require('~/cache/getLogStores');
|
||||
|
||||
/**
|
||||
|
|
@ -89,14 +90,13 @@ async function invalidateCachedTools(options = {}) {
|
|||
* @returns {Promise<LCAvailableTools|null>} The available tools for the server
|
||||
*/
|
||||
async function getMCPServerTools(userId, serverName) {
|
||||
const cache = getLogStores(CacheKeys.TOOL_CACHE);
|
||||
const serverTools = await cache.get(ToolCacheKeys.MCP_SERVER(userId, serverName));
|
||||
|
||||
if (serverTools) {
|
||||
return serverTools;
|
||||
try {
|
||||
const cache = getLogStores(CacheKeys.TOOL_CACHE);
|
||||
return (await cache.get(ToolCacheKeys.MCP_SERVER(userId, serverName))) || null;
|
||||
} catch (error) {
|
||||
logger.error(`[getMCPServerTools] Error fetching cached tools for ${serverName}:`, error);
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue