TreeSitter Code Structure MCP Server

TreeSitter Code Structure MCP Server

Analyzes source code across multiple languages to extract structural elements like classes, functions, and parameters using tree-sitter. It provides LLM-optimized markdown output that includes nesting levels, line numbers, and signatures to facilitate codebase navigation.

Category
Visit Server

README

TreeSitter MCP Server

A fast Model Context Protocol (MCP) server that analyzes source code files and extracts their structure in a markdown format optimized for LLM consumption.

Features

  • Multi-language Support: Python, JavaScript, TypeScript, Java, C#, and Go
  • Fast Parsing: Uses tree-sitter for efficient AST parsing
  • Comprehensive Structure: Extracts classes, functions, nested elements
  • Line Numbers: Tracks start and end lines for each element
  • Nesting Levels: Shows the depth of nested elements
  • Parameters & Return Types: Extracts function signatures
  • Optional Docstrings: Configurable docstring extraction
  • Multi-File Analysis: Analyze single or multiple files in one request
  • Error Handling: Parses as much as possible and indicates error locations
  • LLM-Optimized Output: Markdown format designed for easy LLM consumption

Installation

Using uv (Recommended)

uv sync

Using pip

pip install -r requirements.txt

Usage

Running the MCP Server

uv run python src/server.py

Or directly:

python src/server.py

MCP Configuration

Add the following to your MCP client configuration (e.g., Claude Desktop):

{
  "mcpServers": {
    "CodeStructureAnalyzer": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/TreeSitterMcp",
        "run",
        "python",
        "src/server.py"
      ]
    }
  }
}

MCP Tool: query

Analyzes the structure of one or more source code files.

Parameters:

  • file_path (required): Path to the source code file(s) to analyze. Can be either:
    • A single file path as a string (e.g., "src/models.py")
    • An array of file paths (e.g., ["src/models.py", "src/config.py"])
  • include_docstrings (optional, default: false): Whether to include docstrings in the output

Single File Analysis

Example Request:

{
  "name": "query",
  "arguments": {
    "file_path": "src/models.py",
    "include_docstrings": true
  }
}

Multi-File Analysis

Example Request:

{
  "name": "query",
  "arguments": {
    "file_path": ["src/models.py", "src/config.py", "src/server.py"],
    "include_docstrings": false
  }
}

Output Format

The output is optimized for token efficiency and follows this schema: Format: ### Name (Start-End, Nesting, [Parent]) | - Type | - [Parameters] | - [Return Type] | - [Docstring]

Example Output:

Format: ### `Name` (Start-End, Nesting, [Parent]) | - Type | - [Parameters] | - [Return Type] | - [Docstring]

# `src/models.py`

### `MyClass` (10-50, N:0)
- Class
- A sample class for demonstration.

  ### `__init__` (15-25, N:1, P: `MyClass`)
  - Function
  - (self, param1: str, param2: int)
  - -> None
  - Initialize the class.

Multi-File Output Example

Format: ### `Name` (Start-End, Nesting, [Parent]) | - Type | - [Parameters] | - [Return Type] | - [Docstring]

# `src/models.py`

### `MyClass` (10-50, N:0)
- Class
...

---
# `src/config.py`

### `get_language_from_extension` (10-20, N:0)
- Function
...

Supported Languages

Language File Extensions
Python .py
JavaScript .js, .mjs, .cjs
TypeScript .ts, .tsx
Java .java
C# .cs
Go .go

Architecture

The server is organized into the following modules:

  • src/mcp_impl/server.py: MCP server implementation with tool definitions
  • src/parsers/tree_sitter.py: Tree-sitter parser integration
  • src/extractors/structure.py: Code structure extraction logic
  • src/formatters/markdown.py: Markdown formatting for output
  • src/config.py: Language configuration and mappings
  • src/models.py: Data models for code elements

Error Handling

The server attempts to parse as much of the file as possible, even when there are syntax errors. Errors are reported in a dedicated section:

## Parse Errors

⚠️ **Error at Line 42**: Syntax error
return self.process(item

Development

Running Tests

uv run pytest

Code Formatting

uv run black src/

Type Checking

uv run mypy src/

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
E2B

E2B

Using MCP to run code via e2b.

Official
Featured