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.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).ast_grep_scan(path=”.”, rule_file=None)
Scans code using YAML-based lint rules.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 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

