Skip to content

Conversation

@gambtho
Copy link
Contributor

@gambtho gambtho commented Jun 26, 2025

Problem

When generating JSON schemas for Go structs containing slice or array fields, the schema generator was producing invalid JSON Schema that violated the specification. Array types were missing the required items property, causing downstream validators (notably GitHub Copilot) to reject the schemas with:

Error: tool parameters array type must have items

Solution

Updated the schema generation logic in util/schema/schema.go to properly handle array and slice types by:

  1. Adding an Items field to the PropertyDetail struct to support nested schema definitions
  2. Detecting array/slice types during schema generation and recursively generating the appropriate items schema
  3. Supporting nested arrays (e.g., [][]float64) with proper nested items definitions

Changes

  • Modified util/schema/schema.go:

    • Added Items *PropertyDetail field to PropertyDetail struct
    • Enhanced FromStruct() to generate items schemas for array/slice types
    • Handles primitive arrays, object arrays, and nested arrays
  • Added tests in util/schema/schema_test.go:

    • TestArraySchemaGeneratesItems: Verifies various array types generate correct items
    • TestArraySchemaValidation: Ensures generated schemas work correctly

Example

Before this fix:

{
  "some_slice": {
    "type": "array",
    "description": "List of strings"
    // Missing "items" property!
  }
}

After this fix:

{
  "some_slice": {
    "type": "array",
    "description": "List of strings",
    "items": {
      "type": "string"
    }
  }
}

Testing

All tests pass:

go test ./util/schema/

The fix handles:

  • Simple arrays: []string{"type": "array", "items": {"type": "string"}}
  • Numeric arrays: []int{"type": "array", "items": {"type": "integer"}}
  • Nested arrays: [][]float64{"type": "array", "items": {"type": "array", "items": {"type": "number"}}}
  • Object arrays: []CustomType{"type": "array", "items": {"type": "object"}}

Impact

This fix ensures that all GoMCP-generated schemas are valid according to the JSON Schema specification, enabling proper integration with tools that validate schemas strictly (like GitHub Copilot, OpenAPI validators, etc.).

@gambtho
Copy link
Contributor Author

gambtho commented Jun 26, 2025

closes #9

MaxLength *int `json:"maxLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
Default interface{} `json:"default,omitempty"`
Type string `json:"type"`

Choose a reason for hiding this comment

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

💡Observation so looks fine: Cool, this all seems formatting only

@localrivet
Copy link
Owner

Awesome! Thanks for the addition and fix.

@localrivet localrivet merged commit 0f0f96a into localrivet:main Jul 7, 2025
2 of 14 checks passed
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