Skip to main content
Glama
TreeSitter Code Structure MCP Server

TreeSitter Code Structure MCP Server

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#, Go, and Rust

  • 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

Related MCP server: Scantool - File Scanner MCP

Installation

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

Rust

.rs

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.

Available Tools

1 tool
analyze_code_structureA

Analyze the structure of one or more source code files and return classes, functions, their line numbers, nesting levels, parameters, and return types. Supports Python, JavaScript, TypeScript, Java, C#, and Go. Accepts either a single file path (string) or an array of file paths.

Inputs: file_path (required), include_docstrings.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.6/5.0

    Scored across 1 tool

    Disambiguation5/5

    With only one tool, there is no possibility of ambiguity or overlap between tools. The tool has a clear, singular purpose focused on analyzing code structure across multiple programming languages.

    Naming Consistency5/5

    The single tool name 'analyze_code_structure' follows a clear verb_noun pattern and is descriptive. Since there is only one tool, consistency is inherently perfect with no deviations to assess.

    Tool Count2/5

    A single tool is too few for a server focused on code structure analysis, as it suggests an incomplete or overly simplistic surface. For a domain like code analysis, typical servers would include multiple tools for different aspects (e.g., parsing, querying, modifying).

    Completeness2/5

    The tool set is severely incomplete for the implied domain of code structure analysis. While the single tool provides analysis, there are obvious gaps such as tools for querying specific elements, modifying code, or handling other structural operations, which limits agent workflows.

    Maintenance

    ActivityInactive
    ResponsivenessOutdated data

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      C
      maintenance
      Enables AI assistants to understand and navigate codebases through structural analysis. Provides code mapping, symbol search, and impact analysis using ast-grep for accurate parsing of Python, JavaScript, TypeScript, and Go projects.
      4
      52
      MIT
    • A
      license
      B
      quality
      B
      maintenance
      Analyzes source code structure across multiple languages using tree-sitter, extracting classes, functions, methods, and metadata with precise line numbers for efficient codebase exploration and AI-assisted development.
      13
      3
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      Analyzes codebases and extracts all symbols (functions, classes, methods, interfaces, etc.) from 10+ programming languages into LLM-optimized markdown format. Enables AI assistants to understand entire project structures efficiently without processing full source code.
      2
      9 npm
      MIT