Tree-sitter Code Analysis
Intelligent code analysis via AST parsing with tree-sitter.
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
Setup
MCP server configured in ~/.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
register_project_tool(path="/path/to/project", name="my-project")
2. Explore Files
list_files(project="my-project", pattern="**/*.py")
get_file(project="my-project", path="src/main.py")
3. Analyze Structure
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
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
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
Supported Languages
Python, JavaScript, TypeScript, Go, Rust, C, C++, Swift, Java, Kotlin, Julia, APL, and many more via tree-sitter-language-pack.
1---2name: tree-sitter3description: AST-based code analysis using tree-sitter. Use for parsing code structure, extracting symbols, finding patterns with tree-sitter queries, analyzing complexity, and understanding code architecture. Supports Python, JavaScript, TypeScript, Go, Rust, C, C++, Swift, Java, Kotlin, Julia, and more.4---56# Tree-sitter Code Analysis78Intelligent code analysis via AST parsing with tree-sitter.910## When to Use1112- Understanding code structure across multiple languages13- Extracting function/class definitions14- Finding code patterns with tree-sitter queries15- Analyzing code complexity16- Symbol extraction and dependency analysis1718## Setup1920MCP server configured in `~/.mcp.json`:21```json22{23 "tree-sitter": {24 "command": "python3",25 "args": ["-m", "mcp_server_tree_sitter.server"],26 "cwd": "/Users/alice/mcp-server-tree-sitter"27 }28}29```3031## Usage Pattern3233### 1. Register a Project First34```35register_project_tool(path="/path/to/project", name="my-project")36```3738### 2. Explore Files39```40list_files(project="my-project", pattern="**/*.py")41get_file(project="my-project", path="src/main.py")42```4344### 3. Analyze Structure45```46get_ast(project="my-project", path="src/main.py", max_depth=3)47get_symbols(project="my-project", path="src/main.py")48```4950### 4. Search with Queries51```52find_text(project="my-project", pattern="function", file_pattern="**/*.py")53run_query(54 project="my-project",55 query='(function_definition name: (identifier) @function.name)',56 language="python"57)58```5960### 5. Complexity Analysis61```62analyze_complexity(project="my-project", path="src/main.py")63```6465## Available Tools6667- **Project**: `register_project_tool`, `list_projects_tool`, `remove_project_tool`68- **Language**: `list_languages`, `check_language_available`69- **Files**: `list_files`, `get_file`, `get_file_metadata`70- **AST**: `get_ast`, `get_node_at_position`71- **Search**: `find_text`, `run_query`72- **Symbols**: `get_symbols`, `find_usage`73- **Analysis**: `analyze_project`, `get_dependencies`, `analyze_complexity`74- **Queries**: `get_query_template_tool`, `build_query`, `adapt_query`75- **Similar Code**: `find_similar_code`7677## Supported Languages7879Python, JavaScript, TypeScript, Go, Rust, C, C++, Swift, Java, Kotlin, Julia, APL, and many more via tree-sitter-language-pack.