Skip to main content
Glama
token-context-mcp

token-context-mcp

README.md
<!-- mcp-name: io.github.MCK564/token-context-mcp -->
# Token Context MCP (v0.3.11)

[![CI](https://github.com/MCK564/token-context-mcp/actions/workflows/ci.yml/badge.svg)](https://github.com/MCK564/token-context-mcp/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/token-context-mcp.svg)](https://pypi.org/project/token-context-mcp/)
[![Python](https://img.shields.io/badge/python-3.12%2B-blue.svg)](https://www.python.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

> ๐ŸŒ English: [`README.md`](README.md) | ๐Ÿ‡ป๐Ÿ‡ณ Tiแบฟng Viแป‡t: [`README.vi.md`](README.vi.md) | ๐Ÿ‡จ๐Ÿ‡ณ ็ฎ€ไฝ“ไธญๆ–‡: [`README.cn.md`](README.cn.md) | ๐Ÿ“ Prompt Protocol: [`sample_prompt.md`](sample_prompt.md) | ๐Ÿ“‹ Changelog: [`CHANGELOG.md`](CHANGELOG.md)

> [!NOTE]
> **Architecture Notice**: The governance microkernel design is inspired by the Cordis paper (*A Programming Paradigm for Spatiotemporal Composability*, arXiv:2608.25512). This is an independent open-source implementation not affiliated with or endorsed by DeepSeek.

`token-context-mcp` is an on-demand, budget-governed **Model Context Protocol (MCP)** server for autonomous AI coding agents (Claude Code, Cursor, Codex, Google Antigravity). Instead of blindly dumping an entire repository or compressing it into a static snapshot that strips function bodies down to signatures, it provides an **interactive AST-level pull pipeline** that delivers exact code symbols and downstream impact graphs with verified `file:line` + SHA-256 evidence.

---

## ๐Ÿ–ผ๏ธ Architecture & Benchmark Infographics (2x Retina Dark Green)

<p align="center">
  <img src="assets/en/01_hero_strengths_en.png" alt="Core Architectural Pillars & Key Metrics" width="100%" />
</p>

<details>
<summary><b>๐Ÿ” View detailed technical architecture & comparison diagrams (Click to expand)</b></summary>

### 1. 3-Layer Governance Microkernel & 5-Step Progressive Pull Pipeline
<p align="center">
  <img src="assets/en/02_pipeline_architecture_en.png" alt="Governance Microkernel & Progressive Pull Pipeline" width="100%" />
</p>

### 2. Context Paradigms: Flat Dump vs. Static Pack vs. Interactive MCP Pull
<p align="center">
  <img src="assets/en/03_pack_vs_pull_paradigms_en.png" alt="Context Supply Paradigms Comparison" width="100%" />
</p>

### 3. Empirical Benchmarks: 59-Task Head-to-Head & M10 Equal-Budget Suite
<p align="center">
  <img src="assets/en/04_h2h_and_m10_benchmarks_en.png" alt="Empirical Benchmarks: 59-Task H2H and M10 Suite" width="100%" />
</p>

### 4. 7-Dimension Technical Comparison Matrix (vs. ContextCraft v0.3.2)
<p align="center">
  <img src="assets/en/05_comparison_matrix_en.png" alt="7-Dimension Technical Comparison Matrix" width="100%" />
</p>

</details>

---

## โšก Core Architectural Pillars

1. **3-Stage Governance Microkernel (`kernel/`)**:
   - Every request passes through a lightweight middleware pipeline:
     - **Stage 1 โ€” Identity Resolution (`AuthMiddleware`)**: Binds `TOKEN_CONTEXT_AGENT_ID` when configured and prevents reserved `admin` impersonation.
     - **Stage 2 โ€” Access & Rate Governance (`GovernanceMiddleware`)**: Enforces per-agent policies, emergency halt, and sliding-window rate limits.
     - **Stage 3 โ€” AST-Boundary Cap & Execution (`ExecutionMiddleware` + `retrieve/`)**: Enforces `max_result_tokens` at syntactic boundaries without cutting functions in half.
2. **Zero Schema Bloat**:
   - The default `tools/list` payload exposes **only 10 core read-only retrieval tools** (keeping 11 extension toolsโ€”including `trajectory_record`โ€”and 2 admin tools off the wire until requested). Server-side JSON schemas are slimmed to reduce initial prompt tax.
3. **Incremental Tree-sitter AST & Persistent SQLite Graph (`index/`)**:
   - Fast `mtime_ns` + SHA-256 change detection across **Python, JavaScript/TypeScript, Go, Rust, Java, C/C++**, and C# with symlink-escape and path traversal guards.
4. **Verified Evidence Ledger ("Lรก Phแป•i" โ€” `[ledger].enabled = true` by default)**:
   - Stores verified `"query -> answer"` citation cards (`path:line` + AST body SHA-256) in `ledger.sqlite` outside your repository. Every read re-verifies citations against disk, emits `prior_evidence` hints (or `recheck_from` on `VERIFY_FAILED`), and never prunes unverified code. Zero-hash fast-path adds 0ms overhead on empty ledgers.

---

## ๐Ÿ“Š Empirical Benchmarks (`tiktoken o200k_base` Audited)

All numbers are measured using OpenAI's `o200k_base` tokenizer across two real-world open-source repositories: `rich` (Python, 213 files, 507k tokens) and `fastify` (JS/TS, 291 files, 538k tokens):

| Benchmark Suite | Metric | token-context-mcp | Baseline / Alternative | Empirical Takeaway |
| :--- | :--- | :---: | :---: | :--- |
| **59-Task Head-to-Head** (Equal budget: ~7.6k tokens) | Target function in context with **Full Body** ($\ge 90\%$ lines) | **33 / 59 (55.9%)** | 2 / 59 (3.4%) *(ContextCraft 8k `--focus`)* | **16.5ร— more full function bodies** at equal token budget |
| **59-Task H2H (Sensitivity)** (vs. 4ร— static budget: 32k tokens) | Full Body vs. Signature Only | **33 / 59 Full Body** (at 7.6k) | 6 / 59 Full Body, **40 / 59 (67.8%) Signature Only** *(ContextCraft 32k)* | Static packing prunes bodies to signatures to fit whole-repo maps; interactive MCP pull focuses 100% of budget on the target symbol |
| **M10 Equal-Budget Suite** (`rich`, n=30, ~1.9k tokens) | File Recall & Symbol Recall (95% Bootstrap CI) | **0.90** File Recall<br/>**0.54** Symbol Recall | 0.57 File Recall<br/>0.10 Symbol Recall *(Truncated Grep)* | **+58% file recall**, **5.4ร— symbol recall** over truncated grep at identical token cost |
| **C1 Single-Call Cost** (`fastify` 538k source baseline) | Orientation map `get_repo_map@512` | **682 tokens** | 538,799 tokens (raw source files) | Compact repo map occupies **0.13%** of the repository (~790ร— smaller) |

---

## ๐Ÿš€ Quick Start

### Option 1: Run instantly with `uvx` (Recommended)
```bash
# Register, index, and print agent config for the current directory
uvx token-context-mcp quickstart .
```

### Option 2: Install via `pip`
```bash
pip install token-context-mcp

# Register a codebase and launch server
token-context register --repo-id my-project --root /path/to/project
token-context index --repo-id my-project
token-context serve
```

### Desktop GUI Controller (PySide6)
7-tab dark desktop controller (`Dashboard`, `Repositories`, `Tasks & Graph`, `Cache & DB` with stale snapshot cleanup & `ledger.sqlite` vacuuming, `Ledger & Memory`, `Agents & Security`, and `Settings`):
```bash
uvx --from "token-context-mcp[gui]" token-context-gui
```

### Benchmark & Evaluation Suite (`[eval]` extra with `tiktoken o200k_base`)
```bash
pip install -e ".[eval]"
python evals/chain_eval.py --output evals/reports/chain_l0_l5.json
python evals/mcp_chain_eval.py --limit-chains 6 --output evals/reports/mcp_chain_eval.json
```

---

## ๐Ÿค– Coding Agent Setup

`token-context-mcp` communicates over local `stdio`. Add the configuration below to your agent:

### 1. Claude Code
```bash
claude mcp add --transport stdio --scope user token-context -- uvx token-context-mcp serve
```

### 2. Cursor
In Cursor Settings > Features > MCP Servers:
- **Name**: `token-context`
- **Type**: `command`
- **Command**: `uvx token-context-mcp serve`

### 3. Claude Desktop
Add to `%APPDATA%\Claude\claude_desktop_config.json` (Windows) or `~/.config/Claude/claude_desktop_config.json` (macOS/Linux):
```json
{
  "mcpServers": {
    "token-context": {
      "command": "uvx",
      "args": ["token-context-mcp", "serve"]
    }
  }
}
```

### 4. Google Antigravity IDE / Gemini CLI
Add to `mcp_config.json`:
```json
{
  "mcpServers": {
    "token-context": {
      "command": "uvx",
      "args": [
        "token-context-mcp",
        "serve",
        "--output-mode", "text",
        "--schema-profile", "gemini_safe"
      ]
    }
  }
}
```

---

## ๐Ÿ›  Tool Reference Catalog (10 Default Core Tools)

By default, the server exposes **10 read-only retrieval tools** to preserve agent context:

| Tool | Primary Purpose | Mode |
| :--- | :--- | :---: |
| `list_repositories` | Lists registered `repo_id`s (filesystem root paths are never leaked). | Read-only |
| `get_index_status` | Returns snapshot metadata, indexed file count, commit SHA, and freshness. | Read-only |
| `get_repo_map` | PageRank-based symbol hierarchy within a token budget (`@512`). | Read-only |
| `find_symbols` | Pattern-based symbol matching (`*`, `?` wildcards) with file coordinates. | Read-only |
| `search_source` | FTS5 code search returning matched lines and enclosing symbol spans. | Read-only |
| `get_file_skeleton` | File outline (imports, class/function signatures with elided bodies). | Read-only |
| `get_symbol_context` | Full function/class body, docstrings, and callers/callees (`@1024`). | Read-only |
| `get_impact_slice` | Traverses downstream call graph dependencies before making changes (`depth=2`). | Read-only |
| `get_module_dependents` | Tree-sitter import dependency graph between modules. | Read-only |
| `inspect_symbol` | Composite lookup: symbol resolution + full context + 1-hop impact graph. | Read-only |

*(11 extension tools including SQLite multi-agent memory `memory_*`, Evidence Ledger `trajectory_record`, lazy tool discovery `*_tools`, hardware-adaptive compressor `sample_summarize`, and 2 admin tools `agent_control`, `audit_logs` can be enabled via configuration).*

---

## ๐Ÿ”’ Security Guarantees & Privacy

- **No Remote Code Execution (RCE)**: No tool can execute shell commands, eval code, or modify repository source files.
- **100% Local & Private**: All SQLite WAL indexes remain on your machine; zero cloud telemetry or external API calls.
- **Automatic Secret Redaction**: Regex-based redaction of API keys, tokens, and private keys (`[REDACTED: potential secret]`) plus hard denial of sensitive files (`.env*`, `.ssh`, `*.pem`, `id_rsa`).

---

## ๐Ÿ“š Technical Documentation

- [`sample_prompt.md`](sample_prompt.md): Standard two-phase prompt templates (EN / VI / CN).
- [`CHANGELOG.md`](CHANGELOG.md): Release history and upgrade notes.
- [`launch/00_fact_sheet.md`](launch/00_fact_sheet.md): Verified empirical benchmarks and measurement methodologies.
- [`docs/CLIENT_MATRIX.md`](docs/CLIENT_MATRIX.md): Step-by-step MCP client verification matrix.

---
*Maintained by the Token Context MCP community. Licensed under MIT.*

TDQS

A4.2/5.0

Scored across 10 tools

Disambiguation4/5

Each tool has a documented niche and the descriptions actively redirect the agent (e.g. get_module_dependents says use get_impact_slice for symbol edges, find_symbols says use search_source for code text), which resolves most overlap. The residual ambiguity is between name-based lookups (find_symbols vs inspect_symbol) and between get_symbol_context vs get_impact_slice, where the boundary is 'bulk vs single' and 'body+edges vs edges only' โ€” learnable but easy to misselect on a first pass.

Naming Consistency5/5

All ten tools follow a strict snake_case verb_noun pattern (list_repositories, get_repo_map, find_symbols, inspect_symbol, search_source). The verb choice is also semantically meaningful โ€” get for retrieval, find for pattern matching, search for full-text, inspect for a one-shot packet โ€” rather than arbitrary variation.

Tool Count5/5

Ten tools is squarely in the well-scoped range for a code-context/indexing server. Each tool covers a distinct retrieval granularity (repo, file, module graph, symbol, source text) and none feels like filler.

Completeness4/5

The read-only surface is broad: repo discovery, freshness checking, ranked maps, name matching, file skeletons, symbol bodies, call edges, import graphs, and full-text search โ€” a full navigation lifecycle for the stated purpose. Gaps are minor (no cross-repo search, no way to retrieve a whole file body, budget profiles are only listable, not configurable), but agents can work around them by composing tools.

Maintenance

ActivityNo data
ResponsivenessOutdated data