Skip to main content

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

The build_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

The build_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). Returns: A dictionary with 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. Returns: A dictionary with 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’s Path.relative_to() to verify that the resolved path stays within the configured root. This prevents attacks like:
If a path escapes the root, the tool raises 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:
This ensures that even if the LLM attempts to use tools for unauthorized operations, the sandbox profile blocks execution before any I/O occurs.
  • Tools — Full tool system architecture, including the @tool decorator, 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.