Feat/Implement caching for MCP toolkit in CachePool (#4897)

feat: Implement caching for MCP toolkit in CachePool

- Added methods to CachePool for managing MCP toolkit cache.
- Integrated caching logic in CustomMCP to store and retrieve toolkit data based on workspaceId and configuration.
- Updated node service to pass cachePool to CustomMCP for enhanced performance.
This commit is contained in:
Henry Heng
2025-07-18 19:07:48 +01:00
committed by GitHub
parent cc4a773010
commit 2b7a074c8b
3 changed files with 49 additions and 1 deletions
@@ -3,6 +3,7 @@ import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, IN
import { MCPToolkit } from '../core'
import { getVars, prepareSandboxVars } from '../../../../src/utils'
import { DataSource } from 'typeorm'
import hash from 'object-hash'
const mcpServerConfig = `{
"command": "npx",
@@ -140,6 +141,24 @@ class Custom_MCP implements INode {
sandbox['$vars'] = prepareSandboxVars(variables)
}
const workspaceId = options?.searchOptions?.workspaceId?._value || options?.workspaceId
let canonicalConfig
try {
canonicalConfig = JSON.parse(mcpServerConfig)
} catch (e) {
canonicalConfig = mcpServerConfig
}
const cacheKey = hash({ workspaceId, canonicalConfig, sandbox })
if (options.cachePool) {
const cachedResult = await options.cachePool.getMCPCache(cacheKey)
if (cachedResult) {
return cachedResult.tools
}
}
try {
let serverParams
if (typeof mcpServerConfig === 'object') {
@@ -162,6 +181,10 @@ class Custom_MCP implements INode {
const tools = toolkit.tools ?? []
if (options.cachePool) {
await options.cachePool.addMCPCache(cacheKey, { toolkit, tools })
}
return tools as Tool[]
} catch (error) {
throw new Error(`Invalid MCP Server Config: ${error}`)