|
1 | | -# ast-grep MCP |
| 1 | +# ast-grep MCP Server |
2 | 2 |
|
3 | | -This is an experimental MCP implementation using [ast-grep](https://ast-grep.github.io/) CLI. |
| 3 | +An experimental [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server that provides AI assistants with powerful structural code search capabilities using [ast-grep](https://ast-grep.github.io/). |
4 | 4 |
|
| 5 | +## Overview |
5 | 6 |
|
6 | | -## Prerequisite |
| 7 | +This MCP server enables AI assistants (like Cursor, Claude Desktop, etc.) to search and analyze codebases using Abstract Syntax Tree (AST) pattern matching rather than simple text-based search. By leveraging ast-grep's structural search capabilities, AI can: |
7 | 8 |
|
8 | | -1. Installing `ast-grep` and `uv` |
9 | | -2. Installing MCP client, preferably Cursor |
10 | | -3. Clone this repo, install via `uv` |
11 | | -3. Configure MCP json |
| 9 | +- Find code patterns based on syntax structure, not just text matching |
| 10 | +- Search for specific programming constructs (functions, classes, imports, etc.) |
| 11 | +- Write and test complex search rules using YAML configuration |
| 12 | +- Debug and visualize AST structures for better pattern development |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +1. **Install ast-grep**: Follow [ast-grep installation guide](https://ast-grep.github.io/guide/quick-start.html#installation) |
| 17 | + ```bash |
| 18 | + # macOS |
| 19 | + brew install ast-grep |
| 20 | + nix-shell -p ast-grep |
| 21 | + cargo install ast-grep --locked |
| 22 | + ``` |
| 23 | + |
| 24 | +2. **Install uv**: Python package manager |
| 25 | + ```bash |
| 26 | + curl -LsSf https://astral.sh/uv/install.sh | sh |
| 27 | + ``` |
| 28 | + |
| 29 | +3. **MCP-compatible client**: Such as Cursor, Claude Desktop, or other MCP clients |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +1. Clone this repository: |
| 34 | + ```bash |
| 35 | + git clone https://github.com/ast-grep/ast-grep-mcp.git |
| 36 | + cd ast-grep-mcp |
| 37 | + ``` |
| 38 | + |
| 39 | +2. Install dependencies: |
| 40 | + ```bash |
| 41 | + uv sync |
| 42 | + ``` |
| 43 | + |
| 44 | +3. Verify ast-grep installation: |
| 45 | + ```bash |
| 46 | + ast-grep --version |
| 47 | + ``` |
| 48 | +## Configuration |
| 49 | + |
| 50 | +### For Cursor |
| 51 | + |
| 52 | +Add to your MCP settings (usually in `.cursor-mcp/settings.json`): |
12 | 53 |
|
13 | 54 | ```json |
14 | 55 | { |
15 | | - "mcpServers": { |
16 | | - "server-name": { |
17 | | - "command": "uv", |
18 | | - "args": ["--directory", "/path/to/repo", "run", "main.py"], |
19 | | - "env": {} |
20 | | - } |
| 56 | + "mcpServers": { |
| 57 | + "ast-grep": { |
| 58 | + "command": "uv", |
| 59 | + "args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"], |
| 60 | + "env": {} |
21 | 61 | } |
22 | 62 | } |
| 63 | +} |
23 | 64 | ``` |
24 | 65 |
|
25 | | -4. Ask cursor ai to search your codebase! |
| 66 | +### For Claude Desktop |
| 67 | + |
| 68 | +Add to your Claude Desktop MCP configuration: |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "mcpServers": { |
| 73 | + "ast-grep": { |
| 74 | + "command": "uv", |
| 75 | + "args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"], |
| 76 | + "env": {} |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Usage |
| 83 | + |
| 84 | +This repository includes comprehensive ast-grep rule documentation in [ast-grep.mdc](https://github.com/ast-grep/ast-grep-mcp/blob/main/ast-grep.mdc). The documentation covers all aspects of writing effective ast-grep rules, from simple patterns to complex multi-condition searches. |
| 85 | + |
| 86 | +You can add it to your cursor rule or Claude.md, and attach it when you need AI agent to create ast-grep rule for you. |
| 87 | + |
| 88 | +The prompt will ask LLM to use MCP to create, verify and improve the rule it creates. |
| 89 | + |
| 90 | +## Features |
| 91 | + |
| 92 | +The server provides four main tools for code analysis: |
| 93 | + |
| 94 | +### 🔍 `dump_syntax_tree` |
| 95 | +Visualize the Abstract Syntax Tree structure of code snippets. Essential for understanding how to write effective search patterns. |
| 96 | + |
| 97 | +**Use cases:** |
| 98 | +- Debug why a pattern isn't matching |
| 99 | +- Understand the AST structure of target code |
| 100 | +- Learn ast-grep pattern syntax |
| 101 | + |
| 102 | +### 🧪 `test_match_code_rule` |
| 103 | +Test ast-grep YAML rules against code snippets before applying them to larger codebases. |
| 104 | + |
| 105 | +**Use cases:** |
| 106 | +- Validate rules work as expected |
| 107 | +- Iterate on rule development |
| 108 | +- Debug complex matching logic |
| 109 | + |
| 110 | +### 🎯 `find_code` |
| 111 | +Search codebases using simple ast-grep patterns for straightforward structural matches. |
| 112 | + |
| 113 | +**Use cases:** |
| 114 | +- Find function calls with specific patterns |
| 115 | +- Locate variable declarations |
| 116 | +- Search for simple code constructs |
| 117 | + |
| 118 | +### 🚀 `find_code_by_rule` |
| 119 | +Advanced codebase search using complex YAML rules that can express sophisticated matching criteria. |
| 120 | + |
| 121 | +**Use cases:** |
| 122 | +- Find nested code structures |
| 123 | +- Search with relational constraints (inside, has, precedes, follows) |
| 124 | +- Complex multi-condition searches |
| 125 | + |
| 126 | + |
| 127 | +## Usage Examples |
| 128 | + |
| 129 | +### Basic Pattern Search |
| 130 | + |
| 131 | +Use Query: |
| 132 | + |
| 133 | +> Find all console.log statements |
| 134 | +
|
| 135 | +AI will generate rules like: |
| 136 | + |
| 137 | +```yaml |
| 138 | +id: find-console-logs |
| 139 | +language: javascript |
| 140 | +rule: |
| 141 | + pattern: console.log($$$) |
| 142 | +``` |
| 143 | +
|
| 144 | +### Complex Rule Example |
| 145 | +
|
| 146 | +User Query: |
| 147 | +> Find async functions that use await |
| 148 | +
|
| 149 | +AI will generate rules like: |
| 150 | +
|
| 151 | +```yaml |
| 152 | +id: async-with-await |
| 153 | +language: javascript |
| 154 | +rule: |
| 155 | + all: |
| 156 | + - kind: function_declaration |
| 157 | + - has: |
| 158 | + pattern: async |
| 159 | + - has: |
| 160 | + pattern: await $EXPR |
| 161 | + stopBy: end |
| 162 | +``` |
| 163 | +
|
| 164 | +## Supported Languages |
| 165 | +
|
| 166 | +ast-grep supports many programming languages including: |
| 167 | +- JavaScript/TypeScript |
| 168 | +- Python |
| 169 | +- Rust |
| 170 | +- Go |
| 171 | +- Java |
| 172 | +- C/C++ |
| 173 | +- C# |
| 174 | +- And many more... |
| 175 | +
|
| 176 | +## Troubleshooting |
| 177 | +
|
| 178 | +### Common Issues |
| 179 | +
|
| 180 | +1. **"Command not found" errors**: Ensure ast-grep is installed and in your PATH |
| 181 | +2. **No matches found**: Try adding `stopBy: end` to relational rules |
| 182 | +3. **Pattern not matching**: Use `dump_syntax_tree` to understand the AST structure |
| 183 | +4. **Permission errors**: Ensure the server has read access to target directories |
| 184 | + |
| 185 | +## Contributing |
| 186 | + |
| 187 | +This is an experimental project. Issues and pull requests are welcome! |
| 188 | + |
| 189 | +## Related Projects |
| 190 | + |
| 191 | +- [ast-grep](https://ast-grep.github.io/) - The core structural search tool |
| 192 | +- [Model Context Protocol](https://modelcontextprotocol.io/) - The protocol this server implements |
| 193 | +- [FastMCP](https://github.com/pydantic/fastmcp) - The Python MCP framework used |
0 commit comments