Stealth browser automation for AI agents over MCP and the Chrome DevTools Protocol: navigation, network hooks, DOM extraction, and pixel-accurate UI cloning.
Python ai-agent-toolsai-toolsanthropicanti-bot-bypassautomation-toolsbrowser-automationchrome-devtoolsclaude-mcpcloudflare-bypassdeveloper-toolsfastmcpmcp-servermodel-context-protocolnetwork-interceptionnodriverpythonstealth-browserundetectable-browserweb-automationweb-scraping
Fix stdout pollution corrupting MCP JSON-RPC protocol (#8)
Vibhek Soni64bf5289c823
diff --git a/src/browser_manager.py b/src/browser_manager.pyindex bd442b2..3cc194e 100644--- a/src/browser_manager.py+++ b/src/browser_manager.py@@ -1,6 +1,7 @@"""Browser instance management with nodriver."""import asyncio+import sysimport uuidfrom typing import Dict, Optional, Listfrom datetime import datetime, timedelta@@ -97,7 +98,7 @@ class BrowserManager:width=options.viewport_width,height=options.viewport_height)- print(f"[DEBUG] Set viewport to {options.viewport_width}x{options.viewport_height}")+ print(f"[DEBUG] Set viewport to {options.viewport_width}x{options.viewport_height}", file=sys.stderr)await self._setup_dynamic_hooks(tab, instance_id)diff --git a/src/debug_logger.py b/src/debug_logger.pyindex cc58945..efe24ce 100644--- a/src/debug_logger.py+++ b/src/debug_logger.py@@ -1,4 +1,5 @@import json+import sysimport tracebackfrom datetime import datetimefrom typing import Dict, List, Any, Optional@@ -32,7 +33,7 @@ class DebugLogger:self._info: List[Dict[str, Any]] = []self._stats: Dict[str, int] = defaultdict(int)self._lock = threading.Lock()- self._enabled = True+ self._enabled = Falseself._lock_owner = "none"import timeself._lock_acquired_time = 0@@ -71,7 +72,7 @@ class DebugLogger:}self._errors.append(error_entry)self._stats[f'{component}.{method}.errors'] += 1- print(f"[DEBUG ERROR] {component}.{method}: {error}")+ print(f"[DEBUG ERROR] {component}.{method}: {error}", file=sys.stderr)def log_warning(self, component: str, method: str, message: str, context: Optional[Dict[str, Any]] = None):"""@@ -96,7 +97,7 @@ class DebugLogger:}self._warnings.append(warning_entry)self._stats[f'{component}.{method}.warnings'] += 1- print(f"[DEBUG WARN] {component}.{method}: {message}")+ print(f"[DEBUG WARN] {component}.{method}: {message}", file=sys.stderr)def log_info(self, component: str, method: str, message: str, data: Optional[Any] = None):"""@@ -121,9 +122,9 @@ class DebugLogger:}self._info.append(info_entry)self._stats[f'{component}.{method}.calls'] += 1- print(f"[DEBUG INFO] {component}.{method}: {message}")+ print(f"[DEBUG INFO] {component}.{method}: {message}", file=sys.stderr)if data:- print(f" Data: {data}")+ print(f" Data: {data}", file=sys.stderr)def get_debug_view(self) -> Dict[str, Any]:"""@@ -242,13 +243,13 @@ class DebugLogger:self._warnings.clear()self._info.clear()self._stats.clear()- print("[DEBUG] Debug logs cleared")+ print("[DEBUG] Debug logs cleared", file=sys.stderr)finally:self._lock.release()else:- print("[DEBUG] Failed to clear logs - timeout acquiring lock")+ print("[DEBUG] Failed to clear logs - timeout acquiring lock", file=sys.stderr)except Exception as e:- print(f"[DEBUG] Error clearing logs: {e}")+ print(f"[DEBUG] Error clearing logs: {e}", file=sys.stderr)def clear_debug_view_safe(self):"""@@ -261,7 +262,7 @@ class DebugLogger:self._warnings = []self._info = []self._stats = defaultdict(int)- print("[DEBUG] Debug logs force-cleared (lock bypass)")+ print("[DEBUG] Debug logs force-cleared (lock bypass)", file=sys.stderr)def enable(self):"""@@ -271,7 +272,7 @@ class DebugLogger:self._enabled (bool): Set to True."""self._enabled = True- print("[DEBUG] Debug logging enabled")+ print("[DEBUG] Debug logging enabled", file=sys.stderr)def disable(self):"""@@ -281,7 +282,7 @@ class DebugLogger:self._enabled (bool): Set to False."""self._enabled = False- print("[DEBUG] Debug logging disabled")+ print("[DEBUG] Debug logging disabled", file=sys.stderr)def get_lock_status(self) -> Dict[str, Any]:"""Get current lock status for debugging."""@@ -327,18 +328,18 @@ class DebugLogger:"""import timetry:- print(f"[DEBUG] export_debug_logs attempting lock acquisition...")+ print(f"[DEBUG] export_debug_logs attempting lock acquisition...", file=sys.stderr)current_status = self.get_lock_status()- print(f"[DEBUG] Current lock status: {current_status}")+ print(f"[DEBUG] Current lock status: {current_status}", file=sys.stderr)acquired = self._lock.acquire(timeout=5.0)if not acquired:- print("[DEBUG] Lock timeout - falling back to lock-free export")+ print("[DEBUG] Lock timeout - falling back to lock-free export", file=sys.stderr)return self._export_lockfree(filepath, max_errors, max_warnings, max_info, format)self._lock_owner = "export_debug_logs"self._lock_acquired_time = time.time()- print("[DEBUG] Lock acquired by export_debug_logs")+ print("[DEBUG] Lock acquired by export_debug_logs", file=sys.stderr)try:debug_data = self.get_debug_view_paginated(@@ -350,9 +351,9 @@ class DebugLogger:self._lock_owner = "none"self._lock_acquired_time = 0self._lock.release()- print("[DEBUG] Lock released by export_debug_logs")+ print("[DEBUG] Lock released by export_debug_logs", file=sys.stderr)except Exception as e:- print(f"[DEBUG] Exception in export: {e}")+ print(f"[DEBUG] Exception in export: {e}", file=sys.stderr)return self._export_lockfree(filepath, max_errors, max_warnings, max_info, format)if format == "auto":@@ -429,7 +430,7 @@ class DebugLogger:print(f"[DEBUG] Exported {debug_data['summary']['returned_errors']} errors, "f"{debug_data['summary']['returned_warnings']} warnings, "f"{debug_data['summary']['returned_info']} info logs to {filepath} "- f"({file_size} bytes, gzip-pickle format)")+ f"({file_size} bytes, gzip-pickle format)", file=sys.stderr)return filepathdef _export_pickle(self, debug_data: Dict[str, Any], filepath: str) -> str:@@ -444,7 +445,7 @@ class DebugLogger:print(f"[DEBUG] Exported {debug_data['summary']['returned_errors']} errors, "f"{debug_data['summary']['returned_warnings']} warnings, "f"{debug_data['summary']['returned_info']} info logs to {filepath} "- f"({file_size} bytes, pickle format)")+ f"({file_size} bytes, pickle format)", file=sys.stderr)return filepathdef _export_json(self, debug_data: Dict[str, Any], filepath: str) -> str:@@ -456,7 +457,7 @@ class DebugLogger:print(f"[DEBUG] Exported {debug_data['summary']['returned_errors']} errors, "f"{debug_data['summary']['returned_warnings']} warnings, "f"{debug_data['summary']['returned_info']} info logs to {filepath} "- f"({file_size} bytes, JSON format)")+ f"({file_size} bytes, JSON format)", file=sys.stderr)return filepathdiff --git a/src/network_interceptor.py b/src/network_interceptor.pyindex 4d5c586..3dbcd38 100644--- a/src/network_interceptor.py+++ b/src/network_interceptor.py@@ -2,6 +2,7 @@import asyncioimport base64+import sysfrom datetime import datetimefrom typing import Any, Dict, List, Optional@@ -46,16 +47,16 @@ class NetworkInterceptor:if resource_type.lower() in resource_patterns:url_patterns.extend(resource_patterns[resource_type.lower()])- print(f"[DEBUG] Added URL patterns for {resource_type}: {resource_patterns[resource_type.lower()]}")+ print(f"[DEBUG] Added URL patterns for {resource_type}: {resource_patterns[resource_type.lower()]}", file=sys.stderr)else:# Assume it's already a URL patternurl_patterns.append(resource_type)- print(f"[DEBUG] Added custom URL pattern: {resource_type}")+ print(f"[DEBUG] Added custom URL pattern: {resource_type}", file=sys.stderr)# Use network.set_blocked_ur_ls to block the URL patternsif url_patterns:await tab.send(uc.cdp.network.set_blocked_ur_ls(urls=url_patterns))- print(f"[DEBUG] Blocked {len(url_patterns)} URL patterns: {url_patterns}")+ print(f"[DEBUG] Blocked {len(url_patterns)} URL patterns: {url_patterns}", file=sys.stderr)tab.add_handler(uc.cdp.network.RequestWillBeSent,@@ -70,7 +71,7 @@ class NetworkInterceptor:if instance_id not in self._instance_requests:self._instance_requests[instance_id] = []except Exception as e:- print(f"[DEBUG] Error in setup_interception: {e}")+ print(f"[DEBUG] Error in setup_interception: {e}", file=sys.stderr)raise Exception(f"Failed to setup network interception: {str(e)}")async def _on_request(self, event, instance_id: str):diff --git a/src/server.py b/src/server.pyindex 113892f..92e2785 100644--- a/src/server.py+++ b/src/server.py@@ -2707,7 +2707,7 @@ if __name__ == "__main__":DISABLED_SECTIONS.add("dynamic-hooks")if DISABLED_SECTIONS:- print(f"Disabled tool sections: {', '.join(sorted(DISABLED_SECTIONS))}")+ print(f"Disabled tool sections: {', '.join(sorted(DISABLED_SECTIONS))}", file=sys.stderr)if args.transport == "http":mcp.run(transport="http", host=args.host, port=args.port)