How wrale/mcp-server-tree-sitter works
Project Type & Purpose
• This is a Python-based MCP (Model Context Protocol) microservice/server that exposes code-analysis capabilities (AST traversal, search, symbol extraction, dependency analysis, caching, etc.) via the MCP protocol.
• It is intended to be run as a standalone server process (e.g., via python -m mcp_server_tree_sitter.server or mcp-server-tree-sitter) and to be consumed by clients such as Claude Desktop or the MCP CLI.
File-Structure Analysis
• Top‐level:
• src/mcp_server_tree_sitter/ – main Python package
• scripts/ – helper shell scripts
• docs/ – design, CLI, config, logging, diagnostics guides
• Makefile, CI workflows, pyproject.toml – build & release
• src/mcp_server_tree_sitter/ sub-packages:
• __main__.py / server.py – entry points for CLI vs. server
• api.py – public functions for direct Python usage
• bootstrap/ – early initialization (logging, env)
• config.py – YAML/env var loading & precedence
• di.py – dependency‐injection container for singletons
• cache/ – parse-tree caching module
• capabilities/ – MCP server capability declarations
• language/ – registry of supported languages + query templates
• models/ – AST, project, cursor abstractions
• prompts/ – prebuilt MCP prompts (code_review, explain_code…)
• tools/ – core code-analysis operations (search, AST ops, file I/O)
• utils/ – file system, path, security, Tree-sitter helper functions
• logging_config.py + bootstrap/logging_bootstrap.py – log setup
• context.py – per‐request context management
• exceptions.py – domain‐specific errors
README Insights
• Core functionality invoked via MCP endpoints: get_ast, get_symbols, find_text, run_query, etc.
• Uses in‐memory singletons for state persistence (project registry, parser cache).
• Configurable via CLI flags, Makefile targets, Python API, YAML files, or env vars.
• Supports integration via MCP CLI (mcp install …) or manual JSON config in Claude Desktop.
a. Main Components
• Client Layer:
• MCP CLI (third‐party)
• Claude Desktop UI (MCP protocol client)
• Server Layer (mcp_server_tree_sitter.server):
• Entry point / CLI parser
• HTTP/WebSocket or stdio-based MCP protocol handler
• Capability negotiator
• Request dispatcher
• Core Modules (inside DI container):
• Config Manager
• Logging Manager
• Project Registry (manages registered projects)
• Language Registry (loads/parses tree-sitter grammars)
• Parser Cache (stores parse trees with TTL)
• Context Manager (manages per‐request state)
• Tools & Handlers (search, AST ops, symbol extraction)
• Prompts Module (generates MCP prompts)
• External Dependencies:
• File System (source files)
• Tree-sitter native libraries
• YAML/JSON config stores
b. Relationships & Data Flows
• Clients send MCP requests → Server Entry → Capability Layer → Dispatcher
• Dispatcher retrieves Config & Context → invokes specific Tool (e.g., get_ast)
• Tool asks Language Registry for parser → Parser Cache → Tree-sitter
• Tree-sitter parses file from File System → AST model returned
• Tool processes AST (cursor, models) → builds MCP response → sent back to client
c. Architectural Patterns
• Dependency Injection for singletons & testability
• Modular “Tools” approach for each feature (search, registry, diagnostics)
• Separation of concerns: bootstrap, config, logging, API, server, tools, models
• In‐memory caching with configurable TTL
• Protocol adapter layer (MCP) decouples client transport from business logic
d. Key Technologies
• Python 3.10+
• Tree-sitter parsers (via tree-sitter-language-pack)
• YAML / environment‐variable config
• In‐process DI container (di.py)
• Standard logging module with custom bootstrap
• Makefile / CI workflows for automation