Documentation
¶
Overview ¶
Package schema provides typed JSON Schema structures for type coercion and defaults.
This package wraps raw JSON Schema maps (map[string]any) into typed structures that provide type-safe operations like coercion and default value application.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateSchema ¶ added in v0.8.0
GenerateSchema generates a JSON Schema from a Go struct type using reflection.
The function inspects struct tags to determine:
- json: Field name in the schema (uses json tag name)
- description: Field description (from `description` tag)
- omitempty: Whether the field is optional (not in required array)
Supported types:
- string -> {"type": "string"}
- int, int64, etc. -> {"type": "integer"}
- float64, float32 -> {"type": "number"}
- bool -> {"type": "boolean"}
- []T -> {"type": "array", "items": {...}}
- map[string]any -> {"type": "object"}
- struct -> {"type": "object", "properties": {...}}
Example:
type FindToolInput struct {
ToolDescription string `json:"tool_description" description:"Natural language description"`
ToolKeywords []string `json:"tool_keywords,omitempty" description:"Optional keywords"`
}
schema := GenerateSchema[FindToolInput]()
func Translate ¶ added in v0.8.0
Translate converts an untyped input (typically map[string]any from MCP request arguments) to a typed struct using JSON marshalling/unmarshalling.
This provides a simple, reliable way to convert MCP tool arguments to typed Go structs without manual field-by-field extraction.
Example:
args := request.Params.Arguments // map[string]any
input, err := Translate[FindToolInput](args)
if err != nil {
return nil, fmt.Errorf("invalid arguments: %w", err)
}
Types ¶
type TypeCoercer ¶
TypeCoercer coerces values to their expected types. Implementations return the coerced value, or the original if coercion fails.
func MakeSchema ¶
func MakeSchema(raw map[string]any) TypeCoercer
MakeSchema parses a raw JSON Schema map into typed Schema structures. Always returns a valid TypeCoercer; callers do not need to nil-check. For nil, empty, or unknown schema types, returns a passthrough coercer that returns values unchanged.