get_console_logs
Retrieve browser console logs from webpages to debug JavaScript errors, monitor network activity, and analyze client-side performance issues.
Instructions
Get the console logs of a webpage
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | The URL to navigate to | |
| steps | No | Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit") |
Implementation Reference
- src/browser_handler.py:249-295 (handler)Main execution logic for get_console_logs: validates URL, runs AI agent for navigation and steps, injects JavaScript to override console methods and capture logs in window._consoleLogs, retrieves and returns the logs.elif command == 'get_console_logs': if not args.get('url'): return { 'success': False, 'error': 'URL is required for get_console_logs command' } console_messages = [] def on_console_message(msg): console_messages.append(f"type: {msg.type}, text: {msg.text}, location: {msg.location}") task = f"1. Go to {args['url']}" if args.get('steps'): steps = args['steps'].split(',') for i, step in enumerate(steps, 2): task += f"\n{i}. {step.strip()}" task += f"\n{len(steps) + 2}. Get the console logs" else: task += f"\n2. Get the console logs" use_vision = os.getenv('USE_VISION', 'false').lower() == 'true' agent = Agent(task=task, llm=llm, use_vision=use_vision, browser_context=context) await agent.run() try: # Execute JavaScript to get console logs await context.execute_javascript(""" window._consoleLogs = []; const originalConsole = window.console; ['log', 'info', 'warn', 'error'].forEach(level => { window.console[level] = (...args) => { window._consoleLogs.push({type: level, text: args.join(' ')}); originalConsole[level](...args); }; }); """) # Wait a bit for any console logs to be captured await asyncio.sleep(1) # Get the captured logs logs = await context.execute_javascript("window._consoleLogs") return { 'success': True, 'logs': logs } finally: await context.close()
- src/index.ts:214-231 (schema)Defines the tool schema for 'get_console_logs', including input schema with required 'url' and optional 'steps'.{ name: 'get_console_logs', description: 'Get the console logs of a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to navigate to', }, steps: { type: 'string', description: 'Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit")', }, }, required: ['url'], }, },
- src/index.ts:149-233 (registration)Registers the 'get_console_logs' tool (among others) in the MCP server's ListTools response, making it discoverable.this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'screenshot', description: 'Take a screenshot of a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to navigate to', }, full_page: { type: 'boolean', description: 'Whether to capture the full page or just the viewport', default: false, }, steps: { type: 'string', description: 'Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit")', }, }, required: ['url'], }, }, { name: 'get_html', description: 'Get the HTML content of a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to navigate to', }, steps: { type: 'string', description: 'Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit")', }, }, required: ['url'], }, }, { name: 'execute_js', description: 'Execute JavaScript code on a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to navigate to', }, script: { type: 'string', description: 'The JavaScript code to execute', }, steps: { type: 'string', description: 'Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit")', }, }, required: ['url', 'script'], }, }, { name: 'get_console_logs', description: 'Get the console logs of a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to navigate to', }, steps: { type: 'string', description: 'Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit")', }, }, required: ['url'], }, }, ], }));
- src/index.ts:291-300 (handler)MCP CallTool request handler formats and returns the console logs result from the Python backend as text content.} else if (request.params.name === 'get_console_logs') { return { content: [ { type: 'text', text: JSON.stringify(result.logs, null, 2), }, ], }; } else {
- src/index.ts:236-236 (registration)Validates that 'get_console_logs' is an allowed tool name in the CallTool handler.const validCommands = ['screenshot', 'get_html', 'execute_js', 'get_console_logs'];