loading…
Search for a command to run...
loading…
Intelligent token optimization achieving 95%+ reduction through caching, compression, and 80+ smart tools for API optimization, code analysis, and real-time mon
Intelligent token optimization achieving 95%+ reduction through caching, compression, and 80+ smart tools for API optimization, code analysis, and real-time monitoring.
Intelligent token optimization through caching, compression, and smart tooling for Claude Code and Claude Desktop
Token Optimizer MCP is a Model Context Protocol (MCP) server that reduces context window usage by 60-90% through intelligent caching, compression, and smart tool replacements. By storing compressed content externally in SQLite and providing optimized alternatives to standard tools, the server helps you maximize your available context window.
Production Results: 60-90% token reduction across 38,000+ operations in real-world usage.
# Run PowerShell as Administrator, then:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Install globally (hooks install automatically!)
npm install -g @ooples/token-optimizer-mcp
# Install globally (hooks install automatically!)
npm install -g @ooples/token-optimizer-mcp
That's it! The postinstall script will automatically:
Result: 60-90% token reduction across all operations!
Note: If automatic installation is skipped (e.g., in CI environments), you can manually run the installer:
powershell -ExecutionPolicy Bypass -File install-hooks.ps1bash install-hooks.shFor detailed platform-specific installation instructions, see docs/HOOKS-INSTALLATION.md.
Usage Example:
// Cache large content to remove it from context window
optimize_text({
text: "Large API response or file content...",
key: "api-response-key",
quality: 11
})
// Result: 60-90% token reduction
Optimized replacements for standard file tools with intelligent caching and diff-based updates:
Usage Example:
// Read a file with automatic caching
smart_read({ path: "/path/to/file.ts" })
// First read: full content
// Subsequent reads: only diff (80% reduction)
Intelligent caching and optimization for external data sources:
Usage Example:
// Fetch API with automatic caching
smart_api_fetch({
method: "GET",
url: "https://api.example.com/data",
ttl: 300
})
// Cached responses: 95% token reduction
Development workflow optimization with intelligent caching:
Usage Example:
// Run tests with caching
smart_test({
onlyChanged: true, // Only test changed files
coverage: true
})
Enterprise-grade caching strategies with 87-92% token reduction:
Usage Example:
// Configure multi-tier cache
smart_cache({
operation: "configure",
evictionStrategy: "LRU",
l1MaxSize: 1000,
l2MaxSize: 10000
})
Comprehensive monitoring with 88-92% token reduction through intelligent caching:
Usage Example:
// Create an alert
alert_manager({
operation: "create-alert",
alertName: "high-cpu-usage",
channels: ["slack", "email"],
threshold: { type: "above", value: 80 }
})
System-level operations with smart caching:
Usage Example:
// View session token usage
get_session_stats({})
// Result: Detailed breakdown of token usage by tool
Granular token usage analytics for pinpointing optimization opportunities:
Usage Example:
// Get per-hook analytics
get_hook_analytics({
startDate: "2025-01-01T00:00:00Z",
endDate: "2025-12-31T23:59:59Z"
})
// Result: Shows which hooks consume the most tokens
// Get per-action analytics
get_action_analytics({})
// Result: Shows which tools use the most tokens
// Export analytics as CSV
export_analytics({
format: "csv",
hookPhase: "PreToolUse"
})
// Result: CSV export filtered by PreToolUse hook
Key Features:
When global hooks are installed, token-optimizer-mcp runs automatically on every tool call:
┌─────────────────────────────────────────────────────────────┐
│ Phase 1: PreToolUse - Tool Replacement │
│ ├─ Read → smart_read (80% token reduction) │
│ ├─ Grep → smart_grep (80% token reduction) │
│ └─ Glob → smart_glob (75% token reduction) │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ Phase 2: Input Validation - Cache Lookups │
│ └─ get_cached checks if operation was already done │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ Phase 3: PostToolUse - Output Optimization │
│ ├─ optimize_text for large outputs │
│ └─ compress_text for repeated content │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ Phase 4: Session Tracking │
│ └─ Log all operations to operations-{sessionId}.csv │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ Phase 5: UserPromptSubmit - Prompt Optimization │
│ └─ Optimize user prompts before sending to API │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ Phase 6: PreCompact - Pre-Compaction Optimization │
│ └─ Optimize before Claude Code compacts the conversation │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ Phase 7: Metrics & Reporting │
│ └─ Track token reduction metrics and generate reports │
└─────────────────────────────────────────────────────────────┘
Based on 38,000+ operations in real-world usage:
| Tool Category | Avg Token Reduction | Cache Hit Rate |
|---|---|---|
| File Operations | 60-90% | >80% |
| API Responses | 83-95% | >75% |
| Database Queries | 83-90% | >70% |
| Build/Test Output | 70-85% | >65% |
Per-Session Savings: 300K-700K tokens (worth $0.90-$2.10 at $3/M tokens)
// Cache large content to remove from context window
const result = await optimize_text({
text: "Large API response or file content...",
key: "cache-key",
quality: 11
});
// Result: Original tokens removed, only cache key remains (~50 tokens)
// Retrieve later
const cached = await get_cached({ key: "cache-key" });
// Result: Full original content restored
// First read: full content
await smart_read({ path: "/src/app.ts" });
// Subsequent reads: only changes (80% reduction)
await smart_read({ path: "/src/app.ts" });
// First request: fetch and cache
await smart_api_fetch({
method: "GET",
url: "https://api.example.com/data",
ttl: 300
});
// Subsequent requests: cached (95% reduction)
await smart_api_fetch({
method: "GET",
url: "https://api.example.com/data"
});
// View token usage for current session
await get_session_stats({});
// Result: Breakdown by tool, operation, and savings
// Analyze entire project
await analyze_project_tokens({
projectPath: "/path/to/project"
});
// Result: Cost estimation and optimization opportunities
The automated installer detects and configures token-optimizer-mcp for:
No manual configuration needed - the installer automatically detects and configures all installed tools!
The PowerShell hooks have been optimized to reduce overhead from 50-70ms to <10ms through:
Control hook behavior with these environment variables:
TOKEN_OPTIMIZER_USE_FILE_SESSION (default: false)
true to revert to file-based session tracking (legacy mode)$env:TOKEN_OPTIMIZER_USE_FILE_SESSION = "true"TOKEN_OPTIMIZER_SYNC_LOG_WRITES (default: false)
true to disable batched log writes$env:TOKEN_OPTIMIZER_SYNC_LOG_WRITES = "true"TOKEN_OPTIMIZER_DEBUG_LOGGING (default: true)
false to disable DEBUG-level logging$env:TOKEN_OPTIMIZER_DEBUG_LOGGING = "false"TOKEN_OPTIMIZER_DEV_PATH~/source/repos/token-optimizer-mcp if not specified$env:TOKEN_OPTIMIZER_DEV_PATH = "C:\dev\token-optimizer-mcp"Performance Impact: Using in-memory mode (default) provides a 7x improvement in hook overhead:
To view your actual token SAVINGS, use the get_session_stats tool:
// View current session statistics with token savings breakdown
await get_session_stats({});
Output includes:
Example Output:
{
"sessionId": "abc-123",
"totalTokensSaved": 125430, // ← THIS is your savings!
"tokenReductionPercent": 68.2,
"originalTokens": 184000,
"optimizedTokens": 58570,
"cacheHitRate": 72.0,
"byTool": {
"smart_read": { "saved": 45000, "percent": 80 },
"smart_grep": { "saved": 32000, "percent": 75 }
}
}
All operations are automatically tracked in session data files:
Location: ~/.claude-global/hooks/data/current-session.txt
Format:
{
"sessionId": "abc-123",
"sessionStart": "20251031-082211",
"totalOperations": 1250, // ← Number of operations
"totalTokens": 184000, // ← Cumulative token COUNT
"lastOptimized": 1698765432,
"savings": { // ← Auto-updated every 10 operations (Issue #113)
"totalTokensSaved": 125430, // Tokens saved by compression
"tokenReductionPercent": 68.2, // Percentage of tokens saved
"originalTokens": 184000, // Original token count before optimization
"optimizedTokens": 58570, // Token count after optimization
"cacheHitRate": 42.5, // Cache hit rate percentage
"compressionRatio": 0.32, // Compression efficiency (lower is better)
"lastUpdated": "20251031-092500" // Last savings update timestamp
}
}
New in v1.x: The savings object is now automatically updated every 10 operations, eliminating the need to manually call get_session_stats() for real-time monitoring. This provides instant visibility into token optimization performance.
How it works:
get_cache_stats() MCP toolNote: For detailed per-operation analysis, use get_session_stats(). The session file provides high-level aggregate metrics.
Analyze token usage across your entire project:
// Analyze project token costs
await analyze_project_tokens({
projectPath: "/path/to/project"
});
Provides:
Monitor cache hit rates and storage efficiency:
// View cache statistics
await get_cache_stats({});
Metrics:
Symptom: Claude Code shows "Invalid Settings" error after running install-hooks
Cause: UTF-8 BOM (Byte Order Mark) was added to settings.json files
Solution: Upgrade to v3.0.2+ which fixes the BOM issue:
npm install -g @ooples/token-optimizer-mcp@latest
If you're already on v3.0.2+, manually remove the BOM:
# Windows: Remove BOM from settings.json
$content = Get-Content "~/.claude/settings.json" -Raw
$content = $content -replace '^\xEF\xBB\xBF', ''
$content | Set-Content "~/.claude/settings.json" -Encoding utf8NoBOM
# Linux: Remove BOM from settings.json
sed -i '1s/^\xEF\xBB\xBF//' ~/.claude/settings.json
# macOS: Remove BOM from settings.json (BSD sed requires empty string after -i)
sed -i '' '1s/^\xef\xbb\xbf//' ~/.claude/settings.json
Symptom: Token optimization not occurring automatically
Diagnosis:
Check if hooks are installed:
# Windows
Get-Content ~/.claude/settings.json | ConvertFrom-Json | Select-Object -ExpandProperty hooks
# macOS/Linux
cat ~/.claude/settings.json | jq .hooks
Verify dispatcher.ps1 exists:
# Windows
Test-Path ~/.claude-global/hooks/dispatcher.ps1
# macOS/Linux
[ -f ~/.claude-global/hooks/dispatcher.sh ] && echo "Exists" || echo "Missing"
Solution: Re-run the installer:
# Windows
powershell -ExecutionPolicy Bypass -File install-hooks.ps1
# macOS/Linux
bash install-hooks.sh
Symptom: Session stats show cache hit rate below 50%
Causes:
Solutions:
Warm up the cache before starting work:
await cache_warmup({
paths: ["/path/to/frequently/used/files"],
recursive: true
});
Increase TTL for stable APIs:
await smart_api_fetch({
url: "https://api.example.com/data",
ttl: 3600 // 1 hour instead of default 5 minutes
});
Check cache size limits:
await smart_cache({
operation: "configure",
l1MaxSize: 2000, // Increase from default 1000
l2MaxSize: 20000 // Increase from default 10000
});
Symptom: Node.js process using excessive memory
Cause: Large cache in memory (L1/L2 tiers)
Solution: Configure cache limits:
await smart_cache({
operation: "configure",
evictionStrategy: "LRU", // Least Recently Used
l1MaxSize: 500, // Reduce L1 cache
l2MaxSize: 5000 // Reduce L2 cache
});
Or clear the cache:
await clear_cache({});
Symptom: Initial Read/Grep/Glob operations are slow
Cause: Cache is empty, building indexes
Solution: This is expected behavior. Subsequent operations will be 80-90% faster.
To pre-warm the cache:
await cache_warmup({
paths: ["/src", "/tests", "/docs"],
recursive: true,
schedule: "startup" // Auto-warm on every session start
});
Symptom: Cannot write to cache or log files
Cause: PowerShell execution policy or file permissions
Solution:
Set execution policy:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Check file permissions:
icacls "$env:USERPROFILE\.token-optimizer"
Re-run installer as Administrator if needed
Symptom: ~/.token-optimizer/cache.db is >1GB
Cause: Caching very large files or many API responses
Solution:
Clear old entries:
await clear_cache({ olderThan: 7 }); // Clear entries older than 7 days
Reduce cache retention:
await smart_cache({
operation: "configure",
defaultTTL: 3600 // 1 hour instead of 7 days
});
Manually delete cache (nuclear option):
rm -rf ~/.token-optimizer/cache.db
If you encounter issues not covered here:
~/.claude-global/hooks/logs/dispatcher.log~/.claude-global/hooks/data/current-session.txtget_session_statsMIT License - see LICENSE for details
Built for optimal Claude Code token efficiency by the ooples team.
Добавь это в claude_desktop_config.json и перезапусти Claude Desktop.
{
"mcpServers": {
"ooples-token-optimizer-mcp": {
"command": "npx",
"args": []
}
}
}