Normandy.Tools.SchemaBaseTool (normandy v0.6.2)

View Source

Mixin module for creating tools with Normandy schema-based input definitions.

This module provides a convenient way to define tools using Normandy's schema system, automatically generating JSON schemas and providing runtime validation of tool inputs.

Benefits

  • DRY: Single source of truth for tool input structure
  • Validation: Automatic runtime validation before tool execution
  • Type Safety: Leverage Normandy's type system with constraints
  • Format Validation: Built-in support for email, UUID, URI, etc.
  • Better Errors: Path-based error messages for invalid inputs

Usage

defmodule MyTool do
  use Normandy.Tools.SchemaBaseTool

  tool_schema "my_tool", "Does something useful" do
    field(:query, :string, required: true, description: "Search query", min_length: 1)
    field(:limit, :integer, description: "Max results", default: 10, minimum: 1, maximum: 100)
    field(:email, :string, description: "Contact email", format: "email")
  end

  @impl true
  def execute(%__MODULE__{query: query, limit: limit}) do
    # Your tool logic here
    {:ok, "Processed: #{query} with limit #{limit}"}
  end
end

Automatic Features

The tool_schema macro automatically:

  • Defines a struct with the specified fields
  • Generates JSON Schema from field definitions
  • Implements Normandy.Tools.BaseTool protocol
  • Provides validate/1 function for runtime validation
  • Handles default values and type coercion

Validation

Input validation happens automatically when tools are executed through Normandy.Tools.Executor. Invalid inputs return {:error, validation_errors}.

You can also manually validate:

tool_params = %{query: "test", limit: 5}
case MyTool.validate(tool_params) do
  {:ok, validated} -> MyTool.execute(validated)
  {:error, errors} -> {:error, errors}
end

Summary

Functions

Defines a tool with a schema-based input specification.

Functions

tool_schema(tool_name, description, list)

(macro)

Defines a tool with a schema-based input specification.

Parameters

  • tool_name - The unique identifier for this tool (string)
  • description - Human-readable description of what the tool does
  • do block - Schema field definitions using field/3

Examples

tool_schema "calculator", "Performs arithmetic operations" do
  field(:operation, :string, required: true, enum: ["add", "subtract", "multiply", "divide"])
  field(:a, :float, required: true, description: "First operand")
  field(:b, :float, required: true, description: "Second operand")
end