Handling MCPClient timeouts in an agent #1442
Replies: 2 comments
-
|
Hi @ben-gineer! 👋 Thank you for this incredibly detailed writeup! This is a known issue that several users have encountered. Related Issues & WorkYour issue is related to:
The PR addresses part of this problem by checking the session state before making calls, which prevents hangs when the connection is closed. Current StatusPR #1396 is in progress and adds session state checking. However, as you've identified, there's still a gap around automatic reconnection after timeout. Potential Workarounds1. Recreate MCPClient per requestFor now, the most reliable pattern is to recreate the MCPClient for each agent invocation: async def handle_request(messages):
# Create fresh MCP client per request
with create_mcp_client() as mcp_client:
agent = Agent(
model=model,
tools=[mcp_client],
system_prompt=system_prompt,
)
return agent(messages)2. Increase SSE timeout significantlyIf your use case allows, increase sse_read_timeout=3600 # 1 hour3. Use a connection health checkBefore invoking the agent, check if the MCP client is still connected: try:
# Quick health check
await mcp_client.list_tools()
except Exception:
# Recreate client
mcp_client = create_mcp_client()Feature RequestYour suggestion for automatic reconnection is valuable! Would you consider filing a separate feature request for "Automatic reconnection support for MCPClient"? This would help track and prioritize the work. Key requirements:
I'll keep an eye on this discussion and PR #1396. Let us know if the workarounds help! 🦆 🤖 This is an experimental AI agent response from the Strands team, powered by Strands Agents. We're exploring how AI agents can help with community support and development. Your feedback helps us improve! If you'd prefer human assistance, please let us know. |
Beta Was this translation helpful? Give feedback.
-
|
It was suggested that this might be an issue with the deprecated SSE client. However, I see the same behaviour with the Streamable HTTP transport. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
MCPClient SSE Timeout - Help Needed!
What We're Experiencing
We're using
strands.tools.mcp.MCPClientwith SSE connections in a long-running agent, and we're running into a challenge. After the SSE connection times out (configured viasse_read_timeout), all subsequent tool calls fail and the MCPClient doesn't automatically reconnect.Important context: We're wrapping our Strands agent with the AG-UI Strands wrapper (
StrandsAgentfromag_ui_strands), which might be part of the issue. Not sure if this affects how the MCP tools behave or if we're missing something in our setup.Current Behavior
Here's what happens:
sse_read_timeoutperiod of inactivity, the SSE connection times outWhat We're Hoping For
We'd love to have the MCPClient:
But we're not sure if this is the right approach or if we're missing something! How do others handle SSE timeouts with MCPClient in long-running agents?
How to Reproduce
If you'd like to see what we're experiencing, here's how to reproduce it:
1. Setup Environment
2. Run the Test Agent
cd apps/agentic-backend uv run python mcp_timeout_issue.py3. Make Initial Request (Works)
Result: ✅ Works - MCP tools are called successfully
4. Wait for Timeout
# Wait 30+ seconds for SSE connection to timeout sleep 355. Make Second Request (Fails)
Result: ❌ Fails with connection error like:
httpx.ReadTimeoutSSE connection closedConnection reset by peer6. Subsequent Requests Continue to Fail
All future requests will continue to fail until the server is restarted.
Our Current Setup
Here's how we're creating and using the MCPClient:
Is there something we should be doing differently here?
What We've Tried
We attempted to implement a
ReconnectingMCPClientwrapper that would:However, this approach failed because MCPClient doesn't properly initialize when wrapped. The wrapper pattern doesn't work with how MCPClient's internals are structured, and the tools don't become available to the agent.
Questions for the Community
Is this expected behavior? Should MCPClient handle reconnection automatically, or is there a pattern we should be following?
AG-UI Strands wrapper issue? Could wrapping the Strands agent with
StrandsAgentfromag_ui_strandsbe interfering with how MCP tools maintain their connections?How do others handle this? For those running long-running agents with MCP tools, how do you handle SSE connection timeouts?
Alternative approaches? Should we be creating the MCP client differently, or is there a way to detect and handle connection failures we're missing?
Any insights or suggestions would be greatly appreciated!
Environment Details
Impact
This issue affects any long-running agent that uses MCPClient with SSE connections. In production:
Files
mcp_timeout_issue.py- Minimal reproduction scripttest_mcp_timeout.py- Simple test script demonstrating the issuetest_mcp_timeout.py
mcp_timeout_issue.py
Beta Was this translation helpful? Give feedback.
All reactions