Skip to main content

Tree-sitter MCP Server

An MCP server 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

Install

Requires Python 3.10+.

# Install directly
uv pip install treesitter-mcp

# 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

Metadata

Release files for treesitter-mcp 2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for treesitter-mcp 2.1
File Size Uploaded Provenance
treesitter_mcp-2.1.tar.gz 28.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for treesitter-mcp 2.1
File Interpreter ABI Platform
treesitter_mcp-2.1-py3-none-any.whl Python 3 none any Details

Total release size: 61.3 kB

Release files / treesitter_mcp-2.1.tar.gz

Download URL treesitter_mcp-2.1.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4ed45af0d85a19c1387b8d68563e9d72ac0edf08fab43c11a4ba1276f497fadc
BLAKE2b-256 checksum
How to use checksums
e154bef309b93cfd01782a6b07a5306f51cc7c3ee4bb836abb29df68cf436d98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / treesitter_mcp-2.1-py3-none-any.whl

Download URL treesitter_mcp-2.1-py3-none-any.whl
Size 32.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c84c6b60dea815686be1866537cfc034a0400f1674d8874d0ea49540ab5b9db7
BLAKE2b-256 checksum
How to use checksums
18fd98566530b4474eff4cd8047db53ffbc346e074085f55b2fb260d730f344c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for treesitter-mcp 2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page