Local-First Code Intelligence and Context Management for AI Coding Agents Compress context, prevent exploratory rediscovery loops, and bridge architectural disconnects across Python, TypeScript, Rust, and Flutter (Dart).
About this project | Contributing | Security | M8ven Live Verification
Independent Verification: This MCP server is independently inspected and ⚡ Live Monitored by M8ven, which automatically re-verifies the repository on every code change.
AI coding agents waste a large portion of their token budget on file discovery rather than actual work.
A typical agent session without TokenMS looks like this:
| Without TokenMS | With TokenMS |
|---|---|
| Agent reads 8-15 whole files to find relevant code | Agent receives only the exact symbols needed |
Repeated grep and exploratory reads per task |
Structured AST retrieval in a single call |
| Manual relationship discovery across modules | Cross-module pathway traversal built in |
| 6,000-330,000 tokens consumed per task | 1,500-3,500 tokens per task (budget-controlled) |
| Agent loses critical context in long conversations | Persistent curated knowledge ledger across sessions |
The root cause: agents are optimized for chat, not for navigating large codebases. They read files sequentially, re-read them when context shifts, and have no way to know that AuthService in auth/service.py is the class that actually matters for the task at hand.
TokenMS solves this at the infrastructure level, not by prompting the agent to "be smarter."
TMS runs locally alongside your editor as a Model Context Protocol (MCP) service. It parses your repository into an Abstract Syntax Tree (AST) symbol graph, maps cross-module relationships, and uses Knapsack optimization to assemble only the exact classes, functions, and interfaces required for a task into a compact token budget.
- Multi-Language AST Parsing: Extracts classes, decorators, async functions, interfaces, type aliases, enums, signatures, and docstrings for Python, TypeScript, JavaScript, Rust, and Dart via Tree-sitter, with explicit syntax-error diagnostics and
unknownreturn-type handling when unannotated. - Hybrid Multi-Modal Retrieval: Blends SQLite FTS5 (BM25 lexical search) with phrase proximity n-grams, Personalized PageRank (PPR) graph walks, and local CPU vector embeddings.
- Enclosing Class Context & Dependency Preservation: Slices focal functions while preserving surrounding class headers, decorators, and essential cross-boundary dependency chains (
READS_TABLE,CONSTRAINED_BY,CALLS_API,GENERATED_FROM) under tight token budgets, explicitly reportingomitted_dependenciesandcoverage_completewhen extreme budgets evict edges. - Monorepo Ownership & Schema Evolution Diagnostics: Qualifies package-scoped SQL/client/API ownership across monorepos (
AMBIGUOUSvsVERIFIED/INFERRED), tracksALTER TABLEschema migrations (RENAME COLUMN,ADD/DROP/ALTER COLUMN), inspects generated DB types (database.types.ts,*.g.dart), and surfaces stale frontend/client field warnings. - Production-Path Cache Freshness: Tracks persisted
index_generationacross SQLite, FTS5, embeddings, graph adjacency, and resolution caches, purging deleted/renamed/migrated artifacts on reconciliation, edit, or buffer-overlay resets. - Transactional AST Patching & Outlines: Supports TOCTOU-hardened single-symbol edits, two-phase preflight/rollback
batch_edit, standalone zero-indextms outline/tms readCLI commands, optional PreToolUse outline-first host hooks, and optional configurable helper-delegation verification. - Local-First & Zero Cloud Cost: Runs entirely on your CPU using SQLite with Write-Ahead Logging (WAL) and local Model2Vec embeddings. No external API keys or cloud dependencies required.
- Adaptive Token Budget Controller: Dynamically scales context from 1,200 to 4,000 tokens based on query complexity and multi-channel candidate consensus.
- v2 Tandem KV-Cache & Delta Ledger: Partitions output into an immutable sorted Zone A static prefix (with Turn 1 inline playbook) and append-only Zone B suffix, deduplicates unchanged symbols across turns via SQLite
session_symbol_ledger, and halts retry loops viasession_loop_guard. - Global Multi-Project Isolation & Async Job Runner: Routes any
workspace_rootto isolated~/.tokenms/workspaces/<repo_hash>/storage, serializes uniform arrays in compact TOON format, and runs long commands asynchronously (start_job/get_job_summary) with a <=400-token universal log distiller.
Here is exactly what happens after installation, step by step.
git clone https://github.com/HARSHNEXX/TokenMS.git
cd TokenMS
python -m venv .venv
# Windows:
.venv\Scripts\activate
# Linux / macOS:
source .venv/bin/activate
pip install -e .Note on the embedding model: On the very first run, TMS downloads the Model2Vec static embedding model (~30 MB) from HuggingFace. This is a one-time download; afterwards the model is cached locally and TMS operates entirely offline. No API keys are needed at any point.
Run this once inside the repository you want TMS to understand:
cd /path/to/your/project
python -m tms.app index
# Verify the index is healthy
python -m tms.app statusTMS parses every supported source file, extracts AST symbols, computes cross-module call graphs, and stores everything in .tms/index.sqlite. Indexing a 50 KLOC Python project typically takes 10-30 seconds.
Add to .cursor/mcp.json or your Global Cursor Settings:
{
"mcpServers": {
"tms": {
"command": "python",
"args": ["-m", "tms.app", "serve"],
"cwd": "${workspaceFolder}"
}
}
}Add to your claude_desktop_config.json:
{
"mcpServers": {
"tms": {
"command": "python",
"args": ["-m", "tms.app", "serve"],
"cwd": "/path/to/your/project"
}
}
}When your agent starts a task, it calls resolve_task_context with a natural language description:
Task: "Add retry logic to the payment processor"
TMS responds with the precise symbols your agent needs: the PaymentProcessor class signature, its process() method body, the RetryPolicy interface it depends on, and any decorators or error handlers in scope. The response is typically 1,500-3,500 tokens.
Your agent reads only the symbols TMS returned, makes its edits, and calls record_outcome to reinforce verified symbols into the curated knowledge ledger. Future tasks on the same codebase get even more accurate context.
resolve_task_context("Add retry logic to the payment processor")
-> 2,840 tokens of exactly relevant code returned
-> Agent edits PaymentProcessor.process() with full context
-> record_outcome() persists verified symbols to ledger
AI Coding Agent
(Claude Desktop / Cursor / VS Code)
|
MCP Protocol (stdio)
|
+-------------------------------------------------------------------+
| TMS Gateway |
| |
| [resolve_task_context] [query] [batch] [edit] [record_outcome]|
+-------------------------------------------------------------------+
| | |
v v v
+----------------------+ +---------------------+ +----------------+
| Hybrid Retrieval | | AST Safe Patcher | | Output Parser |
| - SQLite FTS5 (BM25) | | - AST Fingerprints | | - Error traces |
| - Graph PageRank | | - Preconditions | | - Log cleaner |
| - Local Embeddings | | - Live Buffer Sync | +----------------+
| - Knapsack Packer | +---------------------+
+----------------------+ |
| |
+--------------+--------------+
|
+-------------------------------------------------------------------+
| Storage & Persistence Layer |
| |
| - SQLite WAL Database (.tms/index.sqlite) |
| - Serialized single-writer queue (avoids SQLITE_BUSY locks) |
| - Git-versioned curated ledger (.tms/curated_knowledge.jsonl) |
+-------------------------------------------------------------------+
In v2 (tms serve / create_mcp_server(core_only=True) default), TMS exposes a minimal, strictly alphabetical 7-tool core surface with minified JSON wire serialization, slimmed *_json parameter schemas, non-duplicated TOON responses, active SessionLoopGuard protection, workspace_root isolation, and an instructions behavioral contract:
| Core Tool | Purpose | Primary Inputs |
|---|---|---|
apply_patches |
Transactional two-phase multi-file/multi-symbol AST edit with loop guard | edits_json, unsaved_buffers_json, write_to_disk, session_id, workspace_root |
discover_tools |
On-demand TOON-only catalog of secondary graph/outline/memory tools | intent, workspace_root |
dispatch |
Route secondary tool calls (query, outline, read_range, trace_symbol, validate, record_outcome, reconcile_knowledge, get_status, expand_toon) |
tool, args_json, workspace_root |
get_job_summary |
Fetch async job status and <=400-token distilled errors + source snippets with loop guard | job_id, max_tokens, session_id, workspace_root |
hydrate_symbols |
Batch-read exact symbol bodies (items) with mtime-checked Tier 1 cache |
ids_json, max_lines, workspace_root |
resolve_task_context |
Turn 1 bootstrap + Zone A/B prefix-stable context, delta ledger, and loop guard | task_query, token_budget, session_id, semantic_hints_json, target_layers_json, workspace_root |
start_job |
Spawn long-running test/build commands asynchronously | cmd, workspace_root |
(For legacy integrations, create_mcp_server(service, core_only=False) exposes all 18 tools in alphabetical order.)
For fast local inspection without starting SQLite or Model2Vec, TMS provides standalone CLI commands:
# Extract a bounded structural outline (JSON or text; supports multiple files or --stdin-json)
python -m tms.app outline src/tms/app.py --format text
# Read a specific symbol or bounded line range
python -m tms.app read src/tms/app.py --lines 1:40
# Evaluate a PreToolUse read payload against configurable outline-first hook policy
python -m tms.app hook pre-read < hook_payload.jsonTo achieve maximum token savings, configure your agent system prompt with these 4 rules. Without them, agents can fall into the additive trap of querying TMS and reading entire files anyway.
- Step 0 (Resolve Context First):
Call
resolve_task_contextexactly once at the beginning of any coding task with the natural language query. - Trust Retrieved Code: Treat the AST symbols, enclosing classes, and interface contracts returned by TMS as primary context. Do not run exploratory grep or inspect unedited companion files.
- Strict File Read Limits: Read at most 2 to 3 files that are actively being modified. Read each target file once in full rather than in micro-chunks.
- One-Pass Consolidated Edits: Apply all imports, methods, and widget modifications in a single contiguous edit block to prevent re-reading files to verify shifted line numbers.
When agents follow these rules, cumulative session tokens drop by 40% to 50%.
Measured across 17 real-world development tasks on the TMS v1.0.1 codebase itself (139 files, 775 symbols, 10,543 relationships), spanning storage backends, cross-boundary extractors, AST parsers, caching, editing, and client-side architectures. Results below are live-verified by running python -m tms.app benchmark --deep and python -m tms.app benchmark --workflows --budget 2500 against the live SQLite index.
| Parameter | Detail |
|---|---|
| Tasks | 17 built-in DeepBenchmarkTask / EvaluationScenario tasks across TMS subsystems |
| Tokenizer | char/4 approximation (~4 chars per token), consistent across all strategies |
| Baseline: Full Repo Dump | All source files concatenated (372,677 tokens for this repo) |
| Baseline: Lexical / Grep | BM25 top-5 hits per query; matched files read in full |
| Baseline: Whole Subsystem | Every file in the subsystem directory for the task read in full |
| Baseline: Targeted Search + Reads | Lexical/grep token matching followed by bounded file/range reads |
| TMS | Single resolve_task_context call per task (ordering_mode=mmr_heuristic, 2,500 token budget) |
| Quality check | File-level & entity-level Recall@5, MRR, dependency-chain coverage, and executable task checks |
| Strategy | Total Tokens (17 tasks) | Avg / Task | Savings vs Baseline |
|---|---|---|---|
| Full Repository Dump | 6,335,509 | 372,677 | 99.21% fewer tokens with TMS |
| Targeted Search + Reads | 206,326 | 12,137 | 75.85% fewer tokens with TMS |
| Lexical Whole-Files (Grep) | 152,163 | 8,951 | 67.25% fewer tokens with TMS |
| Whole-Subsystem Reading | 138,630 | 8,155 | 64.05% fewer tokens with TMS |
| TMS Knapsack Resolution | 49,837 | 2,932 | 2.78x compression vs subsystem |
Context quality: Across the 17 tasks, TMS achieved 0.9412 File Recall@5 (0.8529 File MRR), 0.6127 Entity Recall@5 (0.6863 Entity MRR), and 1.00 dependency-chain coverage (compared to 0.7745 File Recall@5 for targeted search + reads).
| Workflow Arm | Main Tokens | Total System Tokens | Avg Latency | File Recall@5 | Entity Recall@5 | MRR | Verified Task Correctness |
|---|---|---|---|---|---|---|---|
targeted_search_reads |
31,624 | 31,624 | 1,168.6 ms | 0.2843 | 0.0000 | 0.0000 | 11.76% (2/17) |
tms_indexed |
49,702 | 49,702 | 1,728.8 ms | 0.9412 | 0.6127 | 0.6863 | 82.35% (14/17) |
outline_only_reads |
50,967 | 50,967 | 1,615.2 ms | 0.6078 | 0.1765 | 0.3586 | 35.29% (6/17) |
hook_outline_first |
50,390 | 50,390 | 1,338.3 ms | 0.5196 | 0.0980 | 0.1992 | 29.41% (5/17) |
helper_deterministic |
25,384 | 49,139 | 50.5 ms | 0.7157 | 0.3431 | 0.4510 | 64.71% (11/17) |
| Task | Domain | Subsystem tokens | TMS tokens | Saving | Entity R@5 | File R@5 | File MRR |
|---|---|---|---|---|---|---|---|
| DB-01 | Storage & WAL | 5,748 | 2,384 | 58.5% | 0.67 | 1.00 | 1.00 |
| EDIT-02 | AST Editing | 7,392 | 2,429 | 67.1% | 1.00 | 1.00 | 1.00 |
| CB-03 | Cross Boundary | 10,094 | 3,062 | 69.7% | 0.33 | 1.00 | 0.50 |
| PARSE-04 | AST Parsing | 20,788 | 3,018 | 85.5% | 1.00 | 1.00 | 1.00 |
| BUDGET-09 | Token Budgeting | 41,865 | 2,860 | 93.2% | 0.67 | 0.50 | 1.00 |
| MCP-11 | Gateway & MCP | 9,790 | 3,011 | 69.2% | 0.00 | 1.00 | 1.00 |
| SEMANTIC-15 | Semantic & RRF | 7,627 | 3,092 | 59.5% | 0.33 | 1.00 | 0.50 |
| RUST-16 | Rust & Cargo | 4,313 | 3,070 | 28.8% | 0.67 | 1.00 | 1.00 |
| DART-17 | Flutter & Dart | 6,502 | 3,111 | 52.2% | 0.33 | 1.00 | 1.00 |
| DOCINGEST-13 | Unstructured Asset Ingestion | 2,799 | 2,478 | 11.5% | 1.00 | 1.00 | 1.00 |
Why some tasks show 0% saving: 5 tasks have subsystems smaller than the 2,500 token budget floor (e.g., Validation = 546 tokens). There is simply nothing to compress when the source subsystem is already smaller than the target budget.
| Metric | Value |
|---|---|
| Average cold latency (cache empty) | 1,688.0 ms |
| Cached latency (generation-validated hit) | ~9.4 ms |
| Cache speedup factor | 187.2x |
| Average compression ratio vs subsystem | 2.78x |
You can reproduce these numbers on your own repo:
python -m tms.app benchmark --deep
python -m tms.app benchmark --workflows --budget 2500
python scripts/benchmark_external_repo.py --repo-root /path/to/your/project# Run all unit and integration tests (186 tests)
pytest
# Run v2 tandem cache, delta ledger, job distiller, and KV simulation tests
pytest tests/test_workspace_isolation.py tests/test_delta_ledger_and_loops.py tests/test_async_jobs_and_distiller.py tests/test_kv_prefix_simulation.pyContributions are welcome. This project is open to forks, bug reports, feature requests, and pull requests.
Please read CONTRIBUTING.md for contribution guidelines and the SECURITY.md for responsible disclosure.
Apache License 2.0. See LICENSE for details.