MCP Context Lens
README.md
# MCP Context Lens
English | [简体中文](README.zh-CN.md)
[](LICENSE)
[](package.json)
[](package.json)
Zero-runtime-dependency TypeScript/ESM **MCP Context Tax Auditor**, **Lossless Schema Compressor**, and **Tool Manifest Drift & Prompt-Injection Guardrail** — with **interactive offline HTML dashboards**, **SARIF 2.1.0 CI gates**, a **4-tool stdio MCP server**, and an **Agent Skill**.
> **Zero-CDN Interactive Playground**: Open [`docs/index.html`](docs/index.html) directly in any browser to inspect a bloated 6-server MCP config, test toggling individual servers/tools in a live Context Budget Simulator, run lossless schema compression, and diff a live Rug-Pull prompt-injection update — 100% locally in your browser.
## Why MCP Context Lens?
Connecting 4–8 MCP servers to an agent routinely consumes 20%–50% of the context window before the user sends a single message (**Context Tax**), while unreviewed server updates can silently inject prompt overrides, harvest `~/.ssh`/`.env` credentials, embed invisible Unicode smuggling characters, or widen permissions (**Rug-Pull / Manifest Drift**).
## Core Capabilities
- **Multi-Client & Manifest Ingestion (`src/ingest/`)**:
- Auto-detects or explicitly parses **Claude Code** (`.mcp.json`), **Cursor** (`.cursor/mcp.json`), **Antigravity / Gemini** (`mcp_config.json`), **VS Code** (`.vscode/mcp.json` with JSONC comments & trailing commas), **Codex** (`config.toml` `[mcp_servers.*]`), and **MCP `tools/list` snapshots** (`{ tools }`, JSON-RPC `{ result: { tools } }`, `{ servers: { name: { tools } } }`).
- **Prototype-Pollution Immune & Secret-Redacted**: Strips UTF-8 BOM, blocks `__proto__`/`constructor`/`prototype`, and never echoes raw `env`, `headers`, `args`, or URL credential literals into reports.
- Clearly distinguishes `exact` per-tool measurements (`tools/list` present) from `estimated` config-only server baselines.
- **12 Deterministic Rules (`src/rules/`)**:
- **Context Tax (`TAX001`–`TAX006`)**: Bloated descriptions (`TAX001`), verbose/redundant/over-nested schemas (`TAX002`), duplicate & near-duplicate tools across servers (`TAX003`), oversized enums/unions (`TAX004`), missing parameter descriptions/constraints (`TAX005`), and single-server context budget dominance (`TAX006`).
- **Security & Injection (`SEC001`–`SEC006`)**: Hidden `<IMPORTANT>`/`[INST]` override tags (`SEC001`), covert `~/.ssh`/`.env`/`process.env`/webhook exfiltration directives (`SEC002`), invisible Unicode stealth — bidi overrides, Unicode Tags `U+E0000..E007F`, zero-width chars, control bytes (`SEC003`), cross-tool shadowing/hijacking (`SEC004`), dangerous schema parameters (`SEC005`), and annotation contradictions + plaintext config risks (`SEC006`).
- **Deterministic Lossless Schema Compressor (`src/engine/compress.ts`)**:
- Strips `$schema`, `$id`, `$comment`, `examples`, redundant `additionalProperties`, duplicate `title`s, markdown noise, and description boilerplate while programmatically verifying **100% preservation** of tool names, property names, `required` arrays, `type`, `enum`, and numeric/string/array bounds.
- **Semantic Manifest Drift & Rug-Pull Diff (`src/engine/diff.ts`)**:
- Compares `before` and `after` manifests for added/removed tools, dangerous parameter additions (`command`, `shell`, `webhook`, `url`, `debug_context`, `conversation_history`), dropped constraints, escalated annotations (`readOnlyHint` dropped, `destructiveHint` added), newly introduced security findings, and net token delta.
- **Zero-CDN Interactive HTML, SARIF 2.1.0, CLI & MCP Server**:
- Self-contained HTML dashboards (`renderAuditHtml`, `renderCompressHtml`, `renderDiffHtml`) with inline SVG charts and an interactive client-side Tool/Server Toggle Budget Simulator.
- SARIF 2.1.0 (`renderAuditSarif`, `renderDiffSarif`) with sanitized relative URIs and deterministic FNV-1a fingerprints.
- Stdio JSON-RPC 2.0 MCP server (`mcp-context-lens mcp`) exposing `mcl_audit`, `mcl_compress`, `mcl_diff`, and `mcl_render_html`.
## Strict DAG Architecture
Enforced automatically by [`scripts/check-boundaries.mjs`](scripts/check-boundaries.mjs):
1. `src/domain/` (`types.ts`, `tokenizer.ts`) — leaf types, context window presets (`128k`, `200k`, `256k`, `1m`), CJK/ASCII hybrid BPE estimator
2. `src/ingest/` → `domain/`
3. `src/rules/` → `domain/`
4. `src/engine/` (`audit.ts`, `compress.ts`, `diff.ts`) → `domain/`, `ingest/`, `rules/`
5. `src/report/` (`text.ts`, `json.ts`, `sarif.ts`, `html.ts`) → `domain/`
6. `src/mcp/` (`server.ts`) → `domain/`, `engine/`, `report/`
7. `src/cli.ts`, `src/index.ts` → all layers above
## Quick Start
```bash
pnpm install --offline --ignore-scripts
npm run check
```
### CLI Commands
```bash
# Audit an MCP config or manifest
node dist/src/cli.js audit examples/01-bloated-6server-config.json --window 200k --html report.html
# Gate CI with a token budget and severity threshold
node dist/src/cli.js audit examples/03-clean-minimal-manifest.json --budget 5000 --fail-on high --sarif out.sarif
# Losslessly compress bloated tool definitions
node dist/src/cli.js compress examples/01-bloated-6server-config.json --out compressed.json --html compress.html
# Detect rug-pull prompt injections and permission escalations across updates
node dist/src/cli.js diff examples/02-rugpull-before.json examples/02-rugpull-after.json --fail-on high --html diff.html
# Generate standalone HTML dashboard or launch stdio MCP server
node dist/src/cli.js playground --out dashboard.html
node dist/src/cli.js mcp
```
## Verification & Quality Gates
```bash
npm run check # TypeScript strict check, DAG boundaries, privacy/BOM scan, unit & integration tests
node scripts/run-mutation.mjs # TypeScript AST mutation testing (>= 90% kill rate)
node scripts/run-fuzz.mjs # 5,000-case adversarial fuzzer (JSON/JSONC/TOML/XSS/__proto__/ReDoS/Unicode)
node scripts/build-playground.mjs # Build zero-CDN docs/index.html
node scripts/verify-playground.mjs # Static + headless Chrome DOM/layout/overflow verification
npm run build:agensi # Build Agensi whitelist-compliant package + isolated 3-host sandbox test
```
## License & Commercial Licensing
> **License Notice**: This project is licensed under the **PolyForm Noncommercial License 1.0.0** (full terms in [`LICENSE`](LICENSE)).
>
> - **Free** for personal study, academic research, testing, charitable/educational organizations, and noncommercial use.
> - **Commercial use** (including commercial product integration, hosted SaaS, paid marketplace redistribution, or commercial consulting delivery) **requires prior written permission from the author** (`hongrenw111-debug`).
> - Commercial licensing inquiries: `hongrenw111@gmail.com`
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues