fix: Fix McpToolset hanging indefinitely

This fixes McpToolset from hanging indefinitely during a list_tools call (https://github.com/google/adk-python/issues/3084) by adding a timeout.

Co-authored-by: Kathy Wu <wukathy@google.com>
PiperOrigin-RevId: 829499276
This commit is contained in:
Kathy Wu
2025-11-07 10:50:38 -08:00
committed by Copybara-Service
parent 0ccc43cf49
commit 9761fc6bbb
3 changed files with 34 additions and 1 deletions
+12 -1
View File
@@ -14,6 +14,7 @@
from __future__ import annotations
import asyncio
import logging
import sys
from typing import Callable
@@ -177,7 +178,17 @@ class McpToolset(BaseToolset):
session = await self._mcp_session_manager.create_session(headers=headers)
# Fetch available tools from the MCP server
tools_response: ListToolsResult = await session.list_tools()
timeout_in_seconds = (
self._connection_params.timeout
if hasattr(self._connection_params, "timeout")
else None
)
try:
tools_response: ListToolsResult = await asyncio.wait_for(
session.list_tools(), timeout=timeout_in_seconds
)
except Exception as e:
raise ConnectionError("Failed to get tools from MCP server.") from e
# Apply filtering based on context and tool_filter
tools = []