Package Exports
- @cabbages/tree-grep
- @cabbages/tree-grep/build/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@cabbages/tree-grep) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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:
{
"mcpServers": {
"tree-ast-grep": {
"command": "npx",
"args": ["-y", "@cabbages/tree-grep", "--auto-install"]
}
}
}Using Bun (for development):
{
"mcpServers": {
"tree-ast-grep": {
"command": "bunx",
"args": ["@cabbages/tree-grep", "--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,$$$BODYwork perfectly - Auto-Install - Downloads platform-specific ast-grep binary
- Minimal Codebase - ~300 lines, crystal clear logic
- π MCP Inspector - Enhanced testing with Model Context Protocol integration
π Usage Examples
Search for Patterns
// Find all console.log statements
ast_search({
pattern: "console.log($ARG)",
language: "javascript",
code: "console.log('hello'); console.log('world');"
})Replace Code Structures
// 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
// 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 errorsEach tool: Validate β Build Command β Execute β Parse β Return
π§ͺ Testing
Patterns work exactly like ast-grep CLI:
# Run unit suites via Vitest adapter (fast)
npm test
# Using Bun (faster)
bun test
# or
npm run test:bun
# Watch mode
npm run test:watch
# Run specific test suites
npm run test:unit # Node.js
npm run test:unit:bun # Bun
npm run test:integration:bun
# Integration/e2e via adapter (requires ast-grep availability)
$env:AST_GREP_AVAILABLE="1"; npm test # PowerShell
# or
AST_GREP_AVAILABLE=1 npm test # bash
# Direct ast-grep CLI examples
ast-grep run --pattern "console.log($ARG)" --lang js file.js
ast-grep run --pattern "var $NAME" --rewrite "let $NAME" --lang js file.js
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
# Lightweight (requires system ast-grep)
npx @cabbages/tree-grep --use-system
# Platform-specific binary
npx @cabbages/tree-grep --platform=win32
# Auto-detect platform (recommended)
npx @cabbages/tree-grep --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:
- Always name multi-node variables: use
$$$BODY,$$$ARGSinstead of bare$$$ - Bare
$$$in replacements does not expand and is now rejected by this server - Keep patterns aligned with ast-grep docs; test them with the CLI
π€ Language IDs and Paths
- Accepted languages are ast-grepβs IDs:
js,ts,jsx,tsx, etc. - Aliases like
javascript/typescriptare mapped internally tojs/ts. - Inline
coderequireslanguage. - For file scans:
- Paths are resolved relative to
WORKSPACE_ROOT(auto-detected if unset). - Absolute paths are supported; Windows paths are normalized.
- If a single file with a known extension is provided and
languageis omitted, the server infers--langfrom the filename.
- Paths are resolved relative to
π« 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
π MCP Inspector Integration
Enhanced testing capabilities with Model Context Protocol integration for real-world agent usage alignment:
# Run tests with MCP Inspector
npm run test:mcp
# View MCP Inspector demo
npm run demo:mcp
# Generate comprehensive MCP reports
npm run test:mcp-allKey MCP Inspector Features:
- Pattern matching validation with structured results
- Code transformation inspection and verification
- Real-world usage simulation for AI agents
- MCP-compliant test reporting format
- Enhanced debugging with inspection data
See docs/MCP_INSPECTOR.md for detailed documentation.
π Development
This project supports both Node.js (for users) and Bun (for developers):
# Quick start with Bun (recommended for development)
bun install
bun run dev:bun
bun test
# Or use Node.js (works everywhere)
npm install
npm run dev
npm testSee DEVELOPMENT.md for detailed setup and performance comparison.
π€ 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!