MCP connection leaks fixed after execution
MCP connections from MCP Client and MCP Client Tool nodes now close properly, preventing resource leaks after workflow execution ends.
Workflows using MCP Client or MCP Client Tool nodes would leave network connections open indefinitely after execution completed. These leaked connections consumed memory and held ports open with no way to reclaim them until the process restarted.
The fix wraps MCP connection usage in try/finally blocks across five node files, guaranteeing client.close() runs regardless of how execution ends — whether successful, errored, or cancelled. A companion fix in the core execution engine ensures cleanup callbacks registered by nodes also run in all cases, including when execution returns early or throws an error.
In the @n8n/nodes-langchain MCP nodes and core @n8n/core execution engine, resources are now properly released at the end of every workflow run.
View Original GitHub Description
Summary
MCP connections (Streamable HTTP) created by MCP Client and MCP Client Tool nodes never close and leak because cleanup (client.close()) is either missing or isn't guaranteed on all exit paths. MCP connections persist indefinitely even after workflow execution ends, consuming memory and ports.
Two root causes fixed:
1. MCP Client node-level leaks
- Problem:
McpClient.node.ts, McpClientTool.node.ts, listSearch.ts, resourceMapping.ts, and loadOptions.tscreate MCP connections but don't callclient.close(). - Fix: Wrapped connection usage in try/finally blocks, ensuring
client.close()is called at the end of execution.
2. Core engine leak
- Problem:
closeFunctions(cleanup callbacks registered by nodes like MCP Client Tool) are skipped inexecuteNode()(workflow-execute.ts) when:execute()throws an error — execution jumps past cleanupexecute()returns anEngineRequest— early return exits beforecloseFunctionsrun
- Fix: Moved
closeFunctionsinvocation into a finally block so it always runs.
Please see https://github.com/n8n-io/n8n/issues/25740 for more details
Related Linear tickets, Github issues, and Community forum posts
https://linear.app/n8n/issue/GHC-6819 Closes https://github.com/n8n-io/n8n/issues/25740
<!-- Include links to **Linear ticket** or Github issue or Community forum post. Important in order to close *automatically* and provide context to reviewers. https://linear.app/n8n/issue/ --> <!-- Use "closes #<issue-number>", "fixes #<issue-number>", or "resolves #<issue-number>" to automatically close issues when the PR is merged. -->Review / Merge checklist
- PR title and summary are descriptive. (conventions) <!-- **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** -->
- Docs updated or follow-up ticket created.
- Tests included. <!-- A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. -->
- PR Labeled with
release/backport(if the PR is an urgent fix that needs to be backported)