anthropic/types/tool

Tool definition types for the Anthropic Messages API

This module defines types for tool definitions following the Anthropic schema. Tools allow Claude to call external functions and receive results.

Example

let weather_tool = Tool(
  name: "get_weather",
  description: Some("Get the current weather for a location"),
  input_schema: InputSchema(
    schema_type: "object",
    properties: Some([
      #("location", PropertySchema(
        property_type: "string",
        description: Some("City and state, e.g. 'San Francisco, CA'"),
        enum_values: None,
      )),
    ]),
    required: Some(["location"]),
  ),
)

Types

Schema defining the input parameters for a tool

pub type InputSchema {
  InputSchema(
    schema_type: String,
    properties: option.Option(List(#(String, PropertySchema))),
    required: option.Option(List(String)),
  )
}

Constructors

  • InputSchema(
      schema_type: String,
      properties: option.Option(List(#(String, PropertySchema))),
      required: option.Option(List(String)),
    )

    Arguments

    schema_type

    Always “object” for Anthropic tools

    properties

    Property definitions for the input object

    required

    List of required property names

Schema for a single property in a tool’s input

pub type PropertySchema {
  PropertySchema(
    property_type: String,
    description: option.Option(String),
    enum_values: option.Option(List(String)),
    items: option.Option(PropertySchema),
    properties: option.Option(List(#(String, PropertySchema))),
    required: option.Option(List(String)),
  )
}

Constructors

  • PropertySchema(
      property_type: String,
      description: option.Option(String),
      enum_values: option.Option(List(String)),
      items: option.Option(PropertySchema),
      properties: option.Option(List(#(String, PropertySchema))),
      required: option.Option(List(String)),
    )

    Arguments

    property_type

    The JSON type of the property (e.g., “string”, “number”, “boolean”, “array”, “object”)

    description

    Human-readable description of the property

    enum_values

    If this is an enum, the allowed values

    items

    For array types, the schema of items

    properties

    For object types, nested properties

    required

    For object types, which nested properties are required

A tool definition that can be provided to Claude

pub type Tool {
  Tool(
    name: String,
    description: option.Option(String),
    input_schema: InputSchema,
  )
}

Constructors

  • Tool(
      name: String,
      description: option.Option(String),
      input_schema: InputSchema,
    )

    Arguments

    name

    The name of the tool (must match the regex ^[a-zA-Z0-9_-]{1,64}$)

    description

    Human-readable description of what the tool does

    input_schema

    JSON Schema defining the tool’s input parameters

Represents a tool call extracted from a response

pub type ToolCall {
  ToolCall(id: String, name: String, input: String)
}

Constructors

  • ToolCall(id: String, name: String, input: String)

    Arguments

    id

    Unique identifier for this tool call

    name

    Name of the tool being called

    input

    JSON string of input arguments

Specifies how Claude should choose which tool to use

pub type ToolChoice {
  Auto
  Any
  ToolName(name: String)
  NoTool
}

Constructors

  • Auto

    Claude decides whether to use a tool and which one

  • Any

    Claude must use one of the provided tools

  • ToolName(name: String)

    Claude must use the specified tool

  • NoTool

    Claude should not use any tools (respond directly)

Represents the result of executing a tool

pub type ToolResult {
  ToolSuccess(tool_use_id: String, content: String)
  ToolFailure(tool_use_id: String, error: String)
}

Constructors

  • ToolSuccess(tool_use_id: String, content: String)

    Successful tool execution

    Arguments

    tool_use_id

    ID of the tool call this responds to

    content

    The result content

  • ToolFailure(tool_use_id: String, error: String)

    Failed tool execution

    Arguments

    tool_use_id

    ID of the tool call this responds to

    error

    Error message

Values

pub fn any_choice() -> ToolChoice

Create an Any tool choice

pub fn array_property(
  description: option.Option(String),
  item_schema: PropertySchema,
) -> PropertySchema

Create an array property schema

pub fn auto_choice() -> ToolChoice

Create an Auto tool choice

pub fn empty_input_schema() -> InputSchema

Create an empty input schema (for tools with no parameters)

pub fn enum_property(
  description: option.Option(String),
  values: List(String),
) -> PropertySchema

Create an enum property schema

pub fn input_schema(
  properties: List(#(String, PropertySchema)),
  required: List(String),
) -> InputSchema

Create an input schema with properties

pub fn input_schema_to_json(schema: InputSchema) -> json.Json

Encode an InputSchema to JSON

pub fn is_tool_success(result: ToolResult) -> Bool

Check if a tool result is successful

pub fn no_tool_choice() -> ToolChoice

Create a NoTool choice

pub fn object_property(
  description: option.Option(String),
  properties: List(#(String, PropertySchema)),
  required: List(String),
) -> PropertySchema

Create an object property schema with nested properties

pub fn property(property_type: String) -> PropertySchema

Create a simple property schema with just a type

pub fn property_schema_to_json(
  schema: PropertySchema,
) -> json.Json

Encode a PropertySchema to JSON

pub fn property_with_description(
  property_type: String,
  description: String,
) -> PropertySchema

Create a property schema with type and description

pub fn tool(name: String, input_schema: InputSchema) -> Tool

Create a tool with no description

pub fn tool_call(
  id: String,
  name: String,
  input: String,
) -> ToolCall

Create a ToolCall

pub fn tool_choice_to_json(choice: ToolChoice) -> json.Json

Encode a ToolChoice to JSON

pub fn tool_failure(
  tool_use_id: String,
  error: String,
) -> ToolResult

Create a failed tool result

pub fn tool_name_choice(name: String) -> ToolChoice

Create a specific tool choice

pub fn tool_result_id(result: ToolResult) -> String

Get the tool_use_id from a ToolResult

pub fn tool_success(
  tool_use_id: String,
  content: String,
) -> ToolResult

Create a successful tool result

pub fn tool_to_json(t: Tool) -> json.Json

Encode a Tool to JSON

pub fn tool_to_json_string(t: Tool) -> String

Convert a tool to a JSON string

pub fn tool_with_description(
  name: String,
  description: String,
  input_schema: InputSchema,
) -> Tool

Create a tool with a description

pub fn tools_to_json(tools: List(Tool)) -> json.Json

Encode a list of tools to JSON

Search Document