Repository navigation
_dereference_schema fails with KeyError 'definitions' on JSON Schema draft-07 tools β violates MCP specΒ #5940
Description
π΄ Required Information
Describe the Bug:
When using McpToolset with an MCP server that returns tool inputSchema using JSON Schema draft-07 (i.e., definitions + $ref: "#/definitions/..." instead of $defs), the ADK crashes with KeyError: 'definitions'.
This is a spec-compliance bug: the MCP specification (2025-11-25), section 5.3.2 explicitly supports JSON Schema draft-07 in tool definitions β meaning any MCP server sending "$schema": "http://json-schema.org/draft-07/schema#" is fully spec-compliant and ADK must handle it.
This was previously reported in #3685 and closed as stale without a fix.
Steps to Reproduce:
- Install
google-adk(any recent version) - Connect
McpToolsetto any MCP server whose tools use draft-07 schemas withdefinitions(e.g., Apollo MCP Server) - Run the agent and send a message that triggers a call to one of those tools
- Observe the crash with the stacktrace below
Expected Behavior:
ADK should handle both $defs (draft 2019+/2020-12) and definitions (draft-07) when dereferencing schemas, since both are valid per the MCP spec.
Observed Behavior:
File ".../google/adk/tools/mcp_tool/mcp_tool.py", line 119, in _get_declaration
parameters = _to_gemini_schema(input_schema)
File ".../google/adk/tools/_gemini_schema_util.py", line 207, in _to_gemini_schema
return Schema.from_json_schema(
File ".../google/genai/types.py", line 2720, in from_json_schema
return convert_json_schema(...)
File ".../google/genai/types.py", line 2492, in _resolve_ref
current = current[part]
KeyError: 'definitions'
Root cause: _dereference_schema in _gemini_schema_util.py only reads $defs:
defs = schema.get("$defs", {}) # ignores `definitions` entirely_sanitize_schema_formats_for_gemini then silently strips the definitions block, leaving dangling $ref pointers that crash Schema.from_json_schema.
Model Information:
- Are you using LiteLLM: No
- Which model is being used: gemini-2.5-flash
π‘ Optional Information
Regression:
Unknown β definitions support was likely never implemented, as the bug appears to be a missing code path rather than a regression.
Logs:
File ".../google/adk/flows/llm_flows/base_llm_flow.py", line 579, in _preprocess_async
await tool.process_llm_request(
tool_context=tool_context, llm_request=llm_request
)
File ".../google/adk/tools/base_tool.py", line 129, in process_llm_request
llm_request.append_tools([self])
File ".../google/adk/models/llm_request.py", line 255, in append_tools
declaration = tool._get_declaration()
File ".../google/adk/tools/mcp_tool/mcp_tool.py", line 119, in _get_declaration
parameters = _to_gemini_schema(input_schema)
File ".../google/adk/tools/_gemini_schema_util.py", line 207, in _to_gemini_schema
return Schema.from_json_schema(
File ".../google/genai/types.py", line 2492, in _resolve_ref
current = current[part]
KeyError: 'definitions'
Additional Context:
The MCP spec section 5.3.2 explicitly provides a "Tool with explicit draft-07 schema" example, and states:
"Defaults to 2020-12 if no
$schemafield is present"
This means draft-07 is a first-class citizen of the spec. The previous issue #3685 was closed suggesting the problem was on the server side, but that is incorrect β the server is spec-compliant.
Proposed fix β a one-line change in _dereference_schema:
# Before
defs = schema.get("$defs", {})
# After
defs = schema.get("$defs") or schema.get("definitions") or {}Minimal Reproduction Code:
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
from google.adk.agents.llm_agent import LlmAgent
# Any MCP server that returns draft-07 schemas with `definitions`
toolset = McpToolset(
connection_params=StreamableHTTPConnectionParams(
url="http://your-draft07-mcp-server/mcp"
)
)
agent = LlmAgent(
model="gemini-2.5-flash",
name="TestAgent",
instruction="Use the available tools.",
tools=[toolset]
)
# Invoking any tool from this server will crash with KeyError: 'definitions'References:
- Closed issue (no fix applied): JSON Schema Compatibility IssueΒ #3685
- MCP Spec β Tools, section 5.3.2: https://modelcontextprotocol.io/specification/2025-11-25/server/tools#schema-examples
How often has this issue occurred?
- Always (100%)
Activity
rrazvd commentedon Jun 2, 2026
While waiting for an official fix, I'm working around this issue by monkey-patching
_to_gemini_schemaat application startup to normalizedefinitionsβ$defsbefore ADK processes the schema:This must be applied before any
McpToolsetorLlmAgentis instantiated. It works, but it's obviously fragile β it breaks if ADK refactors the internal module structure and shouldn't be necessary given the MCP spec explicitly supports draft-07.fix(tools): dereference draft-07 `definitions` in MCP tool schemas
xuanyang15 commentedon Jun 9, 2026
#5941 merged