Skip to main content
Prerequisites
  • Python 3.10 or higher
  • PraisonAI Agents package installed
  • ast-grep CLI (pip install ast-grep-cli or npm install -g @ast-grep/cli)

AST-Grep Tools

Use AST-Grep Tools to search, analyze, and rewrite code using structural patterns instead of regex.
Unlike regex, AST patterns understand code structure — they won’t match patterns inside comments or strings. $VAR captures a single node, $$$ captures multiple nodes.
1

Install Dependencies

Install PraisonAI Agents and ast-grep:
2

Import Components

Import the AST-grep tools:
3

Create Agent

Create a code analysis agent:
4

Run Agent

Start the agent:

How It Works

The user describes a refactor or search; the agent runs AST-grep search, rewrite, or scan tools on the codebase.

Quick Start

1

Simple Usage

2

With Configuration


Available Functions

Function Details

ast_grep_search(pattern, lang, path=”.”, json_output=True)

Searches code using AST patterns. Returns structured JSON results.
A path that starts with - (e.g. -my-dir) is passed as a literal path, not a flag. The tool inserts -- before the path in the sg argv.

ast_grep_rewrite(pattern, replacement, lang, path=”.”, dry_run=True)

Rewrites code matching an AST pattern. Dry-run by default (shows changes without modifying files).

Return values

The Applied N changes receipt confirms that files were rewritten. No changes made now really means nothing was written — trust both messages.
Setting dry_run=False will modify files in place. Always preview first with the default dry_run=True.

ast_grep_scan(path=”.”, rule_file=None)

Scans code using YAML-based lint rules.

Path Confinement

path and rule_file are constrained to the current workspace. Anything that resolves outside is rejected before sg runs — no destructive rewrite can escape the project.
If you previously passed absolute paths or ..-traversal paths to reach sibling projects, those calls now return an error. Run the tools from inside the workspace you want to touch.

Output Limits

Each call returns at most 10,000 characters. When sg produces more (millions of matches against a big tree are common), the full buffer is written to a workspace-local artifact and the returned string is a head + tail preview plus an unmissable pointer.

Autonomy Mode

AST-grep tools are automatically included when using autonomy mode:
In autonomy mode, ast-grep tools are available without explicit tools= — the agent can use them whenever it decides to search or rewrite code.

Graceful Fallback

AST-grep tools work safely even when ast-grep is not installed:
Agents with ast-grep tools will never crash if ast-grep is missing. The tools return install instructions instead.

Pattern Syntax

Pattern Examples by Language


Examples


Dependencies

Install the optional autonomy extras for ast-grep + other autonomy tools:

Error Handling

All functions return errors as strings — they never crash the agent:
  • Not installed — returns install instructions (unchanged).
  • Empty pattern / empty replacement — returns a clear error string (unchanged).
  • Path outside the workspace — returns Error: path '<value>' is outside the workspace. See Path Confinement.
  • Rule file outside the workspace (scan) — returns Error: rule_file '<value>' is outside the workspace.
  • Non-zero exit from sg — returns Error: <stderr or stdout> (broadened from stderr-only).
  • No matches (search / scan) — returns No matches found / No issues found.
  • No matches (rewrite)No matches found for rewrite (dry_run=True) or No changes made (dry_run=False).
  • Rewrite applied — returns sg’s stderr receipt, e.g. Applied N changes.
  • Timeout — 60s for search, 120s for rewrite and scan.
  • Output > 10,000 chars — bounded preview plus Full output saved to: <path> pointer. See Output Limits.

Shell Tools

Execute shell commands

Python Tools

Execute Python code

File Tools

File system operations

Security

Security best practices