treesitter-mcp
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@treesitter-mcp show me the call graph for app.py"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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-mcpRunning
As an MCP server (default)
For use with Claude Desktop or other MCP clients:
treesitter-mcpSee 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-languagesUse --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.1Limiting tools
Only expose certain tools with --tools:
treesitter-mcp --http --port 8000 --tools treesitter_analyze_file,treesitter_get_astOr via URL query param: http://127.0.0.1:8000?tools=treesitter_analyze_file,treesitter_get_ast
Tools available:
treesitter_analyze_file- Basic analysistreesitter_get_ast- Full ASTtreesitter_get_call_graph- Function callstreesitter_find_function- Find function definitionstreesitter_find_variable- Find variablestreesitter_get_source_for_range- Extract source code for a rangetreesitter_get_supported_languages- What's supportedtreesitter_get_node_at_point- AST node at a line/columntreesitter_get_node_for_range- AST node for a rangetreesitter_cursor_walk- Walk tree with contexttreesitter_run_query- Custom Tree-sitter queriestreesitter_find_usage- Find symbol usagestreesitter_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 directoryWarn (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++ |
|
Python |
|
JavaScript |
|
TypeScript |
|
Go |
|
Java |
|
PHP |
|
Rust |
|
Ruby |
|
Docs
Available Tools
13 toolstreesitter_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
Scored across 13 tools
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.
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.
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.
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
Related MCP Connectors
Repository knowledge graph MCP server for codebase understanding and debugging.
An MCP server that gives your AI access to the source code and docs of all public github repos
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA runtime-free MCP server that converts source code into AST🌲, regardless of language.89MIT
- AlicenseNot gradedqualityBmaintenanceStandalone 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.7Apache 2.0
- AlicenseNot gradedqualityDmaintenanceA modular MCP server for code analysis using ast-grep, enabling structural pattern matching and transformations across multiple languages.MIT
- AlicenseNot gradedqualityDmaintenanceAn 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