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.
ParameterTypeDefaultDescription
patternstrrequiredAST pattern ($VAR = single node, $$$ = multiple)
langstrrequiredLanguage: python, javascript, typescript, rust, go, java, c, cpp
pathstr"."File or directory to search
json_outputboolTrueReturn 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).
ParameterTypeDefaultDescription
patternstrrequiredAST pattern to match
replacementstrrequiredReplacement pattern (can reference $VAR captures)
langstrrequiredProgramming language
pathstr"."File or directory
dry_runboolTruePreview only — set False to apply
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.
ParameterTypeDefaultDescription
pathstr"."File or directory to scan
rule_filestrNonePath to YAML rule file (uses sgconfig.yml if not set)

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

PatternMeaningExample
$VARMatch single AST nodedef $FN() matches any no-arg function
$$$Match multiple nodesdef $FN($$$) matches any function
Literal codeMatch exactlyprint("hello") matches that exact call

Pattern Examples by Language


Examples


Dependencies

PackageRequiredInstall
praisonaiagentsYespip install praisonaiagents
ast-grep-cliOptionalpip 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)

Shell Tools

Execute shell commands

Python Tools

Execute Python code

File Tools

File system operations

Security

Security best practices