internal/mcp: meta and progress token
Meta is a struct that contains a map[string]any, and a ProgressToken field. Each Params and Result type has a Meta as a value field. Meta marshals and unmarshals specially, so the ProgressToken field becomes part of the map. The marshalling code is generalized in anticipation of every Params and Result growing a map of additional properties. Change-Id: I6d2f34270d33501c96437f24f91ef3e51d20d955 Reviewed-on: https://go-review.googlesource.com/c/tools/+/674956 Reviewed-by: Sam Thanawalla <samthanawalla@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Jonathan Amsterdam <jba@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
dbbdf5a181
commit
7130f0f081
1 changed files with 548 additions and 115 deletions
|
|
@ -299,13 +299,21 @@ func loadSchema(schemaFile string) (data []byte, err error) {
|
|||
|
||||
func writeDecl(configName string, config typeConfig, def *jsonschema.Schema, named map[string]*bytes.Buffer) error {
|
||||
var w io.Writer = io.Discard
|
||||
if typeName := config.Name; typeName != "-" {
|
||||
var typeName string
|
||||
if typeName = config.Name; typeName != "-" {
|
||||
if typeName == "" {
|
||||
typeName = configName
|
||||
}
|
||||
if _, ok := named[typeName]; ok {
|
||||
return nil
|
||||
}
|
||||
// The JSON schema does not accurately represent the source of truth, which is typescript.
|
||||
// Every Params and Result type should have a _meta property.
|
||||
// Also, those with a progress token will turn into a struct; we want the progress token to
|
||||
// be a map item. So replace all metas.
|
||||
if strings.HasSuffix(typeName, "Params") || strings.HasSuffix(typeName, "Result") {
|
||||
def.Properties["_meta"] = metaSchema
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
w = buf
|
||||
named[typeName] = buf
|
||||
|
|
@ -318,6 +326,12 @@ func writeDecl(configName string, config typeConfig, def *jsonschema.Schema, nam
|
|||
return err // Better error here?
|
||||
}
|
||||
fmt.Fprintf(w, "\n")
|
||||
|
||||
// Any decl with a _meta field gets a GetMeta method.
|
||||
if _, ok := def.Properties["_meta"]; ok {
|
||||
fmt.Fprintf(w, "\nfunc (x *%s) GetMeta() *Meta { return &x.Meta }", typeName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -354,11 +368,7 @@ 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.
|
||||
//
|
||||
// TODO(jba): further refine this classification of object schemas.
|
||||
// For example, the typescript "object" type, which should map to a Go "any",
|
||||
// is represented in schema.json by `{type: object, properties: {}, additionalProperties: true}`.
|
||||
// json.RawMessage. We use any.
|
||||
if def.Type == "object" && canHaveAdditionalProperties(def) && def.Properties == nil {
|
||||
w.Write([]byte("map[string]"))
|
||||
return writeType(w, nil, def.AdditionalProperties, named)
|
||||
|
|
@ -372,7 +382,7 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
|
|||
fmt.Fprintf(w, "*Content")
|
||||
} else {
|
||||
// E.g. union types.
|
||||
fmt.Fprintf(w, "json.RawMessage")
|
||||
fmt.Fprintf(w, "any")
|
||||
}
|
||||
} else {
|
||||
switch def.Type {
|
||||
|
|
@ -398,6 +408,11 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
|
|||
if fieldDef.Description != "" {
|
||||
fmt.Fprintf(w, "%s\n", toComment(fieldDef.Description))
|
||||
}
|
||||
if name == "_meta" {
|
||||
fmt.Fprintln(w, "\tMeta Meta `json:\"_meta,omitempty\"`")
|
||||
continue
|
||||
}
|
||||
|
||||
export := exportName(name)
|
||||
fmt.Fprintf(w, "\t%s ", export)
|
||||
|
||||
|
|
@ -551,6 +566,12 @@ func isStruct(s *jsonschema.Schema) bool {
|
|||
return s.Type == "object" && s.Properties != nil && !canHaveAdditionalProperties(s)
|
||||
}
|
||||
|
||||
// The schema for "_meta".
|
||||
// We only need the description: the rest is a special case.
|
||||
var metaSchema = &jsonschema.Schema{
|
||||
Description: "This property is reserved by the protocol to allow clients and servers to attach additional metadata to their responses.",
|
||||
}
|
||||
|
||||
// schemaJSON returns the JSON for s.
|
||||
// For debugging.
|
||||
func schemaJSON(s *jsonschema.Schema) string {
|
||||
|
|
|
|||
Loading…
Reference in a new issue