$brew install ast-grep
Agent Compatibility
JSON Output
Agent Skill
MCP Support
AI Analysis
ast-grep is a structural code search and rewriting CLI built on tree-sitter ASTs. It lets you match code by syntax shape, then run one-off rewrites or reusable lint rules across supported languages.
What It Enables
- Search a repo for syntax patterns and captured nodes instead of raw text matches, including matches read from stdin or filtered by file globs.
- Rewrite matched code with replacement templates, either as targeted one-off codemods or bulk updates across many files.
- Define YAML rules, scaffold and test them, then run repo-wide scans that emit diagnostics or SARIF for CI and cleanup workflows.
Agent Fit
runandscanboth support real--jsonoutput, andscanalso emits SARIF, so matches and diagnostics are straightforward to parse in follow-up steps.- The CLI is built for non-interactive use first: paths, stdin, globs, inline rules, thread control, and inspect output make it easy to chain into search, edit, and verify loops.
- Best for codemods, lint enforcement, and targeted repository inspection; agents still need correct language selection and precise rules, while interactive review mode is mainly for humans.
Caveats
- Coverage depends on supported parsers and correct language detection, so unsupported syntax or the wrong language choice will miss matches.
- Repeatable team workflows usually depend on checked-in rule files and
sgconfig.yml; interactive rewrite sessions are less suitable for unattended runs.