toolkit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package toolkit provides a generic fluent builder for registering JSON MCP tools on a server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserDeclined           = elicit.ErrUserDeclined
	ErrUserCanceled           = elicit.ErrUserCanceled
	ErrNoElicitation          = elicit.ErrNoElicitation
	ErrUnexpectedElicitAction = elicit.ErrUnexpectedElicitAction
	ErrElicitationFailed      = elicit.ErrElicitationFailed
)

Re-exported so callers can match elicitation sentinels without importing elicit.

Functions

func AddRead

func AddRead[In, Out any](t Tool[In, Out])

AddRead registers a read-only MCP tool (ReadOnlyHint=true, IdempotentHint=true, DestructiveHint=false). Panics if an elicitation prompt was set via WithElicitParamsFunc, which is meaningless for a read tool.

func AddWrite

func AddWrite[In, Out any](t Tool[In, Out])

AddWrite registers a state-mutating MCP tool guarded by elicitation (ReadOnlyHint=false, IdempotentHint=false, DestructiveHint=true). Clients lacking elicitation capability get ErrNoElicitation. The prompt is set via WithElicitParamsFunc; when unset the elicitation message is empty.

func InputSchema

func InputSchema[In any]() *jsonschema.Schema

InputSchema reflects a JSON Schema from the Go input type In, for tools whose input is a plain struct with json tags. Panics on reflection failure, like mcp.AddTool: an unbuildable schema is a registration-time programming error.

Types

type CallFunc

type CallFunc[In, Out any] func(ctx context.Context, in In) (Out, error)

CallFunc is the function a tool invokes.

type ElicitParamsFunc

type ElicitParamsFunc[In any] = elicit.ParamsFunc[In]

ElicitParamsFunc builds the elicitation prompt for a write tool.

type Items

type Items[T any] struct {
	Items []T `json:"items"`
}

Items wraps a slice result under the "items" key.

func WrapItems

func WrapItems[T any](v []T, err error) (Items[T], error)

WrapItems adapts a (slice, error) pair — e.g. WrapItems(client.List(ctx)) — into an Items envelope. Items.MarshalJSON handles nil-slice normalization, so the result always marshals to a JSON array.

func (Items[T]) MarshalJSON

func (i Items[T]) MarshalJSON() ([]byte, error)

MarshalJSON normalizes a nil slice to "items":[] rather than null (which an array-typed schema rejects), for every construction path including the zero value, not only WrapItems.

type Tool

type Tool[In, Out any] struct {
	// contains filtered or unexported fields
}

Tool is a fluent registration builder. New infers In/Out from call, so the type name is rarely written at call sites. Distinct from the SDK's mcp.Tool, which it produces internally.

func New

func New[In, Out any](
	server *mcp.Server,
	name, description string,
	inputSchema *jsonschema.Schema,
	call CallFunc[In, Out],
) Tool[In, Out]

New starts a tool registration, inferring In/Out from call. The input schema is required (the SDK panics on nil); the output schema is optional, set via WithOutputSchema.

func (Tool[In, Out]) WithElicitParamsFunc

func (t Tool[In, Out]) WithElicitParamsFunc(
	f ElicitParamsFunc[In],
) Tool[In, Out]

WithElicitParamsFunc sets the function that builds a write tool's elicitation prompt. Optional for AddWrite; AddRead panics if it is set.

func (Tool[In, Out]) WithOutputSchema

func (t Tool[In, Out]) WithOutputSchema(
	schema *jsonschema.Schema,
) Tool[In, Out]

WithOutputSchema sets the optional output schema. When set, the SDK validates the structured result against it; when unset, the tool declares none.

func (Tool[In, Out]) WithValidateFunc

func (t Tool[In, Out]) WithValidateFunc(f ValidateFunc[In]) Tool[In, Out]

WithValidateFunc sets an optional validator run on decoded input before the call (and, for writes, before elicitation).

type ValidateFunc

type ValidateFunc[In any] func(ctx context.Context, in In) error

ValidateFunc validates decoded input before the call.

type Value

type Value[T any] struct {
	Value T `json:"value"`
}

Value wraps a scalar result under the "value" key.

func WrapValue

func WrapValue[T any](v T, err error) (Value[T], error)

WrapValue adapts a (scalar, error) pair into a Value envelope.

Jump to

Keyboard shortcuts

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