fix(core): resolve $ref to $defs defined in nested property schemas#7456
Open
Krishnachaitanyakc wants to merge 1 commit intomicrosoft:mainfrom
Open
fix(core): resolve $ref to $defs defined in nested property schemas#7456Krishnachaitanyakc wants to merge 1 commit intomicrosoft:mainfrom
Krishnachaitanyakc wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When a JSON Schema property defines its own $defs (e.g., an MCP tool schema where a nested parameter object contains enum definitions), schema_to_pydantic_model fails with ReferenceNotFoundError because _json_schema_to_model only processes $defs at the root schema level. This fix: - Merges nested $defs into the root schema's $defs so _resolve_ref can locate them - Caches object-type nested definitions as models in _model_cache - Resolves non-model definitions (enums, primitives) inline when encountered via $ref, avoiding the creation of empty BaseModels Fixes microsoft#7129
Author
|
@ekzhu can you please review these changes? |
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.
Summary
Fixes #7129
When a JSON Schema property defines its own
$defs(e.g., an MCP tool schema where a nested parameter object contains enum definitions),schema_to_pydantic_modelfails withReferenceNotFoundErrorbecause_json_schema_to_modelonly processes$defsat the root schema level.This is triggered when using
mcp_server_tools()with MCP servers whose tool input schemas contain nested$defs-- for example,mcp-server-everything-searchdefines aWindowsSortOptionenum inside thewindows_paramsproperty's$defs.Error before fix:
Changes
_json_schema_to_model, detect and merge nested$defsinto the root schema's$defsso_resolve_refcan locate them_model_cache$refin properties, avoiding the incorrect creation of empty BaseModels for non-object types$defs(exact reproduction of When using mcp_server_tools, an error occurs, but it works normally with McpWorkbench. #7129), nested object$defs, and multiple nested$defsacross different propertiesTest plan
test_nested_defs_enum_ref-- reproduces the exact schema from issue When using mcp_server_tools, an error occurs, but it works normally with McpWorkbench. #7129test_nested_defs_object_ref-- nested$defswith object-type definitionstest_multiple_nested_defs-- multiple properties each with their own$defstest_json_to_pydantic.pycontinue to pass (top-level$defs, self-referencing, allOf merging, etc.)