ast-grep MCP server

Bridges to the ast-grep code search tool for finding code patterns in projects using abstract syntax tree matching, enabling efficient code analysis and refactoring tasks.
Back to servers
Setup instructions
Provider
ast-grep
Release date
Mar 31, 2025
Language
Python
Stats
59 stars

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.

Prerequisites

Before installing the ast-grep MCP server, you'll need to:

  1. 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
    
  2. Install uv (Python package manager):

    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  3. Have an MCP-compatible client like Cursor or Claude Desktop

Installation

Follow these steps to install the ast-grep MCP server:

  1. Clone the repository:

    git clone https://github.com/ast-grep/ast-grep-mcp.git
    cd ast-grep-mcp
    
  2. Install dependencies:

    uv sync
    
  3. Verify ast-grep is properly installed:

    ast-grep --version
    

Configuration

Cursor Configuration

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": {}
    }
  }
}

Claude Desktop Configuration

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": {}
    }
  }
}

Using the MCP Server

The server provides four main tools for code analysis:

Visualizing AST Structure

The dump_syntax_tree tool helps you understand code structure by visualizing the Abstract Syntax Tree. This is useful for:

  • Debugging pattern matching issues
  • Learning how to write effective patterns
  • Understanding code structure

Testing Rules

Use test_match_code_rule to validate your ast-grep YAML rules against code snippets before applying them to larger codebases.

Simple Pattern Searching

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($$$)

Advanced Rule-Based Searching

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

Supported Languages

The MCP server supports numerous programming languages through ast-grep, including:

  • JavaScript/TypeScript
  • Python
  • Rust
  • Go
  • Java
  • C/C++
  • C#
  • And many more

Troubleshooting

If you encounter issues:

  • "Command not found" errors: Ensure ast-grep is installed and in your PATH
  • No matches found: Try adding stopBy: end to relational rules
  • Pattern not matching: Use dump_syntax_tree to understand the AST structure
  • Permission errors: Ensure the server has read access to target directories

How to install this MCP server

For Claude Code

To 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.

For Cursor

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.

Adding an MCP server to Cursor globally

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": []
        }
    }
}

Adding an MCP server to a project

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.

How to use the MCP server

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.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.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

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later