The ast-grep MCP server allows AI assistants to perform powerful code searches using Abstract Syntax Tree (AST) pattern matching instead of simple text-based searches. This enables more precise code analysis through structural pattern matching across many programming languages.
Before installing the server, you need to set up a few tools:
Install ast-grep using one of these methods:
# macOS
brew install ast-grep
# Using Nix
nix-shell -p ast-grep
# Using Cargo
cargo install ast-grep --locked
Install uv (Python package manager):
curl -LsSf https://astral.sh/uv/install.sh | sh
You'll need an MCP-compatible client like Cursor or Claude Desktop
You can either clone the repository or run directly with uvx
:
Option 1: Clone and install
git clone https://github.com/ast-grep/ast-grep-mcp.git
cd ast-grep-mcp
uv sync
Option 2: Run directly using uvx
uvx --from git+https://github.com/ast-grep/ast-grep-mcp ast-grep-server
Add to your MCP settings (usually in .cursor-mcp/settings.json
):
{
"mcpServers": {
"ast-grep": {
"command": "uv",
"args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"],
"env": {}
}
}
}
Add to your Claude Desktop MCP configuration:
{
"mcpServers": {
"ast-grep": {
"command": "uv",
"args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"],
"env": {}
}
}
}
You can use a custom sgconfig.yaml
file to configure ast-grep behavior. Provide it using one of these methods (in order of precedence):
--config /path/to/sgconfig.yaml
AST_GREP_CONFIG=/path/to/sgconfig.yaml
The server provides four main tools for code analysis:
Visualize the Abstract Syntax Tree structure of code snippets:
tool: dump_syntax_tree
code: |
function hello() {
console.log("Hello World");
}
language: javascript
Test ast-grep YAML rules against code snippets:
tool: test_match_code_rule
rule: |
id: find-console-logs
language: javascript
rule:
pattern: console.log($$$)
code: |
function hello() {
console.log("Hello World");
}
Search codebases using simple ast-grep patterns:
tool: find_code
pattern: console.log($$$)
language: javascript
max_results: 10
output_format: text
Advanced search using complex YAML rules:
tool: find_code_by_rule
rule: |
id: async-with-await
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: async
- has:
pattern: await $EXPR
stopBy: end
max_results: 10
output_format: text
tool: find_code_by_rule
rule: |
id: find-console-logs
language: javascript
rule:
pattern: console.log($$$)
tool: find_code_by_rule
rule: |
id: async-with-await
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: async
- has:
pattern: await $EXPR
stopBy: end
ast-grep supports many programming languages including:
For a complete list, see the ast-grep language support documentation.
stopBy: end
to relational rulesdump_syntax_tree
to understand the AST structureTo add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "ast-grep" '{"command":"uv","args":["--directory","/absolute/path/to/ast-grep-mcp","run","main.py"],"env":[]}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"ast-grep": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/ast-grep-mcp",
"run",
"main.py"
],
"env": []
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"ast-grep": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/ast-grep-mcp",
"run",
"main.py"
],
"env": []
}
}
}
3. Restart Claude Desktop for the changes to take effect