From da0766dc67ef55705a44a7cd8a78e8e50435a6e3 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Mon, 8 Jun 2026 02:13:37 +0800 Subject: [PATCH] fix: handle boolean MCP schema leaves --- google/genai/_mcp_utils.py | 8 ++++-- .../tests/mcp/test_mcp_to_gemini_tools.py | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/google/genai/_mcp_utils.py b/google/genai/_mcp_utils.py index cc7a1642b..8b17da4b9 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..37d4eff2e 100644 --- a/google/genai/tests/mcp/test_mcp_to_gemini_tools.py +++ b/google/genai/tests/mcp/test_mcp_to_gemini_tools.py @@ -285,6 +285,34 @@ def test_update_endpoint_labels_conversion(): assert 'additionalProperties' in labels_schema +def test_mcp_boolean_additional_properties_conversion(): + """Test MCP schemas with boolean additionalProperties values.""" + mcp_tools = [ + mcp_types.Tool( + name='tool', + description='tool-description', + inputSchema={ + 'type': 'object', + 'properties': { + 'payload': { + 'type': 'object', + 'additionalProperties': False, + } + }, + }, + ), + ] + + result = _mcp_utils.mcp_to_gemini_tools(mcp_tools) + payload_schema = ( + result[0] + .function_declarations[0] + .parameters.properties['payload'] + ) + + assert payload_schema.type == 'OBJECT' + + def test_agent_platform_preserves_unknown_fields(): """Test that Agent Platform translation passes all schema fields directly to the backend.