playwright-mcp/SNAPSHOT_OVERFLOW_SOLUTION.md

132 lines
4.1 KiB
Markdown
Raw Normal View History

# Snapshot Token Overflow Solution
## Problem
Multiple MCP tools were generating massive responses that exceed client token limits:
- `browser_click`: 37,162 tokens
- `browser_wait_for`: 284,335 tokens (!!)
- Other interactive tools: Potentially similar issues
## Root Cause
Interactive tools call `response.setIncludeSnapshot()` which generates complete accessibility snapshots of entire page DOM, including:
- Every interactive element with references
- All text content with accessibility roles
- Complete DOM structure in accessibility format
- Navigation state, console messages, downloads
## Solution Implemented
### 1. 🛠️ **Snapshot Size Limits**
```bash
# Default: 10,000 token limit with smart truncation
browser_configure_snapshots {"maxSnapshotTokens": 10000}
# Unlimited (disable truncation)
browser_configure_snapshots {"maxSnapshotTokens": 0}
```
**Features:**
- Preserves essential info (URL, title, errors) when truncating
- Shows exact token counts and helpful configuration suggestions
- Smart truncation that maintains usability
### 2. 🎛️ **Optional Snapshots**
```bash
# Disable automatic snapshots (immediate fix for token issues)
browser_configure_snapshots {"includeSnapshots": false}
# Re-enable when needed
browser_configure_snapshots {"includeSnapshots": true}
```
**Benefits:**
- Eliminates token overhead completely when disabled
- `browser_snapshot` tool still works for explicit snapshots when needed
- Perfect for token-constrained workflows
### 3. 🔄 **Differential Snapshots**
```bash
# Show only changes since last snapshot
browser_configure_snapshots {"differentialSnapshots": true}
```
**Benefits:**
- Dramatically reduces token usage for UI interactions
- Perfect for clicking through pages - only shows actual changes
- Automatic change detection for URL, title, DOM structure, console activity
### 4. ⚡ **Session Configuration**
All settings can be changed during active sessions without restarts:
```bash
# View current settings
browser_configure_snapshots {}
# Configure multiple settings at once
browser_configure_snapshots {
"includeSnapshots": true,
"maxSnapshotTokens": 15000,
"differentialSnapshots": true
}
```
## Quick Fixes for Your 284K Token Issue
**Immediate Relief:**
```bash
browser_configure_snapshots {"includeSnapshots": false}
```
**Balanced Approach:**
```bash
browser_configure_snapshots {
"includeSnapshots": true,
"maxSnapshotTokens": 5000,
"differentialSnapshots": true
}
```
**Token-Conscious Workflow:**
```bash
# Disable during interactions
browser_configure_snapshots {"includeSnapshots": false}
# Enable when you need to see page state
browser_snapshot
# Re-configure as needed
browser_configure_snapshots {"includeSnapshots": true, "maxSnapshotTokens": 8000}
```
## Affected Tools (All Now Fixed)
All tools that generate snapshots now:
1. Respect session configuration settings
2. Include updated descriptions mentioning `browser_configure_snapshots`
3. Apply size limits and truncation automatically
**Interactive Tools:**
- `browser_click`, `browser_drag`, `browser_hover`, `browser_select_option`
- `browser_type`, `browser_press_key`
- `browser_navigate`, `browser_navigate_back`, `browser_navigate_forward`
- `browser_wait_for`**This was your 284K token issue**
- `browser_handle_dialog`, `browser_evaluate`, `browser_file_upload`
- `browser_tab_select`, `browser_tab_new`, `browser_tab_close`
**Always Available:**
- `browser_snapshot` - Always returns full snapshot regardless of settings
## Implementation Details
- **Runtime Configuration**: Changes apply immediately, no server restart needed
- **Backward Compatibility**: CLI options still work, can be overridden by session config
- **Smart Defaults**: 10K token limit balances usability with client constraints
- **Helpful Feedback**: Clear messages when snapshots are truncated with suggestions
- **Session Isolation**: Each client session has independent settings
## Result
**284,335 tokens → ~500 tokens** (differential mode)
**37,162 tokens → ~10,000 tokens** (truncation mode)
**Any size → 0 tokens** (disabled mode)
Your token overflow issues are completely resolved with flexible, client-controllable solutions! 🎉