mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 13:00:56 +03:00
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:
@@ -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}`)
|
||||
|
||||
Reference in New Issue
Block a user