Skip to main content
Glama
ast-grep-edit-mcp

ast-grep-edit-mcp

by aorizondo

ast-grep-edit-mcp

An MCP server that gives an AI agent ast-grep structural search and multi-file rewrite as first-class tools, so it reaches for syntax-aware tooling when a task is about the shape of code instead of plain text.

Tool

What it does

ast_grep_search

Finds code by AST pattern: calls with arguments, JSX, declarations, nesting, missing guards. Returns compact file:line:column matches.

ast_grep_edit

Multi-file structural rewrite that keeps the surrounding formatting. Returns a diff by default; apply: true writes.

The server also tells the agent when to use each tool (MCP instructions) and ships a skill, ast-grep, with pattern examples. It works over stdio with any MCP client (OpenCode, Claude Code, VS Code…) in both protocol eras (2026-07-28 and 2025).

The tools are ported from opencode-ast-grep by Don Bowman, an OpenCode plugin; this package makes them available to every MCP client.

Requirements

  • Node 22 or later.

  • ast-grep 0.40+ on PATH (pip install ast-grep-cli, npm i -g @ast-grep/cli, cargo install ast-grep --locked, or brew install ast-grep).

Related MCP server: ast-grep MCP Server

Install

// OpenCode (opencode.json)
{
  "mcp": {
    "servers": {
      "ast-grep": { "type": "local", "command": ["npx", "-y", "ast-grep-edit-mcp"] }
    }
  }
}
# Claude Code
claude mcp add ast-grep -- npx -y ast-grep-edit-mcp

Then ask for something structural: "find every console.log call with its arguments" or "rename all oldApi(...) calls to newApi(...) under src".

Usage

// ast_grep_search — pattern is code with metavariables: $A one node, $$$ many, $_ no capture
{ "pattern": "console.log($$$ARGS)", "lang": "ts", "paths": ["src"] }
{ "pattern": "$A && $A()", "globs": ["!**/node_modules/**"], "limit": 50 }

// ast_grep_edit — 1) preview: returns a diff, writes nothing
{ "pattern": "console.log($$$ARGS)", "rewrite": "logger.info($$$ARGS)", "lang": "ts" }
// 2) apply
{ "pattern": "console.log($$$ARGS)", "rewrite": "logger.info($$$ARGS)", "lang": "ts", "apply": true }

Both tools accept lang, paths, globs and strictness (cst | smart | ast | relaxed | signature | template). Search also accepts context (lines around each match) and limit; edit also accepts apply.

  • Patterns are code, not regex. A pattern matches a whole node: std::env::var($X) does not match a bare env::var($X); try both forms.

  • $$$ absorbs any number of nodes: argument lists, bodies.

  • Zero matches is a normal result, not an error.

Configuration

Variable

Default

Meaning

AST_GREP_BIN

ast-grep, then sg if it is ast-grep

Path to the ast-grep binary

AST_GREP_ROOT

the directory the client starts it in

Project directory that paths are relative to

Timeouts are fixed (20 s search, 30 s edit) and output is cut at about 50,000 characters so a huge match set cannot flood the context. The binary runs directly, never through a shell.

Development

npm install
npm test          # build + smoke test over stdio (needs ast-grep)
npm run typecheck

License

MIT — see LICENSE. Tools ported from opencode-ast-grep © Don Bowman (MIT).

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      D
      maintenance
      Advanced code search and transformation MCP server for AI assistants. Combines ugrep's speed with intelligent replace capabilities, dry-run previews, and language-aware refactoring across 11 tools.
      1
      10
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      Enables AI assistants to search and analyze codebases using Abstract Syntax Tree (AST) pattern matching with ast-grep. Supports structural code search, pattern testing, and AST visualization across multiple programming languages.
      4
      503
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      Enables structural code search and refactoring across 20+ programming languages using ast-grep patterns, with safe dry-run mode and flexible filtering.
      1
      MIT