MCP Tree-sitter Server logo

MCP Tree-sitter Server

Organization
wrale

MCP Server for Tree-sitter

Publisherwrale
Repositorymcp-server-tree-sitter
LanguagePython
Forks
40
Stars
309
Available tools
0
Transport typestdio
Categories
LicenseMIT
Links
  • Connect tools to AI workflows

    MCP Tree-sitter Server exposes MCP capabilities that can be used by compatible AI clients and agents.

  • 0 available tools

    Browse the callable actions below, including names and descriptions when provided by the server.

  • Ready-to-copy setup

    Use the installation snippets to configure this server in your preferred MCP client.

  • Open source signals

    309 stars and 40 forks from the linked repository.

MCP Tree-sitter Server

A Model Context Protocol (MCP) server that provides code analysis capabilities using tree-sitter, designed to give AI assistants intelligent access to codebases with appropriate context management. Claude Desktop is the reference implementation target.

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++, C#, Swift, Java, Kotlin, Dart, 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

For a comprehensive list of all available commands, their current implementation status, and detailed feature matrix, please refer to the FEATURES.md document.

Installation

Prerequisites

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

Basic Installation

bash
pip install mcp-server-tree-sitter

Development Installation

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

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:

bash
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:

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

    Alternatively, if using uv or another package manager:

    json
    {
        "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/PROJECT with the actual absolute path to your project directory.

  3. 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:

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

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

    json
    {
        "mcpServers": {
            "tree_sitter": {
                "command": "uvx",
                "args": [
                    "--directory", "/ABSOLUTE/PATH/TO/YOUR/PROJECT",
                    "mcp-server-tree-sitter"
                ]
            }
        }
    }
  3. 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

There are several ways to run the server:

Using the MCP CLI directly:

bash
python -m mcp run mcp_server_tree_sitter.server

Using Makefile targets:

bash
# 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"

Using the installed script:

bash
# 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

Using the MCP CLI directly:

bash
python -m mcp dev mcp_server_tree_sitter.server

Or using the Makefile target:

bash
make mcp-dev

You can also pass arguments:

bash
make mcp-dev ARGS="--debug"

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

Direct Python Usage

While the primary intended use is through the MCP server, you can also use the library directly in Python code:

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

Configuration

Create a YAML configuration file:

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

Load it with:

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

Logging Configuration

The server's logging verbosity can be controlled using environment variables:

bash
# 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

For comprehensive information about logging configuration, please refer to the logging documentation. For details on the command-line interface, see the CLI documentation.

About preferred_languages

The preferred_languages setting controls which language parsers are pre-loaded at server startup rather than on-demand. This provides several benefits:

  • Faster initial analysis: No delay when first analyzing a file of a pre-loaded language
  • Early error detection: Issues with parsers are discovered at startup, not during use
  • Predictable memory allocation: Memory for frequently used parsers is allocated upfront

By default, all parsers are loaded on-demand when first needed. For optimal performance, specify the languages you use most frequently in your projects.

You can also configure specific settings:

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

Or use environment variables:

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

Environment variables use the format MCP_TS_SECTION_SETTING (e.g., MCP_TS_CACHE_MAX_SIZE_MB) for section settings, or MCP_TS_SETTING (e.g., MCP_TS_LOG_LEVEL) for top-level settings.

Configuration values are applied in this order of precedence:

  1. Environment variables (highest)
  2. Values set via configure() calls
  3. YAML configuration file
  4. Default values (lowest)

The server will look for configuration in:

  1. Path specified in configure() call
  2. Path specified by MCP_TS_CONFIG_PATH environment variable
  3. Default location: ~/.config/tree-sitter/config.yaml

For Developers

Diagnostic Capabilities

The MCP Tree-sitter Server includes a diagnostic framework to help identify and fix issues:

bash
# Run diagnostic tests
make test-diagnostics

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

Diagnostic tests provide detailed information about the server's behavior and can help isolate specific issues. For more information about the diagnostic framework, please see the diagnostics documentation.

Type Safety Considerations

The MCP Tree-sitter Server maintains type safety when interfacing with tree-sitter libraries through careful design patterns and protocols. If you're extending the codebase, please review the type safety guide for important information about handling tree-sitter API variations.

Available Resources

The server provides the following MCP resources:

  • project://{project}/files - List all files in a project
  • project://{project}/files/{pattern} - List files matching a pattern
  • project://{project}/file/{path} - Get file content
  • project://{project}/file/{path}/lines/{start}-{end} - Get specific lines from a file
  • project://{project}/ast/{path} - Get the AST for a file
  • project://{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, get_node_types
  • Similar code detection: find_similar_code
  • Cache management: clear_cache
  • Configuration diagnostics: diagnose_config

See FEATURES.md for detailed information about each tool's implementation status, dependencies, and usage examples.

Available Prompts

The server provides the following MCP prompts:

  • code_review - Create a prompt for reviewing code
  • explain_code - Create a prompt for explaining code
  • explain_tree_sitter_query - Explain tree-sitter query syntax
  • suggest_improvements - Create a prompt for suggesting code improvements
  • project_overview - Create a prompt for a project overview analysis

Feedback & Community

We'd love to hear how you're using mcp-server-tree-sitter and what would make it more useful for your workflow.

License

MIT

Installation

TypingMind
Prerequisites:

Node.js 18+

{
  "mcpServers": {
    "wrale-mcp-server-tree-sitter": {
      "command": "uvx",
      "args": [
        "mcp-server-tree-sitter"
      ]
    }
  }
}

Use MCP Tree-sitter Server MCP with multiple AI models

TypingMind connects MCP tools at the workspace level, so once MCP Tree-sitter Server is connected, you can use it with different AI models in TypingMind instead of setting it up separately for each model. This MCP runs locally through the TypingMind MCP connector on your device.

Setup guide to use the local connector

Use this when the MCP server needs access to local files, apps, or private resources on your computer.

1

Open the MCP settings

In TypingMind, go to Settings, Advanced Settings, then Model Context Protocol and choose Setup Connector.

  1. Open TypingMind in your browser.
  2. Click the Settings icon.
  3. Go to Advanced Settings.
  4. Open the Model Context Protocol section.
  5. Click Setup Connector and choose This Device.
TypingMind MCP connector setup screen with This Device selected
2

Run the connector command

Choose This Device, copy the command from TypingMind, and run it in Terminal. Keep the process running while you use MCP.

  1. Copy the setup command shown by TypingMind.
  2. Open Terminal on macOS or Windows Terminal on Windows.
  3. Paste and run the command.
  4. Approve the package install if Terminal asks you to proceed.
  5. Keep the Terminal window running while using MCP tools.
3

Add MCP Tree-sitter Server as a server

When the connector status is Ready, click Edit Servers and paste the MCP server configuration.

  1. Wait until the connector status shows Ready.
  2. Click Edit Servers.
  3. Paste the MCP Tree-sitter Server MCP server configuration.
  4. Save the server list.
  5. Refresh if you want to confirm the connector is still ready.
TypingMind MCP settings showing active server and Edit Servers button
{
  "mcpServers": {
    "wrale-mcp-server-tree-sitter": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-server-tree-sitter"
      ]
    }
  }
}
4

