PI

pwno-io/treesitter-mcp

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

20 stars 4 forks 质量 49 趋势 0

树木守护者 MCP 服务器

概览

一个MCP服务器和使用Tree-sitter来解析和分析代码的CLI。 - 解析文件并获取其 AST - 提取函数和变量 - 构建调用图 - 查找函数/变量的使用位置 - 运行自定义树状置询 - 列表导入/包含 - 提取特定行/列范围的源代码 - 可作为独立CLI或Claude Desktop的MCP服务器使用 这会同时安装 treesitter-mcp 和 ts-cli 的入口点。 用于 Claude Desktop 或其他 MCP 客户端: 关于如何配置,请参见docs或MCP_USAGE.md。 直接从终端运行分析: 用 --output-file 来写结果到 JSON 文件,而不是 stdout。 或者通过 URL 查询参数:http://127.0.0.1:8000?tools=treesitter_analyze_file,treesitter_get_ast

概述(AI 翻译)

一个MCP服务器和使用Tree-sitter来解析和分析代码的CLI。

它的作用

  • 解析文件并获取其 AST
  • 提取函数和变量
  • 构建调用图
  • 查找函数/变量的使用位置
  • 运行自定义树状置询
  • 列表导入/包含
  • 提取特定行/列范围的源代码
  • 作为独立CLI或Claude Desktop的MCP服务器使用

安装

需要 Python 3.10+。

这会同时安装 treesitter-mcp 和 ts-cli 的入口点。

或者不安装:

跑步

作为 MCP 服务器(默认)

用于 Claude Desktop 或其他 MCP 客户端:

关于如何配置,请参见 docs/MCP_USAGE.md。

独立CLI(ts-cli)

直接从终端运行分析:

用 --output-file 来写结果到 JSON 文件,而不是 stdout。

HTTP 模式

用于测试或手动使用:

限制工具

仅用--tools暴露特定工具:

或者通过 URL 查询参数:http://127.0.0.1:8000?tools=treesitter_analyze_file,treesitter_get_ast

可用工具:

  • treesitter_analyze_file - 基础分析
  • treesitter_get_ast - 全自动攻击
  • treesitter_get_call_graph - 函数调用

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

Tree-sitter MCP Server & CLI

An MCP server and CLI that uses Tree-sitter to parse and analyze code.

What it does

  • Parse files and get their AST
  • Extract functions and variables
  • Build call graphs
  • Find where functions/variables are used
  • Run custom Tree-sitter queries
  • List imports/includes
  • Extract source code for specific line/column ranges
  • Use as a standalone CLI or as an MCP server for Claude Desktop

Install

Requires 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 .

This installs both treesitter-mcp and ts-cli entry points.

Or run without installing:

uvx treesitter-mcp

Running

As an MCP server (default)

For use with Claude Desktop or other MCP clients:

treesitter-mcp

See docs/MCP_USAGE.md for how to configure.

Standalone CLI (ts-cli)

Run analysis directly from the terminal:

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

Use --output-file to write results to JSON files instead of stdout.

HTTP mode

For testing or manual use:

treesitter-mcp --http --port 8000 --host 127.0.0.1

Limiting tools

Only expose certain tools with --tools:

treesitter-mcp --http --port 8000 --tools treesitter_analyze_file,treesitter_get_ast

Or via URL query param: http://127.0.0.1:8000?tools=treesitter_analyze_file,treesitter_get_ast

Tools available:

  • treesitter_analyze_file - Basic analysis
  • treesitter_get_ast - Full AST
  • treesitter_get_call_graph - Function calls
  • treesitter_find_function - Find function definitions
  • treesitter_find_variable - Find variables
  • treesitter_get_source_for_range - Extract source code for a range
  • treesitter_get_supported_languages - What’s supported
  • treesitter_get_node_at_point - AST node at a line/column
  • treesitter_get_node_for_range - AST node for a range
  • treesitter_cursor_walk - Walk tree with context
  • treesitter_run_query - Custom Tree-sitter queries
  • treesitter_find_usage - Find symbol usages
  • treesitter_get_dependencies - Extract imports/includes

If you don’t specify --tools, everything is exposed.

Writing output to file

All tools support an optional output_file parameter. When provided, the tool writes its result directly to the specified file (as pretty-printed JSON) instead of returning it. This is useful for large outputs like ASTs that could cause context overload in agents.

Example:

# 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}
在 GitHub 查看完整 README

安装

uvx treesitter-mcp
新手?先看 mcpskill.org 教程 →