docs/src/mastra-code/tools.mdx
Mastra Code provides built-in tools that the agent uses to interact with your codebase. Each tool is designed for a specific task, and the agent selects the right one based on what it needs to do.
viewRead file contents with line numbers or list directory contents. The agent uses this tool before editing a file.
cat -n) for easy reference.view_range to read specific line ranges in large files.view_range to read specific sections.| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | Path to the file or directory (relative to project root) |
view_range | [number, number] | No | Line range [start, end] to view a section of the file |
string_replace_lspEdit a file by replacing an exact text match with new content. Returns Language Server Protocol (LSP) diagnostics showing any errors or warnings introduced by the edit.
view before editing.old_str must be an exact substring of the current file content.new_str is omitted or empty, the matched text is deleted.path values are resolved against the project root.| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | Path to the file (relative to project root) |
old_str | string | Yes | Exact text to find and replace |
new_str | string | No | Replacement text (omit to delete the matched text) |
start_line | number | No | Line number to narrow the search region |
ast_smart_editEdit code using abstract syntax tree (AST) analysis for intelligent, structure-aware transformations. Powered by ast-grep, this tool understands code structure rather than treating files as raw text.
Supported transformations:
$VARIABLE placeholders (e.g., replace console.log($ARG) with logger.debug($ARG))."FunctionDeclaration").| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | File path relative to project root |
pattern | string | No | AST pattern to search for (supports $VARIABLE placeholders) |
replacement | string | No | Replacement pattern (can use captured $VARIABLES) |
selector | string | No | CSS-like selector for AST nodes |
transform | string | No | Transformation type: add-import, remove-import, rename-function, rename-variable, extract-function, inline-variable |
targetName | string | No | Name of the target element (for rename/remove operations) |
newName | string | No | New name (for rename operations) |
importSpec | object | No | Import specification for add-import: { module, names, isDefault? } |
write_fileCreate a new file or overwrite an existing file entirely.
string_replace_lsp.view.| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | File path to write to (relative to project root) |
content | string | Yes | Full content to write to the file |
search_contentSearch file contents using regular expressions. Uses ripgrep when available, falling back to grep.
"function\\s+\\w+", "import.*from")..gitignore automatically in Git repositories.grep or rg via execute_command.| Parameter | Type | Required | Description |
|---|---|---|---|
pattern | string | Yes | Regex pattern to search for in file contents |
path | string | No | Directory or file to search in (relative to project root, defaults to project root) |
glob | string | No | Glob pattern to filter files (e.g., "*.ts", "*.{js,jsx}", "test/**") |
contextLines | number | No | Number of lines to show before and after each match (default: 0) |
maxResults | number | No | Maximum number of matching lines to return (default: 100) |
caseSensitive | boolean | No | Whether the search is case-sensitive (default: true) |
find_filesFind files by name pattern using glob matching. Results are sorted by modification time (most recent first).
* (any characters), ** (any path segments), ? (single character), {a,b} (alternatives)..gitignore automatically in Git repositories.find or ls via execute_command.| Parameter | Type | Required | Description |
|---|---|---|---|
pattern | string | Yes | Glob pattern (e.g., "**/*.ts", "src/**/*.test.*", "**/package.json") |
path | string | No | Directory to search in (relative to project root, defaults to project root) |
execute_commandExecute a shell command in the project directory.
npm, pnpm), build tools, test runners, Docker, and other terminal operations.timeout parameter for longer operations.| tail -N for commands with long output — full output streams to the user, but only the last N lines are returned to the agent.CI=true environment variable is set automatically to prevent interactive prompts.| Parameter | Type | Required | Description |
|---|---|---|---|
command | string | Yes | Shell command to execute |
cwd | string | No | Working directory (defaults to project root) |
timeout | number | No | Timeout in seconds (default: 30) |
request_sandbox_accessRequest access to directories outside the project root. The user is prompted to approve or deny the request.
| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | Absolute path to the directory needing access |
reason | string | Yes | Explanation of why access is needed |
web_searchSearch the web for documentation, error messages, package APIs, and other information.
This tool is available through three providers:
TAVILY_API_KEY environment variable is set (takes priority).| Parameter | Type | Required | Description |
|---|---|---|---|
query | string | Yes | The search query |
searchDepth | "basic" | "advanced" | No | Search depth (default: "basic") |
maxResults | number | No | Maximum number of results (default: 10) |
includeImages | boolean | No | Include related images (default: false) |
web_extractExtract the full content of one or more web pages. Requires a Tavily API key (TAVILY_API_KEY).
| Parameter | Type | Required | Description |
|---|---|---|---|
urls | string[] | Yes | URLs to extract content from (max 20) |
extractDepth | "basic" | "advanced" | No | Extraction depth (default: "basic") |
includeImages | boolean | No | Include extracted image URLs (default: false) |
task_writeCreate or update a structured task list for tracking progress on complex, multi-step work.
content (imperative form), status (pending, in_progress, completed), and activeForm (present continuous form shown during execution).in_progress at a time.task_checkCheck the completion status of the current task list. The agent uses this before finishing to verify all tasks are complete.
ask_userAsk the user a structured question. The agent uses this when it needs clarification, wants to validate assumptions, or needs the user to make a decision. Questions render inline in the conversation flow and support optional multiple-choice options.
submit_planSubmit a structured implementation plan for user review and approval (Plan mode only). The plan renders inline, and the user can approve, reject, or request changes.
When MCP servers are configured, their tools are automatically available to the agent. MCP tools are namespaced as serverName_toolName to avoid collisions with built-in tools.
MCP tools fall under the mcp permission category. When YOLO mode is disabled, they require approval before execution.
Not all tools are available in every mode:
| Tool | Build | Plan | Fast |
|---|---|---|---|
view | Yes | Yes | Yes |
search_content | Yes | Yes | Yes |
find_files | Yes | Yes | Yes |
execute_command | Yes | Read-only | Yes |
string_replace_lsp | Yes | No | Yes |
ast_smart_edit | Yes | No | Yes |
write_file | Yes | No | Yes |
web_search | Yes | Yes | Yes |
web_extract | Yes | Yes | Yes |
task_write | Yes | Yes | Yes |
task_check | Yes | Yes | Yes |
ask_user | Yes | Yes | Yes |
submit_plan | No | Yes | No |
request_sandbox_access | Yes | Yes | Yes |
In Plan mode, execute_command is available but restricted to read-only commands (git status, git log, git diff, etc.).
Tools are grouped into four categories, each with a configurable approval policy:
| Category | Tools | Default policy |
|---|---|---|
| Read | view, search_content, find_files, web_search, web_extract | allow |
| Edit | string_replace_lsp, ast_smart_edit, write_file, subagent | ask |
| Execute | execute_command | ask |
| MCP | All MCP server tools | ask |
When YOLO mode is enabled (the default), all categories are set to allow. When disabled, the agent prompts for approval based on the category policy.
You can configure per-category or per-tool overrides through /settings, and session-level grants let you approve a category once for the rest of the session.
:::note
Visit Modes for details on how modes affect tool access and approval behavior.
:::