Skip to content

feat: operation input to MCP compatible json schema converter - #1124

Merged
StarpTech merged 16 commits into
masterfrom
dustin/eng-6881-mcp-support-in-the-router
Apr 17, 2025
Merged

feat: operation input to MCP compatible json schema converter#1124
StarpTech merged 16 commits into
masterfrom
dustin/eng-6881-mcp-support-in-the-router

Conversation

@StarpTech

@StarpTech StarpTech commented Apr 14, 2025

Copy link
Copy Markdown
Contributor

This pull request introduces a new jsonschema package to handle JSON Schema definitions in the v2/pkg/engine/jsonschema/schema.go file. 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 $def and working with a fixed recursion depth.

The key changes include defining a JsonSchema struct, custom JSON serialization, and various helper functions to create and manipulate schema objects.

Key changes:

JSON Schema Definition

  • Introduced the JsonSchema struct to represent a JSON Schema definition, including fields for core schema properties, array-specific fields, enum values, and additional validation.

Custom Serialization

  • Added a MarshalJSON method to the JsonSchema struct to customize JSON serialization and omit empty fields.

Helper Functions

  • Created helper functions to generate new schemas for different types (NewObjectSchema, NewArraySchema, NewStringSchema, NewIntegerSchema, NewNumberSchema, NewBooleanSchema, NewEnumSchema).
  • Added methods to the JsonSchema struct to add descriptions, default values, formats, and nullable flags (WithDescription, WithDefault, WithFormat, WithNullable).

Cloning Functionality

  • Implemented a CloneSchema function to create deep copies of existing schema objects.

@StarpTech StarpTech changed the title Dustin/eng 6881 mcp support in the router feat: operation input to MCP compatible json schema converter Apr 14, 2025
@StarpTech
StarpTech marked this pull request as ready for review April 15, 2025 21:03
Comment thread v2/pkg/engine/jsonschema/schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/schema.go
Comment thread v2/pkg/engine/jsonschema/schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema.go
Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema_test.go Outdated
@StarpTech
StarpTech requested a review from jensneuse April 16, 2025 10:07
@StarpTech
StarpTech requested a review from Noroth April 17, 2025 11:38

@Noroth Noroth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nits

Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema.go
Comment thread v2/pkg/engine/jsonschema/variables_schema.go Outdated
Comment thread v2/pkg/engine/jsonschema/variables_schema_test.go
@StarpTech
StarpTech requested a review from jensneuse April 17, 2025 16:14
@StarpTech
StarpTech merged commit 48cc99b into master Apr 17, 2025
@StarpTech
StarpTech deleted the dustin/eng-6881-mcp-support-in-the-router branch April 17, 2025 17:38
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).
@StarpTech
StarpTech restored the dustin/eng-6881-mcp-support-in-the-router branch April 17, 2025 19:38
@ysmolski
ysmolski deleted the dustin/eng-6881-mcp-support-in-the-router branch May 15, 2026 16:36
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants