Skip to main content
Glama

mcp-server-tree-sitter

by wrale
MIT License
33
  • Apple
  • Linux

MCP 树托管服务器

一个模型上下文协议 (MCP) 服务器,它使用 tree-sitter 提供代码分析功能,旨在让 AI 助手能够通过适当的上下文管理智能地访问代码库。Claude Desktop 是参考实现目标。

特征

  • 🔍灵活探索:以多种粒度检查代码
  • 🧠上下文管理:提供足够的信息,而不会压倒上下文窗口
  • 🌐语言无关:通过 tree-sitter-language-pack 支持多种编程语言,包括 Python、JavaScript、TypeScript、Go、Rust、C、C++、Swift、Java、Kotlin、Julia 和 APL
  • 🌳结构感知:使用基于 AST 的理解和高效的基于游标的遍历
  • 🔎可搜索:使用文本搜索和树状查询查找特定模式
  • 🔄缓存:通过解析树缓存优化性能
  • 🔑符号提取:提取和分析函数、类和其他代码符号
  • 📊依赖关系分析:识别和分析代码依赖关系和��系
  • 🧩状态持久性:在调用之间维护项目注册和缓存数据
  • 🔒安全:内置安全边界和输入验证

有关所有可用命令的完整列表、其当前实现状态以及详细的功能矩阵,请参阅FEATURES.md文档。

安装

先决条件

  • Python 3.10+
  • 适用于您首选语言的 Tree-sitter 语言解析器

基本安装

pip install mcp-server-tree-sitter

开发安装

git clone https://github.com/wrale/mcp-server-tree-sitter.git cd mcp-server-tree-sitter pip install -e ".[dev,languages]"

快速入门

使用 Claude Desktop 运行

您可以通过 MCP CLI 或手动配置 Claude Desktop 在 Claude Desktop 中使用该服务器。

使用 MCP CLI

使用 Claude Desktop 注册服务器:

mcp install mcp_server_tree_sitter.server:mcp --name "tree_sitter"
手动配置

或者,您可以手动配置 Claude Desktop:

  1. 打开您的 Claude Desktop 配置文件:
    • macOS/Linux: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%\Claude\claude_desktop_config.json

    如果文件不存在则创建该文件。

  2. 将服务器添加到mcpServers部分:
    { "mcpServers": { "tree_sitter": { "command": "python", "args": [ "-m", "mcp_server_tree_sitter.server" ] } } }
    或者,如果使用 uv 或其他包管理器:
    { "mcpServers": { "tree_sitter": { "command": "uv", "args": [ "--directory", "/ABSOLUTE/PATH/TO/YOUR/PROJECT", "run", "-m", "mcp_server_tree_sitter.server" ] } } }
    注意:请确保将/ABSOLUTE/PATH/TO/YOUR/PROJECT替换为项目目录的实际绝对路径。
  3. 保存文件并重新启动 Claude Desktop。

正确配置至少一个 MCP 服务器后,MCP 工具图标(锤子)将出现在 Claude Desktop 界面中。点击此图标即可访问tree_sitter服务器的功能。

使用发布版本进行配置

如果您不想从 PyPI(发布版本)手动安装包或克隆存储库,只需对 Claude Desktop 使用以下配置:

  1. 打开您的 Claude Desktop 配置文件(与上述相同的位置)。
  2. 将 tree-sitter 服务器添加到mcpServers部分:
    { "mcpServers": { "tree_sitter": { "command": "uvx", "args": [ "--directory", "/ABSOLUTE/PATH/TO/YOUR/PROJECT", "mcp-server-tree-sitter" ] } } }
  3. 保存文件并重新启动 Claude Desktop。

此方法使用uvx直接运行已安装的 PyPI 包,这是发布版本的推荐方法。服务器在其基本配置下运行不需要任何其他参数。

状态持久性

MCP Tree-sitter 服务器在调用之间维护状态。这意味着:

  • 项目保持注册状态,直到明确删除或服务器重新启动
  • 根据配置设置缓存解析树
  • 语言信息在服务器的整个生命周期内保留

在服务器的整个生命周期内,使用关键组件的单例模式在内存中维护这种持久性。

作为独立服务器运行

运行服务器的方法有多种:

