代码结构分析工具

codetree是一款通过tree-sitter提供结构化代码理解的MCP服务器,帮助AI代理高效理解代码结构而非读取整个文件,支持23种工具和11种编程语言。

  • by ThinkyMiner
  • Python
  • 本地部署
  • 30 星标
  • 更新于
在 GitHub 查看 ThinkyMiner/codeTree

使用说明

代码树

停止向您的AI代理提供整个文件。

代码树是一个 主控程序 通过以下方式为编码代理提供结构化代码理解的服务器 树保姆 --所以他们问的是精确的问题,而不是阅读数千行。23个工具,11种语言,约1秒启动时间。没有矢量数据库,没有嵌入模型,没有配置。

快速开始

先决条件: 安装 紫外线 如果你没有(curl -LsSf https://astral.sh/uv/install.sh | sh).

然后 cd 进入任何项目并运行:

claude mcp add codetree -- uvx --from mcp-server-codetree codetree --root .

就是这样 . 意思是“这个项目”。您的代理现在对结构化代码有了理解。

不使用克劳德代码?看 编辑器设置 适用于Cursor、VS Code、Windsurf和Claude Desktop。

之前/之后

在代码树--agent读取原始文件之前:

$ cat calculator.py
import math
from typing import Optional

class Calculator:
    """A scientific calculator with memory."""

    def __init__(self):
        self.memory = 0
        self.history = []

    def add(self, a: float, b: float) -> float:
        """Add two numbers."""
        result = a + b
        self.history.append(('add', a, b, result))
        return result

    def divide(self, a: float, b: float) -> Optional[float]:
        """Divide a by b, returns None on zero division."""
        if b == 0:
            return None
        result = a / b
        self.history.append(('divide', a, b, result))
        return result

    # ... 200 more lines of methods ...

消耗的令牌:完整文件约2000+

在代码树--agent请求骨架之后:

class Calculator → line 4
  "A scientific calculator with memory."
  def __init__(self) (in Calculator) → line 7
  def add(self, a: float, b: float) (in Calculator) → line 11
    "Add two numbers."
  def divide(self, a: float, b: float) (in Calculator) → line 17
    "Divide a by b, returns None on zero division."
  def sqrt(self, x: float) (in Calculator) → line 24
    "Square root using math.sqrt."

消耗的代币:~80。减少了25倍。

代理可以看到每个类、方法和文档字符串(带行号),而无需读取单个函数体。当它需要完整的来源时 divide,它叫 get_symbol("calculator.py", "divide") 只有这6条线。

23工具

了解结构

工具 目的
get_file_skeleton(file_path) 带行号+文档注释的类、函数、方法
get_symbol(file_path, symbol_name) 函数或类的完整源代码
get_skeletons(file_paths) 多个文件的批处理骨架
get_symbols(symbols) 多个符号的批处理源
get_imports(file_path) 带行号的导入报表

导航关系

工具 目的
find_references(symbol_name) 回购中符号的所有用法
get_call_graph(file_path, function_name) 函数调用什么+调用什么
get_blast_radius(file_path, symbol_name) 传递性影响——如果你改变这一点,会发生什么

分析质量

工具 目的
get_complexity(file_path, function_name) 圈复杂度分解
find_dead_code(file_path?) 已定义但从未引用的符号
detect_clones(file_path?, min_lines?) 重复/接近重复的功能

检查和搜索

工具 目的
search_symbols(query?, type?, parent?) 使用过滤器进行灵活的符号搜索
find_tests(file_path, symbol_name) 查找符号的测试函数

入职培训与图表

工具 目的
index_status() 图表索引新鲜度和统计数据
get_repository_map(max_items?) 紧凑型仓库概述:语言、入口点、热点
resolve_symbol(query, kind?, path_hint?) 在排名合格的比赛中消除短名称的歧义
search_graph(query?, kind?, file_pattern?) 使用度过滤器和分页进行图形搜索

更改和数据流

工具 目的
get_change_impact(symbol_query?, diff_scope?) 通过符号或git diff进行影响分析,并注明风险等级
analyze_dataflow(file_path, function_name, mode?) 变量数据流、污染分析或跨功能污染跟踪

可视化和历史

工具 目的
find_hot_paths(top_n?) 高复杂度×高呼叫数优化目标
get_dependency_graph(file_path?, format?) 文件级依赖关系图为Mermaid或列表
git_history(mode?, file_path?, top_n?) Git故障、文件流失或更改耦合分析
suggest_docs(file_path?, symbol_name?) 查找带有上下文的未文档化函数以生成文档

get_file_skeleton, get_skeletons,以及 search_symbols 接受 format="compact" 甚至更少的代币。

支持的语言

语言 扩展
python .py
JavaScript .js, .jsx
TypeScript .ts
TSX .tsx
去吧 .go
生锈 .rs
Java .java
C .c, .h
C .cpp, .cc, .cxx, .hpp, .hh
红宝石 .rb
Kotlin .kt

编辑器设置

这 --root flag告诉代码树要分析哪个项目。使用 . 当前目录或完整路径。

克劳德代码

cd 进入你的项目,然后:

claude mcp add codetree -- uvx --from mcp-server-codetree codetree --root .

光标

添加到 .cursor/mcp.json 在您的项目中:

{
  "mcpServers": {
    "codetree": {
      "command": "uvx",
      "args": ["--from", "mcp-server-codetree", "codetree", "--root", "${workspaceFolder}"]
    }
  }
}

VS代码(副本)

添加到 .vscode/mcp.json 在您的项目中:

{
  "servers": {
    "codetree": {
      "command": "uvx",
      "args": ["--from", "mcp-server-codetree", "codetree", "--root", "${workspaceFolder}"]
    }
  }
}

帆板运动

添加到 ~/.codeium/windsurf/mcp_config.json:

{
  "mcpServers": {
    "codetree": {
      "command": "uvx",
      "args": ["--from", "mcp-server-codetree", "codetree", "--root", "${workspaceFolder}"]
    }
  }
}

克劳德桌面版

添加到 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)或 %APPDATA%\Claude\claude_desktop_config.json (Windows):

