mcp: validate tool output (#352)

Validate the handler's returned output against the output schema of a tool.

Fixes #301.
This commit is contained in:
Jonathan Amsterdam 2025-09-03 16:25:48 -04:00 • committed by GitHub
commit 8314ec099b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -247,8 +247,12 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan
}, nil
}
// TODO(v0.3.0): Validate out.
_ = outputResolved
// Validate output schema, if any.
// Skip if out is nil: we've removed "null" from the output schema, so nil won't validate.
if v := reflect.ValueOf(out); v.Kind() == reflect.Pointer && v.IsNil() {
} else if err := validateSchema(outputResolved, &out); err != nil {
return nil, fmt.Errorf("tool output: %w", err)
}
if res == nil {
res = &CallToolResult{}