8000 fix: timeout issues for mcpstdio server when mcp tools are incorrect. · google/adk-python@45ef668 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45ef668

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: timeout issues for mcpstdio server when mcp tools are incorrect.
Fixes #643 PiperOrigin-RevId: 765342572
1 parent 01965bd commit 45ef668

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/google/adk/tools/mcp_tool/mcp_session_manager.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def __init__(
149149
"""Initializes the MCP session manager.
150150
151151
Args:
152-
connection_params: Parameters for the MCP connection (Stdio, SSE or Streamable HTTP).
152+
connection_params: Parameters for the MCP connection (Stdio, SSE or
153+
Streamable HTTP).
153154
errlog: (Optional) TextIO stream for error logging. Use only for
154155
initializing a local stdio MCP session.
155156
"""
@@ -203,9 +204,23 @@ async def create_session(self) -> ClientSession:
203204
transports = await self._exit_stack.enter_async_context(client)
204205
# The streamable http client returns a GetSessionCallback in addition to the read/write MemoryObjectStreams
205206
# needed to build the ClientSession, we limit then to the two first values to be compatible with all clients.
206-
session = await self._exit_stack.enter_async_context(
207-
ClientSession(*transports[:2])
208-
)
207+
# The StdioServerParameters does not provide a timeout parameter for the
208+
# session, so we need to set a default timeout for it. Other clients
209+
# (SseServerParams and StreamableHTTPServerParams) already provide a
210+
# timeout parameter in their configuration.
211+
if isinstance(self._connection_params, StdioServerParameters):
212+
# Default timeout for MCP session is 5 seconds, same as SseServerParams
213+
# and StreamableHTTPServerParams.
214+
session = await self._exit_stack.enter_async_context(
215+
ClientSession(
216+
*transports[:2],
217+
read_timeout_seconds=timedelta(seconds=5),
218+
)
219+
)
220+
else:
221+
session = await self._exit_stack.enter_async_context(
222+
ClientSession(*transports[:2])
223+
)
209224
await session.initialize()
210225

211226
self._session = session

src/google/adk/tools/mcp_tool/mcp_toolset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ def __init__(
9898
`StdioServerParameters` for using local mcp server (e.g. using `npx` or
9999
`python3`); or `SseServerParams` for a local/remote SSE server; or
100100
`StreamableHTTPServerParams` for local/remote Streamable http server.
101-
tool_filter: Optional filter to select specific tools. Can be either:
102-
- A list of tool names to include
103-
- A ToolPredicate function for custom filtering logic
104-
errlog: TextIO stream for error logging.
101+
tool_filter: Optional filter to select specific tools. Can be either: - A
102+
list of tool names to include - A ToolPredicate function for custom
103+
filtering logic
104+
errlog: TextIO stream for error logging.
105105
"""
106106
super().__init__(tool_filter=tool_filter)
107107

0 commit comments

Comments
 (0)
0