直接使用 MCP CLI:
python -m mcp run mcp_server_tree_sitter.server
使用 Makefile 目标:
# Show available targets make # Run the server with default settings make mcp-run # Show help information make mcp-run ARGS="--help" # Show version information make mcp-run ARGS="--version" # Run with custom configuration file make mcp-run ARGS="--config /path/to/config.yaml" # Enable debug logging make mcp-run ARGS="--debug" # Disable parse tree caching make mcp-run ARGS="--disable-cache"
使用已安装的脚本:
# Run the server with default settings mcp-server-tree-sitter # Show help information mcp-server-tree-sitter --help # Show version information mcp-server-tree-sitter --version # Run with custom configuration file mcp-server-tree-sitter --config /path/to/config.yaml # Enable debug logging mcp-server-tree-sitter --debug # Disable parse tree caching mcp-server-tree-sitter --disable-cache

与 MCP 检查器一起使用

直接使用 MCP CLI:

python -m mcp dev mcp_server_tree_sitter.server

或者使用 Makefile 目标:

make mcp-dev

您还可以传递参数:

make mcp-dev ARGS="--debug"

用法

注册项目

首先注册一个项目来分析:

register_project_tool(path="/path/to/your/project", name="my-project")

探索文件

列出项目中的文件:

list_files(project="my-project", pattern="**/*.py")

查看文件内容:

get_file(project="my-project", path="src/main.py")

分析代码结构

获取语法树:

get_ast(project="my-project", path="src/main.py", max_depth=3)

提取符号:

get_symbols(project="my-project", path="src/main.py")

搜索代码

搜索文本:

find_text(project="my-project", pattern="function", file_pattern="**/*.py")

运行 tree-sitter 查询:

run_query( project="my-project", query='(function_definition name: (identifier) @function.name)', language="python" )

分析复杂性

analyze_complexity(project="my-project", path="src/main.py")

直接使用 Python

虽然主要用途是通过 MCP 服务器,但您也可以直接在 Python 代码中使用该库:

# Import from the API module from mcp_server_tree_sitter.api import ( register_project, list_projects, get_config, get_language_registry ) # Register a project project_info = register_project( path="/path/to/project", name="my-project", description="Description" ) # List projects projects = list_projects() # Get configuration config = get_config() # Access components through dependency injection from mcp_server_tree_sitter.di import get_container container = get_container() project_registry = container.project_registry language_registry = container.language_registry

配置

创建 YAML 配置文件:

cache: enabled: true # Enable/disable caching (default: true) max_size_mb: 100 # Maximum cache size in MB (default: 100) ttl_seconds: 300 # Cache entry time-to-live in seconds (default: 300) security: max_file_size_mb: 5 # Maximum file size to process in MB (default: 5) excluded_dirs: # Directories to exclude from processing - .git - node_modules - __pycache__ allowed_extensions: # Optional list of allowed file extensions # - py # - js # Leave empty or omit for all extensions language: default_max_depth: 5 # Default max depth for AST traversal (default: 5) preferred_languages: # List of languages to pre-load at startup for faster performance - python # Pre-loading reduces latency for first operations - javascript log_level: INFO # Logging level (DEBUG, INFO, WARNING, ERROR) max_results_default: 100 # Default maximum results for search operations

加载它:

configure(config_path="/path/to/config.yaml")

日志配置

可以使用环境变量来控制服务器的日志详细程度:

# Enable detailed debug logging export MCP_TS_LOG_LEVEL=DEBUG # Use normal informational logging (default) export MCP_TS_LOG_LEVEL=INFO # Only show warning and error messages export MCP_TS_LOG_LEVEL=WARNING

有关日志记录配置的完整信息,请参阅日志记录文档。有关命令行界面的详细信息,请参阅CLI 文档

关于 preferred_languages

preferred_languages设置控制哪些语言解析器在服务器启动时预加载,而不是按需加载。这有几个好处:

  • 更快的初始分析:首次分析预加载语言的文件时无延迟
  • 早期错误检测:解析器的问题在启动时发现,而不是在使用过程中
  • 可预测的内存分配:经常使用的解析器的内存是预先分配的

默认情况下,所有解析器都会在首次需要时按需加载。为了获得最佳性能,请指定您在项目中最常用的语言。

您还可以配置特定设置:

