UNPKG

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
# 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!