Skip to content

Eager mcp import in _mcp_utils.py adds ~240ms to every import google.genai #2325

@GWeale

Description

@GWeale

Problem

google/genai/_mcp_utils.py:31-36 (and the same pattern in types.py, _extra_utils.py, _transformers.py, live.py) eagerly imports mcp at module load whenever it's installed. Adds ~240ms cold start to every import google.genai, even for users who never touch MCP.

Repro

pip install google-genai mcp
python -X importtime -c "import google.genai" 2>&1 | grep -c "^import time:.*mcp"
# 60+ mcp.* modules loaded

Fix

Drop the eager import; keep McpTool/McpClientSession typed as Any and do isinstance checks behind if 'mcp' in sys.modules: in the few methods that actually need them. Same pattern works for the other 4 files.
Happy to send a PR if the approach sounds right.

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions