Skip to content

mcp: validate tool output #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions mcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But reflect.ValueOf(untyped nil) is invalid, no? Should you also add a v.IsValid() check here?
In that case, I'm concerned as to why our tests pass.

} else if err := validateSchema(outputResolved, &out); err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap this error to say that the tool output failed to validate.

}

// TODO: return the serialized JSON in a TextContent block, as per spec?
// https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content
Expand Down
15 changes: 10 additions & 5 deletions mcp/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
// "log"

"github.com/google/jsonschema-go/jsonschema"
)
Expand Down Expand Up @@ -42,13 +43,17 @@ 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)
}

// TODO: test with nil args.
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
Expand Down