Tree-sitter MCP server

Provides code analysis capabilities through tree-sitter parsing, enabling structured understanding and manipulation of source code across multiple programming languages for tasks like code review, refactoring, and documentation generation.
Back to servers
Setup instructions
Provider
Joshua M. Dotson
Release date
Mar 17, 2025
Language
Python
Package
Stats
7.1K downloads
143 stars

The MCP Tree-sitter Server provides code analysis capabilities using tree-sitter, giving AI assistants intelligent access to codebases with appropriate context management. It supports multiple programming languages and offers structure-aware code exploration features.

Installation

Prerequisites

  • Python 3.10+
  • Tree-sitter language parsers for your preferred languages

Basic Installation

pip install mcp-server-tree-sitter

Integrating with Claude Desktop

You can make the server available in Claude Desktop either through the MCP CLI or by manually configuring it.

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:

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

  2. Add the server to the mcpServers section:

    {
        "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"
                ]
            }
        }
    }
    
  3. Save the file and restart Claude Desktop.

Using Released Version

If you prefer to use the released version from PyPI:

  1. Open your Claude Desktop configuration file (same location as above).

  2. Add the tree-sitter server to the mcpServers section:

    {
        "mcpServers": {
            "tree_sitter": {
                "command": "uvx",
                "args": [
                    "--directory", "/ABSOLUTE/PATH/TO/YOUR/PROJECT",
                    "mcp-server-tree-sitter"
                ]
            }
        }
    }
    
  3. Save the file and restart Claude Desktop.

Running the Server

As a Standalone Server

There are several ways to run the server:

Using the MCP CLI directly:

python -m mcp run mcp_server_tree_sitter.server

Using the installed script:

# 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

Using with the MCP Inspector

python -m mcp dev mcp_server_tree_sitter.server

Usage

Project Management

Register a project for analysis:

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

List registered projects:

list_projects_tool()

Remove a project:

remove_project_tool(name="my-project")

File Operations

List files in the project:

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

View file content:

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

Get file metadata:

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

Code Structure Analysis

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")

Find where a symbol is used:

find_usage(project="my-project", symbol="MyClass", path="src/main.py")

Code Search

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"
)

Code Analysis

Analyze code complexity:

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

Analyze project structure:

analyze_project(project="my-project")

Identify dependencies:

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

Find similar code:

find_similar_code(project="my-project", path="src/main.py", function_name="process_data")

Configuration

Create a YAML configuration file:

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

Apply configuration:

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

Or configure specific settings:

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

Environment Variables

You can also use environment variables to configure the server:

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

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "tree_sitter" '{"command":"python","args":["-m","mcp_server_tree_sitter.server"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "tree_sitter": {
            "command": "python",
            "args": [
                "-m",
                "mcp_server_tree_sitter.server"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "tree_sitter": {
            "command": "python",
            "args": [
                "-m",
                "mcp_server_tree_sitter.server"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later