ast-grep MCP Server is an experimental implementation of the Model Context Protocol that leverages ast-grep's powerful Abstract Syntax Tree (AST) search capabilities. It allows AI assistants to search and analyze code structurally rather than just through text matching, enabling more precise code pattern identification across multiple programming languages.
Before installing the ast-grep MCP server, you'll need to:
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
Have an MCP-compatible client like Cursor or Claude Desktop
Follow these steps to install the ast-grep MCP server:
Clone the repository:
git clone https://github.com/ast-grep/ast-grep-mcp.git
cd ast-grep-mcp
Install dependencies:
uv sync
Verify ast-grep is properly installed:
ast-grep --version
Add the following to your MCP settings (typically in .cursor-mcp/settings.json
):
{
"mcpServers": {
"ast-grep": {
"command": "uv",
"args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"],
"env": {}
}
}
}
Add this to your Claude Desktop MCP configuration:
{
"mcpServers": {
"ast-grep": {
"command": "uv",
"args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"],
"env": {}
}
}
}
The server provides four main tools for code analysis:
The dump_syntax_tree
tool helps you understand code structure by visualizing the Abstract Syntax Tree. This is useful for:
Use test_match_code_rule
to validate your ast-grep YAML rules against code snippets before applying them to larger codebases.
The find_code
tool lets you search codebases using simple ast-grep patterns for straightforward structural matches.
Example search for console.log statements:
id: find-console-logs
language: javascript
rule:
pattern: console.log($$$)
With find_code_by_rule
, you can perform advanced codebase searches using complex YAML rules that express sophisticated matching criteria.
Example rule to find async functions that use await:
id: async-with-await
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: async
- has:
pattern: await $EXPR
stopBy: end
The MCP server supports numerous programming languages through ast-grep, including:
If you encounter issues:
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