ast-grep-edit-mcp
by aorizondo
README.md
# ast-grep-edit-mcp
An [MCP](https://modelcontextprotocol.io) server that gives an AI agent [ast-grep](https://ast-grep.github.io/)
**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](https://github.com/donbowman/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`).
## Install
```jsonc
// OpenCode (opencode.json)
{
"mcp": {
"servers": {
"ast-grep": { "type": "local", "command": ["npx", "-y", "ast-grep-edit-mcp"] }
}
}
}
```
```sh
# 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
```jsonc
// 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
```sh
npm install
npm test # build + smoke test over stdio (needs ast-grep)
npm run typecheck
```
## License
MIT — see [LICENSE](LICENSE). Tools ported from opencode-ast-grep © Don Bowman (MIT).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues