Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion mcp/struct_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func schemaForStructFields(t reflect.Type, opts *jsonschema.ForOptions) (*jsonsc
continue
}

fieldSchema, err := jsonschema.ForType(fieldType, opts)
fieldSchema, err := schemaForFieldType(fieldType, opts)
if err != nil {
if opts.IgnoreInvalidTypes {
continue
Expand All @@ -102,6 +102,37 @@ func schemaForStructFields(t reflect.Type, opts *jsonschema.ForOptions) (*jsonsc
return schema, nil
}

func schemaForFieldType(t reflect.Type, opts *jsonschema.ForOptions) (*jsonschema.Schema, error) {
schema, err := jsonschema.ForType(t, opts)
if err == nil || !isJSONSchemaTagOptionError(err) {
return schema, err
}

switch t.Kind() {
case reflect.Pointer:
return schemaForFieldType(t.Elem(), opts)
case reflect.Struct:
return schemaForStructFields(t, opts)
case reflect.Array, reflect.Slice:
items, itemErr := schemaForFieldType(t.Elem(), opts)
if itemErr != nil {
return nil, itemErr
}
return &jsonschema.Schema{Type: "array", Items: items}, nil
case reflect.Map:
if t.Key().Kind() != reflect.String {
return nil, err
}
values, valueErr := schemaForFieldType(t.Elem(), opts)
if valueErr != nil {
return nil, valueErr
}
return &jsonschema.Schema{Type: "object", AdditionalProperties: values}, nil
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
default:
return nil, err
}
}

func applyStructFieldTags(t reflect.Type, schema *jsonschema.Schema) {
if schema == nil || schema.Properties == nil {
return
Expand Down Expand Up @@ -146,12 +177,32 @@ func applyStructFieldTags(t reflect.Type, schema *jsonschema.Schema) {
if len(enums) > 0 {
prop.Enum = enums
}
applyStructFieldTagsToType(fieldType, prop)
}
}

walk(t)
}

func applyStructFieldTagsToType(t reflect.Type, schema *jsonschema.Schema) {
if schema == nil {
return
}

switch t.Kind() {
case reflect.Pointer:
applyStructFieldTagsToType(t.Elem(), schema)
case reflect.Struct:
applyStructFieldTags(t, schema)
case reflect.Array, reflect.Slice:
applyStructFieldTagsToType(t.Elem(), schema.Items)
case reflect.Map:
if t.Key().Kind() == reflect.String {
applyStructFieldTagsToType(t.Elem(), schema.AdditionalProperties)
}
}
}

func fieldSchemaAnnotations(field reflect.StructField) (string, []any) {
description := ""
var enums []any
Expand Down
46 changes: 46 additions & 0 deletions mcp/struct_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,52 @@ func TestSchemaFor_JSONSchemaEnumTagWithoutLeadingSpace(t *testing.T) {
assert.ElementsMatch(t, []any{"Alice", "Bob"}, nameProp["enum"])
}

func TestSchemaFor_NestedStructTags(t *testing.T) {
type mode struct {
Name string `json:"name" jsonschema_description:"Run mode" jsonschema:"enum=fast,enum=safe"`
}
type request struct {
Primary mode `json:"primary"`
Secondary mode `json:"secondary"`
Optional *mode `json:"optional"`
Modes []mode `json:"modes"`
ByName map[string]mode `json:"byName"`
}

raw, err := SchemaForRaw[request]()
require.NoError(t, err)

var schema map[string]any
require.NoError(t, json.Unmarshal(raw, &schema))

properties, ok := schema["properties"].(map[string]any)
require.True(t, ok)

assertModeSchema := func(t *testing.T, nested any) {
t.Helper()
nestedSchema, ok := nested.(map[string]any)
require.True(t, ok)
nestedProperties, ok := nestedSchema["properties"].(map[string]any)
require.True(t, ok)
modeName, ok := nestedProperties["name"].(map[string]any)
require.True(t, ok)
assert.Equal(t, "Run mode", modeName["description"])
assert.ElementsMatch(t, []any{"fast", "safe"}, modeName["enum"])
}

for _, name := range []string{"primary", "secondary", "optional"} {
assertModeSchema(t, properties[name])
}

modes, ok := properties["modes"].(map[string]any)
require.True(t, ok)
assertModeSchema(t, modes["items"])

byName, ok := properties["byName"].(map[string]any)
require.True(t, ok)
assertModeSchema(t, byName["additionalProperties"])
}

func TestSchemaFor_StructuredInputOutputExampleTags(t *testing.T) {
type WeatherRequest struct {
Location string `json:"location,required" jsonschema_description:"City or location"` //nolint:staticcheck // required is interpreted by schemaFor, not encoding/json
Expand Down