ast grep MCP服务器(Rust)
上游归因
此存储库是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(默认)或jsonmax_results:返回匹配项的可选正限制
需求
- 安装ast grep CLI(必须在您的
PATH)
# Choose one installation method from ast-grep docs
brew install ast-grep
cargo install ast-grep --locked- 安装Rust工具链
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh- 使用MCP兼容客户端(Cursor、Claude Desktop等)
安装
选项1:从crates.io安装(推荐)
cargo install ast-grep-mcp这会将可执行文件安装为:
ast-grep-mcp-server在Linux/macOS上,Cargo将二进制文件安装到 ~/.cargo/bin 默认情况下。 确保该目录位于您的 PATH.
选项2:从GitHub发布版下载预构建的二进制文件
- 打开“发布”页面:
- 下载适用于您平台的存档。
- 取出并放置
ast-grep-mcp-server(或ast-grep-mcp-server.exe在Windows上)的某个位置PATH. - 使用验证校验和
SHA256SUMS.txt从同一个释放。
发布资产的名称如下:
ast-grep-mcp-server-vX.Y.Z-x86_64-unknown-linux-gnu.tar.gzast-grep-mcp-server-vX.Y.Z-x86_64-apple-darwin.tar.gzast-grep-mcp-server-vX.Y.Z-aarch64-apple-darwin.tar.gzast-grep-mcp-server-vX.Y.Z-x86_64-pc-windows-msvc.zipSHA256SUMS.txt
校验和验证示例:
sha256sum -c SHA256SUMS.txt快速入门(已安装二进制文件)
- 安装ast grep CLI和此服务器。
- 验证
ast-grep-mcp-server从你的壳里跑出来。 - 配置您的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.
故障排除
ast-grep找不到命令:安装astgrep并验证它是否在PATH.- 没有符合复杂关系规则的匹配项:添加
stopBy: end. - 意外的解析/匹配行为:使用
dump_syntax_tree检查CST/模式表示。 - 配置路径错误:确保
--config或AST_GREP_CONFIG指向现有文件。
许可证
MIT。看 许可证.
