Prerequisites
- Python 3.10 or higher
- PraisonAI Agents package installed
- ast-grep CLI (
pip install ast-grep-cliornpm install -g @ast-grep/cli)
AST-Grep Tools
Use AST-Grep Tools to search, analyze, and rewrite code using structural patterns instead of regex.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
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.
Output Limits
Each call returns at most 10,000 characters. Whensg 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: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
Python
Python
JavaScript / TypeScript
JavaScript / TypeScript
Rust
Rust
Examples
Security Audit Agent
Security Audit Agent
Code Migration Agent
Code Migration Agent
Multi-Agent Code Review
Multi-Agent Code Review
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— returnsError: <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) orNo 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.
Related
Shell Tools
Execute shell commands
Python Tools
Execute Python code
File Tools
File system operations
Security
Security best practices

