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.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
Available Functions
Function Details
ast_grep_search(pattern, lang, path=”.”, json_output=True)
Searches code using AST patterns. Returns structured JSON results.| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | required | AST pattern ($VAR = single node, $$$ = multiple) |
lang | str | required | Language: python, javascript, typescript, rust, go, java, c, cpp |
path | str | "." | File or directory to search |
json_output | bool | True | Return JSON format |
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).| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | required | AST pattern to match |
replacement | str | required | Replacement pattern (can reference $VAR captures) |
lang | str | required | Programming language |
path | str | "." | File or directory |
dry_run | bool | True | Preview only — set False to apply |
ast_grep_scan(path=”.”, rule_file=None)
Scans code using YAML-based lint rules.| Parameter | Type | Default | Description |
|---|---|---|---|
path | str | "." | File or directory to scan |
rule_file | str | None | Path to YAML rule file (uses sgconfig.yml if not set) |
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 | Meaning | Example |
|---|---|---|
$VAR | Match single AST node | def $FN() matches any no-arg function |
$$$ | Match multiple nodes | def $FN($$$) matches any function |
| Literal code | Match exactly | print("hello") matches that exact call |
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
| Package | Required | Install |
|---|---|---|
praisonaiagents | Yes | pip install praisonaiagents |
ast-grep-cli | Optional | pip install ast-grep-cli or npm install -g @ast-grep/cli |
Install the optional autonomy extras for ast-grep + other autonomy tools:
Error Handling
All functions include comprehensive error handling:- Not installed: Returns helpful install instructions
- Empty pattern: Returns clear error message
- Timeout: 60s for search, 120s for rewrite/scan
- Subprocess errors: Captured and returned as strings
- No matches: Returns “No matches found” (not an error)
Related
Shell Tools
Execute shell commands
Python Tools
Execute Python code
File Tools
File system operations
Security
Security best practices

