internal/mcp/internal/jsonschema: schema definition

The first of several CLs providing JSON Schema validation.

Replace the existing Schema type with one that has all the keywords
of the most current version of the JSON Schema spec, called 2020-12.

The 2020-12 spec can be represented type-safely in Go with one exception:
the "type" keyword can be either a string or a []string. To handle this
safely, both Type and Types fields are defined. Custom
JSONMarshal/Unmarshal methods handle the conversion. We need the
unmarshal anyway because "true" and "false" are valid schemas.

Unfortunately, the MCP spec uses the older "draft7" version of JSON
Schema, but the two are close enough for code generation to work.
I confirmed that the protocol generator produces the identical output.

Change-Id: I44f6ea2f3a3f8498979946f69dfb7f50afa1acad
Reviewed-on: https://go-review.googlesource.com/c/tools/+/667855
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Jonathan Amsterdam 2025-04-24 11:40:50 -04:00
commit 3434e57007

View file

@ -23,6 +23,7 @@ import (
"log"
"net/http"
"os"
"reflect"
"slices"
"strings"
@ -240,14 +241,16 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
// For types that explicitly allow additional properties, we can either
// unmarshal them into a map[string]any, or delay unmarshalling with
// json.RawMessage. For now, use json.RawMessage as it defers the choice.
if def.Type == "object" && def.AdditionalProperties != nil && def.AdditionalProperties != false {
if def.Type == "object" && canHaveAdditionalProperties(def) {
w.Write([]byte("json.RawMessage"))
return nil
}
switch typ := def.Type.(type) {
case string:
switch typ {
if def.Type == "" {
// E.g. union types.
fmt.Fprintf(w, "json.RawMessage")
} else {
switch def.Type {
case "array":
fmt.Fprintf(w, "[]")
return writeType(w, nil, def.Items, named)
@ -281,8 +284,7 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
// TODO: use omitzero when available.
needPointer := !required &&
(strings.HasPrefix(fieldDef.Ref, "#/definitions/") ||
fieldDef.Type == "object" &&
(fieldDef.AdditionalProperties == nil || fieldDef.AdditionalProperties == false))
fieldDef.Type == "object" && !canHaveAdditionalProperties(fieldDef))
if config != nil && config.Fields[export] != nil {
r := config.Fields[export]
@ -320,10 +322,6 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
default:
fmt.Fprintf(w, "any")
}
default:
// E.g. union types.
fmt.Fprintf(w, "json.RawMessage")
}
return nil
}
@ -363,6 +361,16 @@ func toComment(description string) string {
return strings.TrimRight(buf.String(), "\n")
}
// The MCP spec improperly uses the absence of the additionalProperties keyword to
// mean that additional properties are not allowed. In fact, it means just the opposite
// (https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.6.5.6).
// If the MCP spec wants to allow additional properties, it will write "true" or
// an object explicitly.
func canHaveAdditionalProperties(s *jsonschema.Schema) bool {
ap := s.AdditionalProperties
return ap != nil && !reflect.DeepEqual(ap, &jsonschema.Schema{Not: &jsonschema.Schema{}})
}
// exportName returns an exported name for a Go symbol, based on the given name
// in the JSON schema, removing leading underscores and capitalizing.
func exportName(s string) string {