Skip to content

Commit 9744f55

Browse files
committed
fix toolschemas
1 parent a205a5a commit 9744f55

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

examples/server/toolschemas/main.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,35 @@ func (t *manualGreeter) greet(_ context.Context, req *mcp.CallToolRequest) (*mcp
5454
// Handle the parsing and validation of input and output.
5555
//
5656
// Note that errors here are treated as tool errors, not protocol errors.
57+
58+
// First, unmarshal to a map[string]any and validate.
59+
if err := unmarshalAndValidate(req.Params.Arguments, t.inputSchema); err != nil {
60+
return errf("invalid input: %v", err), nil
61+
}
62+
63+
// Now unmarshal again to input.
5764
var input Input
5865
if err := json.Unmarshal(req.Params.Arguments, &input); err != nil {
5966
return errf("failed to unmarshal arguments: %v", err), nil
6067
}
61-
if err := validateStruct(input, t.inputSchema); err != nil {
62-
return errf("invalid input: %v", err), nil
63-
}
6468
output := Output{Greeting: "Hi " + input.Name}
65-
if err := validateStruct(output, t.outputSchema); err != nil {
66-
return errf("tool produced invalid output: %v", err), nil
67-
}
6869
outputJSON, err := json.Marshal(output)
6970
if err != nil {
7071
return errf("output failed to marshal: %v", err), nil
7172
}
73+
//
74+
if err := unmarshalAndValidate(outputJSON, t.outputSchema); err != nil {
75+
return errf("invalid output: %v", err), nil
76+
}
77+
7278
return &mcp.CallToolResult{
7379
Content: []mcp.Content{&mcp.TextContent{Text: string(outputJSON)}},
7480
StructuredContent: output,
7581
}, nil
7682
}
7783

78-
// validateStruct validates x against schema by first changing the struct to
79-
// a map[string]any, then validating that.
80-
func validateStruct(x any, res *jsonschema.Resolved) error {
81-
data, err := json.Marshal(x)
82-
if err != nil {
83-
return err
84-
}
84+
// unmarshalAndValidate unmarshals data to a map[string]any, then validates that against res.
85+
func unmarshalAndValidate(data []byte, res *jsonschema.Resolved) error {
8586
var m map[string]any
8687
if err := json.Unmarshal(data, &m); err != nil {
8788
return err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23.0
55
require (
66
github.com/golang-jwt/jwt/v5 v5.2.2
77
github.com/google/go-cmp v0.7.0
8-
github.com/google/jsonschema-go v0.2.4-0.20250922144851-e08864c65371
8+
github.com/google/jsonschema-go v0.3.0
99
github.com/yosida95/uritemplate/v3 v3.0.2
1010
golang.org/x/tools v0.34.0
1111
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/google/jsonschema-go v0.2.3 h1:dkP3B96OtZKKFvdrUSaDkL+YDx8Uw9uC4Y+euk
66
github.com/google/jsonschema-go v0.2.3/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
77
github.com/google/jsonschema-go v0.2.4-0.20250922144851-e08864c65371 h1:e1VCqWtKpTYBOBhPcgGV5whTlMFpTbH5Ghm56wpxBsk=
88
github.com/google/jsonschema-go v0.2.4-0.20250922144851-e08864c65371/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
9+
github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
10+
github.com/google/jsonschema-go v0.3.0/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
911
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
1012
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
1113
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=

0 commit comments

Comments
 (0)