Skip to content

Built-in Tools

The worker registers six built-in tools that the LLM can call during a session. These tools handle shell execution, file operations, and codebase search.

Overview

ToolDescription
shell_execExecute a shell command
read_fileRead file contents (text or binary)
write_fileWrite content to a file
edit_fileFind-and-replace within a file
globFind files matching a glob pattern
grepSearch file contents with regex

In addition to the six worker-side tools, the server builds a skill tool when skills are available. See Skills for details.

Tool Reference

shell_exec

Execute a shell command and return combined stdout/stderr and exit code.

Commands run via the user's shell ($SHELL), with fallbacks to /bin/zsh (macOS), bash, or /bin/sh. Shells known to be incompatible (fish, nu) are skipped automatically.

ParameterTypeRequiredDefaultDescription
commandstringYes--Shell command to execute
cwdstringNoWorker workdirWorking directory for the command
timeoutnumberNo120,000 (120s)Timeout in milliseconds

Output format:

{combined stdout + stderr}

exit code: {exitCode}

Stdout and stderr are drained concurrently into a single buffer, providing chunk-level interleaving of both streams.

Timeout behavior: Sends SIGTERM to the entire process group, waits 200 ms for graceful shutdown, then sends SIGKILL if the process is still alive. Commands run in a detached process group for clean cleanup.

Truncation: The handler manages its own truncation. If combined output exceeds the truncation threshold, the full output is saved to .molf/tool-output/{toolCallId}.txt and a truncated preview is returned with a pointer to the full file.

read_file

Read the contents of a file. Supports optional line-range reading for large files. For binary files (images, PDFs, audio), returns the file as a base64 media attachment that can be inlined into the LLM context.

ParameterTypeRequiredDefaultDescription
pathstringYes--Absolute or relative path to the file
startLinenumberNo--First line to read (1-indexed, inclusive)
endLinenumberNo--Last line to read (1-indexed, inclusive)

write_file

Write content to a file. Creates the file if it does not exist, overwrites if it does.

ParameterTypeRequiredDefaultDescription
pathstringYes--Absolute or relative path to the file
contentstringYes--Content to write
createDirectoriesbooleanNofalseCreate parent directories if they do not exist

edit_file

Edit a file by replacing exact string matches. Fails if oldString is not found or matches multiple locations (unless replaceAll is true).

ParameterTypeRequiredDefaultDescription
pathstringYes--Absolute or relative path to the file
oldStringstringYes--Exact text to find
newStringstringYes--Replacement text
replaceAllbooleanNofalseReplace all occurrences instead of requiring a unique match

glob

Find files matching a glob pattern. Returns matching file paths sorted by modification time, newest first.

ParameterTypeRequiredDefaultDescription
patternstringYes--Glob pattern (e.g., **/*.ts, src/**/*.js)
pathstringNoWorker workdirDirectory to search in

grep

Search file contents using regex patterns. Uses ripgrep (rg) if available, falls back to system grep. Returns matching lines with file path and line number, sorted by file modification time.

ParameterTypeRequiredDefaultDescription
patternstringYes--Regex pattern to search for
pathstringNoWorker workdirFile or directory to search in
includestringNo--File glob filter (e.g., *.ts, *.{js,jsx})

Workdir Path Resolution

All built-in tools resolve relative paths against the worker's working directory. Some tools also default their path argument to the workdir when it is omitted:

ToolPath ArgumentDefaults to Workdir
shell_execcwdYes
read_filepathNo
write_filepathNo
edit_filepathNo
globpathYes
greppathYes

This resolution is handled by the ToolExecutor via pathArgs metadata on each tool. The LLM can use relative paths like src/main.ts and they resolve correctly against the workdir.

Tool Result Envelope

Every tool execution returns a result with these fields:

FieldTypeDescription
outputstringThe tool's text output
errorstring?Error message if execution failed
metaobject?Metadata: truncated, outputId, exitCode, outputPath
attachmentsAttachment[]?Binary attachments (e.g., images from read_file)

Truncation and Storage

Tool output is subject to truncation limits:

LimitValue
Maximum lines2,000
Maximum bytes50 KB

When a tool's output exceeds these limits, the truncateAndStore function:

  1. Saves the full output to {workdir}/.molf/tool-output/{toolCallId}.txt
  2. Returns a truncated preview with a message pointing to the full file
  3. Sets meta.truncated = true and meta.outputId on the result

The truncation message suggests using read_file with line offsets or grep to access specific sections of the full output.

Some tools (like shell_exec) manage their own truncation. When a handler explicitly sets meta.truncated (to true or false), the safety-net truncation pass is skipped.

Hooks

Tool execution fires two hooks through the plugin system:

  • before_tool_execute -- called before the handler runs; can modify arguments or block execution
  • after_tool_execute -- called after the handler returns; can modify the result

See Plugins for the full hook reference.

See Also