feat: operation input to MCP compatible json schema converter - #1124
Merged
Conversation
jensneuse
requested changes
Apr 16, 2025
Noroth
reviewed
Apr 17, 2025
jensneuse
requested changes
Apr 17, 2025
Co-authored-by: Ludwig <ludwig.bedacht@gmail.com>
Noroth
approved these changes
Apr 17, 2025
jensneuse
approved these changes
Apr 17, 2025
StarpTech
added a commit
that referenced
this pull request
Apr 17, 2025
🤖 I have created a release *beep* *boop* --- ## [2.0.0-rc.170](v2.0.0-rc.169...v2.0.0-rc.170) (2025-04-17) ### Features * operation input to MCP compatible json schema converter ([#1124](#1124)) ([48cc99b](48cc99b)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
asoorm
added a commit
that referenced
this pull request
May 29, 2026
## Problem `BuildJsonSchema` inlines input types and truncates recursion at a fixed depth, dropping self-referential fields (e.g. `left`/`right`). With `additionalProperties: false`, valid nested payloads are then rejected: `additional properties 'left', 'right' not allowed`. The same input passes normal GraphQL; only the generated JSON Schema (MCP path) rejects it. Also fixes a tracker leak: the early `return nil` at the depth limit skipped cleanup, leaking the recursion counter across sibling uses of a type. ## Fix Emit `$ref`/`$defs` for recursive input types: a first pass finds self-/mutually-recursive types; each is emitted once under root `$defs` and referenced via `$ref`. Recursion terminates by construction and nesting works to any depth. Non-recursive types are unchanged (still inlined). `maxRecursionDepth` is kept on the exported signatures for compatibility but is now a no-op. ## Design note Revisits #1124, which deliberately dropped `$def` for LLM compatibility. That holds for non-recursive inputs (kept inline); for recursive inputs the only alternative to `$ref` is a schema that rejects valid data, so `$defs` is reintroduced for recursive types only. The validator (`santhosh-tekuri/jsonschema`) supports it. ## Tests - New `recursive_input_test.go`: validates a nested recursive payload against the generated schema using `santhosh-tekuri/jsonschema/v5`. - Updated the two tests that encoded the old truncation behaviour; non-recursive golden tests unchanged. Closes ENG-9631
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.
This pull request introduces a new
jsonschemapackage to handle JSON Schema definitions in thev2/pkg/engine/jsonschema/schema.gofile. The conversion logic was optimized for the use case to produce JSON schemas that are compatible with LLM (Large Language Models). This includes removing support for$defand working with a fixed recursion depth.The key changes include defining a
JsonSchemastruct, custom JSON serialization, and various helper functions to create and manipulate schema objects.Key changes:
JSON Schema Definition
JsonSchemastruct to represent a JSON Schema definition, including fields for core schema properties, array-specific fields, enum values, and additional validation.Custom Serialization
MarshalJSONmethod to theJsonSchemastruct to customize JSON serialization and omit empty fields.Helper Functions
NewObjectSchema,NewArraySchema,NewStringSchema,NewIntegerSchema,NewNumberSchema,NewBooleanSchema,NewEnumSchema).JsonSchemastruct to add descriptions, default values, formats, and nullable flags (WithDescription,WithDefault,WithFormat,WithNullable).Cloning Functionality
CloneSchemafunction to create deep copies of existing schema objects.