使用说明
树保姆MCP服务器和CLI
使用Tree sitter解析和分析代码的MCP服务器和CLI。
它做什么
- 解析文件并获取其AST
- 提取函数和变量
- 构建调用图
- 查找函数/变量的使用位置
- 运行自定义树定位查询
- 列出进口/包括
- 提取特定行/列范围的源代码
- 用作独立的CLI或Claude Desktop的MCP服务器
安装
需要Python 3.10+。
# Install directly
uv pip install treesitter-mcp
# Or install a specific version
uv pip install treesitter-mcp==2.1
# Or clone and install
git clone https://github.com/pwno-io/treesitter-mcp.git
cd treesitter-mcp
uv pip install -e .
这将同时安装 treesitter-mcp 和 ts-cli 入口点。
或者在不安装的情况下运行:
uvx treesitter-mcp
跑步
作为MCP服务器(默认)
用于Claude Desktop或其他MCP客户端:
treesitter-mcp
看 docs/MCP_USAGE.md 了解如何配置。
独立命令行界面(ts-CLI)
直接从终端运行分析:
ts-cli path/to/file.py
ts-cli path/to/file.py --ast --max-depth 2
ts-cli path/to/file.py --find-function main --include-source
ts-cli --supported-languages
使用 --output-file 将结果写入JSON文件而不是stdout。
HTTP模式
用于测试或手动使用:
treesitter-mcp --http --port 8000 --host 127.0.0.1
限制工具
仅暴露某些工具 --tools:
treesitter-mcp --http --port 8000 --tools treesitter_analyze_file,treesitter_get_ast
或者通过URL查询参数: http://127.0.0.1:8000?tools=treesitter_analyze_file,treesitter_get_ast
可用工具:
treesitter_analyze_file-基本分析treesitter_get_ast-全ASTtreesitter_get_call_graph-函数调用treesitter_find_function-查找函数定义treesitter_find_variable-查找变量treesitter_get_source_for_range-提取某个范围的源代码treesitter_get_supported_languages-支持什么treesitter_get_node_at_point-行/列上的AST节点treesitter_get_node_for_range-一个范围的AST节点treesitter_cursor_walk-带着背景走树treesitter_run_query-自定义树保姆查询treesitter_find_usage-查找符号用法treesitter_get_dependencies-提取进口/包括
如果您没有指定 --tools一切都暴露了。
将输出写入文件
所有工具都支持可选 output_file 参数。如果提供,该工具
将结果直接写入指定文件(作为打印精美的JSON)
返回它。这对于像AST这样的大型输出很有用,因为它可能会导致
代理中的上下文过载。
例子:
# Returns result to file, minimal response to agent
treesitter_get_ast(file_path="large_file.py", output_file="~/output/ast.json")
# Returns: {"status": "written", "output_file": "/home/user/output/ast.json", "bytes_written": 123456}
该工具将:
- 如果父目录不存在,则自动创建父目录
- 扩展
~到您的主目录 - 如果覆盖现有文件,则警告(到stderr)
- 成功时返回最小确认字典,如果写入失败则返回错误字典
在结果中包含源代码
这 treesitter_find_function 和 treesitter_find_variable 工具支持
可选的 include_source 参数。当设置为 True,每个匹配的符号
在结果中包含其源代码:
treesitter_find_function(file_path="server.py", name="main", include_source=True)
# Returns: {"query": "main", "matches": [{"name": "main", ..., "source": "def main():\n ..."}]}
语言支持
| 语言 | analyze_file | get_ast | get_call_graph | find_function | find_variable | find_usage | get_dependencies |
|---|---|---|---|---|---|---|---|
| C✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| C✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| python✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| JavaScript | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| TypeScript | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 去吧✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| Java✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| PHP | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 锈蚀 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Ruby | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
文件扩展名
| 语言 | 扩展 |
|---|---|
C .c |
|
C .cpp, .cc, .cxx, .h, .hpp |
|
python .py |
|
| JavaScript | .js, .jsx, .mjs, .cjs |
| TypeScript | .ts, .tsx, .cts, .mts |
| 去吧 | .go |
Java .java |
|
| PHP | .php, .phtml |
| 生锈 | .rs |
| 红宝石 | .rb |
