internal/mcp/internal/jsonschema: schema definition
The first of several CLs providing JSON Schema validation. Replace the existing Schema type with one that has all the keywords of the most current version of the JSON Schema spec, called 2020-12. The 2020-12 spec can be represented type-safely in Go with one exception: the "type" keyword can be either a string or a []string. To handle this safely, both Type and Types fields are defined. Custom JSONMarshal/Unmarshal methods handle the conversion. We need the unmarshal anyway because "true" and "false" are valid schemas. Unfortunately, the MCP spec uses the older "draft7" version of JSON Schema, but the two are close enough for code generation to work. I confirmed that the protocol generator produces the identical output. Change-Id: I44f6ea2f3a3f8498979946f69dfb7f50afa1acad Reviewed-on: https://go-review.googlesource.com/c/tools/+/667855 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
14b18b2645
commit
3434e57007
1 changed files with 606 additions and 169 deletions
70
internal/jsonschema/infer_test.go
Normal file
70
internal/jsonschema/infer_test.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package jsonschema_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/jsonschema"
|
||||
)
|
||||
|
||||
func forType[T any]() *jsonschema.Schema {
|
||||
s, err := jsonschema.For[T]()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestForType(t *testing.T) {
|
||||
type schema = jsonschema.Schema
|
||||
tests := []struct {
|
||||
name string
|
||||
got *jsonschema.Schema
|
||||
want *jsonschema.Schema
|
||||
}{
|
||||
{"string", forType[string](), &schema{Type: "string"}},
|
||||
{"int", forType[int](), &schema{Type: "integer"}},
|
||||
{"int16", forType[int16](), &schema{Type: "integer"}},
|
||||
{"uint32", forType[int16](), &schema{Type: "integer"}},
|
||||
{"float64", forType[float64](), &schema{Type: "number"}},
|
||||
{"bool", forType[bool](), &schema{Type: "boolean"}},
|
||||
{"intmap", forType[map[string]int](), &schema{
|
||||
Type: "object",
|
||||
AdditionalProperties: &schema{Type: "integer"},
|
||||
}},
|
||||
{"anymap", forType[map[string]any](), &schema{
|
||||
Type: "object",
|
||||
AdditionalProperties: &schema{},
|
||||
}},
|
||||
{"struct", forType[struct {
|
||||
F int `json:"f"`
|
||||
G []float64
|
||||
P *bool
|
||||
Skip string `json:"-"`
|
||||
NoSkip string `json:",omitempty"`
|
||||
unexported float64
|
||||
unexported2 int `json:"No"`
|
||||
}](), &schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*schema{
|
||||
"f": {Type: "integer"},
|
||||
"G": {Type: "array", Items: &schema{Type: "number"}},
|
||||
"P": {Type: "boolean"},
|
||||
"NoSkip": {Type: "string"},
|
||||
},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if diff := cmp.Diff(test.want, test.got); diff != "" {
|
||||
t.Errorf("ForType mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue