tree-ast-grep-mcp
Version:
Simple, direct ast-grep wrapper for AI coding agents. Zero abstractions, maximum performance.
161 lines (123 loc) โข 4.07 kB
Markdown
# tree-ast-grep MCP Server
A **simple, direct wrapper** around ast-grep for AI coding agents. Zero abstractions, maximum performance, perfect ast-grep compatibility.
## ๐ Quick Start
Add to your MCP settings:
```json
{
"mcpServers": {
"tree-ast-grep": {
"command": "npx",
"args": ["-y", "tree-ast-grep-mcp", "--auto-install"]
}
}
}
```
## ๐ฏ What It Does
Three simple tools that directly execute ast-grep commands:
- **`ast_search`** โ `ast-grep run --pattern` (structural code search)
- **`ast_replace`** โ `ast-grep run --rewrite` (AST-aware replacements)
- **`ast_run_rule`** โ `ast-grep scan --rule` (generate & run custom rules)
## โจ Key Features
- **Zero Overhead** - Direct ast-grep execution, no abstractions
- **Perfect Compatibility** - Behaves exactly like ast-grep CLI
- **Inline Code Support** - Test patterns without files
- **Named Metavariables** - `$NAME`, `$ARG`, `$$$BODY` work perfectly
- **Auto-Install** - Downloads platform-specific ast-grep binary
- **Minimal Codebase** - ~300 lines, crystal clear logic
## ๐ Usage Examples
### Search for Patterns
```javascript
// Find all console.log statements
ast_search({
pattern: "console.log($ARG)",
language: "javascript",
code: "console.log('hello'); console.log('world');"
})
```
### Replace Code Structures
```javascript
// Convert var to let
ast_replace({
pattern: "var $NAME = $VALUE",
replacement: "let $NAME = $VALUE",
language: "javascript",
code: "var x = 5; var y = 10;"
})
```
### Generate Custom Rules
```javascript
// Create linting rule
ast_run_rule({
id: "no-console-log",
pattern: "console.log($ARG)",
message: "Use logger.info instead",
language: "javascript",
fix: "logger.info($ARG)"
})
```
## ๐๏ธ Architecture
**Intentionally Simple:**
```
src/
โโโ index.ts # MCP server
โโโ core/
โ โโโ binary-manager.ts # Execute ast-grep
โ โโโ workspace-manager.ts # Find workspace root
โโโ tools/
โ โโโ search.ts # Direct search
โ โโโ replace.ts # Direct replace
โ โโโ scan.ts # Direct scan
โโโ types/errors.ts # Basic errors
```
Each tool: Validate โ Build Command โ Execute โ Parse โ Return
## ๐งช Testing
Patterns work exactly like ast-grep CLI:
```bash
# Test patterns directly
ast-grep run --pattern "console.log($ARG)" --lang javascript file.js
# Test replacements
ast-grep run --pattern "var $NAME" --rewrite "let $NAME" --lang javascript file.js
# Test rules
ast-grep scan --rule rule.yml file.js
```
## โก Performance
- **Direct Execution** - No overhead vs ast-grep CLI
- **Streaming JSON** - Fast results parsing
- **Binary Caching** - One-time download per platform
- **Minimal Memory** - No complex abstractions
## ๐ง Configuration Options
```bash
# Lightweight (requires system ast-grep)
npx tree-ast-grep-mcp --use-system
# Platform-specific binary
npx tree-ast-grep-mcp --platform=win32
# Auto-detect platform (recommended)
npx tree-ast-grep-mcp --auto-install
```
## ๐ Metavariable Guide
**โ
Reliable Patterns:**
- `$NAME`, `$ARG`, `$VALUE` (single nodes)
- `$$$BODY`, `$$$ARGS` (named multi-nodes)
- `console.log($ARG)` โ `logger.info($ARG)`
**โ ๏ธ Use With Care:**
- Bare `$$$` produces literal "$$$" in replacements
- Complex structural patterns may not match reliably
## ๐ซ What This ISN'T
- โ A complex AST manipulation framework
- โ A wrapper with proprietary pattern syntax
- โ An abstraction layer over ast-grep
- โ A reimplementation of ast-grep functionality
## โ
What This IS
- โ
Direct ast-grep command execution
- โ
Minimal MCP protocol wrapper
- โ
Perfect CLI compatibility
- โ
Zero-overhead tool integration
- โ
Simple, maintainable codebase
## ๐ค Contributing
Keep it simple! Follow the CLAUDE.md guidelines:
- No abstractions or base classes
- Direct command execution only
- Test against ast-grep CLI behavior
- Favor duplication over complexity
## ๐ License
MIT License - Use freely, keep it simple!