Use it across models

Save the server list, open Plugins, enable the MCP Tree-sitter Server MCP tools, then select any supported AI model in TypingMind and use the tools in chat or assign them to an AI agent.

  1. Open the Plugins page in TypingMind.
  2. Enable the MCP Tree-sitter Server MCP tools.
  3. Start a chat and choose the AI model you want to use.
  4. Use the MCP tools in chat or assign them to an AI agent.
  5. Switch to another AI model whenever needed without reconnecting MCP.
TypingMind chat using enabled MCP tools with a selected AI model
Can you use MCP Tree-sitter Server to help me with this task?
MCP Tree-sitter Server
Sure. I read it.
Here is what I found using MCP Tree-sitter Server.

Frequently asked questions

What is the MCP Tree-sitter Server MCP server used for?

MCP Tree-sitter Server is an MCP server that lets compatible AI clients connect to external tools and context. In TypingMind, you can add this MCP server once and make its tools available in your AI workspace.

Can I use MCP Tree-sitter Server MCP with multiple AI models in TypingMind?

Yes. TypingMind connects MCP tools at the workspace level, so you can use MCP Tree-sitter Server with different AI models such as Claude, ChatGPT, Gemini, or other models you have configured in TypingMind without setting up the MCP server separately for each model.

Why use MCP Tree-sitter Server MCP with TypingMind?

TypingMind is one of the best frontends for LLM chat because it brings multiple AI models, prompts, plugins, AI agents, API keys, and MCP tools into one workspace. With MCP Tree-sitter Server connected, you can use its MCP tools across your preferred models while keeping your chat workflow organized in TypingMind.

How do I connect MCP Tree-sitter Server MCP to TypingMind?

MCP Tree-sitter Server runs through the TypingMind local MCP connector. This is best when the MCP server needs access to local files, desktop apps, command-line tools, or private resources on your computer.

What tools does MCP Tree-sitter Server MCP provide in TypingMind?

MCP Tree-sitter Server exposes MCP capabilities that can be enabled from the TypingMind Plugins page and used in chat or assigned to AI agents.

Do I need to share my API keys with TypingMind to use MCP Tree-sitter Server MCP?

No. TypingMind is local-first and lets you keep your model providers, API keys, prompts, and MCP configuration under your control. If MCP Tree-sitter Server requires authentication, add the required headers, OAuth settings, or local configuration for that MCP server when you create the connection.

Related MCP Servers

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