Agent skill

Ast Grep

by BiFangKNT in BiFangKNT/mtga

Write and debug ast-grep rules for structural code search and rewrite.

AGPL-3.0Auto-check passedDevelopment

Install Ast Grep

skills CLI
$ npx skills add BiFangKNT/mtga --skill ast-grep -a claude-code

Project install by default; add -g for ~/.claude/skills/.

GitHub CLI
$ gh skill install BiFangKNT/mtga ast-grep --agent claude-code

Project scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).

Manual copy
$ git clone --depth 1 https://github.com/BiFangKNT/mtga.git skills-src && mkdir -p .claude/skills && cp -r skills-src/.codex/skills/ast-grep .claude/skills/ast-grep && rm -rf skills-src

Use ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.

Claude Code skills documentation · loads skills from .claude/skills/

Facts

Skill name
ast-grep
GitHub stars
1.2k
Token cost
~802 tokens
SKILL.md length
258 words
Files
3 (incl. references)
Skills in repo
6
Repo updated
First seen
Licence
AGPL-3.0

At a glance

Write and debug ast-grep rules for structural code search and rewrite.

  • Works in 4 steps: Understand the Query → Start with the Smallest Pattern → Escalate to YAML Rule for Structure Logic → …
  • Tasks require AST-aware matching (not plain text)
  • SKILL.md covers Overview, General Workflow, CLI Quick Reference and Tips for Writing Effective Rules, plus 1 more section
  • Instructions only: no scripts, shell commands, URLs or credentials in SKILL.md

What it does

Ast Grep is an agent skill from BiFangKNT/mtga. Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.

Its SKILL.md is about 800 tokens, which your agent loads only when the skill is triggered. The skill folder holds 3 other files, including reference files (for example `references/rule_reference.md` and `references/windows.md`).

It sits in Development, covering Codebase onboarding and Refactoring. The repository describes itself as: 基于本地代理的方式,绕过 IDE 的固定模型服务商限制. The licence is AGPL-3.0.

When your agent uses it

  • Tasks require AST-aware matching (not plain text)
  • Such as finding specific syntax shapes
  • Relational patterns (inside/has)
  • Composite logic (all/any/not)

Example prompts

  • “/ast-grep”

Workflow steps

4 steps, taken from the step headings in SKILL.md.

  1. Understand the Query
  2. Start with the Smallest Pattern
  3. Escalate to YAML Rule for Structure Logic
  4. Validate and Search

What it can do on your machine

Read from SKILL.md and the folder at commit 72669fb. It shows what the files ask for, not the result of running them.

  • Tool permissions

    Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.

    From allowed-tools in the SKILL.md frontmatter.

  • Runs code

    No scripts in the folder and no shell commands in SKILL.md (its code samples are bash and yaml).

    From the folder's file list and the shell code blocks in SKILL.md.

  • Network

    No URLs in SKILL.md.

    From URLs in SKILL.md, links to its own repository left out.

  • Credentials

    Names no API keys, tokens, secrets or passwords.

    From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.

Context cost

Ast Grep loads about 802 tokens when it runs, and up to ~3.7k if it reads all its reference files. Until then it costs about 72 tokens; SKILL.md has 258 words of instructions outside code blocks.

Always · name and description, kept in context so the agent knows when to use it
~72
When it runs · the whole SKILL.md, loaded when a task matches
~802
With references · SKILL.md plus every file in references/, read only if the agent opens them
~3.7k

Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.

Safety

Auto-check passed

The automated check found no risky patterns in SKILL.md.

Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.

SKILL.md

The full file from BiFangKNT/mtga at commit 72669fb, republished under its AGPL-3.0 licence (© BiFangKNT). 258 words, ~802 tokens.

