MCP Gateway Connection Drops After Some Time (503+ ClosedResourceError) -Best practices ? #7073
user1500177
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Community,
I’m using Agno (v2.5.9)+ MCP (via an MCP gateway) and wanted some guidance on connection handling and resilience.
What I did
Initially connected directly to an MCP server → worked fine
After redeploys, I had to manually restart my agent (tool calls failed)
Later had to switch to MCP gateway (via streaming HTTP)
Current Problem
Initially everything works (LLM + tool calls succeed)
After some time, connection drops and I start seeing errors like below
Errors (sanitized, same order)
Error in post_writer
Traceback (most recent call last):
File ".../mcp/client/streamable_http.py", line 571, in post_writer
await handle_request_async()
File ".../mcp/client/streamable_http.py", line 565, in handle_request_async
await self._handle_post_request(ctx)
File ".../mcp/client/streamable_http.py", line 358, in _handle_post_request
response.raise_for_status()
File ".../httpx/_models.py", line 829, in raise_for_status
raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Server error '503 Service Unavailable' for url '/mcp'
Error parsing SSE message
Traceback (most recent call last):
File ".../mcp/client/streamable_http.py", line 229, in _handle_sse_event
await read_stream_writer.send(session_message)
File ".../anyio/streams/memory.py", line 249, in send
self.send_nowait(item)
File ".../anyio/streams/memory.py", line 218, in send_nowait
raise ClosedResourceError
anyio.ClosedResourceError
Error while sending ping / tool call
Traceback (most recent call last):
File ".../agno/utils/mcp.py", line 75, in call_tool
await active_session.send_ping()
File ".../mcp/client/session.py", line 222, in send_ping
return await self.send_request(...)
File ".../mcp/shared/session.py", line 281, in send_request
await self._write_stream.send(...)
File ".../anyio/streams/memory.py", line 249, in send
self.send_nowait(item)
File ".../anyio/streams/memory.py", line 218, in send_nowait
raise ClosedResourceError
anyio.ClosedResourceError
ERROR Traceback (most recent call last):
File ".../agno/utils/mcp.py", line 75, in call_tool
await active_session.send_ping()
File ".../mcp/client/session.py", line 222, in send_ping
return await self.send_request(...)
File ".../mcp/shared/session.py", line 281, in send_request
await self._write_stream.send(...)
File ".../anyio/streams/memory.py", line 249, in send
self.send_nowait(item)
File ".../anyio/streams/memory.py", line 218, in send_nowait
raise ClosedResourceError
anyio.ClosedResourceError
ERROR Failed to call MCP tool '':
Traceback (most recent call last):
File ".../agno/utils/mcp.py", line 80, in call_tool
result = await active_session.call_tool(tool_name, kwargs)
File ".../mcp/client/session.py", line 383, in call_tool
result = await self.send_request(...)
File ".../mcp/shared/session.py", line 281, in send_request
await self._write_stream.send(...)
File ".../anyio/streams/memory.py", line 249, in send
self.send_nowait(item)
File ".../anyio/streams/memory.py", line 218, in send_nowait
raise ClosedResourceError
anyio.ClosedResourceError
I saw that there is a flag called refresh_connection , which can be set while creating the tool instance of MCPTools.
Questions
Is it expected to see connection closed errors when using refresh_connection parameter ?
Should MCP/tool connections be created at startup or handled dynamically?
Is refresh/revalidation the standard approach for handling MCP restarts/outages?
Do these errors (503, ClosedResourceError, SSE issues) indicate gateway timeouts/instability? (Or is it any mistake from my part How i connect to the mcp or something ?)
server_params = StreamableHTTPClientParams(
url=mcp_gateway_url,
headers=headers,
timeout=30.0,
)
mcp_gateway_tools = MCPTools(
server_params=server_params,
transport="streamable-http",
timeout_seconds=30,
refresh_connection=True,
)
Later in the lifespan of the FastAPI app i connect to this with mcp_gateway_tools.connect(), and later call the await tool.close() also
Any idea about performance impact of frequent connection refresh?
Beta Was this translation helpful? Give feedback.
All reactions