token-shrink
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@token-shrinkShow me the compressed context for my current file and its dependencies."
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
token-shrink
A local-first, framework-aware token reduction engine — a polyglot AST semantic proxy and MCP server. It prunes implementation bodies out of dependency files while preserving every type signature, interface, and module export, so LLM agents see the full shape of the code at a fraction of the tokens.
The 80–90% reduction target: full type information, no implementation noise. Ring 0 (your active file) stays complete; Ring 1 (its direct imports) is delivered as pruned skeletons.
How it works
active file imports (Ring 1)
┌──────────────────┐ ┌──────────────────────┐
│ src/page.ts │ ──► │ src/util.ts │
└──────────────────┘ └──────────────────────┘
▾ ▾
tree-sitter (WASM) ─────────────► prune impl blocks
parse & query keep interfaces · types ·
signatures · exports
▾
pruned skeleton (Ring 0 full source)
▾
Compressed Code Context (Markdown)
│ │
via MCP (stdio) via HTTP (Fastify)
get_compressed_code_context POST /v1/contextPipeline stages:
Parse —
web-tree-sitterloads a.wasmgrammar per language (auto-downloaded on first run).Prune — an S-expression query matches implementation blocks (
statement_block,block,compound_statement…), which are replaced with a short token (/* ... */, orpassfor Python) using descending-order splicing so offsets stay valid.Watch —
chokidarwatches the repo,sha1-hashes file contents, and refreshes the cache only on change.Assemble — the active file's imports are resolved and merged into a Markdown context payload (Ring 0 + Ring 1).
Related MCP server: MCP File Compaction
Install
Requires Node.js 18+.
# run anywhere without installing
# --root project root --port http port --host bind address
npx @ajdev0/token-shrink --root /path/to/project
# or install locally
npm install @ajdev0/token-shrinkBuild from source
# install deps
npm install
# compile (tsup -> dist/), typecheck, and run tests
npm run build
npm run typecheck
npm testThe build produces three binaries:
Binary | Entry | Purpose |
|
| Fastify HTTP server ( |
|
| MCP stdio server for AI agents |
library |
|
|
Publish to npm
npm login
npm run build && npm test
npm pack --dry-run # preview tarball contents
npm publishThe package name on npm is @ajdev0/token-shrink. After publishing, users can run:
npx @ajdev0/token-shrink-mcp --root /path/to/projectWASM grammars (auto-download)
Grammars are fetched from the official tree-sitter GitHub releases on first use and cached in wasm/:
wasm/
├── tree-sitter-typescript.wasm
├── tree-sitter-javascript.wasm
├── tree-sitter-tsx.wasm
├── tree-sitter-python.wasm
├── tree-sitter-go.wasm
├── ...First run requires network access; afterwards everything is offline and fast.
Files are written atomically (
*.tmp→ rename) with an in-flight lock, so concurrent first-run parses never corrupt the cache.
Usage
1. MCP server (AI agents — Cursor, Claude, Cline, etc.)
Run the stdio MCP server and expose the get_compressed_code_context tool:
# point it at your project
token-shrink-mcp --root /path/to/project
# root also works via env or cwd
ROOT=/path/to/project token-shrink-mcp
cd /path/to/project && token-shrink-mcpCursor MCP config (.cursor/mcp.json):
{
"mcpServers": {
"token-shrink": {
"command": "token-shrink-mcp",
"args": ["--root", "/absolute/path/to/your/project"]
}
}
}Claude Code MCP config — add it to the project's .mcp.json, or register with the Claude CLI:
# register the server for this project
claude mcp add token-shrink -- token-shrink-mcp --root /path/to/project
# persistent flag: -- transport stdio
claude mcp add token-shrink --transport stdio -- token-shrink-mcp --root /path/to/projector place in .claude/settings.json / project .mcp.json:
{
"mcpServers": {
"token-shrink": {
"command": "token-shrink-mcp",
"args": ["--root", "/path/to/project"]
}
}
}Cline MCP config — add it to the project's .mcp.json (or mcp.json in the .cline settings directory), or add the server via the Cline UI (MCP Servers → Configure MCP Servers):
{
"mcpServers": {
"token-shrink": {
"command": "token-shrink-mcp",
"args": ["--root", "/path/to/project"]
}
}
}Auto rule: by default the server writes agent integration rules so the tool is used automatically on every prompt:
Cursor:
.cursor/rules/token-shrink.mdcClaude Code:
.claude/rules/token-shrink.mdCline:
.clinerules/token-shrink.md(Cline's.clinerules/directory — every.md/.txtfile there is loaded on every task)
All are sentinel-tagged and never rewrite a user-authored file at the same path. Repeated starts are no-ops. Choose the target(s) with --rule-target=cursor|claude|cline|all (default all, comma-separated values allowed):
# only Claude Code
token-shrink-mcp --root /path/to/project --rule-target=claude
# Cursor + Cline, no Claude rule
token-shrink-mcp --root /path/to/project --rule-target=cursor,cline
# completely disable auto-rules
token-shrink-mcp --root /path/to/project --no-create-ruleOpt out also via --create-rule=false or TOKEN_SHRINK_CREATE_RULE=0.
Tool: get_compressed_code_context
Argument | Type | Required | Description |
|
| yes | The file the agent is working on |
|
| no | Cap on Ring-1 files (default |
|
| no | Append approximate token counts |
Returns a Markdown payload with the active file fully inlined (Ring 0) and the pruned skeletons of its direct imports (Ring 1).
2. HTTP server (Fastify)
token-shrink --root /path/to/project --port 3000
# env equivalents: ROOT=… PORT=… HOST=…Route | Method | Body | Returns |
|
| — | status, root, indexed file count |
|
|
| assembled Markdown + deps |
curl -s http://localhost:3000/health
# {"status":"ok","service":"token-shrink","version":"2.0.0","root":".","indexed":182}
curl -s -X POST http://localhost:3000/v1/context \
-H 'Content-Type: application/json' \
-d '{"activeFilePath":"./src/page.ts","includeStats":true}'3. Library API
import { prune, assemble, createWatcher } from 'token-shrink';
// prune a single file -> skeleton (keeps signatures, strips bodies)
const { code, removed } = await prune('src/util.ts', sourceText);
// assemble context for an active file from a warm cache
const { markdown } = assemble('src/page.ts', watcher.cache.entries, {
includeStats: true,
});
// incremental watcher
const watcher = createWatcher({ root: process.cwd(), ignored: ['node_modules'] });
await watcher.indexAll();Supported languages
S-expression queries match implementation blocks; interfaces, signatures, and exports are never touched. The Block node column shows the AST node that gets collapsed during pruning.
Language | Extensions | Grammar wasm | Block node |
TypeScript |
|
|
|
JavaScript |
|
|
|
React / Next.js |
|
|
|
React (JSX) |
|
|
|
Python |
|
|
|
Dart / Flutter |
|
|
|
Swift / SwiftUI |
|
|
|
Go |
|
|
|
Rust |
|
|
|
Java |
|
|
|
Kotlin |
|
|
|
C |
|
|
|
C++ |
|
|
|
PHP |
|
|
|
¹ TSX/JSX also preserve
'use client'/'use server'directive lines inside otherwise-pruned bodies (framework-aware).
Language IDs: typescript · javascript · tsx · jsx · python · dart · swift · go · rust · java · kotlin · c · cpp · php.
Example
Input src/util.ts
export interface User {
id: number;
name: string;
}
export function buildGreeting(u: User) {
const parts = [u.name, u.email];
return parts.join(' | ');
}
export const formatEmail = (u: User) => {
return u.email.toLowerCase().trim();
};Pruned skeleton (Ring 1) — signatures and the interface intact, bodies collapsed:
export interface User {
id: number;
name: string;
}
export function buildGreeting(u: User) /* ... */
export const formatEmail = (u: User) => /* ... */;Design notes
Bottom-up splicing — ranges are sorted by start index descending and replaced in place, so earlier offsets never shift and the output stays a valid, parseable file.
Regex-based import extraction — resilient across languages; resolves relative imports (
./x,../y), aliases (@/,~), and skips bare package specifiers.Incremental hashing — files are re-pruned only when their
sha1hash changes; the watcher is debounced (100 ms) and zero-CPU while idle.Ram-safe watchers — sockets / non-regular files are never opened with
fs.watch, so stray unix sockets in the tree can't crash the server.
Project layout
token-shrink/
├── package.json / tsconfig.json / tsup.config.ts / vitest.config.ts
├── src/
│ ├── index.ts # library entry (exports)
│ ├── cli.ts # Fastify HTTP server
│ ├── mcp.ts # MCP stdio server
│ ├── parser/
│ │ ├── registry.ts # extension → language spec + S-queries
│ │ ├── wasm.ts # auto-download + cache of .wasm files
│ │ └── pruner.ts # prune(filePath, source) → skeleton
│ ├── watcher/
│ │ └── sync.ts # chokidar watch + hash cache + import graph
│ └── server/
│ └── assembler.ts # Ring 0 + Ring 1 Markdown payload
├── tests/ # pruning integrity + token-reduction tests
└── wasm/ # auto-downloaded grammars (gitignored)License
MIT
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
Shared memory for coding agents. Stop re-explaining your codebase every session.
Codebase intelligence for agents: 152 structured artifacts across 21 programs, one call.
Project memory, semantic code search, and grounded agent context.
Provide your AI coding tools with token-efficient access to up-to-date technical documentation for…
Related MCP Servers
- AlicenseAqualityDmaintenanceProvides intelligent code context and analysis through semantic compression, AST parsing, and multi-language support. Offers 60-80% token reduction while enabling AI assistants to understand codebases through local analysis, OpenAI-enhanced insights, and GitHub repository integration.6223MIT
- FlicenseAqualityDmaintenanceReduces Claude's context window costs by automatically summarizing inactive files to their public interfaces using AST parsing, keeping only the full contents of the currently active file.6
- AlicenseAqualityDmaintenanceProvides code-aware context compression by stripping comments, docstrings, and whitespace while maintaining full logic fidelity for AI agents. It features tools for architectural mapping, symbol searching, and token-budgeted multi-file reading.9104MIT
- AlicenseBqualityCmaintenanceMaximizes AI agent context window by enabling compact code reading and editing, reducing tokens by 40% for deeper codebase understanding.19203MIT
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/ajdev0/token-shrink'
If you have feedback or need assistance with the MCP directory API, please join our Discord server