Skip to content

Commit 630e524

Browse files
authored
fix: Prevent MCPToolset GC during toolset add (#2019)
* Prevent MCPToolset GC during toolset add * Improve typing
1 parent a8a5b7e commit 630e524

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
from collections.abc import Callable
56
from typing import Any
67
from urllib.parse import urlparse
78

@@ -13,6 +14,7 @@
1314

1415
from .mcp_tool import (
1516
AsyncExecutor,
17+
MCPClient,
1618
MCPConnectionError,
1719
MCPServerInfo,
1820
MCPToolNotFoundError,
@@ -145,13 +147,19 @@ def __init__(
145147
)
146148

147149
# This is a factory that creates the invocation function for the Tool
148-
def create_invoke_tool(mcp_client, tool_name, tool_timeout):
150+
def create_invoke_tool(
151+
owner_toolset: "MCPToolset",
152+
mcp_client: MCPClient,
153+
tool_name: str,
154+
tool_timeout: float,
155+
) -> Callable[..., Any]:
156+
"""Return a closure that keeps a strong reference to *owner_toolset* alive."""
157+
149158
def invoke_tool(**kwargs) -> Any:
150-
"""Invoke a tool using the existing client and AsyncExecutor."""
151-
result = AsyncExecutor.get_instance().run(
159+
_ = owner_toolset # strong reference so GC can't collect the toolset too early
160+
return AsyncExecutor.get_instance().run(
152161
mcp_client.call_tool(tool_name, kwargs), timeout=tool_timeout
153162
)
154-
return result
155163

156164
return invoke_tool
157165

@@ -170,7 +178,7 @@ def invoke_tool(**kwargs) -> Any:
170178
name=tool_info.name,
171179
description=tool_info.description,
172180
parameters=tool_info.inputSchema,
173-
function=create_invoke_tool(client, tool_info.name, self.invocation_timeout),
181+
function=create_invoke_tool(self, client, tool_info.name, self.invocation_timeout),
174182
)
175183
haystack_tools.append(tool)
176184

0 commit comments

Comments
 (0)