Skip to main content

Tool Use API Specification

Version: v1.1.9 | Status: Active | Last Updated: March 2026

Overview​

The tool_use module provides infrastructure for registering, validating, invoking, and composing tools in a pipeline. It includes JSON-schema-like validation, a central registry with decorator support, and a chain abstraction for sequential multi-tool execution.

Core API​

ValidationResult (dataclass)​

Result of a validation operation.

FieldTypeDefaultDescription
validboolTrueWhether the data passed all validation checks
errorslist[str][]List of human-readable error descriptions

Methods:

MethodSignatureReturnsDescription
merge(other: ValidationResult) -> ValidationResultValidationResultMerge another result; combined valid and errors
to_dict() -> dict[str, Any]dictConvert to plain dictionary

validate_input​

def validate_input(data: Any, schema: dict[str, Any]) -> ValidationResult

Validate tool input data against a JSON-schema-like specification.

Parameters:

ParameterTypeRequiredDescription
dataAnyYesThe input data to validate (typically a dict)
schemadict[str, Any]YesJSON-schema-like dict describing expected structure

Supported schema keywords: type, required, properties, items, enum, minimum, maximum, minLength, maxLength, minItems, maxItems, additionalProperties.

Returns: ValidationResult with valid=True if data conforms, or valid=False with error messages.

validate_output​

def validate_output(data: Any, schema: dict[str, Any]) -> ValidationResult

Validate tool output data against a JSON-schema-like specification. Uses the same validation logic as validate_input, separated for clarity in error reporting and independent extensibility.

Parameters: Same as validate_input.

Returns: ValidationResult.


ToolEntry (dataclass)​

A registered tool entry in the registry.

FieldTypeDefaultDescription
namestr(required)Unique identifier for the tool
descriptionstr(required)Human-readable description
handlerCallable[..., Any](required)The callable that executes tool logic
input_schemadict[str, Any]{}JSON-schema-like input specification
output_schemadict[str, Any]{}JSON-schema-like output specification
tagslist[str][]Categorization tags for filtering

Methods:

MethodSignatureReturnsDescription
to_tool_definition(module: str = "tool_use") -> ToolDefinitionToolDefinitionConvert to shared ToolDefinition schema type
to_dict() -> dict[str, Any]dictConvert to plain dictionary (excludes handler)

ToolRegistry​

Central registry for managing available tools. Supports registration, lookup, search by tags/name, and invocation with optional input/output validation.

Constructor:

ToolRegistry()

Methods:

MethodSignatureReturnsDescription
register(entry: ToolEntry) -> NoneNoneRegister a tool. Raises ValueError if name exists
unregister(name: str) -> boolboolRemove by name. Returns True if found
get`(name: str) -> ToolEntry \None`ToolEntry \ None Retrieve by exact name
list() -> list[ToolEntry]list[ToolEntry]All entries sorted by name
list_names() -> list[str]list[str]All tool names sorted
search(*, name_contains, tags, match_all_tags) -> list[ToolEntry]list[ToolEntry]Search by name substring and/or tags
invoke(name, input_data, *, validate=True) -> ResultResultLook up and invoke a tool with validation

search() parameters:

ParameterTypeRequiredDescription
name_contains`str \None`No Case-insensitive name substring
tags`list[str] \None`No Tags to filter by
match_all_tagsboolNoIf True, all tags must match (default False = any)

invoke() parameters:

ParameterTypeRequiredDescription
namestrYesRegistered tool name
input_dataAnyNoData passed to tool handler
validateboolNoValidate input/output (default True)

Returns: Result object with status, data, message, errors, and duration_ms.

Dunder methods: __len__, __contains__, __repr__.

tool (Decorator)​

@tool(
name: str,
description: str = "",
input_schema: dict | None = None,
output_schema: dict | None = None,
tags: list[str] | None = None,
registry: ToolRegistry | None = None,
)

Marks a function as a tool and optionally registers it. The decorated function retains a tool_entry attribute containing the ToolEntry, usable for deferred registration.

Parameters:

ParameterTypeRequiredDescription
namestrYesUnique tool name
descriptionstrNoHuman-readable description
input_schema`dict \None`No JSON-schema-like input spec
output_schema`dict \None`No JSON-schema-like output spec
tags`list[str] \None`No Categorization tags
registry`ToolRegistry \None`No If provided, auto-registers the tool

ChainStep (dataclass)​

A single step in a tool chain.

FieldTypeDefaultDescription
tool_namestr(required)Name of the tool to invoke
input_mappingdict[str, str]{}Maps tool input keys to context keys (supports dot-notation)
output_keystr""Key to store output under in context. Empty = merge into context

Methods:

MethodSignatureReturnsDescription
to_dict() -> dict[str, Any]dictConvert to plain dictionary

ChainResult (dataclass)​

Result of executing an entire tool chain.

FieldTypeDefaultDescription
successboolTrueWhether all steps completed successfully
contextdict[str, Any]{}Accumulated context after all steps
step_resultslist[Result][]Per-step Result objects in execution order
errorslist[str][]Aggregated errors from all steps
duration_msfloat0.0Total wall-clock time for the chain

Methods:

MethodSignatureReturnsDescription
to_dict() -> dict[str, Any]dictConvert to plain dictionary

ToolChain​

A pipeline of tools that execute sequentially. Outputs from earlier steps are available to later steps via the accumulated context.

Constructor:

ToolChain(registry: ToolRegistry)

Methods:

MethodSignatureReturnsDescription
add_step(step: ChainStep) -> ToolChainToolChainAppend step (fluent API)
validate() -> ValidationResultValidationResultValidate all referenced tools exist
execute(initial_input, *, stop_on_failure, validate_tools) -> ChainResultChainResultExecute all steps sequentially
clear() -> NoneNoneRemove all steps

execute() parameters:

ParameterTypeDefaultDescription
initial_input`dict[str, Any] \None`None Starting context for the first step
stop_on_failureboolTrueStop chain on first tool failure
validate_toolsboolTruePre-validate all referenced tools exist

Properties: steps (read-only copy of step list).

Dunder methods: __len__, __repr__.

Error Handling​

ExceptionRaised When
ValueErrorRegistering a tool with a duplicate name

Validation failures return ValidationResult with valid=False rather than raising exceptions. Invocation failures return Result with status=FAILURE.

Integration Points​

  • codomyrmex.schemas -- Uses Result, ResultStatus, ToolDefinition types
  • model_context_protocol -- ToolEntry converts to ToolDefinition for MCP exposure
  • agents -- Agent frameworks use ToolRegistry for tool discovery and invocation