internal/mcp: validate tool input schemas

A tool now validates its input with its InputSchema.

Change schema inference to allow explicit nulls for fields of
pointer type.

We assume the schema has no external references. For example, the
following schema cannot be handled:

  {
    "$ref": "https://example.com/other.json"
  }

Schemas with internal references, like to a "$defs", are fine.

Change-Id: I6ee7c18c2c5cb609df0b22a66da986f7ea64bbe4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/670676
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Jonathan Amsterdam 2025-05-06 06:55:07 -04:00
commit a07ce11b37

View file

@ -42,6 +42,10 @@ func NewPrompt[TReq any](name, description string, handler func(context.Context,
if schema.Type != "object" || !reflect.DeepEqual(schema.AdditionalProperties, &jsonschema.Schema{Not: &jsonschema.Schema{}}) {
panic(fmt.Sprintf("handler request type must be a struct"))
}
resolved, err := schema.Resolve(nil)
if err != nil {
panic(err)
}
prompt := &ServerPrompt{
Prompt: &Prompt{
Name: name,
@ -70,7 +74,7 @@ func NewPrompt[TReq any](name, description string, handler func(context.Context,
return nil, err
}
var v TReq
if err := unmarshalSchema(rawArgs, schema, &v); err != nil {
if err := unmarshalSchema(rawArgs, resolved, &v); err != nil {
return nil, err
}
return handler(ctx, ss, v, params)