{
  "mcpServers": {
    "codetree": {
      "command": "uvx",
      "args": ["--from", "mcp-server-codetree", "codetree", "--root", "/path/to/your/project"]
    }
  }
}

Claude Desktop不支持 ${workspaceFolder},所以在这里使用完整路径。

为什么是代码树?

替代 限制 代码树
直接读取文件 Burns代币,无结构,无关系 25倍代币缩减,结构化输出
grep/ripgrep 纯文本,无AST感知,无调用图 理解代码结构,而不仅仅是文本
LSP服务器 重量级、有状态、特定语言的设置 一个命令、10种语言、无状态MCP
SCIP/LSIF索引器 构建速度慢,设置复杂,索引庞大 ~1s启动,JSON缓存,零配置
AST专用工具 原始树对于代理来说冗长而困难 为代理设计的预结构化输出

建筑

Agent (Claude, Copilot, Cursor, etc.)
    │ MCP (stdio)
    ▼
codetree server (FastMCP)
    │
    ├── Indexer → LanguagePlugin → tree-sitter → structured results
    │   Cache (.codetree/index.json, mtime-based)
    │
    └── Graph Layer → SQLite (.codetree/graph.db)
        Persistent symbols + edges, incremental updates
        Change impact, dataflow, taint analysis
模块 责任
server.py FastMCP服务器——定义了所有23个工具
indexer.py 文件发现、插件分发、定义索引
cache.py 具有mtime无效的骨架缓存
registry.py 将文件扩展名映射到语言插件
languages/ 每种语言一个插件(Python、JS、TS、Go、Rust、Java、C、C++、Ruby)
graph/store.py 符号和边的SQLite持久性
graph/builder.py 增量图形生成器(sha256变化检测)
graph/queries.py 存储库映射、符号解析、更改影响、热路径、依赖关系图、文档建议
graph/dataflow.py 函数内和跨函数数据流和污染分析
graph/git_analysis.py Git故障、流失、变更耦合分析

添加语言

  1. pip install tree-sitter-LANG 并添加到 pyproject.toml
  2. 复制 src/codetree/languages/_template.py 到 languages/yourlang.py
  3. 实现抽象方法
  4. 在中注册扩展名 registry.py
  5. 添加测试

发展

git clone https://github.com/ThinkyMiner/codeTree.git
cd codeTree
python -m venv .venv
source .venv/bin/activate
pip install -e .
pip install pytest

# Run all tests (~1058 tests, ~35s)
pytest

# Run a single test file
pytest tests/languages/test_python.py -v

贡献

欢迎投稿!看 贡献.md 有关设置说明和指南。