View Source Anthropic.Tools behaviour (anthropic_community v0.5.0)

Behaviour for user-defined tools, driving the native Anthropic tools API field (JSON Schema input_schema) rather than a hand-rolled prompt-injection protocol.

Example

defmodule MyApp.WeatherTool do
  use Anthropic.Tools

  @impl true
  def name, do: "get_weather"

  @impl true
  def description, do: "Get the current weather for a given city."

  @impl true
  def input_schema do
    %{
      "type" => "object",
      "properties" => %{
        "location" => %{"type" => "string", "description" => "City and state, e.g. San Francisco, CA"}
      },
      "required" => ["location"]
    }
  end

  @impl true
  def execute(%{"location" => location}) do
    {:ok, "72F and sunny in #{location}"}
  end
end

Register tools with Anthropic.Messages.create/2 via the :tools option (either tool modules or raw wire-shape maps), or drive a full agentic loop with Anthropic.ToolRunner.run/4.

Summary

Functions

Serializes a tool module (or a raw wire-shape map, passed through unchanged) into a tools request param entry.

Callbacks

@callback description() :: String.t()
@callback execute(input :: map()) :: {:ok, String.t() | map()} | {:error, String.t()}
@callback input_schema() :: map()
@callback name() :: String.t()

Functions

@spec to_param(module() | map()) :: map()

Serializes a tool module (or a raw wire-shape map, passed through unchanged) into a tools request param entry.