Skip to main content
Back to Community & Earn Hub

Tree-Sitter Symbol Graphs for Coding Agents: Replacing Blind Grep With Sub-20ms AST Call-Hierarchy Lookups

@ethan_miller3 days ago·1 min read 1968 views
Tree-Sitter Symbol Graphs for Coding Agents: Replacing Blind Grep With Sub-20ms AST Call-Hierarchy Lookups

Why grep + read_file Wastes Context in Large Repos

Watch an unindexed coding agent debug a TypeScript or Python bug in a 200k-LOC monorepo:

  1. It runs grep -rn "chargeCustomer" -> gets 42 noisy matches including comments and test mocks.
  2. It calls read_file on 4 files (~14,000 tokens) just to locate the actual signature and its upstream callers.
  3. Half of the agent's context window is already full before it writes a single line of code!

Architecture: Incremental Tree-Sitter Symbol Graph

By parsing changed files incrementally with Tree-Sitter into a lightweight in-memory / SQLite symbol table, your MCP server gives the agent IDE-grade "Go to Definition" & "Find Callers" in <18ms:

text
[Git Repo on Disk]
       │ (Tree-Sitter AST Walk: ~4ms/file, incremental on mtime/sha256)
       ▼
[Symbol Table: {name, kind, file, start_line, end_line, signature}]
       │
       ├──► MCP Tool `get_symbol_def("chargeCustomer")` -> Exact 18-line function body!
       └──► MCP Tool `get_callers("chargeCustomer")`    -> Exact 4 call-site snippets!

Python Tree-Sitter Extractor (tree-sitter-python)

python
import tree_sitter_python as tspython
from tree_sitter import Language, Parser
from pathlib import Path

PY_LANG = Language(tspython.language())
parser = Parser(PY_LANG)

def extract_python_symbols(file_path: Path) -> list[dict]:
    source = file_path.read_bytes()
    tree = parser.parse(source)
    symbols = []

    def walk(node):
        if node.type in ("function_definition", "class_definition"):
            name_node = node.child_by_field_name("name")
            if name_node:
                symbols.append({
                    "name": source[name_node.start_byte:name_node.end_byte].decode(),
                    "kind": "class" if node.type == "class_definition" else "function",
                    "file": str(file_path),
                    "start_line": node.start_point[0] + 1,
                    "end_line": node.end_point[0] + 1,
                })
        for child in node.children:
            walk(child)

    walk(tree.root_node)
    return symbols

Net Impact on SWE-Bench Style Tasks

  • Blind grep + full-file reads: 34,000 average input tokens, 6.2 turns to locate root cause.
  • Tree-Sitter Symbol MCP Tools: 4,800 average input tokens (86% reduction), 1.8 turns to locate root cause.
2 Comments

Discussion

@aarav_sharma3 days ago

86% reduction in input tokens just by replacing blind grep + full-file reads with get_symbol_def and get_callers matches what we saw in production too.

@pooja_nair3 days ago

Tree-sitter's incremental tree.edit() API also means re-parsing a file after an agent applies a diff takes <1ms.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.