English
Tools: Attributes & Context
A tool is a plain Python function. ToolBox derives the OpenAI tool schema from its signature and docstring — the docstring becomes the description, the parameters become the JSON schema.
python
from xun import ToolBox
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
toolbox = ToolBox().register(multiply)Type hints are required for anything the model should fill in; defaults become optional arguments. Extra keys in the arguments are rejected (extra="forbid"), and sequence arguments are forgiving: a model that sends "[1, 2]" as a string still gets a list.
Return any JSON-serializable value. To report a failure in a way the model can read, return Result.Err(ErrorInfo(...)) — or simply raise: registration wraps the function so an exception becomes an error result in the transcript instead of killing the run.
ToolBox
| Method | Notes |
|---|---|
register(*funcs) | Wrap and register plain functions. |
tool | Decorator form: @toolbox.tool. |
register_raw(*functions) | Register prepared Function objects. |
with_defaults(*sets) | Register built-in sets; no arguments = all of them. Chainable. |
pop(name) | Remove a tool entirely. |
disable(*names_or_globs) / enable(...) | Keep the registration but hide it from the model; */? patterns allowed. |
is_disabled(name), list_tools(capabilities=None, include_disabled=False) | Introspection; capability filtering is applied by the loop against config.model.capabilities. |
clone() | Shallow copy — what Agent.inherit uses. |
with_subagent_provider(...), disable_subagent() | See Sub-agents. |
Name conflicts raise, unless one side declares override=True.
Built-in tool sets
| Set | Tools |
|---|---|
fs | list_dir, read_file, file_info, write_file, move, copy, mkdir, delete, temp_dir, glob, grep, request_image |
patch | apply_patch (git-style unified diff, applied with git apply) |
cmd | bash, bash_spawn, bash_wait, bash_running, bash_kill — not registered on Windows |
search | web_search (browser-driven web search) |
browser | browser_page, browser_resize, browser_snapshot, browser_interact, browser_evaluate, browser_logs, browser_screenshot (Playwright; needs playwright install) |
system | system_info, datetime, sleep |
framework | ask_preference, extract_compacted_tool_result |
diagnostic | check_syntax, check_lint |
File tools scope themselves to the agent's Workspace (workdir plus its temp dir), and writes/deletions and risky commands go through the confirmation policy unless auto_confirm is on; /policy manages the allowlists.
Tool attributes
@tool_attr attaches metadata without changing how the function is called:
python
from xun import tool_attr
@tool_attr(name="MultiplyTool", required_capabilities=["vision"], override=False)
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""| Field | Effect |
|---|---|
name | The name the model sees; the Python name stays yours (the built-ins use short names like grep for fs_grep_files). |
required_capabilities | The tool is only offered when the model's config.model.capabilities include them (currently vision — e.g. screenshot and image tools). |
override | Replace an already-registered tool of the same name instead of raising. An override wins over a later plain registration, which is how an extension swaps a built-in. |
Tool context
A tool that needs the agent, the tool name, or per-run data declares a ToolCallContext parameter; the framework injects it and excludes it from the schema.
python
from xun import ToolCallContext as Context, setup_agent, NullDisplay
import datetime
from pathlib import Path
def workdir_name(context: Context[dict]):
"""Name of the agent's working directory."""
context.value["tool_call_time"] = datetime.datetime.now().strftime("%H:%M:%S")
return context.agent.workspace.workdir.name
state = {"tool_call_time": "?"}
agent = setup_agent(tools=[workdir_name], display=NullDisplay())
agent.instruct("What is your work directory's name?").execute(context=state)
print(state) # the tool wrote into it| Member | Contents |
|---|---|
ctx.agent | The running agent — workspace, config, state, check_cancel(), display helpers. |
ctx.tool_name | The registered name of the tool being called. |
ctx.value | Whatever was passed as execute(context=…). It is shared by all tools in the run, so use it as scratch space or a results bag. |
The annotation may be bare ToolCallContext or generic ToolCallContext[dict] — the type parameter is a hint for ctx.value, and execute(context=…) defaults to None. Tools called by the same run share one context object, which is also what sub-agents inherit from their parent's tool call.