返回 MCP 目录
public公开dns本地运行

code-to-tree

code-to-tree是一个MCP服务器,能够将多种编程语言的源代码准确转换为抽象语法树(AST),支持C、C++、Rust、Ruby、Go、Java和Python语言,使用tree-sitter解析器,提供独立可执行文件。

article

README

🚀 MCP Server: code-to-tree

MCP Server: code-to-tree 项目致力于为大语言模型(LLMs)提供将源代码准确转换为抽象语法树(AST)的能力,并且只需一个独立的二进制文件就能满足 MCP 客户端的所有需求。

📚 目录

✨ 主要特性

项目目标

  1. 赋予大语言模型(LLMs)准确将源代码转换为抽象语法树(AST)的能力,且不受编程语言的限制。
  2. 让 MCP 客户端仅依赖一个独立的二进制文件。

实现方式

  1. 选择多功能的语法解析器:选用 tree-sitter 作为底层语法解析器,支持的语言包括 C、C++、Rust、Ruby、Go、Java 和 Python。
  2. 减少软件依赖:选择 mcpc,使服务器自身具备所有功能,将最终用户机器上的软件依赖降至最低

截图展示

截图

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

⚠️ 重要提示

大语言模型(LLMs)对于相同问题不一定会生成完全相同的结果,你很可能会得到风格或内容完全不同的输出。这里提供的截图或问题仅作参考。

🚀 快速开始

在使用之前,你需要在本地机器上准备好 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. 准备环境

  1. 下载并安装 MSYS2。
  2. 打开 "MSYS2 MINGW64" 应用程序。
  3. 运行以下命令安装必要的工具:
pacman -S make gcc git

2. 准备 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 Command Line Tools。

2. 准备 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
help

运行方式说明

cloud

托管运行

托管运行通常表示这个 MCP Server 由服务方环境承载,用户一般按页面提供的连接方式或授权流程接入,不需要在本地长期启动一个 MCP 进程

  1. 打开服务方连接页
  2. 完成授权或复制端点
  3. 在 MCP 客户端中连接
terminal

本地运行 / 其它方式

本地运行通常需要用户在自己的电脑或服务器上安装依赖,把 server_config 复制到 MCP 客户端,并按 env_schema 补齐环境变量、密钥或其它配置

  1. 复制 server_config
  2. 安装所需依赖
  3. 补齐环境变量后重启客户端