tree-sitter

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Tree-sitter Code Analysis

Tree-sitter 代码分析

Intelligent code analysis via AST parsing with tree-sitter.
通过tree-sitter进行AST解析实现智能代码分析。

When to Use

使用场景

  • Understanding code structure across multiple languages
  • Extracting function/class definitions
  • Finding code patterns with tree-sitter queries
  • Analyzing code complexity
  • Symbol extraction and dependency analysis
  • 跨语言理解代码结构
  • 提取函数/类定义
  • 通过tree-sitter查询查找代码模式
  • 分析代码复杂度
  • 符号提取与依赖分析

Setup

配置

MCP server configured in
~/.mcp.json
:
json
{
  "tree-sitter": {
    "command": "python3",
    "args": ["-m", "mcp_server_tree_sitter.server"],
    "cwd": "/Users/alice/mcp-server-tree-sitter"
  }
}
~/.mcp.json
中配置MCP服务器:
json
{
  "tree-sitter": {
    "command": "python3",
    "args": ["-m", "mcp_server_tree_sitter.server"],
    "cwd": "/Users/alice/mcp-server-tree-sitter"
  }
}

Usage Pattern

使用模式

1. Register a Project First

1. 先注册项目

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

2. Explore Files

2. 浏览文件

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

3. Analyze Structure

3. 分析结构

get_ast(project="my-project", path="src/main.py", max_depth=3)
get_symbols(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")

4. Search with Queries

4. 查询搜索

find_text(project="my-project", pattern="function", file_pattern="**/*.py")
run_query(
  project="my-project",
  query='(function_definition name: (identifier) @function.name)',
  language="python"
)
find_text(project="my-project", pattern="function", file_pattern="**/*.py")
run_query(
  project="my-project",
  query='(function_definition name: (identifier) @function.name)',
  language="python"
)

5. Complexity Analysis

5. 复杂度分析

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

Available Tools

可用工具

  • Project:
    register_project_tool
    ,
    list_projects_tool
    ,
    remove_project_tool
  • Language:
    list_languages
    ,
    check_language_available
  • Files:
    list_files
    ,
    get_file
    ,
    get_file_metadata
  • AST:
    get_ast
    ,
    get_node_at_position
  • Search:
    find_text
    ,
    run_query
  • Symbols:
    get_symbols
    ,
    find_usage
  • Analysis:
    analyze_project
    ,
    get_dependencies
    ,
    analyze_complexity
  • Queries:
    get_query_template_tool
    ,
    build_query
    ,
    adapt_query
  • Similar Code:
    find_similar_code
  • 项目管理
    register_project_tool
    list_projects_tool
    remove_project_tool
  • 语言支持
    list_languages
    check_language_available
  • 文件操作
    list_files
    get_file
    get_file_metadata
  • AST分析
    get_ast
    get_node_at_position
  • 搜索查询
    find_text
    run_query
  • 符号处理
    get_symbols
    find_usage
  • 代码分析
    analyze_project
    get_dependencies
    analyze_complexity
  • 查询工具
    get_query_template_tool
    build_query
    adapt_query
  • 相似代码查找
    find_similar_code

Supported Languages

支持语言

Python, JavaScript, TypeScript, Go, Rust, C, C++, Swift, Java, Kotlin, Julia, APL, and many more via tree-sitter-language-pack.
Python、JavaScript、TypeScript、Go、Rust、C、C++、Swift、Java、Kotlin、Julia、APL,以及更多通过tree-sitter-language-pack支持的语言。