From 71f39c87c36d7c59fbc36830f32e59322f7c2d9d Mon Sep 17 00:00:00 2001 From: pragnyanramtha Date: Sun, 17 May 2026 01:05:34 +0000 Subject: [PATCH] fix: handle boolean MCP schema leaves --- google/genai/_mcp_utils.py | 8 ++-- .../tests/mcp/test_mcp_to_gemini_tools.py | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/google/genai/_mcp_utils.py b/google/genai/_mcp_utils.py index 3d4ae2a17..97e48580f 100644 --- a/google/genai/_mcp_utils.py +++ b/google/genai/_mcp_utils.py @@ -24,7 +24,6 @@ import google.auth from google.auth.transport.requests import Request -from . import _common from . import types from ._api_client import _MULTI_REGIONAL_LOCATIONS @@ -124,9 +123,12 @@ def set_mcp_usage_header(headers: dict[str, str]) -> None: def _filter_to_supported_schema( - schema: _common.StringDict, -) -> _common.StringDict: + schema: Any, +) -> Any: """Filters the schema to only include fields that are supported by JSONSchema.""" + if not isinstance(schema, dict): + return schema + supported_fields: set[str] = set(types.JSONSchema.model_fields.keys()) supported_fields.update([ diff --git a/google/genai/tests/mcp/test_mcp_to_gemini_tools.py b/google/genai/tests/mcp/test_mcp_to_gemini_tools.py index 61d64102c..de8d5b127 100644 --- a/google/genai/tests/mcp/test_mcp_to_gemini_tools.py +++ b/google/genai/tests/mcp/test_mcp_to_gemini_tools.py @@ -198,6 +198,45 @@ def test_properties_conversion(): ] +def test_additional_properties_boolean_conversion(): + """Test conversion of MCP tools with boolean additionalProperties.""" + mcp_tools = [ + mcp_types.Tool( + name='tool', + description='tool-description', + inputSchema={ + 'type': 'object', + 'properties': { + 'key1': { + 'type': 'object', + 'additionalProperties': False, + }, + }, + 'additionalProperties': False, + }, + ), + ] + + result = _mcp_utils.mcp_to_gemini_tools(mcp_tools) + + assert result == [ + types.Tool( + function_declarations=[ + types.FunctionDeclaration( + name='tool', + description='tool-description', + parameters=types.Schema( + type='OBJECT', + properties={ + 'key1': types.Schema(type='OBJECT'), + }, + ), + ), + ], + ), + ] + + def test_defs_conversion(): """Test conversion of MCP tools with shared definitions ($defs).""" mcp_tools = [