ExAgent. Tool
(ExAgent v2.3.0)
View Source
A tool the model may call.
ExAgent.Tool bundles everything: the serialisable ToolDefinition
(name, description, JSON-Schema parameters) and the actual callable that
runs when the model invokes it. Use definition/1 to project the shape a
provider sends to the model.
Tools are normally built via the deftool macro (see ExAgent.Tools),
which derives the JSON-Schema from the function's @spec. They can also be
built by hand with new/1.
execution_binding is private, data-only adapter identity used by persisted
continuation fingerprints, never by definition/1. Local tools retain nil.
MCP creates a validated descriptor or marks missing host identity :unbound;
the latter supports ordinary calls and rejects persisted execution before IO.
Do not strip this field or put credentials in it to make a tool resumable.
Execution controls
Calls from one model response run concurrently by default, bounded by the
agent's :tool_timeout. Two optional fields refine that per tool:
timeout— milliseconds for this tool's calls instead of the agent's:tool_timeout(still capped by the run deadline). A call that exceeds it is killed and reported with an:unknownoutcome, which fails the run, as with:tool_timeout.concurrency—:parallel(default) or:exclusive. An exclusive call runs alone: calls before it finish first and calls after it start only once it has finished. Use it for tools that mutate shared state (writing files, running migrations) while read-only tools keep running in parallel.
Consecutive parallel calls run together even when their timeouts differ.
Results always keep the model's call order. A call that pauses for approval
does not hold back later calls in the same response: ordering applies to the
calls executed in one attempt. Neither field is sent to the model or recorded
in persisted continuation fingerprints, so a resumed run uses the current
definition, even when its timeout exceeds the persisted :tool_timeout.
tool = ExAgent.Tool.new(name: "apply_patch", call: &patch/2, concurrency: :exclusive)
tools = Enum.map(MyApp.Tools.tools(), &%{&1 | timeout: 5_000})
Summary
Functions
Serialisable projection sent to the model: name + description + params.
Build a tool by hand. Raises ArgumentError for invalid execution controls.
Prepare a tool's local JSON Schema validator before requesting the model.
Validate arguments without invoking the callable or changing the original map.
Check that a successful result payload can be encoded as JSON, preserving it.
Types
@type kind() :: :function | :output | :external | :unapproved
@type t() :: %ExAgent.Tool{ call: (ExAgent.RunContext.t(), map() -> term()) | (map() -> term()) | nil, concurrency: :parallel | :exclusive, delegation: term(), description: String.t() | nil, execution_binding: map() | :unbound | nil, kind: kind(), max_retries: non_neg_integer(), name: String.t(), parameters_json_schema: map() | boolean(), prepared_validator: term(), takes_ctx: boolean(), timeout: pos_integer() | nil }
Functions
Serialisable projection sent to the model: name + description + params.
Build a tool by hand. Raises ArgumentError for invalid execution controls.
@spec prepare(t()) :: {:ok, t()} | {:error, {:invalid_tool_schema, [validation_error()]}}
Prepare a tool's local JSON Schema validator before requesting the model.
The opaque prepared_validator field is internal, is not serialized by
definition/1, and is reused only for the exact immutable schema it was built
from. No process-global cache is used. Draft 2020-12 (default) and draft 7 are
supported with embedded/local references; executable casts, module references,
and fetching external schemas are not supported.
Nested schemas must use the root dialect. The reference preflight checks candidate targets conservatively across embedded resources. Inert defaults are omitted only from the private build projection; referenced defaults are kept verbatim and remain subject to JSV's identifier-scanning restrictions.
@spec validate_args(t(), term()) :: {:ok, map()} | {:error, [validation_error()]}
Validate arguments without invoking the callable or changing the original map.
Atom and string object keys are compared as JSON strings; collisions and values
outside the JSON data model are rejected. Values are never coerced and defaults
are never inserted. Error paths are JSON pointers (the root is "").
@spec validate_result(t(), term()) :: {:ok, term()} | {:error, [validation_error()]}
Check that a successful result payload can be encoded as JSON, preserving it.
Pass the payload after unwrapping {:ok, payload}. Jason-encodable structs and
atoms are supported; duplicate JSON object keys and encoding failures reject.
This does not impose a result schema or retry an already executed callable.