Skip to main content
Mastra 1.0 is available 🎉 Read announcement

Tools

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.

File tools
Direct link to File tools

view
Direct link to view

Read file contents with line numbers or list directory contents. The agent uses this tool before editing a file.

  • Displays line-numbered output (like cat -n) for easy reference.
  • For directories, lists files up to 2 levels deep, excluding hidden items.
  • Supports view_range to read specific line ranges in large files.
  • Output is truncated if the file exceeds the token limit. Use view_range to read specific sections.
ParameterTypeRequiredDescription
pathstringYesPath to the file or directory (relative to project root)
view_range[number, number]NoLine range [start, end] to view a section of the file

string_replace_lsp
Direct link to string_replace_lsp

Edit 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.

  • The agent reads the file first with view before editing.
  • old_str must be an exact substring of the current file content.
  • Include enough surrounding context to uniquely identify the replacement location.
  • When new_str is omitted or empty, the matched text is deleted.
  • After editing, TypeScript errors, linting warnings, and other diagnostics are returned automatically.
ParameterTypeRequiredDescription
pathstringYesPath to the file
old_strstringYesExact text to find and replace
new_strstringNoReplacement text (omit to delete the matched text)
start_linenumberNoLine number to narrow the search region

ast_smart_edit
Direct link to ast_smart_edit

Edit 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:

  • Pattern-based search and replace: Find and replace code using AST patterns with $VARIABLE placeholders (e.g., replace console.log($ARG) with logger.debug($ARG)).
  • Add/remove imports: Intelligently insert or remove import statements.
  • Rename functions: Rename function declarations and all call sites with scope awareness.
  • Rename variables: Rename variable declarations and all references.
  • Selector queries: Find AST nodes matching a CSS-like selector (e.g., "FunctionDeclaration").
ParameterTypeRequiredDescription
pathstringYesFile path relative to project root
patternstringNoAST pattern to search for (supports $VARIABLE placeholders)
replacementstringNoReplacement pattern (can use captured $VARIABLES)
selectorstringNoCSS-like selector for AST nodes
transformstringNoTransformation type: add-import, remove-import, rename-function, rename-variable, extract-function, inline-variable
targetNamestringNoName of the target element (for rename/remove operations)
newNamestringNoNew name (for rename operations)
importSpecobjectNoImport specification for add-import: { module, names, isDefault? }

write_file
Direct link to write_file

Create a new file or overwrite an existing file entirely.

  • Use for creating new files. For editing existing files, prefer string_replace_lsp.
  • Parent directories are created automatically if they don't exist.
  • If overwriting an existing file, the agent reads it first with view.
ParameterTypeRequiredDescription
pathstringYesFile path to write to (relative to project root)
contentstringYesFull content to write to the file

Search tools
Direct link to Search tools

search_content
Direct link to search_content

Search file contents using regular expressions. Uses ripgrep when available, falling back to grep.

  • Supports full regex syntax (e.g., "function\\s+\\w+", "import.*from").
  • Respects .gitignore automatically in Git repositories.
  • Always preferred over running grep or rg via execute_command.
ParameterTypeRequiredDescription
patternstringYesRegex pattern to search for in file contents
pathstringNoDirectory or file to search in (relative to project root, defaults to project root)
globstringNoGlob pattern to filter files (e.g., "*.ts", "*.{js,jsx}", "test/**")
contextLinesnumberNoNumber of lines to show before and after each match (default: 0)
maxResultsnumberNoMaximum number of matching lines to return (default: 100)
caseSensitivebooleanNoWhether the search is case-sensitive (default: true)

find_files
Direct link to find_files

Find files by name pattern using glob matching. Results are sorted by modification time (most recent first).

  • Supports standard glob syntax: * (any characters), ** (any path segments), ? (single character), {a,b} (alternatives).
  • Respects .gitignore automatically in Git repositories.
  • Always preferred over running find or ls via execute_command.
ParameterTypeRequiredDescription
patternstringYesGlob pattern (e.g., "**/*.ts", "src/**/*.test.*", "**/package.json")
pathstringNoDirectory to search in (relative to project root, defaults to project root)

Shell execution
Direct link to Shell execution

execute_command
Direct link to execute_command

Execute a shell command in the project directory.

  • Use for Git commands, package managers (npm, pnpm), build tools, test runners, Docker, and other terminal operations.
  • Commands run with a 30-second default timeout. Use the timeout parameter for longer operations.
  • Output is stripped of ANSI codes and streamed to the TUI in real time.
  • Pipe to | tail -N for commands with long output — full output streams to the user, but only the last N lines are returned to the agent.
  • The CI=true environment variable is set automatically to prevent interactive prompts.
ParameterTypeRequiredDescription
commandstringYesShell command to execute
cwdstringNoWorking directory (defaults to project root)
timeoutnumberNoTimeout in seconds (default: 30)

request_sandbox_access
Direct link to request_sandbox_access

Request access to directories outside the project root. The user is prompted to approve or deny the request.

ParameterTypeRequiredDescription
pathstringYesAbsolute path to the directory needing access
reasonstringYesExplanation of why access is needed

Web tools
Direct link to Web tools

Search the web for documentation, error messages, package APIs, and other information.

This tool is available through two providers:

  • Tavily: When the TAVILY_API_KEY environment variable is set.
  • Anthropic web search: Built into Anthropic models, used as a fallback when no Tavily key is configured.
ParameterTypeRequiredDescription
querystringYesThe search query
searchDepth"basic" | "advanced"NoSearch depth (default: "basic")
maxResultsnumberNoMaximum number of results (default: 10)
includeImagesbooleanNoInclude related images (default: false)

web_extract
Direct link to web_extract

Extract the full content of one or more web pages. Requires a Tavily API key (TAVILY_API_KEY).

ParameterTypeRequiredDescription
urlsstring[]YesURLs to extract content from (max 20)
extractDepth"basic" | "advanced"NoExtraction depth (default: "basic")
includeImagesbooleanNoInclude extracted image URLs (default: false)

Task management
Direct link to Task management

task_write
Direct link to task_write

Create or update a structured task list for tracking progress on complex, multi-step work.

  • Each task has a content (imperative form), status (pending, in_progress, completed), and activeForm (present continuous form shown during execution).
  • Pass the full task list each time — it replaces the previous list.
  • Only one task should be in_progress at a time.

task_check
Direct link to task_check

Check the completion status of the current task list. The agent uses this before finishing to verify all tasks are complete.

Interactive tools
Direct link to Interactive tools

ask_user
Direct link to ask_user

Ask 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_plan
Direct link to submit_plan

Submit 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.

MCP tools
Direct link to MCP tools

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.

Tool availability by mode
Direct link to Tool availability by mode

Not all tools are available in every mode:

ToolBuildPlanFast
viewYesYesYes
search_contentYesYesYes
find_filesYesYesYes
execute_commandYesRead-onlyYes
string_replace_lspYesNoYes
ast_smart_editYesNoYes
write_fileYesNoYes
web_searchYesYesYes
web_extractYesYesYes
task_writeYesYesYes
task_checkYesYesYes
ask_userYesYesYes
submit_planNoYesNo
request_sandbox_accessYesYesYes

In Plan mode, execute_command is available but restricted to read-only commands (git status, git log, git diff, etc.).

Permission system
Direct link to Permission system

Tools are grouped into four categories, each with a configurable approval policy:

CategoryToolsDefault policy
Readview, search_content, find_files, web_search, web_extractallow
Editstring_replace_lsp, ast_smart_edit, write_file, subagentask
Executeexecute_commandask
MCPAll MCP server toolsask

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.