anthropic/tools/builder

Fluent builder for tool definitions

This module provides an ergonomic builder API for creating tool definitions. The builder pattern allows for type-safe, readable tool construction.

Example

let weather_tool =
  tool_builder("get_weather")
  |> with_description("Get the current weather for a location")
  |> add_string_param("location", "City and state, e.g. 'San Francisco, CA'", True)
  |> add_enum_param("unit", "Temperature unit", ["celsius", "fahrenheit"], False)
  |> build()

// For tools with no parameters
let time_tool =
  tool_builder("get_time")
  |> with_description("Get the current time")
  |> build_simple()

Types

Builder state for constructing a Tool

pub type ToolBuilder {
  ToolBuilder(
    name: String,
    description: option.Option(String),
    properties: List(#(String, tool.PropertySchema)),
    required: List(String),
  )
}

Constructors

  • ToolBuilder(
      name: String,
      description: option.Option(String),
      properties: List(#(String, tool.PropertySchema)),
      required: List(String),
    )

    Arguments

    name

    Name of the tool

    description

    Optional description

    properties

    Properties accumulated so far

    required

    Required property names

Validation error for tool definitions

pub type ToolBuilderError {
  EmptyName
  InvalidNameCharacters(name: String)
  NameTooLong(name: String, length: Int)
  DuplicateProperty(name: String)
}

Constructors

  • EmptyName

    Tool name is empty

  • InvalidNameCharacters(name: String)

    Tool name contains invalid characters

  • NameTooLong(name: String, length: Int)

    Tool name is too long (max 64 characters)

  • DuplicateProperty(name: String)

    Duplicate property name

Values

pub fn add_boolean_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  is_required: Bool,
) -> ToolBuilder

Add a boolean parameter

pub fn add_enum_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  values: List(String),
  is_required: Bool,
) -> ToolBuilder

Add an enum parameter (string with allowed values)

pub fn add_integer_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  is_required: Bool,
) -> ToolBuilder

Add an integer parameter

pub fn add_number_array_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  is_required: Bool,
) -> ToolBuilder

Add an array parameter with number items

pub fn add_number_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  is_required: Bool,
) -> ToolBuilder

Add a number parameter

pub fn add_object_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  nested_properties: List(#(String, tool.PropertySchema)),
  nested_required: List(String),
  is_required: Bool,
) -> ToolBuilder

Add an object parameter with nested properties

pub fn add_property(
  builder: ToolBuilder,
  name: String,
  property: tool.PropertySchema,
  is_required: Bool,
) -> ToolBuilder

Add a custom property schema

pub fn add_string_array_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  item_description: String,
  is_required: Bool,
) -> ToolBuilder

Add an array parameter with string items

pub fn add_string_param(
  builder: ToolBuilder,
  name: String,
  description: String,
  is_required: Bool,
) -> ToolBuilder

Add a string parameter

pub fn build(builder: ToolBuilder) -> tool.Tool

Build the tool from the builder state

pub fn build_simple(builder: ToolBuilder) -> tool.Tool

Build a simple tool with no parameters

pub fn build_validated(
  builder: ToolBuilder,
) -> Result(tool.Tool, ToolBuilderError)

Build and validate the tool

pub fn tool_builder(name: String) -> ToolBuilder

Start building a new tool with the given name

pub fn tool_builder_with_description(
  name: String,
  description: String,
) -> ToolBuilder

Start building a new tool with name and description

pub fn validate_name(
  name: String,
) -> Result(String, ToolBuilderError)

Validate a tool name according to Anthropic’s requirements Must match regex: ^[a-zA-Z0-9_-]{1,64}$

pub fn with_description(
  builder: ToolBuilder,
  description: String,
) -> ToolBuilder

Set the description for the tool

Search Document