Repository navigation
Conversation
MCP defines `outputSchema` for client-side validation of structured results, but the Gemini path forwarded it verbatim as `FunctionDeclaration.response_json_schema` with no size limit. Gemini prices a schema as if every `$ref` were inlined, so proto-derived schemas (e.g. Google Cloud's logging/monitoring MCP servers) can push a single declaration past the model's input limit. Mirror the LiteLLM path: omit `response_json_schema` when the serialized schema exceeds the same 1024-character budget used there (`_MAX_TOOL_DESCRIPTION_LENGTH`), and log a warning. Schemas within the budget are sent unchanged. Fixes google#7690
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
6 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
Problem:
When
JSON_SCHEMA_FOR_FUNC_DECLis on (the default),McpTool._build_declaration()forwards the MCP server'soutputSchemato Gemini verbatim asFunctionDeclaration.response_json_schema, with no size limit. Gemini counts a schema as if every$refwere inlined, so Google Cloud's proto-derived MCP servers (logging, monitoring, cloudtrace, clouderrorreporting) push an 18-tool request past the 1,048,576-token input limit — those servers cannot be used with ADK as-is.Solution:
Mirror the existing LiteLLM path:
McpTool._build_declaration()now measures the serialized output schema and omitsresponse_json_schemawhen it exceeds the same 1024-character budget the LiteLLM path already uses (_MAX_TOOL_DESCRIPTION_LENGTH). The drop is logged at warning level.FunctionDeclaration#2828 use case (the model describing a small tool's result)._to_gemini_schemapath never sent output schemas.I deliberately kept this to the size cap (the smallest change that unblocks the reported servers). A per-toolset opt-out can follow as a separate change if maintainers would like one.
Testing Plan
Unit Tests:
Added to
TestMCPToolWithJsonSchemaintests/unittests/tools/mcp_tool/test_mcp_tool.py:test_get_declaration_drops_oversized_output_schema— a ~2.4k-character output schema is omitted from the declaration whileparameters_json_schemais preserved.test_get_declaration_logs_when_dropping_oversized_output_schema— the drop is logged at warning level.test_get_declaration_keeps_output_schema_within_budget— a within-budget schema is still sent.pytest tests/unittests/tools/mcp_tool/ -q→ 453 passed, 1 skipped, 0 failed (the full mcp_tool test directory on Python 3.12 / Windows, package installed editable from this branch; includes the 3 new tests).pyink --checkpasses for both touched files with the repo's pyink 25.12 config.Manual End-to-End (E2E) Tests:
Not run against the Gemini API directly (needs a Vertex AI project). The repro in #7690 verifies the fix end to end: with this change,
_get_declaration()on the Google Cloud logging/monitoring MCP servers returnsresponse_json_schema=Nonefor every tool whose serialized schema exceeds 1024 characters (all of the >7k-character entries in the issue's table), so the 18-toolgenerate_contentrequest stays well under the input limit.Checklist
Additional context
The 1024-character budget intentionally matches the LiteLLM path's existing
_MAX_TOOL_DESCRIPTION_LENGTH, so both model paths now apply the same policy to MCP output schemas. Formatting verified with the repo'spyink==25.12config (line-length 80, 2-space indentation).