AST代码搜索工具

基于Rust实现的MCP服务器,通过ast-grep提供结构化代码搜索功能,适用于AI助手和开发工具集成。

  • by GodSpeedAI
  • Rust
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 GodSpeedAI/ast-grep-mcp-rs

使用说明

ast grep MCP服务器(Rust)

Rust实现a 模型上下文协议(MCP) 通过以下方式为AI助手提供结构化代码搜索的服务器 ast grep.

上游归因

此存储库是Rust移植的 ast-grep/ast-grep-mcp. 原始归属和MIT许可证通知保存在 许可证.

此服务器提供什么

服务器公开了四个MCP工具:

  • dump_syntax_tree:检查语法树或模式结构以了解调试规则。
  • test_match_code_rule:针对stdin中的代码测试YAML ast grep规则。
  • find_code:使用ast-grep模式搜索项目。
  • find_code_by_rule:使用完整的YAML ast grep规则搜索项目。

find_code 和 find_code_by_rule 支持:

  • output_format: text (默认)或 json
  • max_results:返回匹配项的可选正限制

需求

  1. 安装ast grep CLI(必须在您的 PATH)
# Choose one installation method from ast-grep docs
brew install ast-grep
cargo install ast-grep --locked
  1. 安装Rust工具链
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  1. 使用MCP兼容客户端(Cursor、Claude Desktop等)

安装

选项1:从crates.io安装(推荐)

cargo install ast-grep-mcp

这会将可执行文件安装为:

ast-grep-mcp-server

在Linux/macOS上,Cargo将二进制文件安装到 ~/.cargo/bin 默认情况下。 确保该目录位于您的 PATH.

选项2:从GitHub发布版下载预构建的二进制文件

  1. 打开“发布”页面: https://github.com/GodSpeedAI/ast-grep-mcp-rs/releases
  2. 下载适用于您平台的存档。
  3. 取出并放置 ast-grep-mcp-server (或 ast-grep-mcp-server.exe 在Windows上)的某个位置 PATH.
  4. 使用验证校验和 SHA256SUMS.txt 从同一个释放。

发布资产的名称如下:

  • ast-grep-mcp-server-vX.Y.Z-x86_64-unknown-linux-gnu.tar.gz
  • ast-grep-mcp-server-vX.Y.Z-x86_64-apple-darwin.tar.gz
  • ast-grep-mcp-server-vX.Y.Z-aarch64-apple-darwin.tar.gz
  • ast-grep-mcp-server-vX.Y.Z-x86_64-pc-windows-msvc.zip
  • SHA256SUMS.txt

校验和验证示例:

sha256sum -c SHA256SUMS.txt

快速入门(已安装二进制文件)

  1. 安装ast grep CLI和此服务器。
  2. 验证 ast-grep-mcp-server 从你的壳里跑出来。
  3. 配置您的MCP客户端以启动 ast-grep-mcp-server.

快速验证命令:

ast-grep-mcp-server --help

跑

默认传输为stdio:

ast-grep-mcp-server

使用自定义ast-grep配置:

ast-grep-mcp-server --config /absolute/path/to/sgconfig.yaml

您还可以设置:

export AST_GREP_CONFIG=/absolute/path/to/sgconfig.yaml

运输支持

  • stdio:支持和推荐
  • sse:当前未在此Rust端口中实现

如果你与 --transport sse,服务器退出时出错,提示您使用stdio。

客户端配置

如果您是通过安装的 cargo install ast-grep-mcp 或从GitHub发布中,使用命令名 ast-grep-mcp-server 直接。

光标

添加到MCP设置中:

{
  "mcpServers": {
    "ast-grep": {
      "command": "ast-grep-mcp-server",
      "args": [],
      "env": {}
    }
  }
}

克劳德桌面版

添加到您的Claude Desktop MCP配置中:

{
  "mcpServers": {
    "ast-grep": {
      "command": "ast-grep-mcp-server",
      "args": [],
      "env": {}
    }
  }
}

VS Code

添加到您的VS代码MCP配置中:

{
  "mcpServers": {
    "ast-grep": {
      "command": "ast-grep-mcp-server",
      "args": [],
      "env": {}
    }
  }
}

常用VS Code用户设置位置:

  • Linux: ~/.config/Code/User/settings.json
  • macOS: ~/Library/Application Support/Code/User/settings.json
  • 窗户: %APPDATA%\\Code\\User\\settings.json

CLI选项

  • --config PATH:路径 sgconfig.yaml
  • --transport {stdio|sse}:默认值为 stdio (sse未实施)
  • --port PORT:已解析,但目前仅与未来的SSE支持相关

工具行为注释

  • project_folder 参数必须是绝对路径。
  • test_match_code_rule 未找到匹配项时返回错误。
  • 对于关系规则(inside, has),添加 stopBy: end 以避免不完整的遍历。
  • 文本输出紧凑(file:start-end +匹配的片段)以减少令牌使用。

MCP客户端的示例查询

  • 在此仓库中查找所有Python函数定义。
  • 查找锈迹 impl 具有模式匹配的块。
  • 用代码片段测试这个ast grep YAML规则。
  • 转储此TypeScript代码段的CST。

贡献

贡献者和发布维护者说明见 贡献.md.

支持的语言

内置语言集包括:

  • bash、c、cpp、csharp、css、elixir、go、haskell、html、java
  • javascript、json、jsx、kotlin、lua、nix、php、python、ruby、rust
  • scala、solidity、swift、tsx、typescript、yaml

您可以在中使用自定义语言扩展语言支持 sgconfig.yaml.

故障排除

  1. ast-grep 找不到命令:安装astgrep并验证它是否在 PATH.
  2. 没有符合复杂关系规则的匹配项:添加 stopBy: end.
  3. 意外的解析/匹配行为:使用 dump_syntax_tree 检查CST/模式表示。
  4. 配置路径错误:确保 --config 或 AST_GREP_CONFIG 指向现有文件。

许可证

MIT。看 许可证.