64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package mcp
|
|
|
|
import "encoding/json"
|
|
|
|
// Tiny JSON-Schema builders for tool input schemas. They keep tool definitions
|
|
// readable without a schema dependency.
|
|
|
|
// Props is a set of named property schemas for Object.
|
|
type Props map[string]json.RawMessage
|
|
|
|
// Object builds an object JSON Schema from named property schemas and the list
|
|
// of required keys. required may be nil.
|
|
func Object(p Props, required []string) json.RawMessage {
|
|
schema := map[string]any{"type": "object"}
|
|
if len(p) > 0 {
|
|
schema["properties"] = p
|
|
} else {
|
|
schema["properties"] = map[string]any{}
|
|
}
|
|
if len(required) > 0 {
|
|
schema["required"] = required
|
|
}
|
|
b, _ := json.Marshal(schema)
|
|
return b
|
|
}
|
|
|
|
// Scalar builds a single-typed schema with a description (e.g. Scalar("integer", …)).
|
|
func Scalar(typ, desc string) json.RawMessage {
|
|
b, _ := json.Marshal(map[string]string{"type": typ, "description": desc})
|
|
return b
|
|
}
|
|
|
|
func String(desc string) json.RawMessage { return Scalar("string", desc) }
|
|
func Integer(desc string) json.RawMessage { return Scalar("integer", desc) }
|
|
func Number(desc string) json.RawMessage { return Scalar("number", desc) }
|
|
func Bool(desc string) json.RawMessage { return Scalar("boolean", desc) }
|
|
func ObjectValue(desc string) json.RawMessage {
|
|
return Scalar("object", desc)
|
|
}
|
|
|
|
// StringArray builds an array-of-strings schema.
|
|
func StringArray(desc string) json.RawMessage {
|
|
b, _ := json.Marshal(map[string]any{
|
|
"type": "array", "description": desc, "items": map[string]string{"type": "string"},
|
|
})
|
|
return b
|
|
}
|
|
|
|
// ObjectArray builds an array-of-objects schema.
|
|
func ObjectArray(desc string) json.RawMessage {
|
|
b, _ := json.Marshal(map[string]any{
|
|
"type": "array", "description": desc, "items": map[string]string{"type": "object"},
|
|
})
|
|
return b
|
|
}
|
|
|
|
// IntegerArray builds an array-of-integers schema.
|
|
func IntegerArray(desc string) json.RawMessage {
|
|
b, _ := json.Marshal(map[string]any{
|
|
"type": "array", "description": desc, "items": map[string]string{"type": "integer"},
|
|
})
|
|
return b
|
|
}
|