返回市场
代码转树结构

代码转树结构

作者:micl2e266 星标更新:2025-05-17

项目介绍

【技术文档摘要】

目录

<a id="orgf542482"></a>

MCP 服务器:code-to-tree

code-to-tree 服务器的目标是:

  1. 给 LLM 提供准确地将源代码转换成抽象语法树(AST)的能力,无论语言是什么。
  2. 一个独立的二进制文件应该包含 MCP 客户端所需的一切。

这些目标意味着:

  1. 底层的语法解析器需要足够灵活。这里我们选择 tree-sitter,支持的语言包括:C、C++、Rust、Ruby、Go、Java 和 Python。
  2. 服务器应该能够自给自足,对终端用户的机器施加尽可能少的软件依赖。这里我们选择 mcpc

截图:

<img src="./chathistory.png" width="450px" /><img src="./wholeast.png" width="200px" />

以上截图是通过在 q.md 中指定的问题获得的。

重要提示:LLMs 对于相同问题生成完全相同的答案没有责任,你可能会得到完全不同风格或内容的结果。这里提供的截图或问题是仅供参考)

<a id="org862e7dc"></a>

使用 code-to-tree

首先,你需要在你的机器上安装 code-to-tree 可执行文件(Windows 上为 code-to-tree.exe,macOS 上为 code-to-tree),你可以从 GitHub 发布页面下载或自行编译。下载后,配置你的 MCP 客户端来安装它,详情请参阅“配置 MCP 客户端”部分。

<a id="orge54fa87"></a>

配置 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" }
    }
}

<a id="org48a8180"></a>

构建(Windows)

1. 准备环境

  1. 下载并安装 MSYS2。
  2. 打开应用“MSYS2 MINGW64”。
  3. 运行 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-s[...]

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

<a id="orgbaa740e"></a>

构建(macOS)

1. 准备环境

  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