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:
parent
d0c5943f25
commit
8314ec099b
1 changed files with 35 additions and 7 deletions
14
mcp/tool.go
14
mcp/tool.go
|
|
@ -9,6 +9,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
// "log"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
)
|
||||
|
|
@ -42,13 +43,16 @@ func unmarshalSchema(data json.RawMessage, resolved *jsonschema.Resolved, v any)
|
|||
if err := dec.Decode(v); err != nil {
|
||||
return fmt.Errorf("unmarshaling: %w", err)
|
||||
}
|
||||
// TODO: test with nil args.
|
||||
return validateSchema(resolved, v)
|
||||
}
|
||||
|
||||
func validateSchema(resolved *jsonschema.Resolved, value any) error {
|
||||
if resolved != nil {
|
||||
if err := resolved.ApplyDefaults(v); err != nil {
|
||||
return fmt.Errorf("applying defaults from \n\t%s\nto\n\t%s:\n%w", schemaJSON(resolved.Schema()), data, err)
|
||||
if err := resolved.ApplyDefaults(value); err != nil {
|
||||
return fmt.Errorf("applying defaults from \n\t%s\nto\n\t%v:\n%w", schemaJSON(resolved.Schema()), value, err)
|
||||
}
|
||||
if err := resolved.Validate(v); err != nil {
|
||||
return fmt.Errorf("validating\n\t%s\nagainst\n\t %s:\n %w", data, schemaJSON(resolved.Schema()), err)
|
||||
if err := resolved.Validate(value); err != nil {
|
||||
return fmt.Errorf("validating\n\t%v\nagainst\n\t %s:\n %w", value, schemaJSON(resolved.Schema()), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Reference in a new issue