Fix: Improve Custom MCP node performance by caching toolkits#4809
Fix: Improve Custom MCP node performance by caching toolkits#4809toi500 wants to merge 9 commits into
Conversation
|
@HenryHengZJ, this is working very well for me now, very smooth.
I will definitely be keeping this change for my own use, as it is much better to have a minor memory leak (especially since the current MCP implementation has some leaks anyway) than to have the constant process churn overload my server and force me to spend 20 minutes filling in actions on a single MCP node. |
There was a problem hiding this comment.
Pull Request Overview
Implements caching for MCPToolkit instances to eliminate redundant process spawning on UI refresh, boosting performance.
- Introduces a static
toolkitCachekeyed by workspace, parsed config, and sandbox. - Parses and canonicalizes
mcpServerConfigto generate consistent cache keys. - Clears out invalid
mcpActionsafter loading a new toolkit to keep the UI in sync.
Comments suppressed due to low confidence (1)
packages/components/nodes/tools/MCP/CustomMCP/CustomMCP.ts:161
- Add unit tests to verify that cached toolkits are returned correctly and that the stale-action clearing logic behaves as expected to prevent regressions.
if (Custom_MCP.toolkitCache.has(cacheKey)) {
|
does this takes care of tool availability changes ? when custom mcps are used via |
Not sure if I am following you here. Every time you add a version, it uses a new mcpServerConfig, and you will fetch whatever actions that version has. That version is then cached until Flowise restarts. If what you mean is that you will lose the convenience of using "@latest" in some cases, it is a trade-off I would definitely pay for not spinning up the same server 10 times to add 10 actions and spending 10 minutes on it. |
|
@HenryHengZJ, this PR is ready to review. I have tested it properly, and it works in every edge scenario I thought of. I added it as an option so it may solve any extreme scenario. Since you pointed out that this cache approach is not compatible with If you test this on queue mode, the node should work as it does right now (no cache involved, no "Disable Cache" option visible). |
|
@toi500 I've been thinking about this and trying to see if I can find an optimal way for both cloud and self hosted users. I found a better solution for self hosted: For flowise cloud users in QUEUE mode, unfortunately we can't store the toolkit classes into Redis, so its not achievable. Reason of using centralized Can you try this branch: https://github.com/FlowiseAI/Flowise/tree/bugfix/Custom-MCP-Cache I've tried multiple times and it works |
|
@HenryHengZJ, that makes sense. I just tried it, and it works for me too. |
|
PR opened here: #4897 |


As discussed, this PR fixes a critical performance issue in the
Custom MCPnode caused by redundant process spawning. The STDIO transport is re-spawning a child process on every UI refresh, leading to significant overhead.Solution:
This is a single file solution implemented entirely within
customMCP.ts.static toolkitCacheis used to store and reuseMCPToolkitinstances, preventing the expensive process creation on every refresh.mcpServerConfigJSON. This makes the cache immune to formatting differences (spaces, newlines) and prevents spawning duplicate processes for the same logical configuration.Trade-off:
Since we are not modifyingcore.tsand the node'sdestroy()method has proven unreliable for cleanup (I have tried to implement it without any success), this solution will leak one process per unique server configuration. This is a necessary trade-off to fix the critical performance issue immediately while keeping the changes isolated to this single file.