mcp: update to jsonschema that does not validate structs (#511)
Update to the latest jsonschema commit, which removes the ability to validate structs and changes the type of ForOptions.TypeSchemas.
This commit is contained in:
parent
ef2fc60b5c
commit
33ff851ef3
2 changed files with 107 additions and 45 deletions
|
|
@ -54,27 +54,72 @@ func (t *manualGreeter) greet(_ context.Context, req *mcp.CallToolRequest) (*mcp
|
|||
// Handle the parsing and validation of input and output.
|
||||
//
|
||||
// Note that errors here are treated as tool errors, not protocol errors.
|
||||
|
||||
// First, unmarshal to a map[string]any and validate.
|
||||
if err := unmarshalAndValidate(req.Params.Arguments, t.inputSchema); err != nil {
|
||||
return errf("invalid input: %v", err), nil
|
||||
}
|
||||
|
||||
// Now unmarshal again to input.
|
||||
var input Input
|
||||
if err := json.Unmarshal(req.Params.Arguments, &input); err != nil {
|
||||
return errf("failed to unmarshal arguments: %v", err), nil
|
||||
}
|
||||
if err := t.inputSchema.Validate(input); err != nil {
|
||||
return errf("invalid input: %v", err), nil
|
||||
}
|
||||
output := Output{Greeting: "Hi " + input.Name}
|
||||
if err := t.outputSchema.Validate(output); err != nil {
|
||||
return errf("tool produced invalid output: %v", err), nil
|
||||
}
|
||||
outputJSON, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
return errf("output failed to marshal: %v", err), nil
|
||||
}
|
||||
//
|
||||
if err := unmarshalAndValidate(outputJSON, t.outputSchema); err != nil {
|
||||
return errf("invalid output: %v", err), nil
|
||||
}
|
||||
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: string(outputJSON)}},
|
||||
StructuredContent: output,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// unmarshalAndValidate unmarshals data to a map[string]any, then validates that against res.
|
||||
func unmarshalAndValidate(data []byte, res *jsonschema.Resolved) error {
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
return res.Validate(m)
|
||||
}
|
||||
|
||||
var (
|
||||
inputSchema = &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"name": {Type: "string", MaxLength: jsonschema.Ptr(10)},
|
||||
},
|
||||
}
|
||||
outputSchema = &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"greeting": {Type: "string"},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func newManualGreeter() (*manualGreeter, error) {
|
||||
resIn, err := inputSchema.Resolve(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resOut, err := outputSchema.Resolve(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &manualGreeter{
|
||||
inputSchema: resIn,
|
||||
outputSchema: resOut,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "greeter"}, nil)
|
||||
|
||||
|
|
@ -90,27 +135,7 @@ func main() {
|
|||
//
|
||||
// We don't need to do all this work: below, we use jsonschema.For to start
|
||||
// from the default schema.
|
||||
var (
|
||||
manual manualGreeter
|
||||
err error
|
||||
)
|
||||
inputSchema := &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"name": {Type: "string", MaxLength: jsonschema.Ptr(10)},
|
||||
},
|
||||
}
|
||||
manual.inputSchema, err = inputSchema.Resolve(nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
outputSchema := &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"greeting": {Type: "string"},
|
||||
},
|
||||
}
|
||||
manual.outputSchema, err = outputSchema.Resolve(nil)
|
||||
manual, err := newManualGreeter()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
31
examples/server/toolschemas/main_test.go
Normal file
31
examples/server/toolschemas/main_test.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// 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 main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
func TestGreet(t *testing.T) {
|
||||
manual, err := newManualGreeter()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err := manual.greet(context.Background(), &mcp.CallToolRequest{
|
||||
Params: &mcp.CallToolParamsRaw{
|
||||
Arguments: json.RawMessage(`{"name": "Bob"}`),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.IsError {
|
||||
t.Fatalf("tool error: %q", res.Content[0].(*mcp.TextContent).Text)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue