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
151 stars

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.

Installation

Prerequisites

Before installing the server, you need to set up a few tools:

  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. You'll need an MCP-compatible client like Cursor or Claude Desktop

Setting Up the Server

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

Configuration

For Cursor

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

For Claude Desktop

Add to your Claude Desktop MCP configuration:

{
  "mcpServers": {
    "ast-grep": {
      "command": "uv",
      "args": ["--directory", "/absolute/path/to/ast-grep-mcp", "run", "main.py"],
      "env": {}
    }
  }
}

Custom ast-grep Configuration

You can use a custom sgconfig.yaml file to configure ast-grep behavior. Provide it using one of these methods (in order of precedence):

  1. Command-line argument: --config /path/to/sgconfig.yaml
  2. Environment variable: AST_GREP_CONFIG=/path/to/sgconfig.yaml

Features and Usage

The server provides four main tools for code analysis:

Dump Syntax Tree

Visualize the Abstract Syntax Tree structure of code snippets:

tool: dump_syntax_tree
code: |
  function hello() {
    console.log("Hello World");
  }
language: javascript

Test Match Code Rule

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");
  }

Find Code

Search codebases using simple ast-grep patterns:

tool: find_code
pattern: console.log($$$)
language: javascript
max_results: 10
output_format: text

Find Code By Rule

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

Usage Examples

Finding All Console.log Statements

tool: find_code_by_rule
rule: |
  id: find-console-logs
  language: javascript
  rule:
    pattern: console.log($$$)

Finding Async Functions That Use Await

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

Supported Languages

ast-grep supports many programming languages including:

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

For a complete list, see the ast-grep language support documentation.

Troubleshooting

Common 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