Tools
Tools are registered by name for the whole process. An agent gets a tool when you pass its ToolDesc to AgentBuilder.tool or tools, and Ailoy runs the function registered under that name. Built-in tools are registered from the start; Built-in tools lists them. For explanations and a full example, see Tools.
When a call fails, Python raises ailoy.AiloyError, Node.js throws an Error with code set to "AILOY_ERROR", and Rust returns an Err. Each entry’s Raises line says when. See Errors.
| Function | Does |
|---|---|
register_tool |
Registers a function and returns its description. |
set_tool_provider |
Sets or replaces the function behind a tool name. |
remove_tool_provider |
Removes a tool’s function. |
register_mcp_stdio |
Starts a local MCP server and registers its tools. |
register_mcp_streamable_http |
Connects to a remote MCP server and registers its tools. |
unregister_mcp |
Removes an MCP server’s tools and closes it. |
register_tool
Registers a function as a tool and returns its description, ready to pass to AgentBuilder.tool. Node.js: registerTool. In Rust, build the description with ToolDescBuilder and register the function with set_tool_provider.
ailoy.register_tool(desc: ToolDesc, func: Callable[..., Any | Awaitable[Any]]) -> ToolDescregisterTool(desc: ToolDesc, func: (args: any) => unknown): ToolDescuse ailoy::tool::ToolDescBuilder;
let desc = ToolDescBuilder::new("order_status") .description("Look up an order by its id.") .parameters(to_value!({"type": "object", "properties": {"order_id": {"type": "string"}}})) .build();ToolDescBuilder also takes returns(schema). Without parameters, the schema is Value::Null. sanitize_tool_name(name) replaces every character outside [A-Za-z0-9_-], the set every model API accepts, with _. Names can be up to 64 characters (MAX_TOOL_NAME_LEN), OpenAI’s limit.
| Parameter | Type | Description |
|---|---|---|
desc |
ToolDesc |
The tool’s name, description and parameters (a JSON schema). |
func |
function | Python: called with keyword arguments named as in the schema; may be async. Node.js: called with one object; may return a promise. Must return JSON-compatible data. |
Returns the ToolDesc. Registering a name that already exists replaces its function for every agent built afterwards. If func raises, the model receives the error as the tool result.
Example
from ailoy import register_tool
def order_status(order_id: str) -> dict: return {"status": "shipped"}
desc = register_tool( { "name": "order_status", "description": "Look up an order's status by its id.", "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], }, }, order_status,)agent = await AgentBuilder("anthropic/claude-haiku-5-5").tool(desc).build()const { registerTool } = require('@brekkylab/ailoy')
const desc = registerTool( { name: 'order_status', description: "Look up an order's status by its id.", parameters: { type: 'object', properties: { order_id: { type: 'string' } }, required: ['order_id'], }, }, ({ order_id }) => ({ status: 'shipped' }),)const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').tool(desc).build()use ailoy::{datatype::Value, to_value, tool_func, tool::{ToolDescBuilder, set_tool_provider}};
let desc = ToolDescBuilder::new("order_status") .description("Look up an order's status by its id.") .parameters(to_value!({ "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"] })) .build();set_tool_provider("order_status", tool_func!(|args: Value| -> Value { to_value!({"status": "shipped"}) }));let agent = AgentBuilder::new("anthropic/claude-haiku-5-5").tool(desc).build().await?;set_tool_provider
Sets the function for a tool name without returning a description. Use it when a saved spec already has the description, or in Rust. Node.js: setToolProvider.
ailoy.set_tool_provider(name: str, func: Callable[..., Any | Awaitable[Any]]) -> NonesetToolProvider(name: string, func: (args: any) => unknown): voiduse ailoy::{tool::set_tool_provider, tool_func};
fn set_tool_provider(name: impl Into<String>, provider: impl Into<ToolProvider>)
set_tool_provider("order_status", tool_func!(|args: Value| -> Value { /* ... */ }));Write the function
A function, or an async function, called with the arguments as keyword arguments. Return JSON-compatible data.
async def order_status(order_id: str): return {"status": "shipped"}A function called with the arguments as one object. It may return a promise. Return JSON-compatible data.
async function orderStatus({ order_id }) { return { status: 'shipped' }}tool_func! builds the function, a ToolFunc, from a closure. The closure takes args: Value, then optionally id: String, then optionally sandbox: &Sandbox. It returns a Value, which becomes the tool result, or, when it takes id, a whole Message.
| Form | Body |
|---|---|
tool_func!(|args: Value| -> Value { … }) |
Returns the result. |
tool_func!(async |args: Value| -> Value { … }) |
Can use .await. |
tool_func!(stream |args: Value| -> Value { … }) |
Returns a stream of values. Each becomes a tool message. |
The async and stream forms take with [name = expr, ...] before the body, to clone a captured variable for each call:
tool_func!(async |args: Value| -> Value with [client = client.clone()] { // `client` here is this call's clone to_value!({"status": "shipped"})})ToolFunc::new and ToolFunc::new_with_sandbox build one from a closure that returns a BoxStream<MessageOutput>, for cases the macro doesn’t cover. set_tool_provider also takes a ToolProvider:
pub enum ToolProvider { Function(ToolFactory), // Arc<dyn Fn(&ToolDesc) -> anyhow::Result<ToolFunc>> MCP(MCPToolEntry),}ToolProvider::function(f) builds the function from the description the spec asks for, and an error fails the agent’s build. create_tool(&desc) builds a tool through its provider.
Use the agent’s sandbox
A Python tool isn’t passed the sandbox. To work in it, call the virtx Sandbox you gave the agent from your function, with virtx’s own methods.
A Node.js tool isn’t passed the sandbox. To work in it, call the virtx Sandbox you gave the agent from your function, with virtx’s own methods.
A closure that takes sandbox: &Sandbox receives the agent’s sandbox. ailoy::tool has the functions the built-in tools use in it, which return virtx::Error on failure:
| Function | Does |
|---|---|
exec(&sandbox, argv, timeout_ms) |
Runs argv without a shell and waits for it to end. Returns an ExecOutput. |
read(&sandbox, path) |
The whole file, as Vec<u8>. |
write(&sandbox, path, content) |
Writes the whole file, creating its folder if needed. |
resolve(&sandbox, path) |
path from the root without its leading /, as the virtx files API names it. |
ExecOutput has code, stdout, stderr, truncated (the output was cut), timed_out, and reason (why the sandbox killed the command; None when it ended by itself).
use ailoy::tool::exec;
set_tool_provider("python_version", tool_func!(async |args: Value, sandbox: &Sandbox| -> Value { match exec(sandbox, ["python3", "--version"], Some(10_000)).await { Ok(out) => Value::string(out.stdout), Err(e) => Value::string(format!("error: {e}")), }}));remove_tool_provider
Removes a tool’s function. Node.js: removeToolProvider.
ailoy.remove_tool_provider(name: str) -> boolremoveToolProvider(name: string): booleanfn remove_tool_provider(name: &str) -> Option<ToolProvider>Returns whether the name was registered (Rust: the removed provider).
register_mcp_stdio
Starts an MCP server as a process on this machine and registers each of its tools as <prefix>__<name>. Node.js: registerMcpStdio. See Use MCP servers.
async def register_mcp_stdio(prefix: str, command: str, args: list[str] = []) -> list[ToolDesc]registerMcpStdio(prefix: string, command: string, args?: Array<string>): Promise<Array<ToolDesc>>async fn register_mcp_stdio( prefix: impl AsRef<str>, command: impl AsRef<str>, args: impl IntoIterator<Item = impl AsRef<str>>,) -> anyhow::Result<Vec<ToolDesc>>| Parameter | Type | Description |
|---|---|---|
prefix |
string | Added to each tool’s name, so tools from different servers don’t clash. |
command |
string | The program that starts the server, such as "uvx" or "npx". |
args |
list of strings | Arguments to the command, such as ["mcp-server-fetch"]. |
Returns the descriptions of the server’s tools, to pass to AgentBuilder.tools. The server runs with your program’s access, so only register servers you trust.
Raises if the command isn’t installed or the server exits at startup. See MCP troubleshooting.
Example
from ailoy import register_mcp_stdio
tools = await register_mcp_stdio("fetch", "uvx", ["mcp-server-fetch"])agent = await AgentBuilder("anthropic/claude-haiku-5-5").tools(tools).build()const { registerMcpStdio } = require('@brekkylab/ailoy')
const tools = await registerMcpStdio('fetch', 'uvx', ['mcp-server-fetch'])const agent = await new AgentBuilder('anthropic/claude-haiku-5-5').tools(tools).build()use ailoy::tool::register_mcp_stdio;
let tools = register_mcp_stdio("fetch", "uvx", ["mcp-server-fetch"]).await?;let agent = AgentBuilder::new("anthropic/claude-haiku-5-5").tools(tools).build().await?;register_mcp_streamable_http
Connects to a remote MCP server over Streamable HTTP and registers each of its tools as <prefix>__<name>. Node.js: registerMcpStreamableHttp.
async def register_mcp_streamable_http(prefix: str, url: str) -> list[ToolDesc]registerMcpStreamableHttp(prefix: string, url: string): Promise<Array<ToolDesc>>async fn register_mcp_streamable_http(prefix: impl AsRef<str>, url: impl AsRef<str>) -> anyhow::Result<Vec<ToolDesc>>| Parameter | Type | Description |
|---|---|---|
prefix |
string | Added to each tool’s name, so tools from different servers don’t clash. |
url |
string | The server’s Streamable HTTP endpoint. |
Returns the descriptions of the server’s tools, to pass to AgentBuilder.tools.
Raises if the server can’t be reached.
Example
from ailoy import register_mcp_streamable_http
tools = await register_mcp_streamable_http("deepwiki", "https://mcp.deepwiki.com/mcp")const { registerMcpStreamableHttp } = require('@brekkylab/ailoy')
const tools = await registerMcpStreamableHttp('deepwiki', 'https://mcp.deepwiki.com/mcp')use ailoy::tool::register_mcp_streamable_http;
let tools = register_mcp_streamable_http("deepwiki", "https://mcp.deepwiki.com/mcp").await?;unregister_mcp
Removes all the tools registered with a prefix and closes the connection to the server. Registered servers otherwise stay connected until your program exits. Node.js: unregisterMcp.
ailoy.unregister_mcp(prefix: str) -> intunregisterMcp(prefix: string): numberfn unregister_mcp(prefix: impl AsRef<str>) -> usizeReturns how many tools were removed. An unknown prefix removes nothing.