McpToolset does not handle errors in ListTool calls
** Please make sure you read the contribution guide and file the issues in the right place. ** Contribution guide.
Describe the bug
When an MCP Server is not available (anymore) when the ListTools call happens, the complete Agent fails to run and just finishes without log or error message.
To Reproduce Steps to reproduce the behavior:
- Connect ADK to an MCP server via streamable http
- MCP server must raise an exception when responding to ListTools call
- Try to talk to the agent
There is not error log, the agent immediately fails and does not respond anything
2025-10-30 10:35:53 - /app/agenthub/.venv/lib/python3.13/site-packages/google/adk/cli/utils/envs.py - INFO - Loaded .env file for root at /app/agenthub/agents/.env
2025-10-30 10:35:53 - /app/agenthub/.venv/lib/python3.13/site-packages/google/adk/cli/utils/envs.py - INFO - Loaded .env file for root at /app/agenthub/agents/.env
2025-10-30 10:35:54 - google_adk.google.adk.cli.utils.agent_loader - INFO - Found root_agent in root.agent
2025-10-30 10:35:54 - mcp.client.streamable_http - INFO - Received session ID: 6464e0fd631a47638b838775d84367d7
2025-10-30 10:35:54 - mcp.client.streamable_http - INFO - Negotiated protocol version: 2025-06-18
2025-10-30 10:35:54 - mcp.client.streamable_http - INFO - Received session ID: 9b122639fd914d0b9f96e2b8820abfaf
2025-10-30 10:35:54 - mcp.client.streamable_http - INFO - Negotiated protocol version: 2025-06-18
Expected behavior
When a single MCP Server can not respond to requests,
- do not fail to whole agent
- try to reconnect to it later
- give proper logs
Desktop (please complete the following information):
- OS: MacOS, Linux
- Python version(python -V): 3.13.7
- ADK version(pip show google-adk): 1.17.0
Model Information:
- Are you using LiteLLM: Yes
- Which model is being used(e.g. gemini-2.5-pro): gpt-5
Additional context
We have a patch in place to handle this, but it is kind-of ugly and it does not handle proper reconnect attempts.
--- a/google/adk/tools/mcp_tool/mcp_toolset.py
+++ b/google/adk/tools/mcp_tool/mcp_toolset.py
@@ -152,11 +152,22 @@
Returns:
List[BaseTool]: A list of tools available under the specified context.
"""
- # Get session from session manager
- session = await self._mcp_session_manager.create_session()
+ # Try to get session from session manager, but handle failures gracefully
+ try:
+ session = await self._mcp_session_manager.create_session()
+ except Exception as e:
+ # Log the error but don't crash - just return empty tool list for this MCP server
+ logger = logging.getLogger(__name__)
+ logger.warning(f"Failed to create MCP session for {self._mcp_session_manager._connection_params}: {e}")
+ return [] # Return empty tool list if connection fails
# Fetch available tools from the MCP server
- tools_response: ListToolsResult = await session.list_tools()
+ try:
+ tools_response: ListToolsResult = await session.list_tools()
+ except Exception as e:
+ logger = logging.getLogger(__name__)
+ logger.warning(f"Failed to list tools from MCP server: {e}")
+ return [] # Return empty tool list if tool listing fails
# Apply filtering based on context and tool_filter
tools = []
@flansen Thanks for reporting this and providing the patch!
You're right that the error handling is missing in mcp_toolset.py.
The fix is to wrap both the create_session() (line 177) and list_tools() (line 180) calls in try-except blocks that log warnings and return an empty list on failure, exactly as you've shown.
This prevents the agent from crashing and allows it to continue with other available MCP servers.
We'll get this reviewed.
Thanks!
@hangfei Please have a look into it.
Taking a look.
if this happens you need to recreate the agent, no? Also if it will return the empty list it might crash on "prompt level" ?
To address your concerns:
-
Agent recreation: With the proposed fix (returning empty list on failure), no restart needed. The agent continues running with tools from other working MCP servers. Currently in v1.20.0, the agent still crashes because the exception is re-raised at mcp_toolset.py:176.
-
Empty list concern: Returning an empty list is safe and won't cause prompt-level crashes. The agent simply continues with fewer available tools, which is the desired graceful degradation behavior when one MCP server is down.
The fix needs to wrap both create_session() and replace the raise ConnectionError with return [] plus proper logging.
@surajksharma07 I'm having similar issues but with Agent Engine, the async context is somehow not consistent. It's trying to close the session in a different task it was entered
@rrondir The async context issue you're seeing is a separate problem from the original ListTools error handling.
The "session closed in different task" error happens when MCP sessions are shared across async tasks in Agent Engine deployments due to how sessions are managed in the connection pool.
As a workaround, ensure each agent instance has its own MCP session by setting unique connection params or using session isolation.
The original issue (line 176 still raising ConnectionError) remains unfixed in v1.21.0 - the timeout fixes in v1.19.0 helped with unreachable servers but didn't implement the graceful degradation (returning empty list).
For your Agent Engine async issue,can you share the full traceback?
It might be related to the session lifecycle management in deployed environments vs local adk web.
@wukath Could you please help take a look?