get_console_logs
Retrieve browser console logs with optional filtering by log level (log, info, warn, error, debug) and clear them after retrieval to streamline debugging.
Instructions
Get console logs from the browser
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| level | No | Filter logs by level | |
| clear | No | Clear console logs after retrieving |
Implementation Reference
- src/index.ts:59-62 (schema)Zod schema for the get_console_logs tool input validation. Defines optional 'level' enum filter and 'clear' boolean flag (defaults to false).
const GetConsoleLogsSchema = z.object({ level: z.enum(['log', 'info', 'warn', 'error', 'debug']).optional(), clear: z.boolean().default(false) }); - src/index.ts:295-313 (registration)Registration of the 'get_console_logs' tool in the ListToolsRequestSchema handler. Specifies name, description (Get console logs from the browser), and input schema with level filter and clear flag.
{ name: 'get_console_logs', description: 'Get console logs from the browser', inputSchema: { type: 'object', properties: { level: { type: 'string', enum: ['log', 'info', 'warn', 'error', 'debug'], description: 'Filter logs by level' }, clear: { type: 'boolean', default: false, description: 'Clear console logs after retrieving' } } } }, - src/index.ts:601-630 (handler)Handler implementation for the get_console_logs tool. Parses input via GetConsoleLogsSchema, filters stored consoleLogs by level if specified, optionally clears the log store, and returns formatted log entries with timestamps, level, and message. Logs are collected via a page console event listener set up in launch_browser.
case 'get_console_logs': { if (!currentPage) { throw new Error('No browser page available. Launch a browser first.'); } const params = GetConsoleLogsSchema.parse(args); // Filter logs by level if specified const filteredLogs = params.level ? consoleLogs.filter(log => log.level === params.level) : consoleLogs; // Clear logs if requested if (params.clear) { consoleLogs = []; } const logText = filteredLogs.length > 0 ? filteredLogs.map(log => `[${log.timestamp.toISOString()}] ${log.level.toUpperCase()}: ${log.message}`).join('\n') : '(no console logs)'; return { content: [ { type: 'text', text: `Console Logs:\n${logText}` } ] }; } - src/index.ts:87-88 (helper)Global array storing console log entries. Each entry contains level (string), message (string), and timestamp (Date). Populated by the page console event listener in launch_browser.
// Console logs storage let consoleLogs: Array<{level: string, message: string, timestamp: Date}> = [];