configure(cache_enabled=True, max_file_size_mb=10, log_level="DEBUG")

或者使用环境变量:

export MCP_TS_CACHE_MAX_SIZE_MB=256 export MCP_TS_LOG_LEVEL=DEBUG export MCP_TS_CONFIG_PATH=/path/to/config.yaml

环境变量使用格式MCP_TS_SECTION_SETTING (例如, MCP_TS_CACHE_MAX_SIZE_MB )进行部分设置,或MCP_TS_SETTING (例如, MCP_TS_LOG_LEVEL )进行顶级设置。

配置值按以下优先顺序应用:

  1. 环境变量(最高)
  2. 通过configure()调用设置的值
  3. YAML 配置文件
  4. 默认值(最低)

服务器将在以下位置查找配置:

  1. configure()调用中指定的路径
  2. MCP_TS_CONFIG_PATH环境变量指定的路径
  3. 默认位置: ~/.config/tree-sitter/config.yaml

对于开发人员

诊断能力

MCP Tree-sitter 服务器包含一个诊断框架,可帮助识别和修复问题:

# Run diagnostic tests make test-diagnostics # CI-friendly version (won't fail the build on diagnostic issues) make test-diagnostics-ci

诊断测试提供有关服务器行为的详细信息,并有助于隔离特定问题。有关诊断框架的更多信息,请参阅诊断文档

类型安全注意事项

MCP 树型服务器通过精心设计模式和协议,在与树型服务器库交互时保持类型安全。如果您要扩展代码库,请查看类型安全指南,了解有关处理树型服务器 API 变体的重要信息。

可用资源

服务器提供以下 MCP 资源:

  • project://{project}/files - 列出项目中的所有文件
  • project://{project}/files/{pattern} - 列出与模式匹配的文件
  • project://{project}/file/{path} - 获取文件内容
  • project://{project}/file/{path}/lines/{start}-{end} - 从文件中获取特定行
  • project://{project}/ast/{path} - 获取文件的 AST
  • project://{project}/ast/{path}/depth/{depth} - 获取自定义深度的 AST

可用工具

该服务器提供以下工具:

  • 项目管理: register_project_toollist_projects_toolremove_project_tool
  • 语言管理: list_languagescheck_language_available
  • 文件操作: list_filesget_fileget_file_metadata
  • AST 分析: get_astget_node_at_position
  • 代码搜索: find_textrun_query
  • 符号提取: get_symbolsfind_usage
  • 项目分析: analyze_projectget_dependenciesanalyze_complexity
  • 查询构建: get_query_template_toollist_query_templates_toolbuild_queryadapt_queryget_node_types
  • 相似代码检测: find_similar_code
  • 缓存管理: clear_cache
  • 配置诊断: diagnose_config

有关每个工具的实现状态、依赖关系和使用示例的详细信息,请参阅FEATURES.md

可用提示

服务器提供以下 MCP 提示:

  • code_review - 创建代码审查提示
  • explain_code - 创建解释代码的提示
  • explain_tree_sitter_query - 解释 tree-sitter 查询语法
  • suggest_improvements - 创建建议代码改进的提示
  • project_overview - 创建项目概述分析的提示

执照

麻省理工学院

Related MCP Servers

  • A
    security
    A
    license
    A
    quality
    An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.
    Last updated -
    8
    86
    JavaScript
    MIT License
  • -
    security
    -
    license
    -
    quality
    A specialized server that helps users create new Model Context Protocol (MCP) servers by providing tools and templates for scaffolding projects with various capabilities.
    Last updated -
    1
    TypeScript
  • -
    security
    A
    license
    -
    quality
    An implementation of the Model Context Protocol (MCP) that enables interaction with debug adapters, allowing language models to control debuggers, set breakpoints, evaluate expressions, and navigate source code during debugging sessions.
    Last updated -
    2
    Python
    AGPL 3.0
  • -
    security
    A
    license
    -
    quality
    A Model Context Protocol server that enables IDEs like Cursor and Windsurf to analyze large codebases using Gemini's extensive context window.
    Last updated -
    17
    Python
    MIT License

View all related MCP servers

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/wrale/mcp-server-tree-sitter'

If you have feedback or need assistance with the MCP directory API, please join our Discord server