Tools

Tools are the primary mechanism through which agents interact with the environment. Each tool implements a common trait and is registered in a central registry that the backend session uses for tool calls.

Tool Trait

The Tool trait defines the interface every tool must implement:

#![allow(unused)]
fn main() {
pub trait Tool: Send + Sync {
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn input_schema(&self) -> serde_json::Value;
    fn execution_mode(&self) -> ExecutionMode { ExecutionMode::Native }
    fn execute(&self, input: Value, ctx: ToolContext)
        -> Pin<Box<dyn Future<Output = ToolResult> + Send + '_>>;
}
}
MethodDescription
name()Unique identifier for the tool (e.g., "bash", "read")
description()Human-readable description shown to the LLM
input_schema()JSON Schema defining the expected input parameters
execution_mode()Native (in-process) or Sandboxed (WASM) — defaults to Native
execute()Async execution function taking input JSON and a ToolContext

ToolResult

Every tool execution returns a ToolResult:

FieldTypeDescription
contentVec<ToolResultContent>One or more content blocks
is_errorboolWhether this result represents an error (signals retry to the LLM)

Content blocks can be:

  • Text — String content
  • Image — Base64-encoded image with media type

Helper constructors:

  • ToolResult::text(s) — Creates a successful text result
  • ToolResult::error(s) — Creates an error text result with is_error = true

ExecutionMode

ModeDescription
NativeRuns in-process with full system access
SandboxedRuns in a WASM sandbox with resource limits and capability restrictions

ToolContext

The ToolContext struct provides execution context to tools:

FieldDescription
working_dirThe agent's worktree directory
agent_nameName of the executing agent
session_idCurrent session ID
env_varsEnvironment variables to inject
cancellation_tokenToken to check if the session has been cancelled
permissionsOptional PermissionEvaluator for permission checks

The context supports fluent building with with_env() and with_permissions().

ToolRegistry

The ToolRegistry manages tool registration and lookup:

MethodDescription
register(tool)Add a tool to the registry (insertion-order preserved)
get(name)Look up a tool by name
names()List all registered tool names
definitions()Return tool definitions (name, description, schema) for the LLM
execute(name, input, ctx)Execute a tool by name
retain(predicate)Remove tools that don't match a predicate
register_mcp_tools(tools)Register tools from MCP servers
register_wasm_tools(tools)Register WASM sandboxed tools (feature-gated)

Default Registry

default_registry() creates a registry pre-populated with all built-in tools.

Built-in Tools

Swarm ships with these native tools:

ToolDescription
bashExecute shell commands
readRead file contents
writeWrite/create files
editEdit files with search-and-replace
globFind files by pattern
grepSearch file contents with regex
notebookEdit Jupyter notebook cells
web_fetchFetch and process web content
web_searchSearch the web
ask_userAsk the operator a question
mailboxSend messages to other agents
sub_agentDelegate tasks to sub-agents
taskInteract with the task system
skillExecute a skill by name
mcp_proxyProxy tool calls to MCP servers
workflow_outputReport outputs from workflow stages

WASM Tools

When the wasm-sandbox feature is enabled, additional tools can be loaded from compiled WebAssembly components. These run in a sandboxed environment with configurable resource limits and capabilities. See WASM Tools for details.