Skip to content

Commit e46eb05

Browse files
sasha-gitgcopybara-github
authored andcommitted
fix: fix imports if mcp is not installed
PiperOrigin-RevId: 760789448
1 parent 032d1fe commit e46eb05

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

google/genai/_adapters.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,40 @@
1313
# limitations under the License.
1414
#
1515

16+
import typing
17+
1618
from ._mcp_utils import mcp_to_gemini_tools
1719
from .types import FunctionCall, Tool
1820

19-
try:
20-
from mcp import ClientSession
21+
if typing.TYPE_CHECKING:
2122
from mcp import types as mcp_types
22-
except ImportError as e:
23-
import sys
24-
25-
if sys.version_info < (3, 10):
26-
raise ImportError(
27-
"MCP Tool requires Python 3.10 or above. Please upgrade your Python"
28-
" version."
29-
) from e
30-
else:
31-
raise e
23+
from mcp import ClientSession
3224

3325

3426
class McpToGenAiToolAdapter:
3527
"""Adapter for working with MCP tools in a GenAI client."""
3628

3729
def __init__(
38-
self, session: ClientSession, list_tools_result: mcp_types.ListToolsResult
30+
self,
31+
session: "mcp.ClientSession", # type: ignore # noqa: F821
32+
list_tools_result: "mcp_types.ListToolsResult", # type: ignore
3933
) -> None:
4034
self._mcp_session = session
4135
self._list_tools_result = list_tools_result
4236

4337
async def call_tool(
4438
self, function_call: FunctionCall
45-
) -> mcp_types.CallToolResult:
39+
) -> "mcp_types.CallToolResult": # type: ignore
4640
"""Calls a function on the MCP server."""
4741
name = function_call.name if function_call.name else ""
4842
arguments = dict(function_call.args) if function_call.args else {}
49-
return await self._mcp_session.call_tool(
50-
name=name,
51-
arguments=arguments,
43+
44+
return typing.cast(
45+
"mcp_types.CallToolResult",
46+
await self._mcp_session.call_tool(
47+
name=name,
48+
arguments=arguments,
49+
),
5250
)
5351

5452
@property

google/genai/_extra_utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import pydantic
2525

2626
from . import _common
27+
from . import _mcp_utils
2728
from . import errors
2829
from . import types
30+
from ._adapters import McpToGenAiToolAdapter
31+
2932

3033
if sys.version_info >= (3, 10):
3134
from types import UnionType
@@ -35,20 +38,15 @@
3538
if typing.TYPE_CHECKING:
3639
from mcp import ClientSession as McpClientSession
3740
from mcp.types import Tool as McpTool
38-
from ._adapters import McpToGenAiToolAdapter
39-
from . import _mcp_utils
4041
else:
4142
McpClientSession: typing.Type = Any
42-
McpToGenAiToolAdapter: typing.Type = Any
43+
McpTool: typing.Type = Any
4344
try:
4445
from mcp import ClientSession as McpClientSession
4546
from mcp.types import Tool as McpTool
46-
from ._adapters import McpToGenAiToolAdapter
47-
from . import _mcp_utils
4847
except ImportError:
4948
McpClientSession = None
5049
McpTool = None
51-
McpToGenAiToolAdapter = None
5250

5351
_DEFAULT_MAX_REMOTE_CALLS_AFC = 10
5452

google/genai/_mcp_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from importlib.metadata import PackageNotFoundError, version
1919
import typing
20-
from typing import Any, Union
20+
from typing import Any
2121

2222
from . import types
2323

0 commit comments

Comments
 (0)