Skip to main content
Glama
treesitter-mcp

treesitter-mcp

by pwno-io

Tree-sitter MCP Server & CLI

An MCP server and CLI that uses Tree-sitter to parse and analyze code.

What it does

  • Parse files and get their AST

  • Extract functions and variables

  • Build call graphs

  • Find where functions/variables are used

  • Run custom Tree-sitter queries

  • List imports/includes

  • Extract source code for specific line/column ranges

  • Use as a standalone CLI or as an MCP server for Claude Desktop

Related MCP server: code-analyze-mcp

Install

Requires Python 3.10+.

# Install directly
uv pip install treesitter-mcp

# Or install a specific version
uv pip install treesitter-mcp==2.1

# Or clone and install
git clone https://github.com/pwno-io/treesitter-mcp.git
cd treesitter-mcp
uv pip install -e .

This installs both treesitter-mcp and ts-cli entry points.

Or run without installing:

uvx treesitter-mcp

Running

As an MCP server (default)

For use with Claude Desktop or other MCP clients:

treesitter-mcp

See docs/MCP_USAGE.md for how to configure.

Standalone CLI (ts-cli)

Run analysis directly from the terminal:

ts-cli path/to/file.py
ts-cli path/to/file.py --ast --max-depth 2
ts-cli path/to/file.py --find-function main --include-source
ts-cli --supported-languages

Use --output-file to write results to JSON files instead of stdout.

HTTP mode

For testing or manual use:

treesitter-mcp --http --port 8000 --host 127.0.0.1

Limiting tools

Only expose certain tools with --tools:

treesitter-mcp --http --port 8000 --tools treesitter_analyze_file,treesitter_get_ast

Or via URL query param: http://127.0.0.1:8000?tools=treesitter_analyze_file,treesitter_get_ast

Tools available:

  • treesitter_analyze_file - Basic analysis

  • treesitter_get_ast - Full AST

  • treesitter_get_call_graph - Function calls

  • treesitter_find_function - Find function definitions

  • treesitter_find_variable - Find variables

  • treesitter_get_source_for_range - Extract source code for a range

  • treesitter_get_supported_languages - What's supported

  • treesitter_get_node_at_point - AST node at a line/column

  • treesitter_get_node_for_range - AST node for a range

  • treesitter_cursor_walk - Walk tree with context

  • treesitter_run_query - Custom Tree-sitter queries

  • treesitter_find_usage - Find symbol usages

  • treesitter_get_dependencies - Extract imports/includes

If you don't specify --tools, everything is exposed.

Writing output to file

All tools support an optional output_file parameter. When provided, the tool writes its result directly to the specified file (as pretty-printed JSON) instead of returning it. This is useful for large outputs like ASTs that could cause context overload in agents.

Example:

# Returns result to file, minimal response to agent
treesitter_get_ast(file_path="large_file.py", output_file="~/output/ast.json")
# Returns: {"status": "written", "output_file": "/home/user/output/ast.json", "bytes_written": 123456}

The tool will:

  • Automatically create parent directories if they don't exist

  • Expand ~ to your home directory

  • Warn (to stderr) if overwriting an existing file

  • Return a minimal confirmation dict on success, or an error dict if writing fails

Including source code in results

The treesitter_find_function and treesitter_find_variable tools support an optional include_source parameter. When set to True, each matched symbol includes its source code in the result:

treesitter_find_function(file_path="server.py", name="main", include_source=True)
# Returns: {"query": "main", "matches": [{"name": "main", ..., "source": "def main():\n    ..."}]}

Language support

Language

analyze_file

get_ast

get_call_graph

find_function

find_variable

find_usage

get_dependencies

C

✅

✅

✅

✅

✅

✅

✅

C++

✅

✅

✅

✅

✅

✅

✅

Python

✅

✅

✅

✅

✅

✅

✅

JavaScript

✅

✅

✅

✅

✅

✅

✅

TypeScript

✅

✅

✅

✅

✅

✅

✅

Go

✅

✅

✅

✅

✅

✅

✅

Java

✅

✅

✅

✅

✅

✅

✅

PHP

✅

✅

✅

✅

✅

✅

✅

Rust

✅

✅

✅

✅

✅

✅

✅

Ruby

✅

✅

✅

✅

✅

✅

✅

File extensions

Language

Extensions

C

.c

C++

.cpp, .cc, .cxx, .h, .hpp

Python

.py

JavaScript

.js, .jsx, .mjs, .cjs

TypeScript

.ts, .tsx, .cts, .mts

Go

.go

Java

.java

PHP

.php, .phtml

Rust

.rs

Ruby

.rb

Docs

Available Tools

13 tools
treesitter_analyze_fileA

Analyze a source code file and extract symbols (functions, classes, etc.).

Args: file_path: Path to the source code file to analyze (supports .py, .c, .cpp, .h, .hpp) output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary containing: - file_path: The analyzed file path - language: Detected programming language - symbols: List of extracted symbols (functions, classes, etc.) - errors: Any parsing errors encountered OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Note: This function does not return the full AST to avoid serialization issues. Use treesitter_get_ast() if you need the complete AST.

Inputs: file_path (required), output_file.

treesitter_cursor_walkA

Return a cursor-style view (focus node + context) at a point.

Args: file_path: Path to the source code file row: Row number (0-based) column: Column number (0-based) max_depth: Maximum depth of the AST to return. output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary with focus, ancestors, siblings, and children OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: row (required), column (required), file_path (required), max_depth, +1 more.

treesitter_find_functionA

Search for a specific function definition by name.

