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
2 changed files with 606 additions and 169 deletions
|
|
@ -92,14 +92,14 @@ func TestEndToEnd(t *testing.T) {
|
|||
Properties: map[string]*jsonschema.Schema{
|
||||
"Name": {Type: "string"},
|
||||
},
|
||||
AdditionalProperties: false,
|
||||
AdditionalProperties: falseSchema,
|
||||
},
|
||||
}, {
|
||||
Name: "fail",
|
||||
Description: "just fail",
|
||||
InputSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
AdditionalProperties: false,
|
||||
AdditionalProperties: falseSchema,
|
||||
},
|
||||
}}
|
||||
if diff := cmp.Diff(wantTools, gotTools); diff != "" {
|
||||
|
|
@ -221,7 +221,6 @@ func TestBatching(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestCancellation(t *testing.T) {
|
||||
|
|
@ -253,3 +252,5 @@ func TestCancellation(t *testing.T) {
|
|||
t.Fatal("timeout waiting for cancellation")
|
||||
}
|
||||
}
|
||||
|
||||
var falseSchema = &jsonschema.Schema{Not: &jsonschema.Schema{}}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ type Tool struct {
|
|||
// a valid schema, as documented by [jsonschema.ForType]; otherwise, MakeTool
|
||||
// panics.
|
||||
func MakeTool[TReq any](name, description string, handler func(context.Context, *ClientConnection, TReq) ([]Content, error)) *Tool {
|
||||
schema, err := jsonschema.ForType[TReq]()
|
||||
schema, err := jsonschema.For[TReq]()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue