What this snippet demonstrates
AFK ships prebuilt tools for common runtime operations like listing directories and reading files. These tools are designed with security-first defaults: every tool is scoped to an explicit root directory that prevents directory traversal attacks. This snippet shows how to create, configure, and compose prebuilt tools with agents and policy guards.Building runtime tools
Thebuild_runtime_tools() factory creates a set of filesystem tools bound to a specific root directory. All path operations within these tools are resolved against this root, and any attempt to access files outside it raises a FileAccessError.
Available prebuilt tools
Thebuild_runtime_tools() factory produces two tools:
list_directory
Lists entries in a directory under the configured root. Returns entry names, paths, and type flags (file or directory).| Parameter | Type | Default | Description |
|---|---|---|---|
path | str | "." | Relative path to list, resolved against the root directory. |
max_entries | int | 200 | Maximum entries to return (1—5000). Prevents unbounded listings. |
root, path, and entries (list of {name, path, is_dir, is_file}).
read_file
Reads the contents of a file under the configured root, with configurable truncation to prevent excessive token consumption.| Parameter | Type | Default | Description |
|---|---|---|---|
path | str | (required) | Relative path to the file, resolved against the root directory. |
max_chars | int | 20_000 | Maximum characters to read (1—500,000). Content is truncated beyond this limit. |
root, path, content, and truncated (boolean indicating whether content was truncated).
Security: directory traversal prevention
Every path operation is validated with an internal containment check that uses Python’sPath.relative_to() to verify that the resolved path stays within the configured root. This prevents attacks like:
FileAccessError immediately, before any file I/O occurs.
Composing with policy checks
For additional security, pair runtime tools with a policy engine that gates specific operations on approval:Composing with custom tools
You can combine prebuilt tools with your own custom tools in a single agent:Command allowlists and sandbox profiles
For production environments, restrict tool capabilities further using sandbox profiles:What to read next
- Tools — Full tool system architecture, including the
@tooldecorator,ToolResult, and execution pipeline. - Snippet 06: Tool Registry Security — Security scoping, policy gates, and sandbox profiles in detail.
- Security Model — Threat model, defense layers, and RunnerConfig security fields.