Download SKILL.mdSave it as .claude/skills/ast-grep/SKILL.md (or your agent's skills folder). This skill also uses 2 other files; get the full folder from GitHub.
name
ast-grep
description
Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.

Overview

Translate user intent into sg patterns/rules, validate quickly, then run against target scope.

General Workflow

Step 1: Understand the Query

Pin down:

  • target syntax shape
  • language
  • file scope
  • include/exclude constraints
Step 2: Start with the Smallest Pattern

Use sg run --pattern first.

bash
sg run --pattern 'console.log($ARG)' --lang javascript src
Step 3: Escalate to YAML Rule for Structure Logic

For relational/composite logic (inside / has / precedes / follows / all / any / not / matches), use sg scan --rule.

yaml
# rule.yml
id: async-with-await
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await $EXPR
    stopBy: end
bash
sg scan --rule rule.yml src

Test on a tiny sample first, then search the project.

bash
echo "async function f(){ await g() }" | sg scan --rule rule.yml --stdin
sg scan --rule rule.yml src --json=compact

CLI Quick Reference

Inspect Code Structure (--debug-query)

Use when kind/pattern mismatch is unclear.

bash
sg run --pattern 'class $NAME { $$$BODY }' \
  --lang javascript \
  --debug-query=pattern
Test Rules (scan with --stdin)
bash
echo "const x = await fetch();" | sg scan --inline-rules "id: test
language: javascript
rule:
  pattern: await \$EXPR" --stdin --json=compact
Search with Patterns (run)
bash
sg run --pattern 'function $NAME($$$)' --lang javascript src --json=compact
Search with Rules (scan)
bash
sg scan --rule rule.yml src
sg scan --inline-rules "id: find-async
language: javascript
rule:
  kind: function_declaration
  has:
    pattern: await \$EXPR
    stopBy: end" src

Tips for Writing Effective Rules

Default to stopBy: end for Deep Searches

For relational rules (inside, has), default to:

yaml
has:
  pattern: await $EXPR
  stopBy: end

This ensures the search traverses the entire subtree rather than stopping at the first non-matching node.

Start Simple, Then Add Complexity
  1. pattern
  2. kind
  3. add inside/has
  4. add all/any/not
Metavariable Rules
  1. Use exact metavariable forms only: $A, $$OP, $$$ARGS.
  2. Keep metavariable text as the only content in its AST node.
  3. If a rule depends on previously captured metavariables, combine sub-rules in all to guarantee match order.
No-Match Triage
  1. Run with --json=compact.
  2. If output is [], treat as no match and refine pattern/scope.
  3. If stderr has parser/arg errors, fix language, pattern, or flags first.

Resources

references/
  • rule_reference.md: Full rule syntax (atomic, relational, composite, metavariables)
  • windows.md: PowerShell quoting/escaping tips and Vue SFC script-block workflow

Load only the reference needed for the current task.

© BiFangKNT, AGPL-3.0. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file

Files

SKILL.md and 2 other files (references) in .codex/skills/ast-grep of BiFangKNT/mtga.

  • SKILL.md
  • references/rule_reference.md
  • references/windows.md

Open the folder on GitHubat commit 72669fb

Compare with similar skills

Ast Grep next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.

Ast Grep compared with similar skills
SkillStarsUsed inTokensAuto-checkLicenceRepo updated
Ast Grep this skillBiFangKNT/mtga1.2k—~802Automated safety check: PassAGPL-3.0
Lsp Code Analysislsp-client/lsp-skill118—~3kAutomated safety check: PassMIT
Gograph Go Repository Intelligenceozgurcd/gograph228—~4.8kAutomated safety check: NotesMIT
Ripwire Graph Queryredhat-et/ripwire2.4k—~1.1kAutomated safety check: NotesApache-2.0
CodeNavitech-leads-club/agent-skills7k—~3.4kAutomated safety check: PassCC-BY-4.0
Golang Goplscontext-labs/whip1.1k1 repos~2.3kAutomated safety check: PassMIT

Similar skills

  • Lsp Code Analysis

    lsp-client/lsp-skill

    Semantic code analysis via LSP. An agent skill from lsp-client/lsp-skill.

    118 GitHub stars~3k tokensUpdated 8 mo ago
    DevelopmentAuto-check passed
  • Gives an agent working in a Go codebase a structural view through a local MCP server: call graphs, blast-radius and impact analysis, and bounded first-call exploration.

    228 GitHub stars~4.8k tokensUpdated 2 days ago
    DevelopmentAuto-check: notes
  • Ripwire Graph Query

    redhat-et/ripwire

    Answers call-graph questions that combine several conditions, such as complex functions that reach a target or untested symbols near main, using ripwire's graph-query mode.

    2.4k GitHub stars~1.1k tokensUpdated today
    DevelopmentAuto-check: notes
  • CodeNavi

    tech-leads-club/agent-skills

    Investigates unfamiliar codebases before changing them, makes small targeted edits and records what it learns in a .notebook/ folder for later sessions.

    7k GitHub stars~3.4k tokensUpdated yesterday
    DevelopmentAuto-check passed
  • Golang Gopls

    context-labs/whip

    Golang semantic code intelligence via gopls — go-to-definition, references, call hierarchy, symbols, diagnostics, rename, refactors.

    1.1k GitHub starsUsed in 1 repo~2.3k tokens
    DevelopmentAuto-check passed
  • Repo Map

    AnastasiyaW/codex-claude-code-config

    Ranked symbol map of a codebase within a token budget — a compact "what matters in this repo" before reading files.

    154 GitHub stars~1.5k tokensUpdated today
    DevelopmentAuto-check passed

More from BiFangKNT/mtga

  • Trae Native Debug

    BiFangKNT/mtga

    Maintain, debug, and recover MTGA's Trae native custom-model route.

    1.2k GitHub stars~920 tokensUpdated 3 mo ago
    Auto-check passed
  • Gh CLI

    BiFangKNT/mtga

    使用 GitHub CLI (gh) 执行非交互式 GitHub 协作与自动化任务(PR、Issue、Actions、Release、API 查询/修改)。当用户要求“用 gh 命令完成任务”、需要结构化 JSON 输出(--json/--jq)、批量处理 PR/Issue(如“批量 review 多个 PR”)、按策略合并(如“检查通过后自动 squash 合并”)时使用此 skill。

    1.2k GitHub stars~599 tokensUpdated 3 mo ago
    Auto-check passed
  • Tauri

    BiFangKNT/mtga

    构建、调试与发布 Tauri v2 应用的工程化工作流。覆盖项目初始化(create-tauri-app)、前端与 Rust 命令通信([tauri::command] + invoke)、状态管理、Capabilities 权限建模、插件接入、跨平台构建与问题排查。当用户需求涉及“创建 Tauri 项目”“把 Web 前端接到 Rust…

    1.2k GitHub stars~466 tokensUpdated 3 mo ago
    Auto-check passed
  • Uv

    BiFangKNT/mtga

    在 Windows/macOS/Linux 上使用 uv 执行 Python 运行、依赖同步、锁文件管理、Python 版本管理与工具命令(uv run、uv sync、uv lock、uv python、uv tool)。当任务涉及“用 uv 运行脚本/命令”“临时依赖(--with)”“锁文件一致性(--locked/--frozen)”“跨平台…

    1.2k GitHub stars~541 tokensUpdated 3 mo ago
    Auto-check passed
  • Pwsh

    BiFangKNT/mtga

    在 Windows PowerShell 5.1 与 PowerShell 7 中编写和执行稳定命令,重点处理引号、管道符、原生命令参数传递与跨版本兼容。遇到 --jq/过滤表达式被 PowerShell 误解析、同一命令在 pwsh5 与 pwsh7 行为不一致、或需要为 pwsh7.3+ 增加参数传递适配时使用此 skill。

    1.2k GitHub stars~338 tokensUpdated 3 mo ago
    Auto-check passed

Categories

Questions about Ast Grep

What does Ast Grep do?

Write and debug ast-grep rules for structural code search and rewrite. Ast Grep is an agent skill from BiFangKNT/mtga. Write and debug ast-grep rules for structural code search and rewrite.

When should I use Ast Grep?

Ast Grep fits situations like: tasks require AST-aware matching (not plain text); such as finding specific syntax shapes; relational patterns (inside/has); composite logic (all/any/not).

How do I install Ast Grep in Claude Code?

Run `npx skills add BiFangKNT/mtga --skill ast-grep -a claude-code`. Or copy the skill folder (.codex/skills/ast-grep in BiFangKNT/mtga) into .claude/skills/ast-grep in your project. Claude Code loads it when a task matches its description.

How do I install Ast Grep in Codex?

Run `npx skills add BiFangKNT/mtga --skill ast-grep -a codex`. Or copy the skill folder (.codex/skills/ast-grep in BiFangKNT/mtga) into .agents/skills/ast-grep in your project. Codex loads it when a task matches its description.

Can I use Ast Grep in Cursor, Gemini CLI or GitHub Copilot?

Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add BiFangKNT/mtga --skill ast-grep -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/ast-grep, .gemini/skills/ast-grep, .github/skills/ast-grep and .opencode/skills/ast-grep in your project.

What does Ast Grep need to run?

SKILL.md names no scripts, command-line tools or credentials: Ast Grep is instructions for the agent only.

Does Ast Grep access the network?

SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.

Is Ast Grep safe to install?

Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.

What licence does Ast Grep use?

Ast Grep is published under the AGPL-3.0 licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.

How many tokens does Ast Grep use?

About 802 tokens (SKILL.md is roughly 3.2k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full. Its references folder adds about 2.9k tokens, read only when the agent opens those files.

What are the alternatives to Ast Grep?

Skills that share tags, products or a category with Ast Grep: Lsp Code Analysis (lsp-client/lsp-skill, 118 stars), Gograph Go Repository Intelligence (ozgurcd/gograph, 228 stars), Ripwire Graph Query (redhat-et/ripwire, 2.4k stars) and CodeNavi (tech-leads-club/agent-skills, 7k stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.

Who maintains Ast Grep?

BiFangKNT (a GitHub user) maintains it in BiFangKNT/mtga, which has 1,169 GitHub stars. The repository holds 6 skills in this directory. The repository was last updated on June 18, 2026.

Source: BiFangKNT/mtga on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.