Args: file_path: Path to the source code file name: Name of the function to find include_source: If True, includes the source code for each matched function. output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary containing: - query: The search query (function name) - matches: List of Symbol objects representing matching function definitions. If include_source is True, each match includes a "source" field. OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: name (required), file_path (required), output_file, include_source.

treesitter_find_usageA

Find all usages/references of a symbol (identifier) in a source file.

Args: name: Symbol name to search for file_path: Path to the source code file language: Optional language override (auto-detected from file extension if not provided) output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary containing: - query: The search query (symbol name) - matches: List of Symbol objects representing all usages of the symbol OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: name (required), file_path (required), language, output_file.

treesitter_find_variableA

Search for variable declarations and usages by name.

Args: file_path: Path to the source code file name: Name of the variable to find include_source: If True, includes the source code for each matched variable. output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary containing: - query: The search query (variable name) - matches: List of Symbol objects representing variable declarations and usages. If include_source is True, each match includes a "source" field. OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: name (required), file_path (required), output_file, include_source.

treesitter_get_astA

Extract the complete Abstract Syntax Tree (AST) from a source file.

Args: file_path: Path to the source code file max_depth: Maximum depth of the AST to return. -1 for no limit (default). Useful for large files to avoid serialization errors. output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary representing the AST root node with: - type: Node type (e.g., 'module', 'function_definition') - start_point: Starting position (row, column) - end_point: Ending position (row, column) - children: List of child AST nodes - text: Optional text content - id: Optional node identifier OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: file_path (required), max_depth, output_file.

treesitter_get_call_graphA

Generate a call graph showing function calls and their relationships.

Args: file_path: Path to the source code file output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary containing: - nodes: List of CallGraphNode objects, each with: - name: Function name - location: Source location (start/end points) - calls: List of function names called by this function OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: file_path (required), output_file.

treesitter_get_dependenciesA

Extract all dependencies (imports/includes) from a source file.

Args: file_path: Path to the source code file output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: List of dependency strings: - For Python: import module names - For C/C++: included file paths (without quotes/brackets) OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: file_path (required), output_file.

treesitter_get_node_at_pointA

Return the AST node covering a specific point (row, column).

Args: file_path: Path to the source code file row: Row number (0-based) column: Column number (0-based) max_depth: Maximum depth of the AST to return. 0 for just the node. output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: AST node as dictionary OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: row (required), column (required), file_path (required), max_depth, +1 more.

treesitter_get_node_for_rangeA

Return the smallest AST node covering a point range.

Args: file_path: Path to the source code file start_row: Starting row (0-based) start_column: Starting column (0-based) end_row: Ending row (0-based) end_column: Ending column (0-based) max_depth: Maximum depth of the AST to return. 0 for just the node. output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: AST node as dictionary OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: end_row (required), file_path (required), start_row (required), end_column (required), +3 more.

treesitter_get_source_for_rangeA

Extract the source code text for a given line/column range.

Args: file_path: Path to the source code file start_row: Starting line number (0-based) start_column: Starting column number (0-based) end_row: Ending line number (0-based) end_column: Ending column number (0-based) output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Dictionary containing: - file_path: The analyzed file path - range: The requested range - source: The extracted source code text OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: end_row (required), file_path (required), start_row (required), end_column (required), +2 more.

treesitter_get_supported_languagesA

Get a list of programming languages supported by the analyzer.

Args: output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: List of supported language names (e.g., ['python', 'c', 'cpp']) OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: output_file.

treesitter_run_queryA

Execute a custom Tree-sitter query against a source file.

Args: query: Tree-sitter query string in S-expression format file_path: Path to the source code file language: Optional language override (auto-detected from file extension if not provided) output_file: If provided, writes result to this file instead of returning. Useful for large outputs to prevent context overload.

Returns: Query results as a dictionary or list, depending on the query structure OR if output_file is set: {"status": "written", "output_file": "...", "bytes_written": N}

Inputs: query (required), file_path (required), language, output_file.

Tool Schema Changelog

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

Loading tool schema changelog…

    TDQS

    A4.1/5.0

    Scored across 13 tools

    Disambiguation2/5

    get_node_for_range and get_node_at_point are nearly identical since a point is a zero-length range. find_function, find_variable, and find_usage all search by symbol name and return Symbol objects, differing only in the type of symbol. cursor_walk also provides node context at a point, overlapping with get_node_at_point, making boundaries unclear.

    Naming Consistency4/5

    All tools follow a consistent treesitter_ prefix with snake_case and a verb-object structure (get_, find_, run_, analyze_). The only minor deviation is cursor_walk, which inverts the typical verb-object order but remains clear and easy to parse.

    Tool Count5/5

    13 tools is well within the ideal range for a code analysis server. Each tool addresses a distinct aspect of Tree-sitter functionality (AST, queries, symbols, dependencies, call graph) without bloat or excessive redundancy.

    Completeness4/5

    The server covers core workflows: AST inspection, query execution, symbol lookup, dependency extraction, call graph generation, and language support. Minor gaps include a unified symbol search (function and variable tools could be merged) and a more direct node navigation tool, though cursor_walk partially fills that need.

    Maintenance

    ActivityInactive
    ResponsivenessOutdated data

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      B
      maintenance
      Standalone MCP server for code structure analysis using tree-sitter. Directory trees, symbol definitions, and call graphs without reading raw source files. Supports Rust, Python, Go, Java, TypeScript, Fortran, JavaScript, C/C++, and C#. Benchmarked up to 68% fewer tokens vs native tools.
      7
      Apache 2.0
    • A
      license
      Not graded
      quality
      D
      maintenance
      An MCP server that indexes your codebase using tree-sitter AST parsing and gives AI tools instant access to structural intelligence like dependency graphs, call trees, and dead code detection from a local SQLite database.
      MIT