# AST Grep Search

Searches and rewrites code by syntax shape across 25 languages.

- Repository: https://github.com/code-yeongyu/oh-my-openagent (70577 stars)
- Category: Engineering
- License: see repository
- Last updated: 2026-11-01
- Runs scripts: yes, review before use

## Install

Claude Code: `npx skills add code-yeongyu/oh-my-openagent --skill ast-grep -g -a claude-code -y`
Codex: `npx skills add code-yeongyu/oh-my-openagent --skill ast-grep -g -a codex -y`
Cursor: `npx skills add code-yeongyu/oh-my-openagent --skill ast-grep -g -a cursor -y`
Gemini CLI: `npx skills add code-yeongyu/oh-my-openagent --skill ast-grep -g -a gemini-cli -y`

## Description

Searches and rewrites code by AST shape across 25 languages. Use when the target is a syntax pattern (every call/class/import shaped like X, a codemod, a YAML rule) rather than literal text; for plain strings, comments, or filenames, use rg.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# ast-grep

`sg` (also installed as `ast-grep`) is an **AST-aware search and rewrite tool** across 25 languages. It treats your pattern as code, parses it the same way it parses your project, and matches structurally. It is the right tool whenever your question depends on **code shape** rather than text bytes.

This skill ships a Python wrapper at `scripts/ast_grep_helper.py` and platform install scripts at `install.sh` (POSIX) and `install.ps1` (Windows). The helper adds offline pattern validation, the two-pass write trick, and binary auto-resolution. Use it as your default entry point.

---

## OmO native: the ast-grep MCP tools (fastest path, no CLI needed)

OmO native also registers a bundled ast-grep MCP server. Its three tools are **deferred**: they are absent from the resident tool list and activate automatically the first time you call them by name, no `tool_search` round trip needed.

- `mcp__ast_grep_search({ pattern, language, paths })`, AST-shape search, the MCP equivalent of `sg run -p`. Prefer this over shelling out to `sg` for one-off lookups: no install, no PATH issues.
- `mcp__ast_grep_rewrite({ pattern, rewrite, language, paths, apply })`, preview matches (`apply: false`, the default) or apply the rewrite (`apply: true`). Same two-pass discipline as `sg run -r`: preview first, then apply.
- `mcp__ast_grep_scan({ paths })`, run project YAML rules (`sgconfig.yml`, `rules/`), the MCP equivalent of `sg scan`.

Same pattern language as the CLI: `$VAR` matches one AST node, `$$$VAR` matches zero or more, and the pattern is code, not regex. `paths` accepts a file or directory list; `language` is required for patterns that are ambiguous across languages.

---

## When to use this skill

Use it whenever the user's question is about **code structure**, not bytes:

The rest: https://github.com/code-yeongyu/oh-my-openagent/blob/HEAD/packages/shared-skills/skills/ast-grep/SKILL.md
