Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/google/adk/tools/mcp_tool/session_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from contextlib import AbstractAsyncContextManager
from contextlib import AsyncExitStack
from datetime import timedelta
from importlib.metadata import version
import logging
from types import TracebackType
from typing import Any
Expand All @@ -36,8 +37,10 @@

_T = TypeVar('_T')

_MCP_MAJOR = int(version('mcp').split('.', 1)[0])

def _read_timeout(seconds: Optional[float]) -> Optional[timedelta]:

def _read_timeout(seconds: Optional[float]) -> Optional[timedelta | float]:
"""Converts a timeout in seconds to the type ``ClientSession`` expects.

ADK carries every timeout as float seconds. MCP SDK 1.x wants a
Expand All @@ -52,6 +55,8 @@ def _read_timeout(seconds: Optional[float]) -> Optional[timedelta]:
"""
if seconds is None:
return None
if _MCP_MAJOR >= 2:
return seconds
return timedelta(seconds=seconds)


Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/tools/mcp_tool/test_session_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from google.adk.features import FeatureName
from google.adk.features._feature_registry import temporary_feature_override
from google.adk.tools.mcp_tool import session_context
from google.adk.tools.mcp_tool.session_context import _format_exception
from google.adk.tools.mcp_tool.session_context import _read_timeout
from google.adk.tools.mcp_tool.session_context import SessionContext
Expand Down Expand Up @@ -1023,3 +1024,14 @@ def test_zero_is_a_real_timeout_not_a_missing_one(self):

def test_fractional_seconds_survive(self):
assert _read_timeout(0.5) == timedelta(seconds=0.5)

def test_mcp_2_uses_float_seconds(self, monkeypatch):
monkeypatch.setattr(session_context, '_MCP_MAJOR', 2)

assert _read_timeout(30) == 30
assert _read_timeout(0.5) == 0.5

def test_mcp_1_uses_timedelta(self, monkeypatch):
monkeypatch.setattr(session_context, '_MCP_MAJOR', 1)

assert _read_timeout(30) == timedelta(seconds=30)