AG

ast-grep/ast-grep-mcp

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

501 stars 28 forks 质量 88 趋势 21MIT

一个实验性的模型上下文协议(MCP)服务器,利用 ast-grep 为 AI 助手提供强大的结构代码搜索能力。

概览

一个实验性的模型上下文协议(MCP)服务器,利用 ast-grep 为 AI 助手提供强大的结构代码搜索能力。 该MCP服务器使AI助手(如Cursor、Claude Desktop等)能够通过抽象语法树(AST)模式匹配而非简单的文本搜索来搜索和分析代码库。通过利用ast-grep的结构搜索功能,AI可以: - 基于语法结构而非仅仅文本匹配寻找代码模式 - 搜索特定的编程构造(函数、类、导入等) - 使用 YAML 配置编写和测试复杂的搜索规则 - 调试并可视化 AST 结构以优化模式开发 3. : 如光标、Claude Desktop 或其他 MCP 客户端 你可以直接在GitHub上用uvx运行服务器: 这有助于快速测试服务器而不克隆仓库。 添加到你的MCP设置中(通常在.cursor-mcp/settings.json): 添加到你的Claude桌面MCP配置中:

概述(AI 翻译)

一个实验性的模型上下文协议(MCP)服务器,利用 ast-grep 为 AI 助手提供强大的结构代码搜索能力。

概述

该MCP服务器使AI助手(如Cursor、Claude Desktop等)能够通过抽象语法树(AST)模式匹配而非简单的文本搜索来搜索和分析代码库。通过利用ast-grep的结构搜索功能,AI可以:

  • 基于语法结构而非仅仅匹配文本的代码模式
  • 搜索特定的编程构造(函数、类、导入等)
  • 使用 YAML 配置编写和测试复杂搜索规则
  • 调试和可视化AST结构,以优化模式开发

先修条件

  1. 安装ast-grep:请遵循ast-grep安装指南

  2. 安装uv: Python包管理器

  3. MCP 兼容客户端:如 Cursor、Claude Desktop 或其他 MCP 客户端

安装

  1. 克隆此仓库:

  2. 安装依赖:

  3. 验证ast-grep安装:

与[[T0]一起奔跑]

你可以直接从 GitHub 运行服务器,使用 uvx:

这有助于快速测试服务器而不克隆仓库。

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

ast-grep MCP Server

An experimental Model Context Protocol (MCP) server that provides AI assistants with powerful structural code search capabilities using ast-grep.

Overview

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:

  • Find code patterns based on syntax structure, not just text matching
  • Search for specific programming constructs (functions, classes, imports, etc.)
  • Write and test complex search rules using YAML configuration
  • Debug and visualize AST structures for better pattern development

Prerequisites

  1. Install ast-grep: Follow ast-grep installation guide

    # macOS
    brew install ast-grep
    nix-shell -p ast-grep
    cargo install ast-grep --locked
    
  2. Install uv: Python package manager

    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  3. MCP-compatible client: Such as Cursor, Claude Desktop, or other MCP clients

Installation

  1. Clone this 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 installation:

    ast-grep --version
    

Running with uvx

You can run the server directly from GitHub using uvx:

uvx --from git+https://github.com/ast-grep/ast-grep-mcp ast-grep-server

This is useful for quickly trying out the server without cloning the repository.

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

The MCP server supports using a custom sgconfig.yaml file to configure ast-grep behavior. See the ast-grep configuration documentation for details on the config file format.

You can provide the config file in two ways (in order of precedence):

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

Custom ast-grep Command

If ast-grep is not in PATH, or must be launched through another command, set AST_GREP_PATH. The value can be an executable path or a command prefix:

{
在 GitHub 查看完整 README

安装

uvx --from git+https://github.com/ast-grep/ast-grep-mcp ast-grep-server
新手?先看 mcpskill.org 教程 →

配置

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