snapshot_page_state
Capture a complete snapshot of a webpage's state including HTML, storage, cookies, scroll position, and form values to enable exact restoration later.
Instructions
Capture a complete snapshot of the page state: HTML, localStorage, sessionStorage, cookies, scroll position, and form values. Use with restore_page_state to return to this exact state.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tabId | No | Target tab ID (defaults to currently active tab) | |
| apiKey | No | API key for authentication if enabled |
Implementation Reference
- src/tools/qa.ts:167-187 (registration)The tool 'snapshot_page_state' is registered via server.tool() in the registerQaTools function. It defines the tool name, description, schema (optional tabId and apiKey), and the handler logic.
server.tool( 'snapshot_page_state', 'Capture a complete snapshot of the page state: HTML, localStorage, sessionStorage, cookies, scroll position, and form values. Use with restore_page_state to return to this exact state.', { tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'), apiKey: z.string().optional().describe('API key for authentication if enabled'), }, async ({ tabId, apiKey }) => { const result = await bridge.sendCommand({ command: 'snapshot_page_state', params: {}, tabId, apiKey, timeout: LONG_TIMEOUT, }); if (!result.success) { return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } ); - src/tools/qa.ts:174-186 (handler)The handler function for 'snapshot_page_state' asyncronously sends a command via bridge.sendCommand with command 'snapshot_page_state', empty params, optional tabId/apiKey, and a long timeout. It returns the result data or an error.
async ({ tabId, apiKey }) => { const result = await bridge.sendCommand({ command: 'snapshot_page_state', params: {}, tabId, apiKey, timeout: LONG_TIMEOUT, }); if (!result.success) { return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } - src/tools/qa.ts:170-173 (schema)Input schema for 'snapshot_page_state': tabId (optional number) and apiKey (optional string), both described via Zod schemas.
{ tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'), apiKey: z.string().optional().describe('API key for authentication if enabled'), }, - src/types.ts:1-16 (helper)BridgeCommand interface defines the structure of commands sent to the WebSocket bridge, including the 'command' field which is set to 'snapshot_page_state'.
export interface BridgeCommand { command: string; params: Record<string, unknown>; tabId?: number; apiKey?: string; timeout?: number; } export interface BridgeResponse { success: boolean; data?: unknown; error?: { code: string; message: string; }; } - src/websocket-bridge.ts:63-103 (helper)WebSocketBridge.sendCommand is the messaging method that sends the command to the browser extension over a WebSocket and returns a BridgeResponse promise.
async sendCommand(cmd: BridgeCommand): Promise<BridgeResponse> { if (!this.isConnected()) { return { success: false, error: { code: 'NOT_CONNECTED', message: 'Chrome extension is not connected. Ensure the extension is installed, enabled, and the browser is running.', }, }; } const id = crypto.randomUUID(); const timeout = cmd.timeout ?? DEFAULT_TIMEOUT; return new Promise<BridgeResponse>((resolve, reject) => { const timer = setTimeout(() => { this.pending.delete(id); resolve({ success: false, error: { code: 'TIMEOUT', message: `Command '${cmd.command}' timed out after ${timeout}ms`, }, }); }, timeout); this.pending.set(id, { resolve, reject, timer }); const message = { id, type: 'request', command: cmd.command, params: cmd.params, tabId: cmd.tabId, apiKey: cmd.apiKey, timestamp: Date.now(), }; this.client!.send(JSON.stringify(message)); }); }