toolschema

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: GPL-3.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package toolschema compiles and enforces the closed JSON Schema subset that PiWorker accepts for the input of MCP tools (ADR-0013, docs/design/mcp.md §5). Agents supply data only; the schema is the contract that data must meet before a flow sees it, so the subset is small enough to be checked completely:

  • the root is `type: object`; every object is closed (`additionalProperties: false`, forced when absent) and has `properties` and `required`; objects nest at most MaxDepth levels;
  • a property has exactly one type: string, integer, number, boolean, an array of one of those with a mandatory `maxItems`, or an object;
  • strings: `enum`, `pattern` (anchored `^…$`, in the regular expression syntax Go and JavaScript share), `minLength`, `maxLength` (DefaultMaxLength when absent), `format` (an annotation only);
  • integers and numbers: `minimum`, `maximum`, `enum`;
  • `title`, `description`, `default` (must validate) and `examples`;
  • `x-piworker-allow` on strings: probes of the corpus (see Probes) the admin accepts, stripped from the schema advertised to clients.

Everything else (`$ref`, `$defs`, composition, conditionals, `patternProperties`, `dependent*`, `unevaluated*`, boolean schemas, untyped schemas, type lists, `x-mcp-header`, unknown keywords…) is rejected with the JSON pointer of the offending keyword.

This package must not import internal/node or internal/flow.

Index

Constants

View Source
const (
	// MaxSchemaBytes caps the raw schema document.
	MaxSchemaBytes = 64 << 10
	// MaxDepth is how many object levels may nest, the root included.
	MaxDepth = 3
	// MaxProperties caps the properties of one object.
	MaxProperties = 64
	// MaxEnumValues caps an enum.
	MaxEnumValues = 256
	// MaxItemsLimit is the largest maxItems an array may declare.
	MaxItemsLimit = 100
	// DefaultMaxLength is forced on strings that declare no maxLength.
	DefaultMaxLength = 256
	// MaxLengthLimit is the largest maxLength a string may declare.
	MaxLengthLimit = 4096
	// MaxPatternLength caps a pattern's source.
	MaxPatternLength = 1024
	// MaxPropertyNameLength caps a property name.
	MaxPropertyNameLength = 128
)

Limits of the subset.

Variables

This section is empty.

Functions

func CheckOutputSchema

func CheckOutputSchema(raw json.RawMessage) (json.RawMessage, error)

CheckOutputSchema checks a tool's optional output schema and returns it in canonical form (sorted keys, normalized numbers). Output schemas describe what the flow replies and are advertised as is, so they are not limited to the input subset: the document must be a JSON object (at most 64 KiB, without duplicate keys) with "type": "object", as MCP requires, and must resolve as JSON Schema without remote references.

func Probes

func Probes() []string

Probes returns a copy of the probe corpus. Each probe stands for a class of values, and a pattern accepts a probe when it accepts any value of the class (see patternProbes):

  • "-x": a value starting with - (an option flag);
  • "--a=b": a value starting with -- (a long option);
  • "..": a value with a .. path segment (the whole value, or between slashes, at the start or at the end);
  • "/": a / anywhere;
  • " ": whitespace anywhere (space, tab and every other Unicode space that is not a line break);
  • "\n": a line break anywhere (\n, \r, U+0085, U+2028, U+2029);
  • "$(", ";", "|", "`": a $, ;, | or backtick anywhere.

Types

type Error

type Error struct {
	Issues []Issue
}

Error is returned by Compile for a schema outside the subset. It lists every problem found, sorted by pointer.

func (*Error) Error

func (e *Error) Error() string

type Issue

type Issue struct {
	Pointer string `json:"pointer"`
	Message string `json:"message"`
}

Issue is one problem, located by a JSON pointer: into the schema document for Compile, into the arguments for Validate, and the argument's property for RequireStrictFor.

func (Issue) String

func (i Issue) String() string

String renders the issue as "pointer: message" ("(root)" for "").

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is a compiled tool input schema. It is immutable and safe for concurrent use.

func Compile

func Compile(raw json.RawMessage) (*Schema, error)

Compile checks raw against the subset and returns the compiled schema, or an *Error listing every problem.

func (*Schema) JSON

func (s *Schema) JSON() json.RawMessage

JSON returns the normalized schema advertised to clients: canonical (sorted keys, normalized numbers), every object closed, every string bounded, x-piworker-allow removed. The caller owns the returned bytes.

func (*Schema) RequireStrictFor

func (s *Schema) RequireStrictFor(paths []string) []Issue

RequireStrictFor checks that the properties at paths cannot carry option flags, path traversal or shell syntax, for arguments that reach a privileged node. paths are JSON pointers of properties in the arguments ("/host", "/opts/host"); nil or empty means every leaf property. An object selects every leaf below it and an array (or one of its elements) its items.

A string needs an enum, or a pattern (always anchored) that rejects every probe of the corpus (see Probes) other than those the property lists in x-piworker-allow. A probe is judged on every value the pattern accepts, not just as a whole value: ^[a-z].*$ rejects "/" but accepts "a/b", so it accepts the "/" probe. An integer or number needs an enum, or both minimum and maximum. Booleans are always fine. The result has one issue per offending property (naming the probes its pattern accepts) and one per path that is not a property, sorted by pointer; nil when everything is strict.

func (*Schema) Validate

func (s *Schema) Validate(args map[string]any) []Issue

Validate checks tool arguments against the schema and returns every problem (nil when the arguments are valid). A nil map is an empty object. Pointers locate the argument (e.g. "/tags/2"); messages never quote more than the first 64 characters of a value.

Jump to

Keyboard shortcuts

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