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)Give every parameter the model should fill a type hint; defaults become optional arguments (an unannotated parameter is still exposed, typed only from its default). 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 a JSON-serializable value, or an object exposing model_dump / to_json. 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. CancelledError and Ctrl-C are the exception: they propagate.
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(agent_getter=None, max_depth=2), disable_subagent() / enable_subagent() | See Sub-agents. |
A name conflict raises unless one side declares override=True (a declared override also beats a later plain registration). With _XUN_INFO_TOOL_OVERRIDE set, the kept or skipped registration is logged instead of passing silently.
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; URLs are limited to http/https, plus file:// paths resolving inside the workspace) |
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). Outside the temp dir, overwriting, moving, copying onto or deleting an existing path goes through the confirmation policy unless auto_confirm is on; creating a new file or directory does not. /policy manages the allowlists, risky commands go through risk assessment.
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 is what marks the parameter as injected: bare ToolCallContext or generic ToolCallContext[dict] both work — the type parameter is a hint for ctx.value, and execute(context=…) defaults to None. Each call gets its own ToolCallContext, but all calls in one run share the same ctx.value, which is also what sub-agents inherit from their parent's tool call.