Labsco
wrale logo

MCP Tree-sitter Server

β˜… 310

from wrale

A server for code analysis using Tree-sitter, with context management capabilities.

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeAdvanced setup

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.

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:

python -m mcp run mcp_server_tree_sitter.server

Using Makefile targets:

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

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

python -m mcp dev mcp_server_tree_sitter.server

Or using the Makefile target:

make mcp-dev

You can also pass arguments:

make mcp-dev ARGS="--debug"

For Developers

Diagnostic Capabilities

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

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