MCP Server for Tree-sitter code analysis
Project description
MCP Tree-sitter Server
A Model Context Protocol (MCP) server that provides code analysis capabilities using tree-sitter, designed to give Claude intelligent access to codebases with appropriate context management.
Features
- 🔍 Flexible Exploration: Examine code at multiple levels of granularity
- 🧠 Context Management: Provides just enough information without overwhelming the context window
- 🌐 Language Agnostic: Supports many programming languages including Python, JavaScript, TypeScript, Go, Rust, C, C++, Swift, Java, Kotlin, Julia, and APL via tree-sitter-language-pack
- 🌳 Structure-Aware: Uses AST-based understanding with efficient cursor-based traversal
- 🔎 Searchable: Find specific patterns using text search and tree-sitter queries
- 🔄 Caching: Optimized performance through parse tree caching
- 🔑 Symbol Extraction: Extract and analyze functions, classes, and other code symbols
- 📊 Dependency Analysis: Identify and analyze code dependencies and relationships
- 🧩 State Persistence: Maintains project registrations and cached data between invocations
- 🔒 Secure: Built-in security boundaries and input validation
Installation
Prerequisites
- Python 3.10+
- Tree-sitter language parsers for your preferred languages
Basic Installation
pip install mcp-server-tree-sitter
Development Installation
git clone https://github.com/wrale/mcp-server-tree-sitter.git
cd mcp-server-tree-sitter
pip install -e ".[dev,languages]"
Quick Start
Running with Claude Desktop
You can make the server available in Claude Desktop either through the MCP CLI or by manually configuring Claude Desktop.
Using MCP CLI
Register the server with Claude Desktop:
mcp install mcp_server_tree_sitter.server:mcp --name "tree_sitter"
Manual Configuration
Alternatively, you can manually configure Claude Desktop:
-
Open your Claude Desktop configuration file:
- macOS/Linux:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
Create the file if it doesn't exist.
- macOS/Linux:
-
Add the server to the
mcpServerssection:{ "mcpServers": { "tree_sitter": { "command": "python", "args": [ "-m", "mcp_server_tree_sitter.server" ] } } }
Alternatively, if using uv or another package manager:
{ "mcpServers": { "tree_sitter": { "command": "uv", "args": [ "--directory", "/ABSOLUTE/PATH/TO/YOUR/PROJECT", "run", "-m", "mcp_server_tree_sitter.server" ] } } }
Note: Make sure to replace
/ABSOLUTE/PATH/TO/YOUR/PROJECTwith the actual absolute path to your project directory. -
Save the file and restart Claude Desktop.
The MCP tools icon (hammer) will appear in Claude Desktop's interface once you have properly configured at least one MCP server. You can then access the tree_sitter server's functionality by clicking on this icon.
Configuring with Released Version
If you prefer not to manually install the package from PyPI (released version) or clone the repository, simply use the following configuration for Claude Desktop:
-
Open your Claude Desktop configuration file (same location as above).
-
Add the tree-sitter server to the
mcpServerssection:{ "mcpServers": { "tree_sitter": { "command": "uvx", "args": [ "--directory", "/ABSOLUTE/PATH/TO/YOUR/PROJECT", "mcp-server-tree-sitter" ] } } }
-
Save the file and restart Claude Desktop.
This method uses uvx to run the installed PyPI package directly, which is the recommended approach for the released version. The server doesn't require any additional parameters to run in its basic configuration.
State Persistence
The MCP Tree-sitter Server maintains state between invocations. This means:
- Projects stay registered until explicitly removed or the server is restarted
- Parse trees are cached according to configuration settings
- Language information is retained throughout the server's lifetime
This persistence is maintained in-memory during the server's lifetime using singleton patterns for key components.
Running as a standalone server
mcp run mcp_server_tree_sitter.server
Using with the MCP Inspector
mcp dev mcp_server_tree_sitter.server
Usage
Register a Project
First, register a project to analyze:
register_project_tool(path="/path/to/your/project", name="my-project")
Explore Files
List files in the project:
list_files(project="my-project", pattern="**/*.py")
View file content:
get_file(project="my-project", path="src/main.py")
Analyze Code Structure
Get the syntax tree:
get_ast(project="my-project", path="src/main.py", max_depth=3)
Extract symbols:
get_symbols(project="my-project", path="src/main.py")
Search Code
Search for text:
find_text(project="my-project", pattern="function", file_pattern="**/*.py")
Run tree-sitter queries:
run_query(
project="my-project",
query='(function_definition name: (identifier) @function.name)',
language="python"
)
Analyze Complexity
analyze_complexity(project="my-project", path="src/main.py")
Configuration
Create a YAML configuration file:
cache:
enabled: true
max_size_mb: 100
ttl_seconds: 300
security:
max_file_size_mb: 5
excluded_dirs:
- .git
- node_modules
- __pycache__
language:
auto_install: false
default_max_depth: 5
Load it with:
configure(config_path="/path/to/config.yaml")
Available Resources
The server provides the following MCP resources:
project://{project}/files- List all files in a projectproject://{project}/files/{pattern}- List files matching a patternproject://{project}/file/{path}- Get file contentproject://{project}/file/{path}/lines/{start}-{end}- Get specific lines from a fileproject://{project}/ast/{path}- Get the AST for a fileproject://{project}/ast/{path}/depth/{depth}- Get the AST with custom depth
Available Tools
The server provides tools for:
- Project management:
register_project_tool,list_projects_tool,remove_project_tool - Language management:
list_languages,check_language_available - File operations:
list_files,get_file,get_file_metadata - AST analysis:
get_ast,get_node_at_position - Code search:
find_text,run_query - Symbol extraction:
get_symbols,find_usage - Project analysis:
analyze_project,get_dependencies,analyze_complexity - Query building:
get_query_template_tool,list_query_templates_tool,build_query,adapt_query - Cache management:
clear_cache
Available Prompts
The server provides the following MCP prompts:
code_review- Create a prompt for reviewing codeexplain_code- Create a prompt for explaining codeexplain_tree_sitter_query- Explain tree-sitter query syntaxsuggest_improvements- Create a prompt for suggesting code improvementsproject_overview- Create a prompt for a project overview analysis
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_server_tree_sitter-0.3.3.tar.gz.
File metadata
- Download URL: mcp_server_tree_sitter-0.3.3.tar.gz
- Upload date:
- Size: 116.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b61da58b35b502c007f48299d11ba75f2a50fbf1f134540c4c21a793bccc9a0c
|
|
| MD5 |
7cbcbdfdfdf1042e42c40edb2968c68e
|
|
| BLAKE2b-256 |
845dbe24a7052274d4ebc0a710a565a68ce7846167a67af4d197ea5c61b112f5
|
Provenance
The following attestation bundles were made for mcp_server_tree_sitter-0.3.3.tar.gz:
Publisher:
release.yml on wrale/mcp-server-tree-sitter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_server_tree_sitter-0.3.3.tar.gz -
Subject digest:
b61da58b35b502c007f48299d11ba75f2a50fbf1f134540c4c21a793bccc9a0c - Sigstore transparency entry: 183096398
- Sigstore integration time:
-
Permalink:
wrale/mcp-server-tree-sitter@60ee62553fa10af5de56f9f6c4a6de6f1c1f3239 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/wrale
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@60ee62553fa10af5de56f9f6c4a6de6f1c1f3239 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mcp_server_tree_sitter-0.3.3-py3-none-any.whl.
File metadata
- Download URL: mcp_server_tree_sitter-0.3.3-py3-none-any.whl
- Upload date:
- Size: 67.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9a34d6b54616629290408afbd6da6a1e656b42277539d28c8ac22d7f4831766
|
|
| MD5 |
d61f0e4b3b1dde5424a5c8f136a5bf42
|
|
| BLAKE2b-256 |
739d116c0c1f9f12877f51f90154f1ea82b19dee855a597177e83e5a550fc679
|
Provenance
The following attestation bundles were made for mcp_server_tree_sitter-0.3.3-py3-none-any.whl:
Publisher:
release.yml on wrale/mcp-server-tree-sitter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_server_tree_sitter-0.3.3-py3-none-any.whl -
Subject digest:
b9a34d6b54616629290408afbd6da6a1e656b42277539d28c8ac22d7f4831766 - Sigstore transparency entry: 183096401
- Sigstore integration time:
-
Permalink:
wrale/mcp-server-tree-sitter@60ee62553fa10af5de56f9f6c4a6de6f1c1f3239 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/wrale
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@60ee62553fa10af5de56f9f6c4a6de6f1c1f3239 -
Trigger Event:
release
-
Statement type: