Skip to content

fix(tools): dereference draft-07 definitions in MCP tool schemas#5941

Open
gaoflow wants to merge 1 commit into
google:mainfrom
gaoflow:fix-5940-draft07-definitions
Open

fix(tools): dereference draft-07 definitions in MCP tool schemas#5941
gaoflow wants to merge 1 commit into
google:mainfrom
gaoflow:fix-5940-draft07-definitions

Conversation

@gaoflow
Copy link
Copy Markdown

@gaoflow gaoflow commented Jun 2, 2026

Summary

McpToolset crashes with KeyError: 'definitions' when an MCP server returns a tool inputSchema written in JSON Schema draft-07 — i.e. using definitions and $ref: "#/definitions/..." instead of the draft 2019-09+/2020-12 $defs. The MCP specification (2025-11-25) explicitly permits draft-07 schemas, so ADK should accept them.

Fixes #5940 (the same crash was previously reported in #3685 and closed as stale).

Root cause

_dereference_schema in src/google/adk/tools/_gemini_schema_util.py only looked up $refs in the $defs block and only stripped $defs after resolving:

defs = schema.get("$defs", {})
...
if "$defs" in dereferenced_schema:
    del dereferenced_schema["$defs"]

For a draft-07 schema there is no $defs, so defs is empty, every $ref: "#/definitions/..." is left unresolved, and the definitions block survives untouched. _to_gemini_schema then passes that leftover definitions keyword into Schema.from_json_schema(...), which raises KeyError: 'definitions'.

Fix

Resolve refs against both definitions (draft-07) and $defs (draft 2019-09+), and strip both blocks once resolved. $defs wins on the (pathological) key collision. Draft 2019+ schemas are unaffected because definitions is simply absent.

defs = {**schema.get("definitions", {}), **schema.get("$defs", {})}
...
for defs_keyword in ("$defs", "definitions"):
    if defs_keyword in dereferenced_schema:
        del dereferenced_schema[defs_keyword]

The $ref lookup itself already keys on the final path segment (ref_uri.split("/")[-1]), so #/definitions/Pet and #/$defs/Pet both resolve to Pet without further changes.

Testing

Added test_to_gemini_schema_draft_07_definitions_and_ref, mirroring the existing $defs test but using draft-07 definitions + $ref: "#/definitions/...". It fails on main with KeyError: 'definitions' and passes with this change. The full tests/unittests/tools/test_gemini_schema_util.py suite (now 69 tests) passes, confirming no regression for the $defs / circular-ref / reused-ref cases.

Thanks to @rrazvd for the reproduction and definitions$defs workaround in the issue thread.

`_dereference_schema` only resolved `$ref`s against `$defs` (JSON Schema
draft 2019-09+/2020-12) and only stripped the `$defs` block afterwards. A
tool whose `inputSchema` uses JSON Schema draft-07 — `definitions` plus
`$ref: "#/definitions/..."`, which the MCP specification explicitly
permits — was therefore left with its refs unresolved and the
`definitions` block intact, so `_to_gemini_schema` raised
`KeyError: 'definitions'` while building the Gemini schema.

Resolve refs against both `definitions` and `$defs` (the latter wins on a
key collision) and strip both blocks once resolved.

Fixes google#5940.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Jun 2, 2026

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.

@adk-bot adk-bot added mcp [Component] Issues about MCP support tools [Component] This issue is related to tools labels Jun 2, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Jun 2, 2026

Response from ADK Triaging Agent

Hello @gaoflow, thank you for submitting this pull request!

It looks like the Contributor License Agreement (CLA) check has failed for this PR. According to our Contribution Guidelines, all contributions to this project must be accompanied by a signed CLA before they can be reviewed and accepted.

Could you please visit https://cla.developers.google.com/ to sign the agreement?

Signing the CLA is necessary to give us permission to use and redistribute your contributions as part of the project.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mcp [Component] Issues about MCP support tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_dereference_schema fails with KeyError 'definitions' on JSON Schema draft-07 tools — violates MCP spec

2 participants