schema

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

func GenerateSchema[T any]() (map[string]any, error)

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

func Translate[T any](input any) (T, error)

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

type TypeCoercer interface {
	TryCoerce(value any) any
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL