Skip to main content

MCP server for code analysis using Tree-sitter

Project description

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

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

Project details


Release history Release notifications | RSS feed

This version

2.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iflow_mcp_pwno_io_treesitter_mcp-2.1.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iflow_mcp_pwno_io_treesitter_mcp-2.1-py3-none-any.whl (32.8 kB view details)

Uploaded Python 3

File details

Details for the file iflow_mcp_pwno_io_treesitter_mcp-2.1.tar.gz.

File metadata

  • Download URL: iflow_mcp_pwno_io_treesitter_mcp-2.1.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_pwno_io_treesitter_mcp-2.1.tar.gz
Algorithm Hash digest
SHA256 2f4698aacdb9a3aea7c98800e5c0bcec2fa6b9672b4865fb1bb23b5f95ab5368
MD5 e4ff534767d86df70c5f6873bede5dd5
BLAKE2b-256 a4f09329b56b5570cb7f34f19c18a60b930b120352670544f4b2148457e4462d

See more details on using hashes here.

File details

Details for the file iflow_mcp_pwno_io_treesitter_mcp-2.1-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_pwno_io_treesitter_mcp-2.1-py3-none-any.whl
  • Upload date:
  • Size: 32.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_pwno_io_treesitter_mcp-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 68be5bd0db47e2711bb39858cb9b236dc5648bd8bfa916098937e45c09928bb3
MD5 f1811aa325ef5f25e33b647f603ce431
BLAKE2b-256 bd333cf78c9b0f57343d2c173af3be5b572e1ae31b9e2325ff64925aa3c9c78e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page