ExAgent.Tool.Search (ExAgent v3.0.0)

View Source

Capability that hides a large tool catalogue behind a search_tools tool.

Stability — Candidate.

Every advertised tool schema is sent with every model request. With dozens of MCP tools that costs many tokens per step and makes the model pick worse. With this capability the model first sees your regular tools plus search_tools; a search returns the best matching deferred tools by name and description, and those tools are offered from the next request on, for the rest of the conversation.

ExAgent.new(
  model: model,
  tools: [lookup_order],
  capabilities: [ExAgent.Tool.Search.new(deferred: mcp_tools, max_results: 5)]
)

Configure it only in :capabilities: its ExAgent.Capability.tools/1 contributes the search_tools tool followed by the deferred tools to the agent's inventory, so validation, permissions, approvals and persisted continuations treat them like any other tool. Which tools are loaded is derived from the search_tools results in the history, so it survives restore without extra state. Loaded tools follow the regular tools in load order, in this and later runs, so the tool list only changes when a search finds a new tool; provider prompt caches are invalidated then, not on every step or turn. On ReqLLM models with reasoning_binding: :prefix, opening the catalogue changes the prefix. The adapter retires affected reasoning and emits [:exagent, :model, :reasoning_prefix, :reset].

Use mode: :bridge to keep the offered catalogue fixed during discovery. The model uses search_tools to find names, describe_tool to read a tool's argument schema, and call_tool with its name and arguments. The runtime applies validation, permissions, approval and effects to the selected tool. Request capabilities can remove deferred tools from req.tools before the provider's tool list is built. The provider sees the original bridge call on replay.

Searching ranks by keyword overlap with tool names (weighted) and descriptions, using Unicode NFC for keywords and folding simple plurals. Keyword matching splits camelCase at Unicode case transitions and keeps combining marks within words, including vowel signs in Indic scripts. When neither matches, Search tries top-level argument names, then their descriptions if the names also yield no matches. Separate exact tool names with commas or whitespace, including newlines, to select them in catalogue order up to :max_results. A miss suggests up to 200 names in catalogue order and indicates when more exist. These suggestions do not load tools. Use ExAgent.Permissions for authority. In :load mode, put a request capability that filters deferred tools after Search so it sees loaded tools. Search reads names from plain result maps with string or atom keys, including %{tools: [%{name: "list_logs"}]}. If a hook shortens the result to text, Search reruns the query recorded in the preceding model call against the current catalogue and keyword rules. Rebuild live search capabilities after a tokenizer upgrade to refresh the catalogue's stored keywords. If before_tool_call changed that query, the recorded query can select different tools. In that case, preserve the structured result and apply ExAgent.Tool.OutputLimit to the other tools. Call IDs can be reused in later turns. A tool result or retry consumes the pending query for its ID, and a user part clears outstanding queries. If trimming removes the matching call, preserve the structured names or run the search again.

Options for new/1 include :deferred (required), :max_results (default 5), :name (default "search_tools") and :mode (:load by default, or :bridge). Bridge mode reserves "describe_tool" and "call_tool" in the tool inventory.

Summary

Functions

Build a search capability from a keyword list. Accepts :deferred (required), :name, :max_results (1 to 50), and :mode. Unknown keys, duplicate keys and invalid options raise ArgumentError.

The search tool, any bridge tools, and the deferred tools in the agent inventory.

Types

t()

@type t() :: %ExAgent.Tool.Search{
  bridge_tools: [ExAgent.Tool.t()],
  deferred: [ExAgent.Tool.t()],
  index: list(),
  max_results: pos_integer(),
  mode: :load | :bridge,
  names: MapSet.t(String.t()),
  tool: ExAgent.Tool.t()
}

Functions

new(opts)

@spec new(keyword()) :: t()

Build a search capability from a keyword list. Accepts :deferred (required), :name, :max_results (1 to 50), and :mode. Unknown keys, duplicate keys and invalid options raise ArgumentError.

tools(search)

@spec tools(t()) :: [ExAgent.Tool.t()]

The search tool, any bridge tools, and the deferred tools in the agent inventory.