tools
js/ai/tools.ts
fino:ai/tools — workspace file and shell tools for coding agents, bounded by explicit grants.
fino:ai/tool defines one tool; this module ships the set a coding agent
needs to work in a project: list_files, read_file and search_files,
plus the mutating write_file, edit_file and shell. Each is an ordinary
Tool, so the set can be handed to an Agent, mounted on an MCP server, or
invoked directly.
Grants
Every tool is bounded by a SandboxFilesystemGrant, the same grant shape
fino:ai/sandbox uses, checked by the same canonical-path rule. Relative
paths resolve against cwd. Reads must resolve, after following links,
beneath a read root; writes and edits beneath a write root. The default
grant is cwd for both. Pass filesystem to widen it (a neighbouring
checkout, a scratch directory) or narrow it (write: [] for a read-only
set). The check bounds what a model asks for; it is not a defence against a
hostile local process racing the filesystem.
shell runs /bin/sh -c under the runtime's strict process sandbox, with
the same read and write roots, read-only system roots (/bin, /usr and
the like, adjustable through systemRoots), and all network access denied.
Toolchains installed elsewhere, such as Homebrew or Xcode command-line
tools, need their directories in the read grant. Where a strict
backend is unavailable the tool fails closed and reports why rather than
running unconfined. A caller that wants a different policy, including
granting network access or choosing bestEffort, passes sandbox
explicitly. Commands see only the environment variables in environment,
which defaults to PATH, HOME, LANG and TERM from this process.
commandTool() is the mechanism under shell: it turns any program and an
argument shape into a tool with the same sandbox, timeout, cancellation and
capped-output contract, running the program directly from an argument
vector rather than through a shell. Use it to give an agent a narrow,
typed command (git status, a project's test runner) instead of shell.
Approval and limits
Mutating tools declare requiresApproval unless approval is 'never',
so a Session suspends for a human decision before they run. Outputs are
capped (48 000 characters per result, 2 000 lines per read, 500 listed
paths, 200 search matches) so one call cannot flood a context window, and
each cap is reported in the result so the model can page or narrow.
import { agent } from 'fino:ai/agent';
import { openai } from 'fino:ai/model';
import { createWorkspaceTools } from 'fino:ai/tools';
const coder = agent({
model: openai({ model: 'gpt-4o' }),
tools: createWorkspaceTools({
cwd: '/repo',
filesystem: { read: ['/repo', '/opt/homebrew'], write: ['/repo'] },
}),
});Interfaces
interface WorkspaceToolOptions {
Options shared by every workspace tool.
import { readFileTool } from 'fino:ai/tools';
const read = readFileTool({ cwd: '/repo', filesystem: { read: ['/repo'] } });Properties
cwd: string
Absolute workspace root that relative tool paths resolve against.
filesystem?: SandboxFilesystemGrant
Roots the tools may read and write. Defaults to cwd for both; an
explicit grant is used exactly as given, so omit write for read-only.
approval?: 'mutations' | 'never'
'mutations' (the default) marks write_file, edit_file and shell
with requiresApproval; 'never' runs them without suspending.
interface ShellToolOptions extends WorkspaceToolOptions {
Options for shellTool().
import { shellTool } from 'fino:ai/tools';
const shell = shellTool({ cwd: '/repo', environment: { PATH: '/usr/bin:/bin' } });Properties
environment?: Record<string, string>
Complete environment for commands. Defaults to PATH, HOME, LANG and
TERM copied from this process when set.
sandbox?: ProcessSandboxOptions
Process sandbox policy, replacing the default derived from filesystem
(strict mode, read and write roots, all network denied).
systemRoots?: string[]
Read-only system roots added to the default policy so commands can find
and load system binaries. Defaults to /bin, /sbin, /usr and
/private/etc on macOS, plus /lib, /lib64 and /etc on Linux; roots
missing on the host are skipped. Ignored when sandbox is given.
maxTimeoutMs?: number
Longest timeout a call may request, in milliseconds. Defaults to 600 000.
interface WorkspaceToolsOptions extends WorkspaceToolOptions {
Options for createWorkspaceTools().
import { createWorkspaceTools, type WorkspaceToolsOptions } from 'fino:ai/tools';
const planning: WorkspaceToolsOptions = { cwd: '/repo', writes: false };
const tools = createWorkspaceTools(planning);Properties
writes?: boolean
Include write_file, edit_file and shell. Defaults to true.
shell?:
| Pick<ShellToolOptions, 'environment' | 'sandbox' | 'systemRoots' | 'maxTimeoutMs'>
| false
Shell-specific options, or false to omit shell from a writable set.
interface CommandToolOptions<Args extends object> extends ShellToolOptions {
Options for commandTool(): the shell options plus the command's name,
description, arguments and argument vector.
import { commandTool, type CommandToolOptions } from 'fino:ai/tools';
import { v } from 'fino:validate';
const options: CommandToolOptions<{ path: string }> = {
cwd: '/repo',
name: 'word_count',
description: 'Count the lines, words and bytes in a project file.',
parameters: { path: v.string().describe('File path relative to the project root') },
command: ({ path }) => ['/usr/bin/wc', '--', path],
mutates: false,
};
const wordCount = commandTool(options);Properties
name: string
Tool name shown to the model.
description: string
What the command does and when to use it.
parameters?: Record<string, unknown>
Property shape of the call arguments, as passed to v.object(). The tool
adds an optional timeoutMs property, so the shape may not declare one.
command: (args: Args) => string[] | Promise<string[]>
Argument vector for one validated call, or a promise of one. The first element is the absolute path of the program; it is run directly, not through a shell. Throwing (or rejecting) refuses the call with the error message as its result.
mutates?: boolean
Whether a call may change files or run project code. Mutating tools are
marked with side effects and, unless approval is 'never',
requiresApproval. Defaults to true.
risk?: string
Risk category for approval UIs. Defaults to 'executes commands'.
Functions
function listFilesTool(opts: WorkspaceToolOptions): Tool
Build list_files: glob for file paths beneath cwd.
Matches outside the read roots are omitted. Results are sorted, reported
relative to cwd, and capped at 500 entries. Read-only.
import { listFilesTool } from 'fino:ai/tools';
const list = listFilesTool({ cwd: '/repo' });function readFileTool(opts: WorkspaceToolOptions): Tool
Build read_file: read a text file with line numbers.
Long lines are clipped to 500 characters, a page is at most 2 000 lines, and the result names the offset to continue from. Read-only.
import { readFileTool } from 'fino:ai/tools';
const read = readFileTool({ cwd: '/repo' });function searchFilesTool(opts: WorkspaceToolOptions): Tool
Build search_files: grep beneath a directory with a regular expression.
The walk starts at path (default cwd), which must be beneath a read
root. It never follows links, so it cannot leave that directory, and it
skips VCS metadata and common build output, files over 1 MiB, and files
with NUL bytes in their first 4 KiB. Results are capped at 200 matches.
Read-only.
import { searchFilesTool } from 'fino:ai/tools';
const search = searchFilesTool({ cwd: '/repo' });function writeFileTool(opts: WorkspaceToolOptions): Tool
Build write_file: create or overwrite one file beneath a write root.
Missing parent directories are created. Requires approval unless approval
is 'never'.
import { writeFileTool } from 'fino:ai/tools';
const write = writeFileTool({ cwd: '/repo' });function editFileTool(opts: WorkspaceToolOptions): Tool
Build edit_file: replace exact text in one existing file beneath a write root.
oldText must match exactly once unless replaceAll is set, so an
ambiguous edit fails instead of guessing. Requires approval unless
approval is 'never'.
import { editFileTool } from 'fino:ai/tools';
const edit = editFileTool({ cwd: '/repo' });function commandTool<Args extends object>(opts: CommandToolOptions<Args>): Tool
Build a tool that runs one program from cwd under the strict process
sandbox, with arguments derived from the call.
The program runs directly from the argument vector command returns, with
the grant's read and write roots, read-only systemRoots, no network
access, and only environment (or under sandbox when given). It is killed
on timeout or run cancellation. Descendants it started are not, because the
runtime has no process-group kill yet, but the tool stops reading their
output 1 s after the program exits, so a background process cannot hold a
call open. The result reports the exit code and capped stdout and stderr; a
non-zero exit, a timeout, an invalid argument vector, or a sandbox that
cannot be enforced is an error result.
import { commandTool } from 'fino:ai/tools';
const status = commandTool<Record<string, never>>({
cwd: '/repo',
name: 'git_status',
description: 'Show the working tree status.',
command: () => ['/usr/bin/git', 'status', '--short'],
mutates: false,
});function shellTool(opts: ShellToolOptions): Tool
Build shell: run a command line from cwd under the strict process
sandbox.
shell is the commandTool() that runs /bin/sh -c <command>, so it has
the same sandbox, environment, timeout, cancellation and output contract.
Requires approval unless approval is 'never'.
import { shellTool } from 'fino:ai/tools';
const shell = shellTool({ cwd: '/repo' });function createWorkspaceTools(opts: WorkspaceToolsOptions): Tool[]
Build the workspace tool set.
Always includes list_files, read_file and search_files. Unless
writes is false, also includes write_file, edit_file and, unless
shell is false, shell. Every tool shares one grant, cwd and
approval policy.
import { createWorkspaceTools } from 'fino:ai/tools';
const planning = createWorkspaceTools({ cwd: '/repo', writes: false });
const unattended = createWorkspaceTools({ cwd: '/repo', approval: 'never', shell: false });