关于 Code To Tree
Code To Tree 是一个单一可执行文件的 MCP 服务,使用 tree-sitter 将源代码转换为 AST(抽象语法树),供 LLM 分析代码结构。
主要功能
- 01将源代码转换为 AST
- 02基于 tree-sitter 的多语言解析
- 03支持 C、C++、Rust、Ruby、Go、Java、Python
- 04单一独立二进制,依赖少
适用场景
- 01让 LLM 查看代码的语法结构
- 02在代码审查或重构前分析函数与类的层级
- 03跨多种语言比较源代码的语法树
MCP 相关
该项目是 MCP 服务,作为独立二进制文件运行,向 MCP 客户端提供源代码到 AST 的转换能力。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
目录
MCP 服务:code-to-tree
code-to-tree 服务的目标是:
- 让大模型能够准确地将源代码转换为 AST(抽象语法树), 不受编程语言限制。
- 一个独立的二进制文件就应包含 MCP 客户端所需的一切。
这些目标意味着:
- 底层语法解析器应具备足够的通用性。这里我们选择 tree-sitter,支持的语言包括:C、C++、Rust、Ruby、Go、Java、Python。
- 服务应能独立承载所有功能,尽可能减少对最终用户 机器上的软件依赖。这里我们选择 mcpc。
截图:


以上截图是通过提出 q.md 中指定的问题得到的。
(重要说明:对于同一个问题,大模型不一定会生成完全相同的结果;你很可能会得到风格或内容完全不同的结果。此处提供的截图和问题仅供参考。)
使用 code-to-tree
首先,你需要在机器上准备好 code-to-tree 可执行文件(Windows 版为 code-to-tree.exe,macOS 版为 code-to-tree),可以从 GitHub 发布页面下载,也可以自行构建。下载后,配置 MCP 客户端以安装它。更多详情请参阅“配置 MCP 客户端”一节。
配置 MCP 客户端
这里以 Claude 为例。
Windows
在 Claude 配置文件
(C:\Users\YOUR_NAME\AppData\Roaming\Claude\claude_desktop_config.json)
中指定 code-to-tree.exe 的位置:
{
"mcpServers": {
"code-to-tree": { "command": "C:\\path\\to\\code-to-tree.exe" }
}
}
macOS
在 Claude 配置文件
(~/Library/Application Support/Claude/claude_desktop_config.json)
中指定 code-to-tree 的位置:
{
"mcpServers": {
"code-to-tree": { "command": "/path/to/code-to-tree" }
}
}
构建(Windows)
1. 准备环境
- 下载并安装 MSYS2。
- 打开应用“MSYS2 MINGW64”。
- 运行
pacman -S make gcc git
2. 准备 tree-sitter 库
这里需要编译并安装 tree-sitter 及所有相关语法。
克隆这些仓库:
git clone https://github.com/tree-sitter/tree-sitter
git clone https://github.com/tree-sitter/tree-sitter-c
git clone https://github.com/tree-sitter/tree-sitter-cpp
git clone https://github.com/tree-sitter/tree-sitter-rust
git clone https://github.com/tree-sitter/tree-sitter-ruby
git clone https://github.com/tree-sitter/tree-sitter-go
git clone https://github.com/tree-sitter/tree-sitter-java
编译并安装它们:
cd tree-sitter && OS=1 make install
cd tree-sitter-c && OS=1 make install
cd tree-sitter-cpp && OS=1 make install
cd tree-sitter-rust && OS=1 make install
cd tree-sitter-ruby && OS=1 make install
cd tree-sitter-go && OS=1 make install
cd tree-sitter-java && OS=1 make install
3. 构建 code-to-tree
安装 mcpc:
git clone https://github.com/micl2e2/mcpc
cd mcpc && make install
编译 code-to-tree:
cd mcpc/example/code-to-tree
CFLAGS="-I/usr/local/include -L/usr/local/lib" make
# 检查二进制文件
file code-to-tree.exe
# 记下二进制文件的位置
pwd
# 假设输出为:/c/path/to/code-to-tree.exe
构建(macOS)
1. 准备环境
- Xcode 命令行工具
2. 准备 tree-sitter 库
这里需要编译并安装 tree-sitter 和所有相关语法。
克隆它们:
git clone https://github.com/tree-sitter/tree-sitter
git clone https://github.com/tree-sitter/tree-sitter-c
git clone https://github.com/tree-sitter/tree-sitter-cpp
git clone https://github.com/tree-sitter/tree-sitter-rust
git clone https://github.com/tree-sitter/tree-sitter-ruby
git clone https://github.com/tree-sitter/tree-sitter-go
git clone https://github.com/tree-sitter/tree-sitter-java
编译并安装它们:
cd tree-sitter && make install
cd tree-sitter-c && make install
cd tree-sitter-cpp && make install
cd tree-sitter-rust && make install
cd tree-sitter-ruby && make install
cd tree-sitter-go && make install
cd tree-sitter-java && make install
3. 构建 code-to-tree
安装 mcpc:
git clone https://github.com/micl2e2/mcpc
cd mcpc && make install
编译 code-to-tree:
cd mcpc/example/code-to-tree
make
# 检查二进制文件
file ./code-to-tree
# 记下二进制文件的位置
pwd
# 假设输出为:/path/to/code-to-tree
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。