Optimizing Codebase Context for Coding LLMs: AST-Driven Token Reduction & Reality-Lock (T-Zero MCP)

Hi everyone,

When building or using AI coding agents on medium-to-large codebases, a common architectural bottleneck is context management:

  1. Context Window Bloat: Feeding entire raw source files quickly inflates prompt tokens, drives up API latency/costs, and dilutes attention.
  2. Phantom API Hallucinations: Models occasionally hallucinate methods or library imports that don’t exist in the actual repository.
  3. Secret Leaks: Raw credentials and environment tokens can accidentally slip into outbound LLM context windows.

To address this deterministically, I developed T-Zero, an open-source, local-first Model Context Protocol (MCP) server designed for context compression and reality verification.

Architectural Mechanism:

  • Deterministic AST Pruning: Instead of ingesting full function implementations, T-Zero parses multi-language codebases (Python, TypeScript, Rust, Go) into typed AST interface skeletons. Internal function bodies are replaced with ..., while preserving 100% of classes, method signatures, parameter types, decorators, and docstrings. This achieves an 85–95% reduction in context token consumption while keeping full architectural awareness for the LLM.
  • Pre-Flight Reality-Lock: Before code generation, the agent validates imported symbols and method calls against the codebase’s real AST, eliminating fake API hallucinations.
  • Local Shannon Entropy Scanner: Outbound context is locally inspected with a mathematical Shannon entropy scanner ($H \ge 4.75$) to intercept raw API keys, JWTs, and cloud credentials before transmission.

Technical Implementation:

  • Dual-Engine Core: Hybrid architecture utilizing a native Rust core (crates/tzero_core with Tree-sitter and tiktoken-rs operating at >940k tokens/sec) with a zero-fail pure-Python fallback.
  • Privacy & Local-First: Runs 100% locally with zero external API dependencies and zero telemetry.
  • Test Coverage: Verified with 630+ unit/integration tests running across multi-platform CI matrices (Ubuntu, macOS, Windows).

How to Test via MCP:

You can run the MCP server directly in any MCP-compatible environment (Cursor, Claude Desktop, Antigravity) with zero configuration:

# Instant runner via npx:
npx -y tzero-mcp@latest

# Or via Python package:
pip install tzero-mcp
tzero init

Links & Code:

How is the community currently handling multi-file repository context for agentic coding workflows? Do you rely on static symbol graphs, vector RAG, or AST-based pruning? Would love to exchange insights and feedback!

I spent a few minutes browsing your repo; the tree-sitter pruning in ast_pruner.rs and the H >= 4.75 entropy check are neat, and I really liked seeing camelCase/snake_case splitting in bm25_search.rs alongside the breaking-change signature checks in tzero_contracts.py.

On your question about multi-file context: I stopped using vector RAG for active code edits because embeddings go stale fast during refactors and lose exact call-site provenance. What has worked much better for me is splitting it into two small local stdio MCP layers. On the codebase side, I scan files into compact symbol signatures with line anchors, map cross-module dependencies, and only pull a full function body when an edit actually needs it. On the session side, I watch live transcript prompt and output growth across turns so handoffs happen cleanly before automatic compaction wipes out constraints.

Also, two quick packaging catches before traffic picks up:

  • Your post says Apache-2.0, while LICENSE and crates/tzero_core/Cargo.toml are currently set to AGPL-3.0-only; worth aligning so teams trying tzero-mcp know the exact licence upfront.
  • .agents/mcp_config.json still points to a local Windows path (C:\Users\...\TZeroMCP.exe) instead of npx -y tzero-mcp@latest; swapping that to the portable runner will make cloning into Antigravity work out of the box.

Always happy to trade notes on local MCP context setups anytime.

Hİ @dllhell Thanks a lot for the thorough review and kind words on the AST pruner, entropy checks, and BM25 tokenization — really appreciate you taking the time to dive into the codebase!

Your insight on vector RAG getting stale during active refactoring hits. That exact call-site issue is why we leaned into deterministic AST signatures over probabilistic embeddings. Pulling compact symbol signatures with line anchors and only hydrating full function bodies on-demand is fantastic validation of this architecture.

Also, huge thank you for the two packaging catches:

  1. Updated .agents/mcp_config.json to use the portable npx -y tzero-mcp@latest runner instead of the local Windows binary path (just pushed the fix to main!).
  2. Confirmed the license: it is strictly GNU AGPL-3.0-only as declared in LICENSE and Cargo.toml; aligned the post reference as well.

Would love to stay connected and trade notes on local MCP context architectures as we keep refining it!