safari_get_console_logs
Retrieve Safari browser console logs for debugging by specifying a session ID and optional log level filter, aiding in monitoring and diagnosing browser activity.
Instructions
Get browser console logs for debugging
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| logLevel | No | Filter logs by level | |
| sessionId | Yes | Session identifier |
Implementation Reference
- src/safari-mcp-server.ts:304-315 (handler)MCP server handler function for 'safari_get_console_logs' tool. Extracts parameters, calls SafariDriverManager.getConsoleLogs, and formats logs as JSON text response.private async getConsoleLogs(args: Record<string, any>): Promise<Array<{ type: string; text: string }>> { const { sessionId, logLevel = 'ALL' } = args; const logs = await this.driverManager.getConsoleLogs(sessionId, logLevel as LogLevel); return [ { type: 'text', text: `Console Logs (${logs.length} entries):\n\n${JSON.stringify(logs, null, 2)}` } ]; }
- src/safari-driver.ts:107-168 (helper)Core implementation in SafariDriverManager that injects JavaScript to override browser console methods, captures log entries, filters by logLevel if specified, and returns structured ConsoleLogEntry array.async getConsoleLogs(sessionId: string, logLevel: LogLevel = 'ALL'): Promise<ConsoleLogEntry[]> { const session = this.getSession(sessionId); if (!session) { throw new Error(`Session ${sessionId} not found`); } try { // First, inject console logging capture if not already present await session.driver.executeScript(` if (!window.__safariMCPConsoleLogs) { window.__safariMCPConsoleLogs = []; // Store original console methods const originalConsole = { log: console.log, warn: console.warn, error: console.error, info: console.info, debug: console.debug }; // Override console methods to capture logs ['log', 'warn', 'error', 'info', 'debug'].forEach(method => { console[method] = function(...args) { const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg) ).join(' '); window.__safariMCPConsoleLogs.push({ level: method.toUpperCase(), message: message, timestamp: Date.now(), source: 'browser' }); // Still call original method originalConsole[method].apply(console, args); }; }); } `); // Retrieve captured logs const logs = await session.driver.executeScript(` return window.__safariMCPConsoleLogs || []; `); const filteredLogs = logLevel === 'ALL' ? logs : logs.filter((log: any) => log.level === logLevel); return filteredLogs.map((log: any) => ({ level: log.level, message: log.message, timestamp: log.timestamp, source: log.source || 'browser' })); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to get console logs: ${errorMessage}`); } }
- src/safari-mcp-server.ts:88-103 (schema)Input schema definition for the tool, specifying required sessionId and optional logLevel enum matching LogLevel type.{ name: 'safari_get_console_logs', description: 'Get browser console logs for debugging', inputSchema: { type: 'object', properties: { sessionId: { type: 'string', description: 'Session identifier' }, logLevel: { type: 'string', enum: ['ALL', 'DEBUG', 'INFO', 'WARNING', 'SEVERE'], description: 'Filter logs by level' } }, required: ['sessionId'] } },
- src/safari-mcp-server.ts:231-232 (registration)Dispatch case in handleToolCall switch statement that registers and routes the tool call to the getConsoleLogs handler.case 'safari_get_console_logs': return await this.getConsoleLogs(args);
- src/types.ts:14-19 (schema)Type definition for ConsoleLogEntry returned by the tool implementation.export interface ConsoleLogEntry { level: string; message: string; timestamp: number; source?: string; }