glean/tool

Types

A tool that can be called by the model. Generic over deps — the dependency type threaded from agent to tool execution.

The execute function receives the raw JSON arguments string — each tool decodes it internally using its own decoder. This is explicit and type-safe: the tool knows its schema and how to decode it.

pub type Tool(deps) {
  Tool(
    name: String,
    description: String,
    input_schema: schema.Schema,
    execute: fn(ToolContext(deps), String) -> Result(
      String,
      error.GleanError,
    ),
    max_retries: Int,
  )
}

Constructors

  • Tool(
      name: String,
      description: String,
      input_schema: schema.Schema,
      execute: fn(ToolContext(deps), String) -> Result(
        String,
        error.GleanError,
      ),
      max_retries: Int,
    )

    Arguments

    name

    Unique name used by the model to call this tool

    description

    Description for the model explaining what this tool does

    input_schema

    JSON Schema for the tool’s input parameters

    execute

    Execute function: takes context + raw JSON arguments string, returns output or error

    max_retries

    Maximum retries for this tool on validation failure (default: 0)

Context passed to tool execute functions. Carries dependencies and execution metadata.

pub type ToolContext(deps) {
  ToolContext(
    deps: deps,
    tool_call_id: String,
    tool_name: String,
    retry: Int,
    max_retries: Int,
    step: Int,
  )
}

Constructors

  • ToolContext(
      deps: deps,
      tool_call_id: String,
      tool_name: String,
      retry: Int,
      max_retries: Int,
      step: Int,
    )

    Arguments

    deps

    User-provided dependencies

    tool_call_id

    Unique ID for this tool call

    tool_name

    Name of the tool being executed

    retry

    Current retry attempt (0 = first attempt)

    max_retries

    Maximum retries allowed

    step

    Current step in the agent loop

A collection of tools keyed by name. Opaque to enforce the invariant that tool names are unique.

pub opaque type Toolset(deps)

Values

pub fn empty_toolset() -> Toolset(deps)

Create an empty toolset.

pub fn find(
  toolset: Toolset(deps),
  name: String,
) -> Result(Tool(deps), error.GleanError)

Look up a tool by name.

pub fn is_empty(toolset: Toolset(deps)) -> Bool

Check if a toolset is empty (has no tools).

pub fn names(toolset: Toolset(deps)) -> List(String)

Get the list of tool names in this toolset.

pub fn new(
  name name: String,
  description description: String,
  input_schema input_schema: schema.Schema,
  execute execute: fn(ToolContext(deps), String) -> Result(
    String,
    error.GleanError,
  ),
) -> Tool(deps)

Create a tool with the required fields.

pub fn size(toolset: Toolset(deps)) -> Int

Get the number of tools in the toolset.

pub fn to_specs(
  toolset: Toolset(deps),
) -> List(provider.ToolSpec)

Convert all tools in the toolset to ToolSpecs for sending to the provider.

pub fn toolset(tools: List(Tool(deps))) -> Toolset(deps)

Create a Toolset from a list of tools. Asserts that all tool names are unique.

pub fn with_max_retries(
  tool: Tool(deps),
  max_retries: Int,
) -> Tool(deps)

Set max retries on a tool.

✨ Search Document