Merged
Size
M
Change Breakdown
Bug Fix75%
Maintenance25%
#28384fix(core): Drain webhook close functions to prevent MCP connection leaks

MCP connection leaks closed after webhooks

MCP client connections opened through webhooks are now properly closed after each request, preventing a resource leak that accumulated open connections over time.

Webhooks that use MCP client tools were leaking connections on every request. The webhook handler received a list of cleanup callbacks but never executed them after returning—a classic resource management oversight. Each time a webhook ran, a new outbound SSE or HTTP connection opened to the MCP server and never closed.

Two specific scenarios caused this: when the webhook handler completed (regardless of success or error), close functions were ignored entirely; and when an MCP server returned no tools, the client connection was left hanging. This PR ensures cleanup code always runs by draining the close functions in a finally block, closing the client explicitly when the server has nothing to offer, and wrapping tool construction in error handling that guarantees the client shuts down cleanly.

The fix lives in the CLI webhook service and the MCP client tool node in the n8n LangChain integration. Connections to MCP servers are now managed correctly whether webhooks succeed, fail, or return empty tool lists.

View Original GitHub Description

Summary

WebhookService.runWebhook() passed an empty closeFunctions array to WebhookContext but never executed the accumulated callbacks after the webhook handler returned. This caused MCP Client Tool connections opened via McpTrigger webhooks to leak on every request — each request opened a new outbound SSE/HTTP connection that was never closed.

This PR:

  1. Drains closeFunctions in a try/finally block after runWebhook() completes (success or error)
  2. Closes the MCP client in supplyData when the server returns no tools (previously the client was left open)
  3. Wraps supplyData tool construction in try/catch to close the client if any error occurs after connection but before closeFunction is registered

Related Linear tickets, Github issues, and Community forum posts

Fixes https://github.com/n8n-io/n8n/issues/25740

Review / Merge checklist

  • I have seen this code, I have run this code, and I take responsibility for this code.
  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
© 2026 · via Gitpulse