toolkit

package
v0.7.0 Latest Latest
Warning

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

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

Documentation

Overview

Package toolkit provides a type-safe fluent builder for registering MCP tools.

New[In, Out](server, name, description, inputSchema, call) infers In/Out from call, so generic type params are rarely written at call sites. The input schema is required (the SDK panics on nil). Chain optional config, then register:

  • WithOutputSchema(schema) — when set, the SDK validates structured results.
  • WithValidateFunc(f) — runs on decoded input before the call (and before elicitation for writes).
  • WithElicitParamsFunc(f) — builds the confirmation prompt for write tools.
  • AddRead(tool) — registers a read-only tool (ReadOnly + Idempotent hints); panics if an elicit-params func was set (meaningless for reads).
  • AddWrite(tool) — registers a state-mutating tool (Destructive hint) gated by MCP elicitation: the client must support elicitation (else ErrNoElicitation); the call runs only on an accept action (decline -> ErrUserDeclined, cancel -> ErrUserCanceled).

toolkit re-exports the elicit sentinels (ErrUserDeclined, ErrUserCanceled, ErrNoElicitation, ErrUnexpectedElicitAction, ErrElicitationFailed) so callers need not import elicit. The shared handler pipeline (runValidated) wraps any validator/gate/call error with the tool name via %w, so a validate/elicit sentinel raised inside a tool stays matchable after registration.

InputSchema[In]() reflects a schema from a plain Go struct via jsonschema.For, panicking on failure like mcp.AddTool does. Tool is a value type — builder methods return a copy, not a pointer.

Handlers are marshalled as-is (no auto-wrapping), so a handler returning a bare slice or scalar would violate MCP's object-root structuredContent contract. result.go provides envelopes: Items[T]/Value[T] (shapes {"items":…} / {"value":…}) and the WrapItems/WrapValue adapters that consume a (slice|scalar, error) pair directly. Items.MarshalJSON normalizes a nil slice to [] so an array-typed output schema still accepts it.

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 elicit sentinels without importing elicit.

Functions

func AddRead

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

AddRead registers a read-only tool; panics if an elicit prompt was set.

func AddReadFunc added in v0.1.4

func AddReadFunc[In, Out any](
	t Tool[In, Out],
	callFunc mcp.ToolHandlerFor[In, Out],
)

AddReadFunc registers a read-only tool running callFunc as-is, unvalidated.

func AddWrite

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

AddWrite registers a state-mutating tool guarded by elicitation.

func AddWriteFunc added in v0.1.4

func AddWriteFunc[In, Out any](
	t Tool[In, Out],
	callFunc mcp.ToolHandlerFor[In, Out],
)

AddWriteFunc registers a state-mutating tool running callFunc as-is, ungated.

func InputSchema

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

InputSchema reflects a JSON Schema from In; panics on reflection failure.

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 into an Items envelope.

func (Items[T]) MarshalJSON

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

MarshalJSON normalizes a nil slice to "items":[] rather than null.

type Tool

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

Tool is a fluent registration builder, distinct from the SDK's mcp.Tool.

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.

func (Tool[In, Out]) WithElicitParamsFunc

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

WithElicitParamsFunc sets the write tool's elicitation prompt builder.

func (Tool[In, Out]) WithOutputSchema

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

WithOutputSchema sets the optional output schema the SDK validates against.

func (Tool[In, Out]) WithValidateFunc

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

WithValidateFunc sets a validator run on decoded input before the call.

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