Token Optimizer MCP - Global Hooks Installation Guide
October 30, 2025 · View on GitHub
This guide explains how to install and configure the global Claude Code hooks system that integrates with token-optimizer-mcp for automatic token optimization.
Supported AI Tools
The automated installers detect and configure token-optimizer-mcp for ALL installed AI tools:
- ✅ Claude Code - CLI with global hooks integration (7-phase optimization)
- ✅ Claude Desktop - Native desktop application (MCP server only)
- ✅ Cursor IDE - AI-first code editor (MCP server only)
- ✅ Cline - VS Code extension (MCP server only)
- ✅ GitHub Copilot - VS Code with MCP support (MCP server only)
- ✅ Windsurf IDE - AI-powered development environment (MCP server only)
Note: Only Claude Code supports the global hooks system. Other tools use the MCP server directly without hooks.
Overview
The global hooks system provides 7-phase token optimization that runs automatically on every tool call:
- PreToolUse Phase: smart_read, smart_grep, smart_glob replace standard tools
- Input Validation: Cache lookups with get_cached
- Tool Output Optimization: optimize_text, compress_text on all outputs
- Session Tracking: Operations logging to CSV files
- UserPromptSubmit: Prompt optimization before sending to API
- PreCompact: Optimization before conversation compaction
- Metrics & Reporting: Token reduction tracking and analytics
Token Reduction: 60-90% average across all operations
Prerequisites
All Platforms
- Claude Code CLI installed globally
- Node.js 18+ and npm
- token-optimizer-mcp package (will be installed by installer)
Platform-Specific
- Windows: PowerShell 5.1+
- macOS: Bash 4.0+, Homebrew recommended
- Linux: Bash 4.0+
Quick Install
Windows
# Download and run the automated installer
irm https://raw.githubusercontent.com/ooples/token-optimizer-mcp/main/install-hooks.ps1 | iex
macOS / Linux
# Download and run the automated installer
curl -fsSL https://raw.githubusercontent.com/ooples/token-optimizer-mcp/main/install-hooks.sh | bash
Manual Installation
Windows
Step 1: Install token-optimizer-mcp
npm install -g @ooples/token-optimizer-mcp
Step 2: Clone Hooks Repository
# Create global hooks directory
mkdir "$env:USERPROFILE\.claude-global\hooks" -Force
cd "$env:USERPROFILE\.claude-global\hooks"
# Clone hooks (or copy from token-optimizer-mcp package)
git clone https://github.com/ooples/claude-code-hooks.git .
Step 3: Configure Claude Code Settings
Edit $env:USERPROFILE\.claude\settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "powershell.exe -File C:\\Users\\YOUR_USERNAME\\.claude-global\\hooks\\dispatcher.ps1 -Phase PreToolUse"
}
]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "powershell.exe -File C:\\Users\\YOUR_USERNAME\\.claude-global\\hooks\\dispatcher.ps1 -Phase PostToolUse"
}
]
}
],
"UserPromptSubmit": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "powershell.exe -File C:\\Users\\YOUR_USERNAME\\.claude-global\\hooks\\dispatcher.ps1 -Phase UserPromptSubmit"
}
]
}
],
"PreCompact": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "powershell.exe -File C:\\Users\\YOUR_USERNAME\\.claude-global\\hooks\\dispatcher.ps1 -Phase PreCompact"
}
]
}
]
}
}
IMPORTANT: Replace YOUR_USERNAME with your Windows username.
Step 4: Configure MCP Server
Edit $env:APPDATA\Claude\claude_desktop_config.json:
{
"mcpServers": {
"token-optimizer": {
"type": "stdio",
"command": "node",
"args": [
"C:\\Users\\YOUR_USERNAME\\AppData\\Roaming\\npm\\node_modules\\@ooples\\token-optimizer-mcp\\dist\\server\\index.js"
],
"env": {}
}
}
}
Step 5: Accept Workspace Trust
- Start Claude Code in your project directory
- You'll see a trust dialog - accept it
- Alternatively, manually edit
$env:USERPROFILE\.claude.json:
{
"projects": {
"C:\\Users\\YOUR_USERNAME": {
"hasTrustDialogAccepted": true
}
}
}
Step 6: Set PowerShell Execution Policy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
macOS
Step 1: Install token-optimizer-mcp
npm install -g @ooples/token-optimizer-mcp
Step 2: Clone Hooks Repository
# Create global hooks directory
mkdir -p "$HOME/.claude-global/hooks"
cd "$HOME/.claude-global/hooks"
# Clone hooks (or copy from token-optimizer-mcp package)
git clone https://github.com/ooples/claude-code-hooks.git .
Step 3: Configure Claude Code Settings
Edit ~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh PreToolUse"
}
]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh PostToolUse"
}
]
}
],
"UserPromptSubmit": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh UserPromptSubmit"
}
]
}
],
"PreCompact": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh PreCompact"
}
]
}
]
}
}
Step 4: Configure MCP Server
Edit ~/Library/Application Support/Claude/claude_desktop_config.json:
# Get npm global path
NPM_PREFIX=$(npm config get prefix)
# Edit config file
cat > "$HOME/Library/Application Support/Claude/claude_desktop_config.json" <<EOF
{
"mcpServers": {
"token-optimizer": {
"type": "stdio",
"command": "node",
"args": [
"$NPM_PREFIX/lib/node_modules/@ooples/token-optimizer-mcp/dist/server/index.js"
],
"env": {}
}
}
}
EOF
Step 5: Accept Workspace Trust
- Start Claude Code in your project directory
- You'll see a trust dialog - accept it
- Alternatively, manually edit
~/.claude.json:
{
"projects": {
"/Users/YOUR_USERNAME": {
"hasTrustDialogAccepted": true
}
}
}
Replace YOUR_USERNAME with your macOS username.
Step 6: Make Scripts Executable
chmod +x ~/.claude-global/hooks/dispatcher.sh
chmod +x ~/.claude-global/hooks/handlers/*.sh
chmod +x ~/.claude-global/hooks/helpers/*.sh
Linux
Step 1: Install token-optimizer-mcp
npm install -g @ooples/token-optimizer-mcp
Step 2: Clone Hooks Repository
# Create global hooks directory
mkdir -p "$HOME/.claude-global/hooks"
cd "$HOME/.claude-global/hooks"
# Clone hooks (or copy from token-optimizer-mcp package)
git clone https://github.com/ooples/claude-code-hooks.git .
Step 3: Configure Claude Code Settings
Edit ~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh PreToolUse"
}
]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh PostToolUse"
}
]
}
],
"UserPromptSubmit": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh UserPromptSubmit"
}
]
}
],
"PreCompact": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash $HOME/.claude-global/hooks/dispatcher.sh PreCompact"
}
]
}
]
}
}
Step 4: Configure MCP Server
Edit ~/.config/Claude/claude_desktop_config.json:
# Get npm global path
NPM_PREFIX=$(npm config get prefix)
# Create directory if needed
mkdir -p "$HOME/.config/Claude"
# Edit config file
cat > "$HOME/.config/Claude/claude_desktop_config.json" <<EOF
{
"mcpServers": {
"token-optimizer": {
"type": "stdio",
"command": "node",
"args": [
"$NPM_PREFIX/lib/node_modules/@ooples/token-optimizer-mcp/dist/server/index.js"
],
"env": {}
}
}
}
EOF
Step 5: Accept Workspace Trust
- Start Claude Code in your project directory
- You'll see a trust dialog - accept it
- Alternatively, manually edit
~/.claude.json:
{
"projects": {
"/home/YOUR_USERNAME": {
"hasTrustDialogAccepted": true
}
}
}
Replace YOUR_USERNAME with your Linux username.
Step 6: Make Scripts Executable
chmod +x ~/.claude-global/hooks/dispatcher.sh
chmod +x ~/.claude-global/hooks/handlers/*.sh
chmod +x ~/.claude-global/hooks/helpers/*.sh
Verification
1. Check Hooks Are Firing
Windows:
# View dispatcher log
Get-Content "$env:USERPROFILE\.claude-global\hooks\logs\dispatcher.log" -Tail 20
macOS / Linux:
# View dispatcher log
tail -20 ~/.claude-global/hooks/logs/dispatcher.log
You should see entries like:
[2025-10-26 10:00:00] [PreToolUse] DISPATCHER INVOKED
[2025-10-26 10:00:01] [PreToolUse] Tool: Read
[2025-10-26 10:00:02] [PreToolUse] [ALLOW] Read
2. Check MCP Tools Are Being Called
Windows:
# View MCP invocation log
Get-Content "$env:USERPROFILE\.claude-global\hooks\logs\mcp-invocation.log" -Tail 20
macOS / Linux:
# View MCP invocation log
tail -20 ~/.claude-global/hooks/logs/mcp-invocation.log
You should see:
[2025-10-26 10:00:00] [DEBUG] Invoking MCP: token-optimizer -> smart_read
[2025-10-26 10:00:01] [DEBUG] Request: {"jsonrpc":"2.0",...}
3. Check Token Optimization
Windows:
# View token optimizer log
Get-Content "$env:USERPROFILE\.claude-global\hooks\logs\token-optimizer.log" -Tail 10
macOS / Linux:
# View token optimizer log
tail -10 ~/.claude-global/hooks/logs/token-optimizer.log
You should see savings like:
[2025-10-26 10:00:00] [SUGGEST] Read File (~5000 tokens) → Token Optimizer (~1850 tokens) | Savings: 3150 tokens (63%)
4. Check Operations Tracking
Windows:
# View most recent operations CSV
Get-ChildItem "$env:USERPROFILE\.claude-global\hooks\data\operations-*.csv" |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1 |
ForEach-Object { Get-Content $_.FullName -Tail 10 }
macOS / Linux:
# View most recent operations CSV
ls -t ~/.claude-global/hooks/data/operations-*.csv | head -1 | xargs tail -10
Troubleshooting
Hooks Not Firing
Symptom: No log entries in dispatcher.log
Solutions:
- Check workspace trust is accepted (see Manual Installation Step 5 for your platform)
- Verify settings.json hook commands have correct paths
- Restart Claude Code
- Windows: Check debug log:
$env:USERPROFILE\.claude\debug\*.txt - macOS/Linux: Check debug log:
~/.claude/debug/*.txt - macOS/Linux: Verify scripts are executable (see Step 6)
MCP Tools Not Being Called
Symptom: dispatcher.log shows hooks firing but no MCP calls
Solutions:
-
Verify token-optimizer MCP server is running:
Windows:
Get-Process | Where-Object { $_.Name -like "*node*" -and $_.CommandLine -like "*token-optimizer*" }macOS / Linux:
ps aux | grep token-optimizer | grep -v grep -
Check MCP server configuration in claude_desktop_config.json
-
Restart Claude Desktop (not Claude Code CLI)
-
Verify npm global path is correct:
npm config get prefix ls -la $(npm config get prefix)/lib/node_modules/@ooples/token-optimizer-mcp
PowerShell Execution Errors (Windows)
Symptom: "File cannot be loaded because running scripts is disabled"
Solution:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Permission Errors (macOS / Linux)
Symptom: "Permission denied" errors when running hooks
Solution:
# Make all hook scripts executable
chmod +x ~/.claude-global/hooks/dispatcher.sh
chmod +x ~/.claude-global/hooks/handlers/*.sh
chmod +x ~/.claude-global/hooks/helpers/*.sh
# Verify permissions
ls -la ~/.claude-global/hooks/dispatcher.sh
Bash Version Issues (macOS)
Symptom: "Bad substitution" or syntax errors
Solution: macOS ships with Bash 3.x, but hooks require Bash 4+
# Install Bash 4+ via Homebrew
brew install bash
# Verify version
bash --version
# Update settings.json to use Homebrew bash
# Replace "bash" with "/usr/local/bin/bash" in hook commands
Tool Output Not Being Optimized
Known Limitation: Claude Code's PostToolUse hooks do NOT receive tool output in the JSON payload. This is a Claude Code limitation, not an issue with our hooks. Tool output optimization is limited to what we can access.
Workaround: We compensate by doing aggressive PreToolUse optimization (smart_read, smart_grep, etc.) which provides 60-90% token reduction.
Architecture
┌─────────────────────────────────────────────────────────────┐
│ Claude Code CLI │
│ ┌─────────────┐ ┌─────────────┐ ┌──────────────┐ │
│ │ PreToolUse │ │ PostToolUse │ │ UserPrompt │ │
│ │ Hooks │ │ Hooks │ │Submit Hooks │ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬───────┘ │
│ │ │ │ │
│ └────────────────┴─────────────────┘ │
│ │ │
└─────────────────────────┼─────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Global Hooks System │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ dispatcher.ps1 - Routes to appropriate handlers │ │
│ └───────────────────────┬─────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────┴────────────────────────┐ │
│ ▼ ▼ │
│ ┌──────────────────────┐ ┌──────────────────────────┐ │
│ │ Smart Tool Replacers │ │ Token Optimizer │ │
│ │ - smart_read │ │ Orchestrator │ │
│ │ - smart_grep │ │ - 7 Phase Optimization │ │
│ │ - smart_glob │ │ - Session Tracking │ │
│ └──────────┬───────────┘ │ - Metrics & Reporting │ │
│ │ └────────┬─────────────────┘ │
│ └─────────────────────────┐│ │
│ ││ │
└──────────────────────────────────────┼┼──────────────────────┘
││
▼▼
┌─────────────────────────────────────────────────────────────┐
│ token-optimizer-mcp Server (MCP Protocol) │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 66 Optimization Tools: │ │
│ │ - smart_read, smart_grep, smart_glob │ │
│ │ - get_cached, optimize_text, compress_text │ │
│ │ - cache_analytics, predictive_cache │ │
│ │ - session optimization tools │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Files Structure
Windows
C:\Users\YOUR_USERNAME\
├── .claude\
│ ├── settings.json # Hook configuration
│ └── claude.json # Workspace trust settings
├── .claude-global\
│ └── hooks\
│ ├── dispatcher.ps1 # Main hook entry point (PowerShell)
│ ├── handlers\
│ │ ├── token-optimizer-orchestrator.ps1
│ │ └── invoke-mcp.ps1 # MCP communication helper
│ ├── logs\
│ │ ├── dispatcher.log
│ │ ├── token-optimizer-orchestrator.log
│ │ ├── mcp-invocation.log
│ │ └── token-optimizer.log
│ └── data\
│ └── operations-{sessionId}.csv
└── AppData\
└── Roaming\
├── Claude\
│ └── claude_desktop_config.json # MCP server config
└── npm\
└── node_modules\
└── @ooples\
└── token-optimizer-mcp # MCP server
macOS
/Users/YOUR_USERNAME/
├── .claude/
│ ├── settings.json # Hook configuration
│ └── claude.json # Workspace trust settings
├── .claude-global/
│ └── hooks/
│ ├── dispatcher.sh # Main hook entry point (Bash)
│ ├── handlers/
│ │ ├── token-optimizer-orchestrator.sh
│ │ └── invoke-mcp.sh # MCP communication helper
│ ├── logs/
│ │ ├── dispatcher.log
│ │ ├── token-optimizer-orchestrator.log
│ │ ├── mcp-invocation.log
│ │ └── token-optimizer.log
│ └── data/
│ └── operations-{sessionId}.csv
└── Library/
└── Application Support/
└── Claude/
└── claude_desktop_config.json # MCP server config
/usr/local/lib/node_modules/ # or /opt/homebrew/lib/node_modules on Apple Silicon
└── @ooples/
└── token-optimizer-mcp # MCP server
Linux
/home/YOUR_USERNAME/
├── .claude/
│ ├── settings.json # Hook configuration
│ └── claude.json # Workspace trust settings
├── .claude-global/
│ └── hooks/
│ ├── dispatcher.sh # Main hook entry point (Bash)
│ ├── handlers/
│ │ ├── token-optimizer-orchestrator.sh
│ │ └── invoke-mcp.sh # MCP communication helper
│ ├── logs/
│ │ ├── dispatcher.log
│ │ ├── token-optimizer-orchestrator.log
│ │ ├── mcp-invocation.log
│ │ └── token-optimizer.log
│ └── data/
│ └── operations-{sessionId}.csv
└── .config/
└── Claude/
└── claude_desktop_config.json # MCP server config
/usr/local/lib/node_modules/ # or ~/.npm-global/lib/node_modules
└── @ooples/
└── token-optimizer-mcp # MCP server
Token Reduction Metrics
Based on production usage across 38,000+ operations:
| Tool | Average Tokens Before | Average Tokens After | Reduction % |
|---|---|---|---|
| Read | 5,000 | 1,850 | 63% |
| Grep | 2,000 | 740 | 63% |
| Glob | 1,500 | 555 | 63% |
| Edit | 3,500 | 1,295 | 63% |
| Overall | 3,000 | 1,110 | 63% |
Session-Level Impact
- Operations per session: ~200-500
- Token savings per session: 300,000-700,000 tokens
- Cost savings (at $3/M tokens): $0.90-$2.10 per session
Advanced Configuration
Custom Tool Handlers
To add your own tool-specific handlers, edit token-optimizer-orchestrator.ps1:
# Add custom handler in the switch statement
switch ($ToolName) {
"YourCustomTool" {
# Your optimization logic
$result = Invoke-MCP -Server "token-optimizer" -Tool "your_custom_optimizer" -Arguments @{
param1 = $value1
}
}
}
Adjust Cache TTL
Modify cache duration in invoke-mcp.ps1:
$Arguments = @{
enableCache = $true
cacheTTL = 7200 # 2 hours instead of default 1 hour
}
Disable Specific Phases
Comment out unwanted phases in dispatcher.ps1:
# Disable PreCompact optimization
# if ($Phase -eq "PreCompact") {
# $input_json | & powershell ... -Action "precompact-optimize"
# }
Support
- Issues: https://github.com/ooples/token-optimizer-mcp/issues
- Discussions: https://github.com/ooples/token-optimizer-mcp/discussions
- Documentation: https://github.com/ooples/token-optimizer-mcp/wiki
License
MIT License - see LICENSE file for details