[Note: I am too lazy, so this issue is also generated by cursor. I did take a look, but I would apologize in advance if there are some minor confusions somewhere]
Environment
- mcp-haystack version: 0.4.0
- mcp version: 1.10.1
- Transport: SSE
- Environment: Client: same issue whether it's inside a docker or local. Server: from url.
Description
When using MCPTool with SSE transport, the underlying ClientSession expires after approximately 5~10 minutes of idle time, causing tool invocations to fail with anyio.ClosedResourceError. The error is then wrapped in MCPInvocationError but the original error message is lost, resulting in an empty error message.
Root Cause
The issue occurs in the call_tool method at line 237:
result = await self.session.call_tool(tool_name, tool_args)
The ClientSession from the MCP SDK becomes closed after idle periods, but mcp-haystack doesn't detect this condition or attempt to reconnect. When call_tool is invoked on a closed session, it raises anyio.ClosedResourceError(), which gets caught by the generic exception handler at line 248 and wrapped in MCPInvocationError with an empty error message.
Steps to Reproduce
- Initialize an
MCPTool with SSE transport:
from haystack.tools import MCPTool, SSEServerInfo
tool = MCPTool(
name="some_tool",
server_info=SSEServerInfo(url="http://localhost:8000/sse")
)
- Use the tool successfully initially
- Wait approximately 10 minutes without invoking the tool
- Attempt to invoke the tool again
Expected Behavior
- The tool should either automatically reconnect when the session expires, or
- Provide a clear error message indicating the session has expired and needs to be reconnected
Actual Behavior
Tool invocation fails with:
MCPInvocationError("Failed to invoke tool 'tool_name' with args: {...} , got error: ")
Note the empty error message after "got error: " - this is because anyio.ClosedResourceError() converts to an empty string.
Technical Details
The current architecture uses _MCPClientSessionManager to maintain a long-running connection in the background, but there's no mechanism to:
- Detect when the session becomes closed/expired
- Automatically reconnect when this occurs
- Provide meaningful error messages when connection issues occur
Proposed Solutions
- Automatic Reconnection: Implement session health checking and automatic reconnection when
ClosedResourceError is detected
- Better Error Handling: Preserve the original error type and message when wrapping in
MCPInvocationError
- Connection Refresh Method: Provide a method to manually refresh the connection when needed
- Keep-Alive Mechanism: Consider implementing periodic keep-alive requests to prevent session expiration
Workaround
Currently, the only workaround is to recreate the MCPTool instance when this error occurs.
Additional Context
This issue is particularly problematic when deploying pipelines as services, where tools may remain idle for extended periods between invocations. The empty error message makes debugging difficult, as it's not immediately clear that the issue is related to session expiration.
[Note: I am too lazy, so this issue is also generated by cursor. I did take a look, but I would apologize in advance if there are some minor confusions somewhere]
Environment
Description
When using
MCPToolwith SSE transport, the underlyingClientSessionexpires after approximately 5~10 minutes of idle time, causing tool invocations to fail withanyio.ClosedResourceError. The error is then wrapped inMCPInvocationErrorbut the original error message is lost, resulting in an empty error message.Root Cause
The issue occurs in the
call_toolmethod at line 237:The
ClientSessionfrom the MCP SDK becomes closed after idle periods, but mcp-haystack doesn't detect this condition or attempt to reconnect. Whencall_toolis invoked on a closed session, it raisesanyio.ClosedResourceError(), which gets caught by the generic exception handler at line 248 and wrapped inMCPInvocationErrorwith an empty error message.Steps to Reproduce
MCPToolwith SSE transport:Expected Behavior
Actual Behavior
Tool invocation fails with:
Note the empty error message after "got error: " - this is because
anyio.ClosedResourceError()converts to an empty string.Technical Details
The current architecture uses
_MCPClientSessionManagerto maintain a long-running connection in the background, but there's no mechanism to:Proposed Solutions
ClosedResourceErroris detectedMCPInvocationErrorWorkaround
Currently, the only workaround is to recreate the
MCPToolinstance when this error occurs.Additional Context
This issue is particularly problematic when deploying pipelines as services, where tools may remain idle for extended periods between invocations. The empty error message makes debugging difficult, as it's not immediately clear that the issue is related to session expiration.