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
Package as stealth_browser_mcp with a console script for pipx
Vibhek Soni933737bf9aab
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex df1cc03..30d8462 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -18,3 +18,36 @@ jobs:- name: Unit testsrun: python -m unittest discover -s tests -v++ package:+ runs-on: ubuntu-latest+ timeout-minutes: 15+ strategy:+ fail-fast: false+ matrix:+ python-version: ['3.10', '3.14']+ steps:+ - uses: actions/checkout@v4+ - uses: actions/setup-python@v5+ with:+ python-version: ${{ matrix.python-version }}+ - run: python -m pip install -U pip build+ - name: Build sdist and wheel+ run: python -m build+ - name: Check wheel contents+ run: |+ python - <<'PY'+ import glob, zipfile+ names = zipfile.ZipFile(glob.glob("dist/*.whl")[0]).namelist()+ top = {name.split("/")[0] for name in names if not name.split("/")[0].endswith(".dist-info")}+ assert top == {"stealth_browser_mcp"}, top+ scripts = [name for name in names if name.startswith("stealth_browser_mcp/js/") and name.endswith(".js")]+ assert len(scripts) >= 5, scripts+ print(f"{len(names)} entries, {len(scripts)} js assets")+ PY+ - name: Install with pipx and run outside the checkout+ working-directory: ${{ runner.temp }}+ run: |+ pipx install --python "$(which python)" "$GITHUB_WORKSPACE"/dist/*.whl+ stealth-browser-mcp --list-sections+ "$(pipx environment --value PIPX_LOCAL_VENVS)/stealth-browser-mcp/bin/python" -c "from stealth_browser_mcp import element_cloner, paths; assert paths.source_checkout_root() is None; assert (element_cloner.JS_DIR / 'extract_structure.js').is_file(); print(paths.clone_output_dir())"diff --git a/AGENTS.md b/AGENTS.mdindex a226a2d..fdfc050 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -22,6 +22,12 @@ This project is sponsored by:- Platform: Windows 11 with PowerShell. Prefer PowerShell-native commands.- Run Python from repo root: `& .\venv\Scripts\python.exe ...`+## Layout++- Server code is the `stealth_browser_mcp` package in `src/stealth_browser_mcp/`. Modules import each other with relative imports (`from .debug_logger import debug_logger`).+- `src/server.py` is only a launcher so existing `python src/server.py` configs keep working. The `stealth-browser-mcp` console script and `python -m stealth_browser_mcp` call the same `main()`.+- JavaScript templates live in `src/stealth_browser_mcp/js/` and ship as package data. Default file locations come from `paths.py`, not from `__file__` paths in other modules.+## Validation```powershell@@ -32,7 +38,7 @@ This project is sponsored by:For broader syntax validation:```powershell-Get-ChildItem src -Filter *.py | ForEach-Object { & .\venv\Scripts\python.exe -m py_compile $_.FullName }+Get-ChildItem src -Recurse -Filter *.py | ForEach-Object { & .\venv\Scripts\python.exe -m py_compile $_.FullName }```Unit tests and the stealth benchmark:diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5281e29..fdc6af6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,6 +6,8 @@ The format is based on Keep a Changelog and adheres to Semantic Versioning where## [Unreleased]### Added+- **pipx install** - `pipx install git+https://github.com/vibheksoni/stealth-browser-mcp.git` installs a `stealth-browser-mcp` command, so MCP clients can use `"command": "stealth-browser-mcp"` instead of a venv interpreter and script path. `python -m stealth_browser_mcp` works too. The code moved into a real `stealth_browser_mcp` package under `src/`, the JavaScript templates ship as package data, and `pyproject.toml` has a build system and console script. `python src/server.py` still works for existing configs (#38).+- **`STEALTH_BROWSER_MCP_DATA_DIR`** - Sets where element clones and large responses are written. An installed package defaults to the per-user data directory, and the file upload allowlist defaults to an `uploads` folder inside it. A clone keeps using the repo root for both.- **`press_key()` tool** - Presses real keys through CDP `Input.dispatchKeyEvent`, producing trusted `keydown`/`keypress`/`keyup` events. Supports named keys (Enter, Tab, Escape, arrows, Backspace, Delete, Home, End, PageUp, PageDown, Space, F1-F12), single printable characters, modifier combinations (Shift, Control/Ctrl, Alt, Meta/Cmd), optional focus selector, repeat count, and inter-press delay.- **Tests** - `tests/test_press_key.py` covers the key-to-CDP mapping, the modifier bitmask, and the dispatch sequence. A browser-backed test asserting `isTrusted` runs when `STEALTH_BROWSER_TESTS=1` is set.@@ -19,6 +21,7 @@ The format is based on Keep a Changelog and adheres to Semantic Versioning where- **Tests** - `tests/test_js_values.py`, `tests/test_element_cloners.py`, and `tests/test_hooks_and_proxy.py` cover result conversion, template escaping, cloner data paths, hook matching, proxy parsing, and debug log export. CI now runs the unit test suite.### Fixed+- **Fresh installs** - `mcp` is pinned to 1.30.0. `mcp` 2.x removed names that fastmcp 2.11.2 imports, so an unpinned install could fail at startup. `py2js` now comes from PyPI (1.2.1, the same code as the old git pin), so installing no longer needs git, and `pydantic` is 2.12.5, which has Python 3.14 wheels.- **Sandbox flags** - Chrome keeps its sandbox on Windows and macOS, including under an administrator account. Only Linux as root or in a container gets `--no-sandbox`. `--disable-setuid-sandbox`, `--single-process`, and `--disable-gpu` are no longer added anywhere, so headed windows no longer show the "unsupported command-line flag" bar, tabs no longer share one crash-prone process in containers, and WebGL stays available.- **Browser startup** - Chrome gets up to 30 seconds to start instead of under 3, which cold CI runners and slow disks missed. A failed start no longer leaves a Chrome process and temp profile behind, and Chrome's output goes to the null device instead of an unread pipe that could fill up and block it.- **Trusted clicks** - `click_element` scrolls the element into view, moves the mouse along a short path, and presses and releases the button with CDP input events, so pages see trusted `pointerdown`, `mousedown`, `mouseup`, and `click` events. It used to call `el.click()` from JavaScript, which produces an untrusted click with no mouse events, and nodriver's click flashed a highlight element into the page DOM. Covered or hidden elements still fall back to a JavaScript click.diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 8666a7a..78c7e46 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -28,7 +28,7 @@ source venv/bin/activatepip install -r requirements.txt```-3. Run the server: `python src/server.py`+3. Run the server: `python src/server.py`. Code lives in the `stealth_browser_mcp` package under `src/stealth_browser_mcp/`, and `pip install -e .` also gives you the `stealth-browser-mcp` command.4. Run the tests: `python -m unittest discover -s tests -v`. Set `STEALTH_BROWSER_TESTS=1` to include tests that launch a real browser.5. If your change can affect detection, run the stealth benchmark before and after: `python -m benchmarks.stealth run --mode headed`. See [STEALTH_TESTS.md](STEALTH_TESTS.md) for targets and statuses.diff --git a/Dockerfile b/Dockerfileindex 100ff8b..0ce2a6b 100644--- a/Dockerfile+++ b/Dockerfile@@ -1,14 +1,13 @@# Use Python 3.11 slim image for smaller sizeFROM python:3.11-slim-# Install system dependencies for Chrome, browser automation, and git (needed for py2js)+# Install system dependencies for Chrome and browser automationRUN apt-get update && apt-get install -y \wget \gnupg \unzip \curl \xvfb \- git \&& apt-get clean \&& rm -rf /var/lib/apt/lists/*diff --git a/README.md b/README.mdindex 86b65b2..9c34672 100644--- a/README.md+++ b/README.md@@ -98,10 +98,22 @@ Navigate Cloudflare challenges, anti-bot checks, and login walls with real Chrom## Quickstart-### 1. Clone and install+### 1. InstallRequires Python 3.10+ and Chrome, Chromium, or Microsoft Edge.+**With pipx (recommended):**++```bash+pipx install git+https://github.com/vibheksoni/stealth-browser-mcp.git+```++This puts a `stealth-browser-mcp` command on your `PATH`. Upgrade with `pipx upgrade stealth-browser-mcp`, or pin a release by adding `@<tag>` to the URL. `uvx --from git+https://github.com/vibheksoni/stealth-browser-mcp.git stealth-browser-mcp` runs it without installing.++The package is not on PyPI yet. `pipx install stealth-browser-mcp` without the GitHub URL installs a different, unrelated project that uses the same name.++**From a clone (for development):**+```bashgit clone https://github.com/vibheksoni/stealth-browser-mcp.gitcd stealth-browser-mcp@@ -118,7 +130,29 @@ pip install -r requirements.txt### 2. Add to your MCP client-**Claude Code CLI (recommended):**+**Installed with pipx:**++```bash+claude mcp add stealth-browser-mcp -- stealth-browser-mcp+```++Or in any client's JSON config:++```json+{+ "mcpServers": {+ "stealth-browser-mcp": {+ "command": "stealth-browser-mcp",+ "args": [],+ "env": {}+ }+ }+}+```++If your client cannot find the command, use the full path printed by `pipx list`.++**From a clone with Claude Code CLI:**Windows:@@ -221,13 +255,15 @@ Choose exactly what functionality you need. Run the full 101-tool suite or strip| **Xpool safe** (`--xpool-safe`) | 84 | Disable CDP function tools that trigger `Runtime.enable` |```bash-python src/server.py --minimal-python src/server.py --disable-cdp-functions --disable-dynamic-hooks-python src/server.py --list-sections-python src/server.py --debug-python src/server.py --transport http --host 127.0.0.1 --port 8000+stealth-browser-mcp --minimal+stealth-browser-mcp --disable-cdp-functions --disable-dynamic-hooks+stealth-browser-mcp --list-sections+stealth-browser-mcp --debug+stealth-browser-mcp --transport http --host 127.0.0.1 --port 8000```+From a clone, `python src/server.py` takes the same flags.+Use `--debug` or set `STEALTH_BROWSER_DEBUG=1` to enable verbose server diagnostics on `stderr`. Normal MCP `stdio` runs stay quiet by default to avoid corrupting JSON-RPC transport output.**Available sections:**@@ -261,7 +297,8 @@ These are environment variables for the MCP server process. Set them in your she| `BROWSER_TAB_RECYCLE_NAVIGATIONS` | `0` | Replace the main tab with a fresh one after this many navigations. `0` keeps the same tab, which preserves history and session storage. || `BROWSER_ORPHAN_PROFILE_MAX_AGE` | `21600` | Startup cleanup threshold in seconds for stale `uc_*` temp profiles. Set `0` to disable this startup sweep. || `STEALTH_BROWSER_EXECUTABLE` | auto-detect | Path to the Chrome, Chromium, or Edge executable to launch instead of the auto-detected one. |-| `BROWSER_FILE_UPLOAD_ALLOWED_DIRS` | repo root | Directories that `file_upload()` may read from. Separate multiple roots with `;` on Windows or `:` on macOS/Linux. |+| `BROWSER_FILE_UPLOAD_ALLOWED_DIRS` | repo root or data dir | Directories that `file_upload()` may read from. Separate multiple roots with `;` on Windows or `:` on macOS/Linux. Defaults to the repo root for a clone and to the `uploads` folder in the data directory for an installed package. |+| `STEALTH_BROWSER_MCP_DATA_DIR` | repo root or user data dir | Where element clones and large responses are written. Defaults to the repo root for a clone, and for an installed package to `%LOCALAPPDATA%\stealth-browser-mcp` on Windows, `~/Library/Application Support/stealth-browser-mcp` on macOS, or `$XDG_DATA_HOME/stealth-browser-mcp` (`~/.local/share/stealth-browser-mcp`) on Linux. || `STEALTH_BROWSER_DEBUG` | `0` | Enable verbose debug logging to `stderr` when set to `1`. || `DEBUG` | `0` | Legacy alias for `STEALTH_BROWSER_DEBUG`. || `XPOOL_SAFE_MODE` | `0` | Disable the `cdp-functions` section at startup. |diff --git a/benchmarks/stealth/runner.py b/benchmarks/stealth/runner.pyindex 81fcdb4..343822a 100644--- a/benchmarks/stealth/runner.py+++ b/benchmarks/stealth/runner.py@@ -15,10 +15,10 @@ if str(SRC_DIR) not in sys.path:import nodriver as ucimport psutil-from browser_manager import BrowserManager-from dom_handler import DOMHandler-from js_values import evaluate_to_python-from models import BrowserOptions+from stealth_browser_mcp.browser_manager import BrowserManager+from stealth_browser_mcp.dom_handler import DOMHandler+from stealth_browser_mcp.js_values import evaluate_to_python+from stealth_browser_mcp.models import BrowserOptionsfrom .environment import base_environment, runner_labelfrom .local_server import LocalPageServerdiff --git a/pyproject.toml b/pyproject.tomlindex 79684e3..be04d34 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1,6 +1,10 @@+[build-system]+requires = ["hatchling>=1.26"]+build-backend = "hatchling.build"+[project]name = "stealth-browser-mcp"-version = "0.2.5"+dynamic = ["version"]description = "Ultimate undetectable browser automation MCP server with CDP-level access"readme = "README.md"requires-python = ">=3.10"@@ -12,10 +16,11 @@ authors = [# Runtime dependenciesdependencies = ["fastmcp==2.11.2",+ "mcp==1.30.0","nodriver==0.47.0",- "pydantic==2.11.7",+ "pydantic==2.12.5","python-dotenv==1.1.1",- "py2js @ git+https://github.com/am230/py2js.git@31a83c7c25a51ab0cc3255f484a2279d26278ec3",+ "py2js==1.2.1","jsbeautifier==1.15.4","strinpy==0.0.4","strbuilder==1.1.3",@@ -25,6 +30,9 @@ dependencies = ["uvicorn[standard]==0.35.0",]+[project.scripts]+stealth-browser-mcp = "stealth_browser_mcp.server:main"+# Development dependencies (optional)[project.optional-dependencies]dev = [@@ -42,10 +50,19 @@ Documentation = "https://github.com/vibheksoni/stealth-browser-mcp#readme"Repository = "https://github.com/vibheksoni/stealth-browser-mcp.git"Issues = "https://github.com/vibheksoni/stealth-browser-mcp/issues"+[tool.hatch.version]+path = "src/stealth_browser_mcp/__init__.py"++[tool.hatch.build.targets.wheel]+packages = ["src/stealth_browser_mcp"]++[tool.hatch.build.targets.sdist]+include = ["src", "tests", "README.md", "LICENSE", "CHANGELOG.md", "requirements.txt"]+# Code formatting[tool.black]line-length = 100-target-version = ["py310", "py311", "py312", "py313"]+target-version = ["py310", "py311", "py312", "py313", "py314"][tool.isort]profile = "black"diff --git a/requirements.txt b/requirements.txtindex f571a25..ef0514a 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,8 +1,9 @@fastmcp==2.11.2+mcp==1.30.0nodriver==0.47.0-pydantic==2.11.7+pydantic==2.12.5python-dotenv==1.1.1-py2js @ git+https://github.com/am230/py2js.git@31a83c7c25a51ab0cc3255f484a2279d26278ec3+py2js==1.2.1jsbeautifier==1.15.4strinpy==0.0.4strbuilder==1.1.3diff --git a/src/__init__.py b/src/__init__.pydeleted file mode 100644index 34ebbe3..0000000--- a/src/__init__.py+++ /dev/null@@ -1,3 +0,0 @@-"""Browser MCP - Undetectable browser automation via MCP protocol."""--__version__ = "0.1.0"\ No newline at end of filediff --git a/src/server.py b/src/server.pyindex f0670e8..cf4a540 100644--- a/src/server.py+++ b/src/server.py@@ -1,3123 +1,6 @@-"""Main MCP server for browser automation."""--import asyncio-import base64-import importlib-import json-import ipaddress-import os-import sys-import tempfile-from collections import defaultdict-from contextlib import asynccontextmanager-from pathlib import Path-from typing import Any, Dict, List, Optional, Union--import nodriver as uc-from fastmcp import FastMCP-from fastmcp.tools.tool import ToolResult-from mcp.types import ImageContent, TextContent-from pydantic_core import to_json--from browser_manager import BrowserManager-from cdp_element_cloner import CDPElementCloner-from cdp_function_executor import CDPFunctionExecutor-from comprehensive_element_cloner import comprehensive_element_cloner-from debug_logger import debug_logger-from dom_handler import DOMHandler-from element_cloner import element_cloner-from file_based_element_cloner import file_based_element_cloner-from http_security import (- create_http_auth_provider,- get_http_auth_token,-)-from models import BrowserOptions-from network_interceptor import NetworkInterceptor-from page_bindings import page_bindings-from dynamic_hook_ai_interface import dynamic_hook_ai-from persistent_storage import persistent_storage-from progressive_element_cloner import progressive_element_cloner-from response_handler import response_handler-from screenshot_encoder import DEFAULT_QUALITY, fit_screenshot, validate_options as validate_screenshot_options-from stdio_guard import install_stdio_guard-from platform_utils import validate_browser_environment, get_platform_info-from process_cleanup import process_cleanup--DISABLED_SECTIONS = set()-SECTION_TOOLS: Dict[str, List[str]] = defaultdict(list)-SECTION_DESCRIPTIONS = {- "browser-management": "Core browser operations",- "element-interaction": "Page interaction and element manipulation",- "element-extraction": "Element cloning and extraction",- "file-extraction": "File-based extraction tools",- "network-debugging": "Network monitoring and interception",- "cdp-functions": "Chrome DevTools Protocol function execution",- "progressive-cloning": "Advanced element cloning system",- "cookies-storage": "Cookie and storage management",- "tabs": "Tab management",- "debugging": "Debug and system tools",- "dynamic-hooks": "AI-powered network hook system",-}---def parse_bool_env(name: str, default: bool = False) -> bool:- value = os.getenv(name)- if value is None:- return default- return value.strip().lower() in {"1", "true", "yes", "on", "enabled"}---DEBUG_LOGGING_ENABLED = (- parse_bool_env("STEALTH_BROWSER_DEBUG", default=False)- or parse_bool_env("DEBUG", default=False)-)-HTTP_AUTH_TOKEN = get_http_auth_token()--def is_loopback_host(host: str) -> bool:- """- Check whether an HTTP bind host only accepts local connections.-- Args:- host (str): Host name or IP address passed to --host.-- Returns:- bool: True for localhost and loopback addresses.- """- if host.lower() == "localhost":- return True- try:- return ipaddress.ip_address(host).is_loopback- except ValueError:- return False---def section_tool(section: str):- """Decorator that registers tools and tracks section membership."""- def decorator(func):- SECTION_TOOLS[section].append(func.__name__)- return mcp.tool(func)- return decorator---def apply_disabled_sections() -> None:- """Apply section disable rules by unregistering tools from FastMCP."""- for section in sorted(DISABLED_SECTIONS):- for tool_name in SECTION_TOOLS.get(section, []):- try:- mcp.remove_tool(tool_name)- except Exception:- # Tool may already be removed by another section policy.- continue--@asynccontextmanager-async def app_lifespan(server):- """- Manage application lifecycle with proper cleanup.-- Args:- server (Any): The server instance for which the lifespan is being managed.- """- debug_logger.log_info("server", "startup", "Starting Browser Automation MCP Server...")- try:- await browser_manager.start_idle_reaper()- yield- finally:- debug_logger.log_info("server", "shutdown", "Shutting down Browser Automation MCP Server...")- try:- await browser_manager.stop_idle_reaper()- except Exception as e:- debug_logger.log_error("server", "cleanup", e)- try:- await browser_manager.close_all()- debug_logger.log_info("server", "cleanup", "All browser instances closed")- except Exception as e:- debug_logger.log_error("server", "cleanup", e)-- try:- process_cleanup._cleanup_all_tracked()- debug_logger.log_info("server", "cleanup", "Process cleanup complete")- except Exception as e:- debug_logger.log_error("server", "cleanup", f"Process cleanup failed: {e}")- try:- persistent_instances = persistent_storage.list_instances()- if persistent_instances.get("instances"):- debug_logger.log_info(- "server",- "storage_cleanup",- f"Clearing in-memory storage with {len(persistent_instances['instances'])} instances...",- )- persistent_storage.clear_all()- debug_logger.log_info("server", "storage_cleanup", "In-memory storage cleared")- except Exception as e:- debug_logger.log_error("server", "storage_cleanup", e)- debug_logger.log_info("server", "shutdown", "Browser Automation MCP Server shutdown complete")--mcp = FastMCP(- name="Browser Automation MCP",- instructions="""- This MCP server provides undetectable browser automation using nodriver (CDP-based).-- Key features:- - Spawn and manage multiple browser instances- - Navigate and interact with web pages- - Query and manipulate DOM elements- - Intercept and analyze network traffic- - Execute JavaScript in page context- - Manage cookies and storage-- Instances are built to avoid common bot detection, with trusted input- events and a clean headless fingerprint. Measured results against public- detection pages are published in STEALTH_TESTS.md.- """,- auth=create_http_auth_provider(HTTP_AUTH_TOKEN),- lifespan=app_lifespan,-)--browser_manager = BrowserManager()-network_interceptor = NetworkInterceptor()-dom_handler = DOMHandler()-cdp_function_executor = CDPFunctionExecutor()---async def _prepare_tab(tab, instance_id: str, options: BrowserOptions) -> None:- """- Attach network capture to a tab an instance uses.-- Args:- tab (Any): Browser tab.- instance_id (str): Browser instance ID.- options (BrowserOptions): Spawn options.- """- await network_interceptor.setup_interception(tab, instance_id, options.block_resources)---async def _forget_instance(instance_id: str) -> None:- """- Drop per-instance state held outside the browser manager.-- Args:- instance_id (str): Closed browser instance ID.- """- await network_interceptor.clear_instance_data(instance_id)- cdp_function_executor.forget_instance(instance_id)---def _register_manager_listeners() -> None:- """Connect tab preparation and close cleanup to the current browser manager."""- browser_manager.add_tab_listener(_prepare_tab)- browser_manager.add_tab_listener(page_bindings.prepare_tab)- browser_manager.add_close_listener(_forget_instance)- browser_manager.add_close_listener(page_bindings.forget_instance)---_register_manager_listeners()--if DEBUG_LOGGING_ENABLED:- debug_logger.enable()--@section_tool("browser-management")-async def spawn_browser(- headless: bool = False,- user_agent: Optional[str] = None,- viewport_width: int = 1920,- viewport_height: int = 1080,- proxy: Optional[str] = None,- browser_args: Optional[List[str]] = None,- timezone_id: Optional[str] = None,- idle_timeout_seconds: Optional[int] = None,- block_resources: Optional[List[str]] = None,- extra_headers: Optional[Dict[str, str]] = None,- user_data_dir: Optional[str] = None,- sandbox: Optional[Any] = None-) -> Dict[str, Any]:- """- Spawn a new browser instance.-- Args:- headless (bool): Run in headless mode.- user_agent (Optional[str]): Custom user agent string.- viewport_width (int): Viewport width in pixels.- viewport_height (int): Viewport height in pixels.- proxy (Optional[str]): Proxy server URL.- browser_args (List[str]): Additional browser launch args.- timezone_id (Optional[str]): IANA timezone ID applied via CDP timezone override.- idle_timeout_seconds (Optional[int]): Idle timeout override in seconds for automatic instance cleanup.- block_resources (List[str]): List of resource types to block (e.g., ['image', 'font', 'stylesheet']).- extra_headers (Dict[str, str]): Additional HTTP headers.- user_data_dir (Optional[str]): Path to user data directory for persistent sessions.- sandbox (Optional[Any]): Enable browser sandbox. Accepts bool, string ('true'/'false'), int (1/0), or None for auto-detect.-- Returns:- Dict[str, Any]: Instance information including instance_id.- """- try:- from platform_utils import sandbox_must_be_disabled-- if sandbox is None:- sandbox = not sandbox_must_be_disabled()- elif isinstance(sandbox, str):- sandbox = sandbox.lower() in ('true', '1', 'yes', 'on', 'enabled')- elif isinstance(sandbox, int):- sandbox = bool(sandbox)- elif not isinstance(sandbox, bool):- sandbox = bool(sandbox)-- options = BrowserOptions(- headless=headless,- user_agent=user_agent,- viewport_width=viewport_width,- viewport_height=viewport_height,- proxy=proxy,- browser_args=browser_args or [],- timezone_id=timezone_id,- idle_timeout_seconds=idle_timeout_seconds,- block_resources=block_resources or [],- extra_headers=extra_headers or {},- user_data_dir=user_data_dir,- sandbox=sandbox- )- instance = await browser_manager.spawn_browser(options)- spawn_diagnostics = await browser_manager.get_spawn_diagnostics(instance.instance_id)- return {- "instance_id": instance.instance_id,- "state": instance.state,- "headless": instance.headless,- "viewport": instance.viewport,- "spawn_diagnostics": spawn_diagnostics or {},- }- except Exception as e:- message = str(e)- if not message.startswith("Failed to spawn browser"):- message = f"Failed to spawn browser: {message}"- raise Exception(message) from e--@section_tool("browser-management")-async def list_instances() -> List[Dict[str, Any]]:- """- List all active browser instances.-- Returns:- List[Dict[str, Any]]: List of browser instances with their current state.- """- memory_instances = await browser_manager.list_instances()- storage_instances = persistent_storage.list_instances()- result = []- for inst in memory_instances:- result.append({- "instance_id": inst.instance_id,- "state": inst.state,- "current_url": inst.current_url,- "title": inst.title,- "source": "active"- })- memory_ids = {inst.instance_id for inst in memory_instances}- for instance_id, inst_data in storage_instances.get("instances", {}).items():- if instance_id not in memory_ids:- result.append({- "instance_id": inst_data["instance_id"],- "state": inst_data["state"] + " (stored)",- "current_url": inst_data["current_url"],- "title": inst_data["title"],- "source": "stored"- })- return result--@section_tool("browser-management")-async def close_instance(instance_id: str) -> bool:- """- Close a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- bool: True if closed successfully.- """- return await browser_manager.close_instance(instance_id)--@section_tool("browser-management")-async def get_instance_state(instance_id: str) -> Optional[Dict[str, Any]]:- """- Get detailed state of a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- Optional[Dict[str, Any]]: Complete state information.- """- state = await browser_manager.get_page_state(instance_id)- if state:- return state.dict()- return None--@section_tool("browser-management")-async def navigate(- instance_id: str,- url: str,- wait_until: str = "load",- timeout: int = 30000,- referrer: Optional[str] = None-) -> Dict[str, Any]:- """- Navigate to a URL.-- Args:- instance_id (str): Browser instance ID.- url (str): URL to navigate to.- wait_until (str): Wait condition - 'load', 'domcontentloaded', or 'networkidle'.- timeout (int): Navigation timeout in milliseconds.- referrer (Optional[str]): Referrer URL.-- Returns:- Dict[str, Any]: Navigation result with final URL and title.- """- if isinstance(timeout, str):- timeout = int(timeout)- return await browser_manager.navigate(- instance_id=instance_id,- url=url,- wait_until=wait_until,- timeout=timeout,- referrer=referrer,- )--@section_tool("browser-management")-async def go_back(instance_id: str) -> bool:- """- Navigate back in history.-- Args:- instance_id (str): Browser instance ID.-- Returns:- bool: True if navigation was successful.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- await tab.back()- return True--@section_tool("browser-management")-async def go_forward(instance_id: str) -> bool:- """- Navigate forward in history.-- Args:- instance_id (str): Browser instance ID.-- Returns:- bool: True if navigation was successful.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- await tab.forward()- return True--@section_tool("browser-management")-async def reload_page(instance_id: str, ignore_cache: bool = False) -> bool:- """- Reload the current page.-- Args:- instance_id (str): Browser instance ID.- ignore_cache (bool): Whether to ignore cache when reloading.-- Returns:- bool: True if reload was successful.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- await tab.reload(ignore_cache=ignore_cache)- return True--@section_tool("element-interaction")-async def query_elements(- instance_id: str,- selector: str,- text_filter: Optional[str] = None,- visible_only: bool = True,- limit: Optional[Any] = None-) -> List[Dict[str, Any]]:- """- Query DOM elements.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector or XPath (starts with '//').- text_filter (Optional[str]): Filter by text content.- visible_only (bool): Only return visible elements.- limit (Optional[Any]): Maximum number of elements to return.-- Returns:- List[Dict[str, Any]]: List of matching elements with their properties.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- debug_logger.log_info('Server', 'query_elements', f'Received limit parameter: {limit} (type: {type(limit)})')- elements = await dom_handler.query_elements(- tab, selector, text_filter, visible_only, limit- )- debug_logger.log_info('Server', 'query_elements', f'DOM handler returned {len(elements)} elements')- result = []- for i, elem in enumerate(elements):- try:- if hasattr(elem, 'model_dump'):- elem_dict = elem.model_dump()- else:- elem_dict = elem.dict()- result.append(elem_dict)- debug_logger.log_info('Server', 'query_elements', f'Converted element {i+1} to dict: {list(elem_dict.keys())}')- except Exception as e:- debug_logger.log_error('Server', 'query_elements', e, {'element_index': i})- debug_logger.log_info('Server', 'query_elements', f'Returning {len(result)} results to MCP client')- return result if result else []--@section_tool("element-interaction")-async def click_element(- instance_id: str,- selector: str,- text_match: Optional[str] = None,- timeout: int = 10000-) -> bool:- """- Click an element.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector or XPath.- text_match (Optional[str]): Click element with matching text.- timeout (int): Timeout in milliseconds.-- Returns:- bool: True if clicked successfully.- """- if isinstance(timeout, str):- timeout = int(timeout)- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.click_element(tab, selector, text_match, timeout)--@section_tool("element-interaction")-async def type_text(- instance_id: str,- selector: str,- text: str,- clear_first: bool = True,- delay_ms: int = 50,- parse_newlines: bool = False,- shift_enter: bool = False-) -> bool:- """- Type text into an input field with trusted keyboard events.-- Every character produces keydown, keypress, input, and keyup events.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector.- text (str): Text to type.- clear_first (bool): Clear field before typing.- delay_ms (int): Delay between keystrokes in milliseconds.- parse_newlines (bool): If True, press Enter for each \n instead of inserting a line break.- shift_enter (bool): If True, press Shift+Enter for each \n (for chat apps).-- Returns:- bool: True if typed successfully.- """- if isinstance(delay_ms, str):- delay_ms = int(delay_ms)- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.type_text(tab, selector, text, clear_first, delay_ms, parse_newlines, shift_enter)--@section_tool("element-interaction")-async def paste_text(- instance_id: str,- selector: str,- text: str,- clear_first: bool = True-) -> bool:- """- Paste text instantly into an input field.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector or XPath.- text (str): Text to paste.- clear_first (bool): Clear field before pasting.-- Returns:- bool: True if pasted successfully.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.paste_text(tab, selector, text, clear_first)--@section_tool("element-interaction")-async def press_key(- instance_id: str,- key: str,- modifiers: Optional[List[str]] = None,- selector: Optional[str] = None,- count: int = 1,- delay_ms: int = 50-) -> Dict[str, Any]:- """- Press a real key using trusted CDP key events.-- Sends Input.dispatchKeyEvent keyDown/keyUp pairs, so the page sees trusted- events. Use this for control keys such as Enter, Tab, Escape, and the- arrow keys in React-select, typeahead, and combobox widgets.-- Args:- instance_id (str): Browser instance ID.- key (str): DOM KeyboardEvent.key name (Enter, Tab, Escape, ArrowDown, ArrowUp, ArrowLeft, ArrowRight, Backspace, Delete, Home, End, PageUp, PageDown, Space, F1-F12) or a single printable character.- modifiers (Optional[List[str]]): Modifier keys to hold: Shift, Control (Ctrl), Alt, Meta (Cmd).- selector (Optional[str]): CSS selector to focus before pressing. Presses on the focused element when omitted.- count (int): Number of times to press the key.- delay_ms (int): Delay between presses in milliseconds.-- Returns:- Dict[str, Any]: { success: True, key: str, count: int, modifiers: [str, ...] }- """- if isinstance(count, str):- count = int(count)- if isinstance(delay_ms, str):- delay_ms = int(delay_ms)- if isinstance(modifiers, str):- modifiers = [modifiers]- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.press_key(tab, key, modifiers, selector, count, delay_ms)--@section_tool("element-interaction")-async def file_upload(- instance_id: str,- selector: str,- paths: List[str]-) -> Dict[str, Any]:- """- Upload allowed local files to a <input type="file"> element via CDP DOM.setFileInputFiles.-- Resolves the actual file input even if the selector matches a wrapper element- (Workday/Ashby/LinkedIn often hide the real input behind a styled button).- Refuses multi-file upload when the input lacks the `multiple` attribute.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the file input or any ancestor that contains it.- paths (List[str]): Absolute allowlisted local file paths to upload.-- Returns:- Dict[str, Any]: { success: True, count: N, files: [basename, ...] }- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.file_upload(tab, selector, paths)--@section_tool("element-interaction")-async def select_option(- instance_id: str,- selector: str,- value: Optional[str] = None,- text: Optional[str] = None,- index: Optional[Any] = None-) -> bool:- """- Select an option from a dropdown.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the select element.- value (Optional[str]): Option value attribute.- text (Optional[str]): Option text content.- index (Optional[Any]): Option index (0-based). Can be string or int.-- Returns:- bool: True if selected successfully.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")-- converted_index = None- if index is not None:- try:- converted_index = int(index)- except (ValueError, TypeError):- raise Exception(f"Invalid index value: {index}. Must be a number.")-- return await dom_handler.select_option(tab, selector, value, text, converted_index)--@section_tool("element-interaction")-async def get_element_state(- instance_id: str,- selector: str-) -> Dict[str, Any]:- """- Get complete state of an element.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector or XPath.-- Returns:- Dict[str, Any]: Element state including attributes, style, position, etc.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.get_element_state(tab, selector)--@section_tool("element-interaction")-async def wait_for_element(- instance_id: str,- selector: str,- timeout: int = 30000,- visible: bool = True,- text_content: Optional[str] = None-) -> bool:- """- Wait for an element to appear.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector or XPath.- timeout (int): Timeout in milliseconds.- visible (bool): Wait for element to be visible.- text_content (Optional[str]): Wait for specific text content.-- Returns:- bool: True if element found.- """- if isinstance(timeout, str):- timeout = int(timeout)- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.wait_for_element(tab, selector, timeout, visible, text_content)--@section_tool("element-interaction")-async def scroll_page(- instance_id: str,- direction: str = "down",- amount: int = 500,- smooth: bool = True-) -> bool:- """- Scroll the page.-- Args:- instance_id (str): Browser instance ID.- direction (str): 'down', 'up', 'left', 'right', 'top', or 'bottom'.- amount (int): Pixels to scroll (ignored for 'top' and 'bottom').- smooth (bool): Use smooth scrolling.-- Returns:- bool: True if scrolled successfully.- """- if isinstance(amount, str):- amount = int(amount)- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await dom_handler.scroll_page(tab, direction, amount, smooth)--@section_tool("element-interaction")-async def execute_script(- instance_id: str,- script: str,- args: Optional[List[Any]] = None-) -> Dict[str, Any]:- """- Execute JavaScript in page context.-- Args:- instance_id (str): Browser instance ID.- script (str): JavaScript code to execute.- args (Optional[List[Any]]): Arguments to pass to the script.-- Returns:- Dict[str, Any]: Script execution result.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- try:- result = await dom_handler.execute_script(tab, script, args)- bounded = response_handler.handle_response(result, "script_result")- return {- "success": True,- "result": bounded,- "error": None- }- except Exception as e:- return {- "success": False,- "result": None,- "error": str(e)- }--@section_tool("element-interaction")-async def get_page_content(- instance_id: str,- include_frames: bool = False-) -> Dict[str, Any]:- """- Get page HTML and text content.-- Args:- instance_id (str): Browser instance ID.- include_frames (bool): Include iframe information.-- Returns:- Dict[str, Any]: Page content including HTML, text, and metadata.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- content = await dom_handler.get_page_content(tab, include_frames)-- return response_handler.handle_response(- content,- "page_content",- {"instance_id": instance_id, "include_frames": include_frames}- )--@section_tool("element-interaction")-async def take_screenshot(- instance_id: str,- full_page: bool = False,- format: str = "png",- file_path: Optional[str] = None,- quality: int = DEFAULT_QUALITY,- max_width: Optional[int] = None-) -> Union[str, ToolResult]:- """- Take a screenshot of the page.-- The image is returned as MCP image content with metadata. It is scaled- down, keeping its aspect ratio, when it is wider than max_width, longer- than 8000 pixels on a side, or too large to send inline. The metadata- reports the original size and the scale used.-- Args:- instance_id (str): Browser instance ID.- full_page (bool): Capture full page (not just viewport).- format (str): Image format ('png' or 'jpeg'). Use jpeg with a lower quality for smaller images.- file_path (Optional[str]): Save the full-resolution screenshot to this path on the server instead.- quality (int): JPEG quality from 1 to 100. PNG is lossless and ignores it.- max_width (Optional[int]): Largest width in pixels to return, for a smaller image.-- Returns:- Union[str, ToolResult]: Server file location when file_path is given, otherwise- an MCP image with format, width, height, original size, scale, quality,- byte count, and SHA-256 metadata.- """- format = validate_screenshot_options(format, quality, max_width)-- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")-- if file_path:- save_path = Path(file_path)- save_path.parent.mkdir(parents=True, exist_ok=True)- await tab.save_screenshot(save_path, format=format, full_page=full_page)- return f"Screenshot saved on the server: {str(save_path.absolute())}"-- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:- tmp_path = Path(tmp_file.name)-- try:- await tab.save_screenshot(tmp_path, format="png", full_page=full_page)- image_bytes, metadata = fit_screenshot(tmp_path, format, quality, max_width)- return ToolResult(- content=[ImageContent(- type="image",- mimeType=f"image/{format}",- data=base64.b64encode(image_bytes).decode("ascii"),- ), TextContent(type="text", text=json.dumps(metadata))],- structured_content=metadata,- )- finally:- if tmp_path.exists():- os.unlink(tmp_path)---@section_tool("network-debugging")-async def list_network_requests(- instance_id: str,- filter_type: Optional[str] = None-) -> Union[List[Dict[str, Any]], Dict[str, Any]]:- """- List captured network requests.-- Args:- instance_id (str): Browser instance ID.- filter_type (Optional[str]): Filter by resource type (e.g., 'image', 'script', 'xhr').-- Returns:- Union[List[Dict[str, Any]], Dict[str, Any]]: List of network requests, or file metadata if response too large.- """- requests = await network_interceptor.list_requests(instance_id, filter_type)- formatted_requests = [- {- "request_id": req.request_id,- "url": req.url,- "method": req.method,- "resource_type": req.resource_type,- "timestamp": req.timestamp.isoformat()- }- for req in requests- ]-- return response_handler.handle_response(formatted_requests, "network_requests")---@section_tool("network-debugging")-async def get_request_details(- request_id: str-) -> Optional[Dict[str, Any]]:- """- Get detailed information about a network request.-- Args:- request_id (str): Network request ID.-- Returns:- Optional[Dict[str, Any]]: Request details including headers, cookies, and body.- """- request = await network_interceptor.get_request(request_id)- if request:- return request.dict()- return None---@section_tool("network-debugging")-async def get_response_details(- request_id: str-) -> Optional[Dict[str, Any]]:- """- Get response details for a network request.-- Args:- request_id (str): Network request ID.-- Returns:- Optional[Dict[str, Any]]: Response details including status, headers, and metadata.- """- response = await network_interceptor.get_response(request_id)- if response:- return response.dict()- return None---@section_tool("network-debugging")-async def get_response_content(- instance_id: str,- request_id: str-) -> Optional[str]:- """- Get response body content.-- Args:- instance_id (str): Browser instance ID.- request_id (str): Network request ID.-- Returns:- Optional[str]: Response body as text (base64 encoded for binary).- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- body = await network_interceptor.get_response_body(tab, request_id)- if body:- try:- return body.decode('utf-8')- except UnicodeDecodeError:- return base64.b64encode(body).decode('utf-8')- return None---@section_tool("network-debugging")-async def search_network_requests(- instance_id: str,- url_pattern: Optional[str] = None,- method: Optional[str] = None,- status_code: Optional[int] = None,- response_contains: Optional[str] = None,- payload_contains: Optional[str] = None,- resource_type: Optional[str] = None,- limit: int = 100,- offset: int = 0,-) -> Dict[str, Any]:- """- Search network requests with advanced filters and pagination.-- Args:- instance_id (str): Browser instance ID.- url_pattern (Optional[str]): Filter by URL pattern (substring match).- method (Optional[str]): Filter by HTTP method.- status_code (Optional[int]): Filter by response status code.- response_contains (Optional[str]): Search in response body.- payload_contains (Optional[str]): Search in request payload.- resource_type (Optional[str]): Filter by resource type.- limit (int): Max results per page.- offset (int): Starting index for pagination.-- Returns:- Dict[str, Any]: Paginated results with metadata.- """- return await network_interceptor.search_requests(- instance_id,- url_pattern,- method,- status_code,- response_contains,- payload_contains,- resource_type,- limit,- offset,- )---@section_tool("network-debugging")-async def export_network_data(- instance_id: str,- filepath: str-) -> bool:- """- Export network data to JSON file.-- Args:- instance_id (str): Browser instance ID.- filepath (str): Path to save JSON file.-- Returns:- bool: True if successful.- """- return await network_interceptor.export_to_json(instance_id, filepath)---@section_tool("network-debugging")-async def import_network_data(- instance_id: str,- filepath: str-) -> bool:- """- Import network data from JSON file.-- Args:- instance_id (str): Browser instance ID.- filepath (str): Path to JSON file.-- Returns:- bool: True if successful.- """- return await network_interceptor.import_from_json(instance_id, filepath)---@section_tool("network-debugging")-async def set_network_capture_filters(- instance_id: str,- include_types: Optional[List[str]] = None,- exclude_types: Optional[List[str]] = None,-) -> bool:- """- Set resource type filters for network capture to reduce memory usage.-- Args:- instance_id (str): Browser instance ID.- include_types (Optional[List[str]]): Only capture these types (e.g., ['XHR', 'Fetch', 'Document']).- exclude_types (Optional[List[str]]): Exclude these types (e.g., ['Image', 'Stylesheet', 'Font', 'Script']).-- Common resource types: Document, Stylesheet, Image, Media, Font, Script, XHR, Fetch, WebSocket, Manifest, Other-- Returns:- bool: True if successful.- """- await network_interceptor.set_capture_filters(instance_id, include_types, exclude_types)- return True---@section_tool("network-debugging")-async def get_network_capture_filters(- instance_id: str-) -> Dict[str, List[str]]:- """- Get current network capture filters.-- Args:- instance_id (str): Browser instance ID.-- Returns:- Dict[str, List[str]]: Current filters with 'include' and 'exclude' lists.- """- return await network_interceptor.get_capture_filters(instance_id)---@section_tool("network-debugging")-async def modify_headers(- instance_id: str,- headers: Dict[str, str]-) -> bool:- """- Modify request headers for future requests.-- Args:- instance_id (str): Browser instance ID.- headers (Dict[str, str]): Headers to add/modify.-- Returns:- bool: True if modified successfully.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await network_interceptor.modify_headers(tab, headers)---@section_tool("cookies-storage")-async def get_cookies(- instance_id: str,- urls: Optional[List[str]] = None-) -> List[Dict[str, Any]]:- """- Get cookies for current page or specific URLs.-- Args:- instance_id (str): Browser instance ID.- urls (Optional[List[str]]): Optional list of URLs to get cookies for.-- Returns:- List[Dict[str, Any]]: List of cookies.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await network_interceptor.get_cookies(tab, urls)---@section_tool("cookies-storage")-async def set_cookie(- instance_id: str,- name: str,- value: str,- url: Optional[str] = None,- domain: Optional[str] = None,- path: str = "/",- secure: bool = False,- http_only: bool = False,- same_site: Optional[str] = None-) -> bool:- """- Set a cookie.-- Args:- instance_id (str): Browser instance ID.- name (str): Cookie name.- value (str): Cookie value.- url (Optional[str]): The request-URI to associate with the cookie.- domain (Optional[str]): Cookie domain.- path (str): Cookie path.- secure (bool): Secure flag.- http_only (bool): HttpOnly flag.- same_site (Optional[str]): SameSite attribute ('Strict', 'Lax', or 'None').-- Returns:- bool: True if set successfully.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")-- if not url and not domain:- current_url = tab.url if hasattr(tab, 'url') else None- if current_url:- url = current_url- else:- raise Exception("At least one of 'url' or 'domain' must be specified")-- cookie = {- "name": name,- "value": value,- "path": path,- "secure": secure,- "http_only": http_only- }- if url:- cookie["url"] = url- if domain:- cookie["domain"] = domain- if same_site:- try:- cookie["same_site"] = uc.cdp.network.CookieSameSite(same_site.capitalize())- except ValueError:- raise ValueError(f"Invalid same_site value: {same_site}. Use 'Strict', 'Lax', or 'None'.")- return await network_interceptor.set_cookie(tab, cookie)---@section_tool("cookies-storage")-async def clear_cookies(- instance_id: str,- url: Optional[str] = None-) -> bool:- """- Clear cookies.-- Args:- instance_id (str): Browser instance ID.- url (Optional[str]): Optional URL to clear cookies for (clears all if not specified).-- Returns:- bool: True if cleared successfully.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await network_interceptor.clear_cookies(tab, url)--[email protected]("browser://{instance_id}/state")-async def get_browser_state_resource(instance_id: str) -> str:- """- Get current state of a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- str: JSON string of the browser state or error message.- """- state = await browser_manager.get_page_state(instance_id)- if state:- return to_json(state, indent=2).decode()- return json.dumps({"error": "Instance not found"})--[email protected]("browser://{instance_id}/cookies")-async def get_cookies_resource(instance_id: str) -> str:- """- Get cookies for a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- str: JSON string of cookies or error message.- """- tab = await browser_manager.get_tab(instance_id)- if tab:- cookies = await network_interceptor.get_cookies(tab)- return to_json(cookies, indent=2).decode()- return json.dumps({"error": "Instance not found"})--[email protected]("browser://{instance_id}/network")-async def get_network_resource(instance_id: str) -> str:- """- Get network requests for a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- str: JSON string of network requests.- """- requests = await network_interceptor.list_requests(instance_id)- return to_json(requests, indent=2).decode()--[email protected]("browser://{instance_id}/console")-async def get_console_resource(instance_id: str) -> str:- """- Get console logs for a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- str: JSON string of console logs or error message.- """- state = await browser_manager.get_page_state(instance_id)- if state:- return json.dumps(state.console_logs, indent=2)- return json.dumps({"error": "Instance not found"})---@section_tool("debugging")-async def get_debug_view(- max_errors: int = 50,- max_warnings: int = 50,- max_info: int = 50,- include_all: bool = False-) -> Dict[str, Any]:- """- Get comprehensive debug view with all logged errors and statistics.-- Args:- max_errors (int): Maximum number of errors to include (default: 50).- max_warnings (int): Maximum number of warnings to include (default: 50).- max_info (int): Maximum number of info logs to include (default: 50).- include_all (bool): Include all logs regardless of limits (default: False).-- Returns:- Dict[str, Any]: Debug information including errors, warnings, and statistics.- """- debug_data = debug_logger.get_debug_view_paginated(- max_errors=max_errors if not include_all else None,- max_warnings=max_warnings if not include_all else None,- max_info=max_info if not include_all else None- )- return debug_data---@section_tool("debugging")-async def clear_debug_view() -> bool:- """- Clear all debug logs and statistics with timeout protection.-- Returns:- bool: True if cleared successfully.- """- try:- await asyncio.wait_for(- asyncio.to_thread(debug_logger.clear_debug_view_safe),- timeout=10.0- )- return True- except asyncio.TimeoutError:- return False---@section_tool("debugging")-async def export_debug_logs(- filename: str = "debug_log.json",- max_errors: int = 100,- max_warnings: int = 100,- max_info: int = 100,- include_all: bool = False,- format: str = "auto"-) -> str:- """- Export debug logs to a file using the fastest available method with timeout protection.-- Args:- filename (str): Name of the file to export to.- max_errors (int): Maximum number of errors to export (default: 100).- max_warnings (int): Maximum number of warnings to export (default: 100).- max_info (int): Maximum number of info logs to export (default: 100).- include_all (bool): Include all logs regardless of limits (default: False).- format (str): Export format: 'json', 'pickle', 'gzip-pickle', 'auto' (default: 'auto').- 'auto' chooses fastest format based on data size:- - Small data (<100 items): JSON (human readable)- - Medium data (100-1000 items): Pickle (fast binary)- - Large data (>1000 items): Gzip-Pickle (fastest, compressed)-- Returns:- str: Path to the exported file.- """- try:- filepath = await asyncio.wait_for(- asyncio.to_thread(- debug_logger.export_to_file_paginated,- filename,- max_errors if not include_all else None,- max_warnings if not include_all else None,- max_info if not include_all else None,- format- ),- timeout=30.0- )- return filepath- except asyncio.TimeoutError:- return "Export timeout - file too large. Try with smaller limits or 'gzip-pickle' format."---@section_tool("debugging")-async def get_debug_lock_status() -> Dict[str, Any]:- """- Get current debug logger lock status for debugging hanging exports.-- Returns:- Dict[str, Any]: Lock status information.- """- try:- return debug_logger.get_lock_status()- except Exception as e:- return {"error": str(e)}---@section_tool("tabs")-async def list_tabs(instance_id: str) -> List[Dict[str, str]]:- """- List all tabs for a browser instance.-- Args:- instance_id (str): Browser instance ID.-- Returns:- List[Dict[str, str]]: List of tabs with their details.- """- return await browser_manager.list_tabs(instance_id)---@section_tool("tabs")-async def switch_tab(- instance_id: str,- tab_id: str-) -> bool:- """- Switch to a specific tab by bringing it to front.-- Args:- instance_id (str): Browser instance ID.- tab_id (str): Target tab ID to switch to.-- Returns:- bool: True if switched successfully.- """- return await browser_manager.switch_to_tab(instance_id, tab_id)---@section_tool("tabs")-async def close_tab(- instance_id: str,- tab_id: str-) -> bool:- """- Close a specific tab.-- Args:- instance_id (str): Browser instance ID.- tab_id (str): Tab ID to close.-- Returns:- bool: True if closed successfully.- """- return await browser_manager.close_tab(instance_id, tab_id)---@section_tool("tabs")-async def get_active_tab(instance_id: str) -> Dict[str, Any]:- """- Get information about the currently active tab.-- Args:- instance_id (str): Browser instance ID.-- Returns:- Dict[str, Any]: Active tab information.- """- tab = await browser_manager.get_active_tab(instance_id)- if not tab:- return {"error": "No active tab found"}- browser = await browser_manager.get_browser(instance_id, touch_activity=False)- if browser:- try:- await browser.update_targets()- except Exception:- pass- return {- "tab_id": str(tab.target.target_id),- "url": getattr(tab, 'url', '') or '',- "title": getattr(tab.target, 'title', '') or 'Untitled',- "type": getattr(tab.target, 'type_', 'page')- }---@section_tool("tabs")-async def new_tab(- instance_id: str,- url: str = "about:blank"-) -> Dict[str, Any]:- """- Open a new tab in the browser instance.-- Args:- instance_id (str): Browser instance ID.- url (str): URL to open in the new tab.-- Returns:- Dict[str, Any]: New tab information.- """- try:- new_tab_obj = await browser_manager.open_tab(instance_id, url)- return {- "tab_id": str(new_tab_obj.target.target_id),- "url": getattr(new_tab_obj, 'url', '') or url,- "title": getattr(new_tab_obj.target, 'title', '') or 'New Tab',- "type": getattr(new_tab_obj.target, 'type_', 'page')- }- except Exception as e:- raise Exception(f"Failed to create new tab: {str(e)}")---@section_tool("element-extraction")-async def extract_element_styles(- instance_id: str,- selector: str,- include_computed: bool = True,- include_css_rules: bool = True,- include_pseudo: bool = True,- include_inheritance: bool = False-) -> Dict[str, Any]:- """- Extract complete styling information from an element.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_computed (bool): Include computed styles.- include_css_rules (bool): Include matching CSS rules.- include_pseudo (bool): Include pseudo-element styles (::before, ::after).- include_inheritance (bool): Include style inheritance chain.-- Returns:- Dict[str, Any]: Complete styling data including computed styles, CSS rules, pseudo-elements.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await element_cloner.extract_element_styles(- tab,- selector=selector,- include_computed=include_computed,- include_css_rules=include_css_rules,- include_pseudo=include_pseudo,- include_inheritance=include_inheritance- )---@section_tool("element-extraction")-async def extract_element_structure(- instance_id: str,- selector: str,- include_children: bool = False,- include_attributes: bool = True,- include_data_attributes: bool = True,- max_depth: int = 3-) -> Dict[str, Any]:- """- Extract complete HTML structure and DOM information.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_children (bool): Include child elements.- include_attributes (bool): Include all attributes.- include_data_attributes (bool): Include data-* attributes specifically.- max_depth (int): Maximum depth for children extraction.-- Returns:- Dict[str, Any]: HTML structure, attributes, position, and children data.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await element_cloner.extract_element_structure(- tab,- selector=selector,- include_children=include_children,- include_attributes=include_attributes,- include_data_attributes=include_data_attributes,- max_depth=max_depth- )---@section_tool("element-extraction")-async def extract_element_events(- instance_id: str,- selector: str,- include_inline: bool = True,- include_listeners: bool = True,- include_framework: bool = True,- analyze_handlers: bool = False-) -> Dict[str, Any]:- """- Extract complete event listener and JavaScript handler information.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_inline (bool): Include inline event handlers (onclick, etc.).- include_listeners (bool): Include addEventListener attached handlers.- include_framework (bool): Include framework-specific handlers (React, Vue, etc.).- analyze_handlers (bool): Analyze handler functions for full details (can be large).-- Returns:- Dict[str, Any]: Event listeners, inline handlers, framework handlers, detected frameworks.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await element_cloner.extract_element_events(- tab,- selector=selector,- include_inline=include_inline,- include_listeners=include_listeners,- include_framework=include_framework,- analyze_handlers=analyze_handlers- )---@section_tool("element-extraction")-async def extract_element_animations(- instance_id: str,- selector: str,- include_css_animations: bool = True,- include_transitions: bool = True,- include_transforms: bool = True,- analyze_keyframes: bool = True-) -> Dict[str, Any]:- """- Extract CSS animations, transitions, and transforms.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_css_animations (bool): Include CSS @keyframes animations.- include_transitions (bool): Include CSS transitions.- include_transforms (bool): Include CSS transforms.- analyze_keyframes (bool): Analyze keyframe rules.-- Returns:- Dict[str, Any]: Animation data, transition data, transform data, keyframe rules.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await element_cloner.extract_element_animations(- tab,- selector=selector,- include_css_animations=include_css_animations,- include_transitions=include_transitions,- include_transforms=include_transforms,- analyze_keyframes=analyze_keyframes- )---@section_tool("element-extraction")-async def extract_element_assets(- instance_id: str,- selector: str,- include_images: bool = True,- include_backgrounds: bool = True,- include_fonts: bool = True,- fetch_external: bool = False-) -> Dict[str, Any]:- """- Extract all assets related to an element (images, fonts, etc.).-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_images (bool): Include img src and related images.- include_backgrounds (bool): Include background images.- include_fonts (bool): Include font information.- fetch_external (bool): Whether to fetch external assets for analysis.-- Returns:- Dict[str, Any]: Images, background images, fonts, icons, videos, audio assets.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- result = await element_cloner.extract_element_assets(- tab,- selector=selector,- include_images=include_images,- include_backgrounds=include_backgrounds,- include_fonts=include_fonts,- fetch_external=fetch_external- )- return response_handler.handle_response(result, f"element_assets_{instance_id}_{selector.replace(' ', '_')}")---@section_tool("element-extraction")-async def extract_element_styles_cdp(- instance_id: str,- selector: str,- include_computed: bool = True,- include_css_rules: bool = True,- include_pseudo: bool = True,- include_inheritance: bool = False,-) -> Dict[str, Any]:- """- Extract element styles using direct CDP calls (no JavaScript evaluation).- This prevents hanging issues by using nodriver's native CDP methods.-- Args:- instance_id (str): Browser instance ID- selector (str): CSS selector for the element- include_computed (bool): Include computed styles- include_css_rules (bool): Include matching CSS rules- include_pseudo (bool): Include pseudo-element styles- include_inheritance (bool): Include style inheritance chain-- Returns:- Dict[str, Any]: Styling data extracted using CDP- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await element_cloner.extract_element_styles_cdp(- tab,- selector=selector,- include_computed=include_computed,- include_css_rules=include_css_rules,- include_pseudo=include_pseudo,- include_inheritance=include_inheritance- )---@section_tool("element-extraction")-async def extract_related_files(- instance_id: str,- analyze_css: bool = True,- analyze_js: bool = True,- follow_imports: bool = False,- max_depth: int = 2-) -> Dict[str, Any]:- """- Discover and analyze related CSS/JS files for context.-- Args:- instance_id (str): Browser instance ID.- analyze_css (bool): Analyze linked CSS files.- analyze_js (bool): Analyze linked JS files.- follow_imports (bool): Follow @import and module imports (uses network).- max_depth (int): Maximum depth for following imports.-- Returns:- Dict[str, Any]: Stylesheets, scripts, imports, modules, framework detection.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- result = await element_cloner.extract_related_files(- tab,- analyze_css=analyze_css,- analyze_js=analyze_js,- follow_imports=follow_imports,- max_depth=max_depth- )- return response_handler.handle_response(result, f"related_files_{instance_id}")---@section_tool("element-extraction")-async def clone_element_complete(- instance_id: str,- selector: str,- extraction_options: Optional[str] = None-) -> Dict[str, Any]:- """- Master function that extracts ALL element data using specialized functions.-- This is the ultimate element cloning tool that combines all extraction methods.- Use this when you want complete element fidelity for recreation or analysis.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- extraction_options (Optional[str]): Dict specifying what to extract and options for each.- Example: {- 'styles': {'include_computed': True, 'include_pseudo': True},- 'structure': {'include_children': True, 'max_depth': 2},- 'events': {'include_framework': True, 'analyze_handlers': False},- 'animations': {'analyze_keyframes': True},- 'assets': {'fetch_external': False},- 'related_files': {'follow_imports': True, 'max_depth': 1}- }-- Returns:- Dict[str, Any]: Complete element clone with styles, structure, events, animations, assets, related files.- """- parsed_options = None- if extraction_options:- try:- parsed_options = json.loads(extraction_options)- except json.JSONDecodeError:- raise Exception(f"Invalid JSON in extraction_options: {extraction_options}")- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- result = await comprehensive_element_cloner.extract_complete_element(- tab,- selector=selector,- include_children=parsed_options.get('structure', {}).get('include_children', True) if parsed_options else True- )-- return response_handler.handle_response(- result,- fallback_filename_prefix="complete_clone",- metadata={- "selector": selector,- "extraction_options": parsed_options,- "url": getattr(tab, 'url', 'unknown')- }- )---@section_tool("debugging")-async def hot_reload() -> str:- """- Hot reload all modules without restarting the server.-- Reloading replaces the browser manager and network interceptor, so it is- refused while browser instances are open to avoid orphaning them.-- Returns:- str: Status message.- """- global browser_manager, network_interceptor, dom_handler, debug_logger- try:- active_instances = await browser_manager.list_instances()- if active_instances:- return (- f"Hot reload refused: {len(active_instances)} browser instance(s) are open. "- "Close them first so they are not orphaned."- )- modules_to_reload = [- 'browser_manager',- 'network_interceptor',- 'dom_handler',- 'debug_logger',- 'models'- ]- reloaded_modules = []- for module_name in modules_to_reload:- if module_name in sys.modules:- importlib.reload(sys.modules[module_name])- reloaded_modules.append(module_name)- if module_name == 'browser_manager':- await browser_manager.stop_idle_reaper()- browser_manager = sys.modules['browser_manager'].BrowserManager()- _register_manager_listeners()- await browser_manager.start_idle_reaper()- elif module_name == 'network_interceptor':- network_interceptor = sys.modules['network_interceptor'].NetworkInterceptor()- elif module_name == 'dom_handler':- dom_handler = sys.modules['dom_handler'].DOMHandler()- elif module_name == 'debug_logger':- debug_logger = sys.modules['debug_logger'].debug_logger- return f"Hot reload completed. Reloaded modules: {', '.join(reloaded_modules)}"- except Exception as e:- return f"Hot reload failed: {str(e)}"---@section_tool("debugging")-async def reload_status() -> str:- """- Check the status of loaded modules.-- Returns:- str: Module status information.- """- try:- modules_info = []- modules_to_check = [- 'browser_manager',- 'network_interceptor',- 'dom_handler',- 'debug_logger',- 'models',- 'persistent_storage'- ]- for module_name in modules_to_check:- if module_name in sys.modules:- module = sys.modules[module_name]- modules_info.append(f"[loaded] {module_name}: {getattr(module, '__file__', 'built-in')}")- else:- modules_info.append(f"[missing] {module_name}: Not loaded")- return "\n".join(modules_info)- except Exception as e:- return f"Error checking module status: {str(e)}"---@section_tool("debugging")-async def validate_browser_environment_tool() -> Dict[str, Any]:- """- Validate browser environment and diagnose potential issues.-- Returns:- Dict[str, Any]: Environment validation results with platform info and recommendations- """- try:- return validate_browser_environment()- except Exception as e:- return {- "error": str(e),- "platform_info": get_platform_info(),- "is_ready": False,- "issues": [f"Validation failed: {str(e)}"],- "warnings": []- }---@section_tool("progressive-cloning")-async def clone_element_progressive(- instance_id: str,- selector: str,- include_children: bool = True-) -> Dict[str, Any]:- """- Clone element progressively - returns lightweight base structure with element_id.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_children (bool): Whether to extract child elements.-- Returns:- Dict[str, Any]: Base structure with element_id for progressive expansion.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await progressive_element_cloner.clone_element_progressive(tab, selector, include_children)---@section_tool("progressive-cloning")-async def expand_styles(- element_id: str,- categories: Optional[List[str]] = None,- properties: Optional[List[str]] = None-) -> Dict[str, Any]:- """- Expand styles data for a stored element.-- Args:- element_id (str): Element ID from clone_element_progressive().- categories (Optional[List[str]]): Style categories to include (layout, typography, colors, spacing, borders, backgrounds, effects, animation).- properties (Optional[List[str]]): Specific CSS property names to include.-- Returns:- Dict[str, Any]: Filtered styles data.- """- return progressive_element_cloner.expand_styles(element_id, categories, properties)---@section_tool("progressive-cloning")-async def expand_events(- element_id: str,- event_types: Optional[List[str]] = None-) -> Dict[str, Any]:- """- Expand event listeners data for a stored element.-- Args:- element_id (str): Element ID from clone_element_progressive().- event_types (Optional[List[str]]): Event types or sources to include (click, react, inline, addEventListener).-- Returns:- Dict[str, Any]: Filtered event listeners data.- """- return progressive_element_cloner.expand_events(element_id, event_types)---@section_tool("progressive-cloning")-async def expand_children(- element_id: str,- depth_range: Optional[List] = None,- max_count: Optional[Any] = None-) -> Dict[str, Any]:- """- Expand children data for a stored element.-- Args:- element_id (str): Element ID from clone_element_progressive().- depth_range (Optional[List]): [min_depth, max_depth] range to include.- max_count (Optional[Any]): Maximum number of children to return.-- Returns:- Dict[str, Any]: Filtered children data.- """- if isinstance(max_count, str):- try:- max_count = int(max_count) if max_count else None- except ValueError:- return {"error": f"Invalid max_count value: {max_count}"}-- if isinstance(depth_range, list):- try:- depth_range = [int(x) if isinstance(x, str) else x for x in depth_range]- except ValueError:- return {"error": f"Invalid depth_range values: {depth_range}"}-- depth_tuple = tuple(depth_range) if depth_range else None-- result = progressive_element_cloner.expand_children(element_id, depth_tuple, max_count)- return response_handler.handle_response(result, f"expand_children_{element_id}")---@section_tool("progressive-cloning")-async def expand_css_rules(- element_id: str,- source_types: Optional[List[str]] = None-) -> Dict[str, Any]:- """- Expand CSS rules data for a stored element.-- Args:- element_id (str): Element ID from clone_element_progressive().- source_types (Optional[List[str]]): CSS rule sources to include (inline, external stylesheet URLs).-- Returns:- Dict[str, Any]: Filtered CSS rules data.- """- return progressive_element_cloner.expand_css_rules(element_id, source_types)---@section_tool("progressive-cloning")-async def expand_pseudo_elements(- element_id: str-) -> Dict[str, Any]:- """- Expand pseudo-elements data for a stored element.-- Args:- element_id (str): Element ID from clone_element_progressive().-- Returns:- Dict[str, Any]: Pseudo-elements data (::before, ::after, etc.).- """- return progressive_element_cloner.expand_pseudo_elements(element_id)---@section_tool("progressive-cloning")-async def expand_animations(- element_id: str-) -> Dict[str, Any]:- """- Expand animations and fonts data for a stored element.-- Args:- element_id (str): Element ID from clone_element_progressive().-- Returns:- Dict[str, Any]: Animations, transitions, and fonts data.- """- return progressive_element_cloner.expand_animations(element_id)---@section_tool("progressive-cloning")-async def list_stored_elements() -> Dict[str, Any]:- """- List all stored elements with their basic info.-- Returns:- Dict[str, Any]: List of stored elements with metadata.- """- return progressive_element_cloner.list_stored_elements()---@section_tool("progressive-cloning")-async def clear_stored_element(- element_id: str-) -> Dict[str, Any]:- """- Clear a specific stored element.-- Args:- element_id (str): Element ID to clear.-- Returns:- Dict[str, Any]: Success/error message.- """- return progressive_element_cloner.clear_stored_element(element_id)---@section_tool("progressive-cloning")-async def clear_all_elements() -> Dict[str, Any]:- """- Clear all stored elements.-- Returns:- Dict[str, Any]: Success message.- """- return progressive_element_cloner.clear_all_elements()---@section_tool("file-extraction")-async def clone_element_to_file(- instance_id: str,- selector: str,- extraction_options: Optional[str] = None-) -> Dict[str, Any]:- """- Clone element completely and save to file, returning file path instead of full data.-- This is ideal when you want complete element data but don't want to overwhelm- the response with large JSON objects. The data is saved to a JSON file that- can be read later.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- extraction_options (Optional[str]): JSON string with extraction options.-- Returns:- Dict[str, Any]: File path and summary information about the cloned element.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- parsed_options = None- if extraction_options:- try:- parsed_options = json.loads(extraction_options)- except json.JSONDecodeError:- return {"error": "Invalid extraction_options JSON"}- return await file_based_element_cloner.clone_element_complete_to_file(- tab, selector=selector, extraction_options=parsed_options- )---@section_tool("file-extraction")-async def extract_complete_element_to_file(- instance_id: str,- selector: str,- include_children: bool = True-) -> Dict[str, Any]:- """- Extract complete element using working comprehensive cloner and save to file.-- This uses the proven comprehensive extraction logic that returns large amounts- of data, but saves it to a file instead of overwhelming the response.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_children (bool): Whether to include child elements.-- Returns:- Dict[str, Any]: File path and concise summary instead of massive data dump.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await file_based_element_cloner.extract_complete_element_to_file(- tab, selector, include_children- )---@section_tool("element-extraction")-async def extract_complete_element_cdp(- instance_id: str,- selector: str,- include_children: bool = True-) -> Dict[str, Any]:- """- Extract complete element using native CDP methods for 100% accuracy.-- This uses Chrome DevTools Protocol's native methods to extract:- - Complete computed styles via CSS.getComputedStyleForNode- - Matched CSS rules via CSS.getMatchedStylesForNode- - Event listeners via DOMDebugger.getEventListeners- - Complete DOM structure and attributes-- This provides the most accurate element cloning possible by bypassing- JavaScript limitations and using CDP's direct browser access.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_children (bool): Whether to include child elements.-- Returns:- Dict[str, Any]: Complete element data with 100% accuracy.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- cdp_cloner = CDPElementCloner()- return await cdp_cloner.extract_complete_element_cdp(tab, selector, include_children)---@section_tool("file-extraction")-async def extract_element_styles_to_file(- instance_id: str,- selector: str,- include_computed: bool = True,- include_css_rules: bool = True,- include_pseudo: bool = True,- include_inheritance: bool = False-) -> Dict[str, Any]:- """- Extract element styles and save to file, returning file path.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_computed (bool): Include computed styles.- include_css_rules (bool): Include matching CSS rules.- include_pseudo (bool): Include pseudo-element styles.- include_inheritance (bool): Include style inheritance chain.-- Returns:- Dict[str, Any]: File path and summary of extracted styles.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await file_based_element_cloner.extract_element_styles_to_file(- tab,- selector=selector,- include_computed=include_computed,- include_css_rules=include_css_rules,- include_pseudo=include_pseudo,- include_inheritance=include_inheritance- )---@section_tool("file-extraction")-async def extract_element_structure_to_file(- instance_id: str,- selector: str,- include_children: bool = False,- include_attributes: bool = True,- include_data_attributes: bool = True,- max_depth: int = 3-) -> Dict[str, Any]:- """- Extract element structure and save to file, returning file path.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_children (bool): Include child elements.- include_attributes (bool): Include all attributes.- include_data_attributes (bool): Include data-* attributes.- max_depth (int): Maximum depth for children extraction.-- Returns:- Dict[str, Any]: File path and summary of extracted structure.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await file_based_element_cloner.extract_element_structure_to_file(- tab,- selector=selector,- include_children=include_children,- include_attributes=include_attributes,- include_data_attributes=include_data_attributes,- max_depth=max_depth- )---@section_tool("file-extraction")-async def extract_element_events_to_file(- instance_id: str,- selector: str,- include_inline: bool = True,- include_listeners: bool = True,- include_framework: bool = True,- analyze_handlers: bool = True-) -> Dict[str, Any]:- """- Extract element events and save to file, returning file path.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_inline (bool): Include inline event handlers.- include_listeners (bool): Include addEventListener handlers.- include_framework (bool): Include framework-specific handlers.- analyze_handlers (bool): Analyze handler functions.-- Returns:- Dict[str, Any]: File path and summary of extracted events.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await file_based_element_cloner.extract_element_events_to_file(- tab,- selector=selector,- include_inline=include_inline,- include_listeners=include_listeners,- include_framework=include_framework,- analyze_handlers=analyze_handlers- )---@section_tool("file-extraction")-async def extract_element_animations_to_file(- instance_id: str,- selector: str,- include_css_animations: bool = True,- include_transitions: bool = True,- include_transforms: bool = True,- analyze_keyframes: bool = True-) -> Dict[str, Any]:- """- Extract element animations and save to file, returning file path.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_css_animations (bool): Include CSS animations.- include_transitions (bool): Include CSS transitions.- include_transforms (bool): Include CSS transforms.- analyze_keyframes (bool): Analyze keyframe rules.-- Returns:- Dict[str, Any]: File path and summary of extracted animations.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await file_based_element_cloner.extract_element_animations_to_file(- tab,- selector=selector,- include_css_animations=include_css_animations,- include_transitions=include_transitions,- include_transforms=include_transforms,- analyze_keyframes=analyze_keyframes- )---@section_tool("file-extraction")-async def extract_element_assets_to_file(- instance_id: str,- selector: str,- include_images: bool = True,- include_backgrounds: bool = True,- include_fonts: bool = True,- fetch_external: bool = False-) -> Dict[str, Any]:- """- Extract element assets and save to file, returning file path.-- Args:- instance_id (str): Browser instance ID.- selector (str): CSS selector for the element.- include_images (bool): Include images.- include_backgrounds (bool): Include background images.- include_fonts (bool): Include font information.- fetch_external (bool): Fetch external assets.-- Returns:- Dict[str, Any]: File path and summary of extracted assets.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- raise Exception(f"Instance not found: {instance_id}")- return await file_based_element_cloner.extract_element_assets_to_file(- tab,- selector=selector,- include_images=include_images,- include_backgrounds=include_backgrounds,- include_fonts=include_fonts,- fetch_external=fetch_external- )---@section_tool("file-extraction")-async def list_clone_files() -> List[Dict[str, Any]]:- """- List all element clone files saved to disk.-- Returns:- List[Dict[str, Any]]: List of clone files with metadata and file information.- """- return file_based_element_cloner.list_clone_files()---@section_tool("file-extraction")-async def cleanup_clone_files(- max_age_hours: int = 24-) -> Dict[str, int]:- """- Clean up old clone files to save disk space.-- Args:- max_age_hours (int): Maximum age in hours for files to keep.-- Returns:- Dict[str, int]: Number of files deleted.- """- deleted_count = file_based_element_cloner.cleanup_old_files(max_age_hours)- return {"deleted_count": deleted_count}---@section_tool("cdp-functions")-async def list_cdp_commands() -> List[str]:- """- List all available CDP Runtime commands for function execution.-- Returns:- List[str]: List of available CDP command names.- """- return await cdp_function_executor.list_cdp_commands()---@section_tool("cdp-functions")-async def execute_cdp_command(- instance_id: str,- command: str,- params: Optional[Dict[str, Any]] = None-) -> Dict[str, Any]:- """- Execute any CDP Runtime command with given parameters.-- Args:- instance_id (str): Browser instance ID.- command (str): CDP command name (e.g., 'evaluate', 'callFunctionOn').- params (Dict[str, Any], optional): Command parameters as a dictionary.- IMPORTANT: Use snake_case parameter names (e.g., 'return_by_value')- NOT camelCase ('returnByValue'). The nodriver library expects- Python-style parameter names.-- Returns:- Dict[str, Any]: Command execution result.-- Example:- # Correct - use snake_case- params = {"expression": "document.title", "return_by_value": True}-- params = {"expression": "document.title", "returnByValue": True}- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- return await cdp_function_executor.execute_cdp_command(tab, command, params or {})---@section_tool("cdp-functions")-async def add_script_to_evaluate_on_new_document(- instance_id: str,- source: str,- world_name: Optional[str] = None,- include_command_line_api: Optional[bool] = None,- run_immediately: bool = True,-) -> Dict[str, Any]:- """- Install JavaScript that runs before page scripts on each new document.-- Args:- instance_id (str): Browser instance ID.- source (str): JavaScript source to evaluate before page scripts.- world_name (Optional[str]): Isolated world name, or None for the main world.- include_command_line_api (Optional[bool]): Whether command line API is available.- run_immediately (bool): Whether to run the script in existing contexts too.-- Returns:- Dict[str, Any]: Result with success state and script identifier.- """- try:- identifier = await browser_manager.add_init_script(- instance_id,- source,- world_name=world_name,- include_command_line_api=include_command_line_api,- run_immediately=run_immediately,- )- return {"success": True, "identifier": identifier}- except Exception as e:- debug_logger.log_error("server", "add_script_to_evaluate_on_new_document", e)- return {"success": False, "error": str(e)}---@section_tool("cdp-functions")-async def get_execution_contexts(- instance_id: str-) -> List[Dict[str, Any]]:- """- Get all available JavaScript execution contexts.-- Args:- instance_id (str): Browser instance ID.-- Returns:- List[Dict[str, Any]]: List of execution contexts with their details.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return []- contexts = await cdp_function_executor.get_execution_contexts(tab)- return [- {- "id": ctx.id,- "name": ctx.name,- "origin": ctx.origin,- "unique_id": ctx.unique_id,- "aux_data": ctx.aux_data- }- for ctx in contexts- ]---@section_tool("cdp-functions")-async def discover_global_functions(- instance_id: str,- context_id: Optional[str] = None-) -> List[Dict[str, Any]]:- """- Discover all global JavaScript functions available in the page.-- Args:- instance_id (str): Browser instance ID.- context_id (str, optional): Optional execution context ID.-- Returns:- List[Dict[str, Any]]: List of discovered functions with their details.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return []- functions = await cdp_function_executor.discover_global_functions(tab, context_id)- result = [- {- "name": func.name,- "path": func.path,- "signature": func.signature,- "description": func.description- }- for func in functions- ]-- file_response = response_handler.handle_response(- result,- fallback_filename_prefix="global_functions",- metadata={- "context_id": context_id,- "function_count": len(result),- "url": getattr(tab, 'url', 'unknown')- }- )-- if isinstance(file_response, dict) and "file_path" in file_response:- return [{- "name": "LARGE_RESPONSE_SAVED_TO_FILE",- "path": "file_storage",- "signature": "automatic_file_fallback",- "description": f"Response too large ({file_response['estimated_tokens']} tokens), saved to: {file_response['filename']}"- }]-- return file_response---@section_tool("cdp-functions")-async def discover_object_methods(- instance_id: str,- object_path: str-) -> List[Dict[str, Any]]:- """- Discover methods of a specific JavaScript object.-- Args:- instance_id (str): Browser instance ID.- object_path (str): Path to the object (e.g., 'document', 'window.localStorage').-- Returns:- List[Dict[str, Any]]: List of discovered methods.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return []- methods = await cdp_function_executor.discover_object_methods(tab, object_path)- methods_data = [- {- "name": method.name,- "path": method.path,- "signature": method.signature,- "description": method.description- }- for method in methods- ]-- return response_handler.handle_response(- methods_data,- f"object_methods_{object_path.replace('.', '_')}"- )---@section_tool("cdp-functions")-async def call_javascript_function(- instance_id: str,- function_path: str,- args: Optional[List[Any]] = None-) -> Dict[str, Any]:- """- Call a JavaScript function with arguments.-- Args:- instance_id (str): Browser instance ID.- function_path (str): Full path to the function (e.g., 'document.getElementById').- args (List[Any], optional): List of arguments to pass to the function.-- Returns:- Dict[str, Any]: Function call result.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- return await cdp_function_executor.call_discovered_function(tab, function_path, args or [])---@section_tool("cdp-functions")-async def inspect_function_signature(- instance_id: str,- function_path: str-) -> Dict[str, Any]:- """- Inspect a JavaScript function's signature and details.-- Args:- instance_id (str): Browser instance ID.- function_path (str): Full path to the function.-- Returns:- Dict[str, Any]: Function signature and details.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- return await cdp_function_executor.inspect_function_signature(tab, function_path)---@section_tool("cdp-functions")-async def inject_and_execute_script(- instance_id: str,- script_code: str,- context_id: Optional[str] = None-) -> Dict[str, Any]:- """- Inject and execute custom JavaScript code.-- Args:- instance_id (str): Browser instance ID.- script_code (str): JavaScript code to execute.- context_id (str, optional): Optional execution context ID.-- Returns:- Dict[str, Any]: Script execution result.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- return await cdp_function_executor.inject_and_execute_script(tab, script_code, context_id)---@section_tool("cdp-functions")-async def create_persistent_function(- instance_id: str,- function_name: str,- function_code: str-) -> Dict[str, Any]:- """- Create a persistent JavaScript function that survives page reloads.-- Args:- instance_id (str): Browser instance ID.- function_name (str): Name for the function.- function_code (str): JavaScript function code.-- Returns:- Dict[str, Any]: Function creation result.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- try:- source = await cdp_function_executor.prepare_persistent_function(tab, function_name, function_code, instance_id)- identifier = await browser_manager.add_init_script(instance_id, source)- except Exception as e:- return {"success": False, "error": str(e), "function_name": function_name}- return {- "success": True,- "function_name": function_name,- "available_as": f"window.{function_name}",- "script_identifier": identifier,- }---@section_tool("cdp-functions")-async def execute_function_sequence(- instance_id: str,- function_calls: List[Dict[str, Any]]-) -> List[Dict[str, Any]]:- """- Execute a sequence of JavaScript function calls.-- Args:- instance_id (str): Browser instance ID.- function_calls (List[Dict[str, Any]]): List of function calls, each with 'function_path', 'args', and optional 'context_id'.-- Returns:- List[Dict[str, Any]]: List of function call results.- """- from cdp_function_executor import FunctionCall- tab = await browser_manager.get_tab(instance_id)- if not tab:- return [{"success": False, "error": f"Instance not found: {instance_id}"}]- calls = []- for call_data in function_calls:- calls.append(FunctionCall(- function_path=call_data['function_path'],- args=call_data.get('args', []),- context_id=call_data.get('context_id')- ))- return await cdp_function_executor.execute_function_sequence(tab, calls)---@section_tool("cdp-functions")-async def create_page_binding(- instance_id: str,- binding_name: str,- auto_resolve: bool = False,- auto_response: Optional[Any] = None-) -> Dict[str, Any]:- """- Expose window.<binding_name>(...args) so page JavaScript can call back to you.-- Each call returns a Promise in the page and is queued here. Read calls with- get_page_binding_calls and answer them with resolve_page_binding_call, or set- auto_resolve to answer every call immediately with auto_response. Nothing- runs on the server host. Arguments must be JSON, payloads are capped at- 64 KB, and at most 500 calls are queued per instance. The binding is- installed on every tab of the instance and survives navigation. Calls are- delivered through Runtime events.-- Args:- instance_id (str): Browser instance ID.- binding_name (str): JavaScript identifier for the function.- auto_resolve (bool): Resolve every call immediately with auto_response.- auto_response (Optional[Any]): JSON value returned to the page when auto_resolve is set.-- Returns:- Dict[str, Any]: Creation result.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- try:- return await page_bindings.create(tab, instance_id, binding_name, auto_resolve, auto_response)- except Exception as e:- return {"success": False, "error": str(e)}---@section_tool("cdp-functions")-async def get_page_binding_calls(- instance_id: str,- binding_name: Optional[str] = None,- wait_seconds: float = 0,- limit: int = 50-) -> Dict[str, Any]:- """- Read calls made by page JavaScript to page bindings.-- Pending calls stay queued until resolved. Auto-resolved calls are returned- once and then removed.-- Args:- instance_id (str): Browser instance ID.- binding_name (Optional[str]): Only return calls to this binding.- wait_seconds (float): Wait up to this many seconds (max 60) for a call when none is queued.- limit (int): Maximum number of calls to return.-- Returns:- Dict[str, Any]: Calls (call_id, binding, args, received_at, status) and binding names.- """- await browser_manager.touch_instance(instance_id)- calls = await page_bindings.get_calls(instance_id, binding_name, wait_seconds, limit)- return {"calls": calls, "bindings": page_bindings.list_bindings(instance_id)}---@section_tool("cdp-functions")-async def resolve_page_binding_call(- instance_id: str,- call_id: str,- result: Optional[Any] = None,- error: Optional[str] = None-) -> Dict[str, Any]:- """- Answer a pending page binding call.-- Args:- instance_id (str): Browser instance ID.- call_id (str): Call ID from get_page_binding_calls.- result (Optional[Any]): JSON value the page Promise resolves with.- error (Optional[str]): Reject the page Promise with this message instead.-- Returns:- Dict[str, Any]: Whether the answer reached the page.- """- await browser_manager.touch_instance(instance_id)- try:- return await page_bindings.resolve(instance_id, call_id, result, error)- except Exception as e:- return {"success": False, "error": str(e)}---@section_tool("cdp-functions")-async def remove_page_binding(- instance_id: str,- binding_name: str-) -> Dict[str, Any]:- """- Remove a page binding from every tab and reject its pending calls.-- Args:- instance_id (str): Browser instance ID.- binding_name (str): Binding to remove.-- Returns:- Dict[str, Any]: Removal result.- """- await browser_manager.touch_instance(instance_id)- try:- return await page_bindings.remove(instance_id, binding_name)- except Exception as e:- return {"success": False, "error": str(e)}---@section_tool("cdp-functions")-async def execute_python_in_browser(- instance_id: str,- python_code: str-) -> Dict[str, Any]:- """- Execute Python code by translating it to JavaScript.-- Args:- instance_id (str): Browser instance ID.- python_code (str): Python code to translate and execute.-- Returns:- Dict[str, Any]: Execution result.- """- tab = await browser_manager.get_tab(instance_id)- if not tab:- return {"success": False, "error": f"Instance not found: {instance_id}"}- return await cdp_function_executor.execute_python_in_browser(tab, python_code)---@section_tool("cdp-functions")-async def get_function_executor_info(- instance_id: Optional[str] = None-) -> Dict[str, Any]:- """- Get information about the CDP function executor state.-- Args:- instance_id (str, optional): Optional browser instance ID for specific info.-- Returns:- Dict[str, Any]: Function executor state and capabilities.- """- return await cdp_function_executor.get_function_executor_info(instance_id)---@section_tool("dynamic-hooks")-async def create_dynamic_hook(- name: str,- requirements: Dict[str, Any],- function_code: str,- instance_ids: Optional[List[str]] = None,- priority: int = 100-) -> Dict[str, Any]:- """- Create a new dynamic hook with AI-generated Python function.-- This is the new powerful hook system that allows AI to write custom Python functions- that process network requests in real-time with no pending state.-- Args:- name (str): Human-readable hook name- requirements (Dict[str, Any]): Matching criteria (url_pattern, method, resource_type, custom_condition)- function_code (str): Python function code that processes requests (must define process_request(request))- instance_ids (Optional[List[str]]): Browser instances to apply hook to (all if None)- priority (int): Hook priority (lower = higher priority)-- Returns:- Dict[str, Any]: Hook creation result with hook_id-- Example function_code:- ```python- def process_request(request):- if "example.com" in request["url"]:- return HookAction(action="redirect", url="https://httpbin.org/get")- return HookAction(action="continue")- ```- """- return await dynamic_hook_ai.create_dynamic_hook(- name=name,- requirements=requirements,- function_code=function_code,- instance_ids=instance_ids,- priority=priority- )---@section_tool("dynamic-hooks")-async def create_simple_dynamic_hook(- name: str,- url_pattern: str,- action: str,- target_url: Optional[str] = None,- custom_headers: Optional[Dict[str, str]] = None,- instance_ids: Optional[List[str]] = None-) -> Dict[str, Any]:- """- Create a simple dynamic hook using predefined templates (easier for AI).-- Args:- name (str): Hook name- url_pattern (str): URL pattern to match- action (str): Action type - 'block', 'redirect', 'add_headers', or 'log'- target_url (Optional[str]): Target URL for redirect action- custom_headers (Optional[Dict[str, str]]): Headers to add for add_headers action- instance_ids (Optional[List[str]]): Browser instances to apply hook to-- Returns:- Dict[str, Any]: Hook creation result- """- return await dynamic_hook_ai.create_simple_hook(- name=name,- url_pattern=url_pattern,- action=action,- target_url=target_url,- custom_headers=custom_headers,- instance_ids=instance_ids- )---@section_tool("dynamic-hooks")-async def list_dynamic_hooks(instance_id: Optional[str] = None) -> Dict[str, Any]:- """- List all dynamic hooks.-- Args:- instance_id (Optional[str]): Optional filter by browser instance-- Returns:- Dict[str, Any]: List of hooks with details and statistics- """- return await dynamic_hook_ai.list_dynamic_hooks(instance_id=instance_id)---@section_tool("dynamic-hooks")-async def get_dynamic_hook_details(hook_id: str) -> Dict[str, Any]:- """- Get detailed information about a specific dynamic hook.-- Args:- hook_id (str): Hook identifier-- Returns:- Dict[str, Any]: Detailed hook information including function code- """- return await dynamic_hook_ai.get_hook_details(hook_id=hook_id)---@section_tool("dynamic-hooks")-async def remove_dynamic_hook(hook_id: str) -> Dict[str, Any]:- """- Remove a dynamic hook.-- Args:- hook_id (str): Hook identifier to remove-- Returns:- Dict[str, Any]: Removal status- """- return await dynamic_hook_ai.remove_dynamic_hook(hook_id=hook_id)---@section_tool("dynamic-hooks")-def get_hook_documentation() -> Dict[str, Any]:- """- Get comprehensive documentation for creating hook functions (AI learning).-- Returns:- Dict[str, Any]: Documentation of request object structure and HookAction types- """- return dynamic_hook_ai.get_request_documentation()---@section_tool("dynamic-hooks")-def get_hook_examples() -> Dict[str, Any]:- """- Get example hook functions for AI learning.-- Returns:- Dict[str, Any]: Collection of example hook functions with explanations- """- return dynamic_hook_ai.get_hook_examples()---@section_tool("dynamic-hooks")-def get_hook_requirements_documentation() -> Dict[str, Any]:- """- Get documentation on hook requirements and matching criteria.-- Returns:- Dict[str, Any]: Requirements documentation and best practices- """- return dynamic_hook_ai.get_requirements_documentation()---@section_tool("dynamic-hooks")-def get_hook_common_patterns() -> Dict[str, Any]:- """- Get common hook patterns and use cases.-- Returns:- Dict[str, Any]: Common patterns like ad blocking, API proxying, etc.- """- return dynamic_hook_ai.get_common_patterns()---@section_tool("dynamic-hooks")-def validate_hook_function(function_code: str) -> Dict[str, Any]:- """- Validate hook function code for common issues before creating.-- Args:- function_code (str): Python function code to validate-- Returns:- Dict[str, Any]: Validation results with issues and warnings- """- return dynamic_hook_ai.validate_hook_function(function_code=function_code)---if parse_bool_env("XPOOL_SAFE_MODE", default=False):- DISABLED_SECTIONS.add("cdp-functions")- apply_disabled_sections()+"""Launch the server from a source checkout with python src/server.py."""+from stealth_browser_mcp.server import mainif __name__ == "__main__":- import argparse-- parser = argparse.ArgumentParser(- description=f"Stealth Browser MCP Server with {sum(len(tools) for tools in SECTION_TOOLS.values())} tools"- )- parser.add_argument("--transport", choices=["stdio", "http"], default="stdio",- help="Transport protocol to use")- parser.add_argument("--port", type=int, default=int(os.getenv("PORT", 8000)),- help="Port for HTTP transport")- parser.add_argument("--host", default="127.0.0.1",- help="Host for HTTP transport (use 0.0.0.0 only with an auth token on trusted networks)")-- parser.add_argument("--disable-browser-management", action="store_true",- help="Disable browser management tools (spawn, navigate, close, etc.)")- parser.add_argument("--disable-element-interaction", action="store_true",- help="Disable element interaction tools (click, type, scroll, etc.)")- parser.add_argument("--disable-element-extraction", action="store_true",- help="Disable element extraction tools (styles, structure, events, etc.)")- parser.add_argument("--disable-file-extraction", action="store_true",- help="Disable file-based extraction tools")- parser.add_argument("--disable-network-debugging", action="store_true",- help="Disable network debugging and interception tools")- parser.add_argument("--disable-cdp-functions", action="store_true",- help="Disable CDP function execution tools")- parser.add_argument("--disable-progressive-cloning", action="store_true",- help="Disable progressive element cloning tools")- parser.add_argument("--disable-cookies-storage", action="store_true",- help="Disable cookie and storage management tools")- parser.add_argument("--disable-tabs", action="store_true",- help="Disable tab management tools")- parser.add_argument("--disable-debugging", action="store_true",- help="Disable debug and system tools")- parser.add_argument("--disable-dynamic-hooks", action="store_true",- help="Disable dynamic network hook system")-- parser.add_argument("--minimal", action="store_true",- help="Enable only core browser management and element interaction (disable everything else)")- parser.add_argument("--list-sections", action="store_true",- help="List all available tool sections and exit")- parser.add_argument(- "--debug",- action="store_true",- default=DEBUG_LOGGING_ENABLED,- help="Enable debug logging to stderr",- )- parser.add_argument(- "--xpool-safe",- action="store_true",- default=parse_bool_env("XPOOL_SAFE_MODE", default=False),- help="Enable xpool-safe surface (disables cdp-functions tools that trigger Runtime.enable)",- )-- args = parser.parse_args()-- if args.debug and not debug_logger._enabled:- debug_logger.enable()-- if args.list_sections:- print("Available tool sections:")- for section, description in SECTION_DESCRIPTIONS.items():- print(f" {section}: {description} ({len(SECTION_TOOLS.get(section, []))} tools)")- print("\nUse --disable-<section-name> to disable specific sections")- print("Use --minimal to enable only core functionality")- sys.exit(0)-- if args.minimal:- DISABLED_SECTIONS.update([- "element-extraction", "file-extraction", "network-debugging",- "cdp-functions", "progressive-cloning", "cookies-storage",- "tabs", "debugging", "dynamic-hooks"- ])-- if args.disable_browser_management:- DISABLED_SECTIONS.add("browser-management")- if args.disable_element_interaction:- DISABLED_SECTIONS.add("element-interaction")- if args.disable_element_extraction:- DISABLED_SECTIONS.add("element-extraction")- if args.disable_file_extraction:- DISABLED_SECTIONS.add("file-extraction")- if args.disable_network_debugging:- DISABLED_SECTIONS.add("network-debugging")- if args.disable_cdp_functions:- DISABLED_SECTIONS.add("cdp-functions")- if args.disable_progressive_cloning:- DISABLED_SECTIONS.add("progressive-cloning")- if args.disable_cookies_storage:- DISABLED_SECTIONS.add("cookies-storage")- if args.disable_tabs:- DISABLED_SECTIONS.add("tabs")- if args.disable_debugging:- DISABLED_SECTIONS.add("debugging")- if args.disable_dynamic_hooks:- DISABLED_SECTIONS.add("dynamic-hooks")-- if args.xpool_safe:- DISABLED_SECTIONS.add("cdp-functions")-- apply_disabled_sections()-- if DISABLED_SECTIONS:- debug_logger.log_info(- "server",- "startup",- f"Disabled tool sections: {', '.join(sorted(DISABLED_SECTIONS))}",- )-- if args.transport == "http":- if HTTP_AUTH_TOKEN is None and not is_loopback_host(args.host):- print(- f"WARNING: HTTP transport is listening on {args.host} without an auth token. "- "Set STEALTH_BROWSER_MCP_AUTH_TOKEN before exposing the server beyond localhost.",- file=sys.stderr,- )- mcp.run(transport="http", host=args.host, port=args.port)- else:- install_stdio_guard()- mcp.run(transport="stdio")+ main()diff --git a/src/stealth_browser_mcp/__init__.py b/src/stealth_browser_mcp/__init__.pynew file mode 100644index 0000000..acd3c29--- /dev/null+++ b/src/stealth_browser_mcp/__init__.py@@ -0,0 +1,3 @@+"""Stealth Browser MCP, undetectable browser automation over the Model Context Protocol."""++__version__ = "0.3.0"diff --git a/src/stealth_browser_mcp/__main__.py b/src/stealth_browser_mcp/__main__.pynew file mode 100644index 0000000..7c53fbb--- /dev/null+++ b/src/stealth_browser_mcp/__main__.py@@ -0,0 +1,5 @@+"""Run the server with python -m stealth_browser_mcp."""++from .server import main++main()diff --git a/src/browser_launcher.py b/src/stealth_browser_mcp/browser_launcher.pysimilarity index 100%rename from src/browser_launcher.pyrename to src/stealth_browser_mcp/browser_launcher.pydiff --git a/src/browser_manager.py b/src/stealth_browser_mcp/browser_manager.pysimilarity index 99%rename from src/browser_manager.pyrename to src/stealth_browser_mcp/browser_manager.pyindex defc4c8..b4577c8 100644--- a/src/browser_manager.py+++ b/src/stealth_browser_mcp/browser_manager.py@@ -11,16 +11,16 @@ from datetime import datetimeimport nodriver as ucfrom nodriver import Browser, Tab-import cdp_raw-from browser_launcher import launch_browser-from debug_logger import debug_logger-from models import BrowserInstance, BrowserState, BrowserOptions, PageState-from persistent_storage import persistent_storage-from dynamic_hook_system import dynamic_hook_system-from platform_utils import get_platform_info, check_browser_executable, merge_browser_args-from process_cleanup import process_cleanup-from proxy_forwarder import AuthenticatedProxyForwarder-from proxy_utils import (+from . import cdp_raw+from .browser_launcher import launch_browser+from .debug_logger import debug_logger+from .models import BrowserInstance, BrowserState, BrowserOptions, PageState+from .persistent_storage import persistent_storage+from .dynamic_hook_system import dynamic_hook_system+from .platform_utils import get_platform_info, check_browser_executable, merge_browser_args+from .process_cleanup import process_cleanup+from .proxy_forwarder import AuthenticatedProxyForwarder+from .proxy_utils import (ProxyConfig,ProxyConfigError,merge_proxy_server_arg,diff --git a/src/cdp_element_cloner.py b/src/stealth_browser_mcp/cdp_element_cloner.pysimilarity index 99%rename from src/cdp_element_cloner.pyrename to src/stealth_browser_mcp/cdp_element_cloner.pyindex 0d6a188..cb2923e 100644--- a/src/cdp_element_cloner.py+++ b/src/stealth_browser_mcp/cdp_element_cloner.py@@ -20,10 +20,7 @@ from typing import Any, Dict, Listimport nodriver as uc-try:- from .debug_logger import debug_logger-except ImportError:- from debug_logger import debug_logger+from .debug_logger import debug_loggerclass CDPElementCloner:diff --git a/src/cdp_function_executor.py b/src/stealth_browser_mcp/cdp_function_executor.pysimilarity index 99%rename from src/cdp_function_executor.pyrename to src/stealth_browser_mcp/cdp_function_executor.pyindex 90bf75f..417fd32 100644--- a/src/cdp_function_executor.py+++ b/src/stealth_browser_mcp/cdp_function_executor.py@@ -17,7 +17,7 @@ from typing import Any, Dict, List, Optionalimport nodriver as ucfrom nodriver import Tab-from debug_logger import debug_logger+from .debug_logger import debug_loggerCONTEXT_COLLECT_SECONDS = 0.3JS_IDENTIFIER = re.compile(r"^[A-Za-z_$][A-Za-z0-9_$]{0,63}$")diff --git a/src/cdp_raw.py b/src/stealth_browser_mcp/cdp_raw.pysimilarity index 100%rename from src/cdp_raw.pyrename to src/stealth_browser_mcp/cdp_raw.pydiff --git a/src/comprehensive_element_cloner.py b/src/stealth_browser_mcp/comprehensive_element_cloner.pysimilarity index 98%rename from src/comprehensive_element_cloner.pyrename to src/stealth_browser_mcp/comprehensive_element_cloner.pyindex d1fe3ee..bc93369 100644--- a/src/comprehensive_element_cloner.py+++ b/src/stealth_browser_mcp/comprehensive_element_cloner.py@@ -19,12 +19,8 @@ import jsonimport timefrom typing import Any, Dict-try:- from .debug_logger import debug_logger- from .js_values import evaluate_to_python-except ImportError:- from debug_logger import debug_logger- from js_values import evaluate_to_python+from .debug_logger import debug_logger+from .js_values import evaluate_to_pythonclass ComprehensiveElementCloner:diff --git a/src/debug_logger.py b/src/stealth_browser_mcp/debug_logger.pysimilarity index 100%rename from src/debug_logger.pyrename to src/stealth_browser_mcp/debug_logger.pydiff --git a/src/dom_handler.py b/src/stealth_browser_mcp/dom_handler.pysimilarity index 99%rename from src/dom_handler.pyrename to src/stealth_browser_mcp/dom_handler.pyindex 662e046..5eb8397 100644--- a/src/dom_handler.py+++ b/src/stealth_browser_mcp/dom_handler.py@@ -8,12 +8,12 @@ from typing import Any, Dict, List, Optionalfrom nodriver import Tab-import input_actions-from debug_logger import debug_logger-from file_upload_security import validate_upload_paths-from js_values import evaluate_to_python-from key_definitions import MODIFIER_SHIFT, resolve_key_descriptor, resolve_modifiers-from models import ElementInfo+from . import input_actions+from .debug_logger import debug_logger+from .file_upload_security import validate_upload_paths+from .js_values import evaluate_to_python+from .key_definitions import MODIFIER_SHIFT, resolve_key_descriptor, resolve_modifiers+from .models import ElementInfoDEFAULT_ELEMENT_TIMEOUT = 10.0WAIT_POLL_INTERVAL = 0.1diff --git a/src/dynamic_hook_ai_interface.py b/src/stealth_browser_mcp/dynamic_hook_ai_interface.pysimilarity index 98%rename from src/dynamic_hook_ai_interface.pyrename to src/stealth_browser_mcp/dynamic_hook_ai_interface.pyindex 6f37957..cab70a9 100644--- a/src/dynamic_hook_ai_interface.py+++ b/src/stealth_browser_mcp/dynamic_hook_ai_interface.py@@ -6,9 +6,9 @@ about dynamic hook functions."""from typing import Dict, List, Any, Optional-from dynamic_hook_system import dynamic_hook_system-from hook_learning_system import hook_learning_system-from debug_logger import debug_logger+from .dynamic_hook_system import dynamic_hook_system+from .hook_learning_system import hook_learning_system+from .debug_logger import debug_loggerclass DynamicHookAIInterface:diff --git a/src/dynamic_hook_system.py b/src/stealth_browser_mcp/dynamic_hook_system.pysimilarity index 99%rename from src/dynamic_hook_system.pyrename to src/stealth_browser_mcp/dynamic_hook_system.pyindex 9b7cb26..e0332e9 100644--- a/src/dynamic_hook_system.py+++ b/src/stealth_browser_mcp/dynamic_hook_system.py@@ -16,7 +16,7 @@ from typing import Any, Callable, Dict, List, Optionalimport nodriver as uc-from debug_logger import debug_logger+from .debug_logger import debug_loggerHOOK_FNMATCH = types.SimpleNamespace(fnmatch=fnmatch.fnmatch, fnmatchcase=fnmatch.fnmatchcase)diff --git a/src/element_cloner.py b/src/stealth_browser_mcp/element_cloner.pysimilarity index 98%rename from src/element_cloner.pyrename to src/stealth_browser_mcp/element_cloner.pyindex 71c858a..29b67f5 100644--- a/src/element_cloner.py+++ b/src/stealth_browser_mcp/element_cloner.py@@ -4,20 +4,16 @@ import asyncioimport jsonimport reimport time-from pathlib import Path+from importlib import resourcesfrom typing import Any, Dict, Optionalfrom urllib.parse import urljoin, urlparseimport requests-try:- from .debug_logger import debug_logger- from .js_values import evaluate_to_python-except ImportError:- from debug_logger import debug_logger- from js_values import evaluate_to_python+from .debug_logger import debug_logger+from .js_values import evaluate_to_python-JS_DIR = Path(__file__).parent / "js"+JS_DIR = resources.files(__package__) / "js"PLACEHOLDER_PATTERN = re.compile(r"\$([A-Z_]+)\$")@@ -87,7 +83,7 @@ class ElementCloner:str: JavaScript source ready to evaluate."""js_file = JS_DIR / filename- if not js_file.exists():+ if not js_file.is_file():raise FileNotFoundError(f"JavaScript file not found: {js_file}")js_code = js_file.read_text(encoding='utf-8')literals = {name.upper(): json.dumps(value) for name, value in values.items()}diff --git a/src/file_based_element_cloner.py b/src/stealth_browser_mcp/file_based_element_cloner.pysimilarity index 97%rename from src/file_based_element_cloner.pyrename to src/stealth_browser_mcp/file_based_element_cloner.pyindex 53905e5..22b4576 100644--- a/src/file_based_element_cloner.py+++ b/src/stealth_browser_mcp/file_based_element_cloner.py@@ -5,14 +5,10 @@ from datetime import datetimefrom pathlib import Pathfrom typing import Any, Callable, Dict, List, Optional-try:- from .comprehensive_element_cloner import comprehensive_element_cloner- from .debug_logger import debug_logger- from .element_cloner import element_cloner-except ImportError:- from comprehensive_element_cloner import comprehensive_element_cloner- from debug_logger import debug_logger- from element_cloner import element_cloner+from .comprehensive_element_cloner import comprehensive_element_cloner+from .debug_logger import debug_logger+from .element_cloner import element_cloner+from .paths import clone_output_dirclass FileBasedElementCloner:@@ -24,11 +20,12 @@ class FileBasedElementCloner:Args:output_dir (Optional[str]): Directory to save clone files.- Defaults to <project_root>/element_clones (absolute path,- avoids startup failures when the host process uses a read-only cwd).+ Defaults to element_clones in the data directory, an absolute+ path that avoids startup failures when the host process uses a+ read-only cwd."""if output_dir is None:- self.output_dir = Path(__file__).resolve().parent.parent / "element_clones"+ self.output_dir = clone_output_dir()else:self.output_dir = Path(output_dir)self.output_dir.mkdir(parents=True, exist_ok=True)diff --git a/src/file_upload_security.py b/src/stealth_browser_mcp/file_upload_security.pysimilarity index 97%rename from src/file_upload_security.pyrename to src/stealth_browser_mcp/file_upload_security.pyindex bcd26b1..3268b87 100644--- a/src/file_upload_security.py+++ b/src/stealth_browser_mcp/file_upload_security.py@@ -4,9 +4,9 @@ import osfrom pathlib import Pathfrom typing import List, Optional, Sequence+from .paths import default_upload_rootFILE_UPLOAD_ALLOWED_DIRS_ENV = "BROWSER_FILE_UPLOAD_ALLOWED_DIRS"-DEFAULT_FILE_UPLOAD_ROOT = Path(__file__).resolve().parent.parentdef _split_configured_roots(config_value: Optional[str]) -> Optional[List[str]]:@@ -51,7 +51,7 @@ def get_allowed_upload_roots() -> List[Path]:List[Path]: Resolved directories that file_upload may read from."""configured_roots = _split_configured_roots(os.getenv(FILE_UPLOAD_ALLOWED_DIRS_ENV))- root_values = configured_roots if configured_roots is not None else [str(DEFAULT_FILE_UPLOAD_ROOT)]+ root_values = configured_roots if configured_roots is not None else [str(default_upload_root())]return [_resolve_upload_root(root_value) for root_value in root_values]diff --git a/src/hook_learning_system.py b/src/stealth_browser_mcp/hook_learning_system.pysimilarity index 100%rename from src/hook_learning_system.pyrename to src/stealth_browser_mcp/hook_learning_system.pydiff --git a/src/http_security.py b/src/stealth_browser_mcp/http_security.pysimilarity index 100%rename from src/http_security.pyrename to src/stealth_browser_mcp/http_security.pydiff --git a/src/input_actions.py b/src/stealth_browser_mcp/input_actions.pysimilarity index 99%rename from src/input_actions.pyrename to src/stealth_browser_mcp/input_actions.pyindex 6fcad23..90214da 100644--- a/src/input_actions.py+++ b/src/stealth_browser_mcp/input_actions.py@@ -7,7 +7,7 @@ from typing import Any, Dict, Optional, Tuplefrom nodriver import Tab, cdp-from key_definitions import resolve_key_descriptor+from .key_definitions import resolve_key_descriptorSHIFTED_SYMBOLS = set('~!@#$%^&*()_+{}|:"<>?')SHIFT_KEY = {"key": "Shift", "code": "ShiftLeft", "virtual_key_code": 16}diff --git a/src/js/extract_animations.js b/src/stealth_browser_mcp/js/extract_animations.jssimilarity index 100%rename from src/js/extract_animations.jsrename to src/stealth_browser_mcp/js/extract_animations.jsdiff --git a/src/js/extract_assets.js b/src/stealth_browser_mcp/js/extract_assets.jssimilarity index 100%rename from src/js/extract_assets.jsrename to src/stealth_browser_mcp/js/extract_assets.jsdiff --git a/src/js/extract_events.js b/src/stealth_browser_mcp/js/extract_events.jssimilarity index 100%rename from src/js/extract_events.jsrename to src/stealth_browser_mcp/js/extract_events.jsdiff --git a/src/js/extract_related_files.js b/src/stealth_browser_mcp/js/extract_related_files.jssimilarity index 100%rename from src/js/extract_related_files.jsrename to src/stealth_browser_mcp/js/extract_related_files.jsdiff --git a/src/js/extract_structure.js b/src/stealth_browser_mcp/js/extract_structure.jssimilarity index 100%rename from src/js/extract_structure.jsrename to src/stealth_browser_mcp/js/extract_structure.jsdiff --git a/src/js_values.py b/src/stealth_browser_mcp/js_values.pysimilarity index 100%rename from src/js_values.pyrename to src/stealth_browser_mcp/js_values.pydiff --git a/src/key_definitions.py b/src/stealth_browser_mcp/key_definitions.pysimilarity index 100%rename from src/key_definitions.pyrename to src/stealth_browser_mcp/key_definitions.pydiff --git a/src/models.py b/src/stealth_browser_mcp/models.pysimilarity index 100%rename from src/models.pyrename to src/stealth_browser_mcp/models.pydiff --git a/src/network_interceptor.py b/src/stealth_browser_mcp/network_interceptor.pysimilarity index 99%rename from src/network_interceptor.pyrename to src/stealth_browser_mcp/network_interceptor.pyindex 968dcdf..5969273 100644--- a/src/network_interceptor.py+++ b/src/stealth_browser_mcp/network_interceptor.py@@ -11,9 +11,9 @@ from typing import Any, Coroutine, Dict, List, Optional, Setimport nodriver as ucfrom nodriver import Tab-import cdp_raw-from debug_logger import debug_logger-from models import NetworkRequest, NetworkResponse+from . import cdp_raw+from .debug_logger import debug_logger+from .models import NetworkRequest, NetworkResponseMAX_REQUESTS_PER_INSTANCE = 1000MAX_CAPTURED_BODY_BYTES = 2 * 1024 * 1024diff --git a/src/page_bindings.py b/src/stealth_browser_mcp/page_bindings.pysimilarity index 99%rename from src/page_bindings.pyrename to src/stealth_browser_mcp/page_bindings.pyindex 6b50936..837326f 100644--- a/src/page_bindings.py+++ b/src/stealth_browser_mcp/page_bindings.py@@ -20,7 +20,7 @@ from typing import Any, Dict, List, Optional, Tupleimport nodriver as ucfrom nodriver import Tab-from debug_logger import debug_logger+from .debug_logger import debug_loggerBINDING_NAME = re.compile(r"^[A-Za-z_$][A-Za-z0-9_$]{0,63}$")MAX_PAYLOAD_BYTES = 64 * 1024diff --git a/src/stealth_browser_mcp/paths.py b/src/stealth_browser_mcp/paths.pynew file mode 100644index 0000000..d9e955b--- /dev/null+++ b/src/stealth_browser_mcp/paths.py@@ -0,0 +1,85 @@+"""Default locations for files the server reads and writes."""++import os+import sys+from pathlib import Path+from typing import Optional++APP_NAME = "stealth-browser-mcp"+DATA_DIR_ENV = "STEALTH_BROWSER_MCP_DATA_DIR"+PACKAGE_DIR = Path(__file__).resolve().parent+++def source_checkout_root() -> Optional[Path]:+ """+ Return the repository root when running from a source checkout.++ Returns:+ Optional[Path]: Repository root, or None for an installed package.+ """+ root = PACKAGE_DIR.parent.parent+ if PACKAGE_DIR.parent.name == "src" and (root / "pyproject.toml").is_file():+ return root+ return None+++def user_data_dir() -> Path:+ """+ Return the per-user data directory for this platform.++ Returns:+ Path: LOCALAPPDATA on Windows, Application Support on macOS, and+ XDG_DATA_HOME or ~/.local/share elsewhere, joined with the app name.+ """+ if sys.platform == "win32":+ base = Path(os.environ.get("LOCALAPPDATA") or Path.home() / "AppData" / "Local")+ elif sys.platform == "darwin":+ base = Path.home() / "Library" / "Application Support"+ else:+ base = Path(os.environ.get("XDG_DATA_HOME") or Path.home() / ".local" / "share")+ return base / APP_NAME+++def data_dir() -> Path:+ """+ Return the directory for generated files such as element clones.++ STEALTH_BROWSER_MCP_DATA_DIR wins when set. A source checkout uses the+ repository root, and an installed package uses the per-user data directory.++ Returns:+ Path: Absolute data directory.+ """+ configured = os.getenv(DATA_DIR_ENV, "").strip()+ if configured:+ return Path(configured).expanduser().resolve()+ return source_checkout_root() or user_data_dir()+++def clone_output_dir() -> Path:+ """+ Return the directory for element clone and large response files.++ Returns:+ Path: data_dir() joined with element_clones.+ """+ return data_dir() / "element_clones"+++def default_upload_root() -> Path:+ """+ Return the default directory that file_upload may read from.++ A source checkout keeps the repository root. An installed package uses an+ uploads folder in the data directory, created on first use, so the default+ never points at the interpreter's install location.++ Returns:+ Path: Existing upload root directory.+ """+ root = source_checkout_root()+ if root is not None:+ return root+ uploads = data_dir() / "uploads"+ uploads.mkdir(parents=True, exist_ok=True)+ return uploadsdiff --git a/src/persistent_storage.py b/src/stealth_browser_mcp/persistent_storage.pysimilarity index 100%rename from src/persistent_storage.pyrename to src/stealth_browser_mcp/persistent_storage.pydiff --git a/src/platform_utils.py b/src/stealth_browser_mcp/platform_utils.pysimilarity index 100%rename from src/platform_utils.pyrename to src/stealth_browser_mcp/platform_utils.pydiff --git a/src/process_cleanup.py b/src/stealth_browser_mcp/process_cleanup.pysimilarity index 99%rename from src/process_cleanup.pyrename to src/stealth_browser_mcp/process_cleanup.pyindex 90f620f..474198f 100644--- a/src/process_cleanup.py+++ b/src/stealth_browser_mcp/process_cleanup.py@@ -15,7 +15,7 @@ from typing import Any, Callable, Dict, Optional, Set, TypeVarimport psutil-from debug_logger import debug_logger+from .debug_logger import debug_loggerF = TypeVar("F", bound=Callable[..., Any])diff --git a/src/progressive_element_cloner.py b/src/stealth_browser_mcp/progressive_element_cloner.pysimilarity index 97%rename from src/progressive_element_cloner.pyrename to src/stealth_browser_mcp/progressive_element_cloner.pyindex 8f9d722..b085f62 100644--- a/src/progressive_element_cloner.py+++ b/src/stealth_browser_mcp/progressive_element_cloner.py@@ -10,14 +10,9 @@ import timeimport uuidfrom typing import Any, Dict, List, Optional, Tuple-try:- from .comprehensive_element_cloner import comprehensive_element_cloner- from .debug_logger import debug_logger- from .persistent_storage import persistent_storage-except ImportError:- from comprehensive_element_cloner import comprehensive_element_cloner- from debug_logger import debug_logger- from persistent_storage import persistent_storage+from .comprehensive_element_cloner import comprehensive_element_cloner+from .debug_logger import debug_logger+from .persistent_storage import persistent_storageMAX_STORED_ELEMENTS = 50STYLE_CATEGORIES = {diff --git a/src/proxy_forwarder.py b/src/stealth_browser_mcp/proxy_forwarder.pysimilarity index 99%rename from src/proxy_forwarder.pyrename to src/stealth_browser_mcp/proxy_forwarder.pyindex 4cd7491..d8b66da 100644--- a/src/proxy_forwarder.py+++ b/src/stealth_browser_mcp/proxy_forwarder.py@@ -11,7 +11,7 @@ from struct import calcsize, error as struct_error, pack, unpackfrom typing import Optionalfrom urllib.parse import unquote, urlparse-from debug_logger import debug_logger+from .debug_logger import debug_loggerDROPPED_HEADERS = ("proxy-authorization:",)HOP_BY_HOP_HEADERS = ("connection:", "proxy-connection:", "keep-alive:")diff --git a/src/proxy_utils.py b/src/stealth_browser_mcp/proxy_utils.pysimilarity index 100%rename from src/proxy_utils.pyrename to src/stealth_browser_mcp/proxy_utils.pydiff --git a/src/response_handler.py b/src/stealth_browser_mcp/response_handler.pysimilarity index 97%rename from src/response_handler.pyrename to src/stealth_browser_mcp/response_handler.pyindex cf5d708..ffe053f 100644--- a/src/response_handler.py+++ b/src/stealth_browser_mcp/response_handler.py@@ -7,6 +7,8 @@ from datetime import datetimefrom pathlib import Pathfrom typing import Any, Dict, Optional+from .paths import clone_output_dir+UNSAFE_FILENAME_CHARS = re.compile(r"[^\w.-]+")MAX_PREFIX_LENGTH = 80@@ -24,7 +26,7 @@ class ResponseHandler:"""self.max_tokens = max_tokensif clone_dir is None:- self.clone_dir = Path(__file__).resolve().parent.parent / "element_clones"+ self.clone_dir = clone_output_dir()else:self.clone_dir = Path(clone_dir)self.clone_dir.mkdir(parents=True, exist_ok=True)diff --git a/src/screenshot_encoder.py b/src/stealth_browser_mcp/screenshot_encoder.pysimilarity index 100%rename from src/screenshot_encoder.pyrename to src/stealth_browser_mcp/screenshot_encoder.pydiff --git a/src/stealth_browser_mcp/server.py b/src/stealth_browser_mcp/server.pynew file mode 100644index 0000000..dcc9066--- /dev/null+++ b/src/stealth_browser_mcp/server.py@@ -0,0 +1,3137 @@+"""Main MCP server for browser automation."""++import argparse+import asyncio+import base64+import importlib+import json+import ipaddress+import os+import sys+import tempfile+from collections import defaultdict+from contextlib import asynccontextmanager+from pathlib import Path+from typing import Any, Dict, List, Optional, Union++import nodriver as uc+from fastmcp import FastMCP+from fastmcp.tools.tool import ToolResult+from mcp.types import ImageContent, TextContent+from pydantic_core import to_json++from .browser_manager import BrowserManager+from .cdp_element_cloner import CDPElementCloner+from .cdp_function_executor import CDPFunctionExecutor+from .comprehensive_element_cloner import comprehensive_element_cloner+from .debug_logger import debug_logger+from .dom_handler import DOMHandler+from .element_cloner import element_cloner+from .file_based_element_cloner import file_based_element_cloner+from .http_security import (+ create_http_auth_provider,+ get_http_auth_token,+)+from .models import BrowserOptions+from .network_interceptor import NetworkInterceptor+from .page_bindings import page_bindings+from .dynamic_hook_ai_interface import dynamic_hook_ai+from .persistent_storage import persistent_storage+from .progressive_element_cloner import progressive_element_cloner+from .response_handler import response_handler+from .screenshot_encoder import DEFAULT_QUALITY, fit_screenshot, validate_options as validate_screenshot_options+from .stdio_guard import install_stdio_guard+from .platform_utils import validate_browser_environment, get_platform_info+from .process_cleanup import process_cleanup++DISABLED_SECTIONS = set()+SECTION_TOOLS: Dict[str, List[str]] = defaultdict(list)+SECTION_DESCRIPTIONS = {+ "browser-management": "Core browser operations",+ "element-interaction": "Page interaction and element manipulation",+ "element-extraction": "Element cloning and extraction",+ "file-extraction": "File-based extraction tools",+ "network-debugging": "Network monitoring and interception",+ "cdp-functions": "Chrome DevTools Protocol function execution",+ "progressive-cloning": "Advanced element cloning system",+ "cookies-storage": "Cookie and storage management",+ "tabs": "Tab management",+ "debugging": "Debug and system tools",+ "dynamic-hooks": "AI-powered network hook system",+}+++def parse_bool_env(name: str, default: bool = False) -> bool:+ value = os.getenv(name)+ if value is None:+ return default+ return value.strip().lower() in {"1", "true", "yes", "on", "enabled"}+++DEBUG_LOGGING_ENABLED = (+ parse_bool_env("STEALTH_BROWSER_DEBUG", default=False)+ or parse_bool_env("DEBUG", default=False)+)+HTTP_AUTH_TOKEN = get_http_auth_token()++def is_loopback_host(host: str) -> bool:+ """+ Check whether an HTTP bind host only accepts local connections.++ Args:+ host (str): Host name or IP address passed to --host.++ Returns:+ bool: True for localhost and loopback addresses.+ """+ if host.lower() == "localhost":+ return True+ try:+ return ipaddress.ip_address(host).is_loopback+ except ValueError:+ return False+++def section_tool(section: str):+ """Decorator that registers tools and tracks section membership."""+ def decorator(func):+ SECTION_TOOLS[section].append(func.__name__)+ return mcp.tool(func)+ return decorator+++def apply_disabled_sections() -> None:+ """Apply section disable rules by unregistering tools from FastMCP."""+ for section in sorted(DISABLED_SECTIONS):+ for tool_name in SECTION_TOOLS.get(section, []):+ try:+ mcp.remove_tool(tool_name)+ except Exception:+ # Tool may already be removed by another section policy.+ continue++@asynccontextmanager+async def app_lifespan(server):+ """+ Manage application lifecycle with proper cleanup.++ Args:+ server (Any): The server instance for which the lifespan is being managed.+ """+ debug_logger.log_info("server", "startup", "Starting Browser Automation MCP Server...")+ try:+ await browser_manager.start_idle_reaper()+ yield+ finally:+ debug_logger.log_info("server", "shutdown", "Shutting down Browser Automation MCP Server...")+ try:+ await browser_manager.stop_idle_reaper()+ except Exception as e:+ debug_logger.log_error("server", "cleanup", e)+ try:+ await browser_manager.close_all()+ debug_logger.log_info("server", "cleanup", "All browser instances closed")+ except Exception as e:+ debug_logger.log_error("server", "cleanup", e)++ try:+ process_cleanup._cleanup_all_tracked()+ debug_logger.log_info("server", "cleanup", "Process cleanup complete")+ except Exception as e:+ debug_logger.log_error("server", "cleanup", f"Process cleanup failed: {e}")+ try:+ persistent_instances = persistent_storage.list_instances()+ if persistent_instances.get("instances"):+ debug_logger.log_info(+ "server",+ "storage_cleanup",+ f"Clearing in-memory storage with {len(persistent_instances['instances'])} instances...",+ )+ persistent_storage.clear_all()+ debug_logger.log_info("server", "storage_cleanup", "In-memory storage cleared")+ except Exception as e:+ debug_logger.log_error("server", "storage_cleanup", e)+ debug_logger.log_info("server", "shutdown", "Browser Automation MCP Server shutdown complete")++mcp = FastMCP(+ name="Browser Automation MCP",+ instructions="""+ This MCP server provides undetectable browser automation using nodriver (CDP-based).++ Key features:+ - Spawn and manage multiple browser instances+ - Navigate and interact with web pages+ - Query and manipulate DOM elements+ - Intercept and analyze network traffic+ - Execute JavaScript in page context+ - Manage cookies and storage++ Instances are built to avoid common bot detection, with trusted input+ events and a clean headless fingerprint. Measured results against public+ detection pages are published in STEALTH_TESTS.md.+ """,+ auth=create_http_auth_provider(HTTP_AUTH_TOKEN),+ lifespan=app_lifespan,+)++browser_manager = BrowserManager()+network_interceptor = NetworkInterceptor()+dom_handler = DOMHandler()+cdp_function_executor = CDPFunctionExecutor()+++async def _prepare_tab(tab, instance_id: str, options: BrowserOptions) -> None:+ """+ Attach network capture to a tab an instance uses.++ Args:+ tab (Any): Browser tab.+ instance_id (str): Browser instance ID.+ options (BrowserOptions): Spawn options.+ """+ await network_interceptor.setup_interception(tab, instance_id, options.block_resources)+++async def _forget_instance(instance_id: str) -> None:+ """+ Drop per-instance state held outside the browser manager.++ Args:+ instance_id (str): Closed browser instance ID.+ """+ await network_interceptor.clear_instance_data(instance_id)+ cdp_function_executor.forget_instance(instance_id)+++def _register_manager_listeners() -> None:+ """Connect tab preparation and close cleanup to the current browser manager."""+ browser_manager.add_tab_listener(_prepare_tab)+ browser_manager.add_tab_listener(page_bindings.prepare_tab)+ browser_manager.add_close_listener(_forget_instance)+ browser_manager.add_close_listener(page_bindings.forget_instance)+++_register_manager_listeners()++if DEBUG_LOGGING_ENABLED:+ debug_logger.enable()++@section_tool("browser-management")+async def spawn_browser(+ headless: bool = False,+ user_agent: Optional[str] = None,+ viewport_width: int = 1920,+ viewport_height: int = 1080,+ proxy: Optional[str] = None,+ browser_args: Optional[List[str]] = None,+ timezone_id: Optional[str] = None,+ idle_timeout_seconds: Optional[int] = None,+ block_resources: Optional[List[str]] = None,+ extra_headers: Optional[Dict[str, str]] = None,+ user_data_dir: Optional[str] = None,+ sandbox: Optional[Any] = None+) -> Dict[str, Any]:+ """+ Spawn a new browser instance.++ Args:+ headless (bool): Run in headless mode.+ user_agent (Optional[str]): Custom user agent string.+ viewport_width (int): Viewport width in pixels.+ viewport_height (int): Viewport height in pixels.+ proxy (Optional[str]): Proxy server URL.+ browser_args (List[str]): Additional browser launch args.+ timezone_id (Optional[str]): IANA timezone ID applied via CDP timezone override.+ idle_timeout_seconds (Optional[int]): Idle timeout override in seconds for automatic instance cleanup.+ block_resources (List[str]): List of resource types to block (e.g., ['image', 'font', 'stylesheet']).+ extra_headers (Dict[str, str]): Additional HTTP headers.+ user_data_dir (Optional[str]): Path to user data directory for persistent sessions.+ sandbox (Optional[Any]): Enable browser sandbox. Accepts bool, string ('true'/'false'), int (1/0), or None for auto-detect.++ Returns:+ Dict[str, Any]: Instance information including instance_id.+ """+ try:+ from .platform_utils import sandbox_must_be_disabled++ if sandbox is None:+ sandbox = not sandbox_must_be_disabled()+ elif isinstance(sandbox, str):+ sandbox = sandbox.lower() in ('true', '1', 'yes', 'on', 'enabled')+ elif isinstance(sandbox, int):+ sandbox = bool(sandbox)+ elif not isinstance(sandbox, bool):+ sandbox = bool(sandbox)++ options = BrowserOptions(+ headless=headless,+ user_agent=user_agent,+ viewport_width=viewport_width,+ viewport_height=viewport_height,+ proxy=proxy,+ browser_args=browser_args or [],+ timezone_id=timezone_id,+ idle_timeout_seconds=idle_timeout_seconds,+ block_resources=block_resources or [],+ extra_headers=extra_headers or {},+ user_data_dir=user_data_dir,+ sandbox=sandbox+ )+ instance = await browser_manager.spawn_browser(options)+ spawn_diagnostics = await browser_manager.get_spawn_diagnostics(instance.instance_id)+ return {+ "instance_id": instance.instance_id,+ "state": instance.state,+ "headless": instance.headless,+ "viewport": instance.viewport,+ "spawn_diagnostics": spawn_diagnostics or {},+ }+ except Exception as e:+ message = str(e)+ if not message.startswith("Failed to spawn browser"):+ message = f"Failed to spawn browser: {message}"+ raise Exception(message) from e++@section_tool("browser-management")+async def list_instances() -> List[Dict[str, Any]]:+ """+ List all active browser instances.++ Returns:+ List[Dict[str, Any]]: List of browser instances with their current state.+ """+ memory_instances = await browser_manager.list_instances()+ storage_instances = persistent_storage.list_instances()+ result = []+ for inst in memory_instances:+ result.append({+ "instance_id": inst.instance_id,+ "state": inst.state,+ "current_url": inst.current_url,+ "title": inst.title,+ "source": "active"+ })+ memory_ids = {inst.instance_id for inst in memory_instances}+ for instance_id, inst_data in storage_instances.get("instances", {}).items():+ if instance_id not in memory_ids:+ result.append({+ "instance_id": inst_data["instance_id"],+ "state": inst_data["state"] + " (stored)",+ "current_url": inst_data["current_url"],+ "title": inst_data["title"],+ "source": "stored"+ })+ return result++@section_tool("browser-management")+async def close_instance(instance_id: str) -> bool:+ """+ Close a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ bool: True if closed successfully.+ """+ return await browser_manager.close_instance(instance_id)++@section_tool("browser-management")+async def get_instance_state(instance_id: str) -> Optional[Dict[str, Any]]:+ """+ Get detailed state of a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ Optional[Dict[str, Any]]: Complete state information.+ """+ state = await browser_manager.get_page_state(instance_id)+ if state:+ return state.dict()+ return None++@section_tool("browser-management")+async def navigate(+ instance_id: str,+ url: str,+ wait_until: str = "load",+ timeout: int = 30000,+ referrer: Optional[str] = None+) -> Dict[str, Any]:+ """+ Navigate to a URL.++ Args:+ instance_id (str): Browser instance ID.+ url (str): URL to navigate to.+ wait_until (str): Wait condition - 'load', 'domcontentloaded', or 'networkidle'.+ timeout (int): Navigation timeout in milliseconds.+ referrer (Optional[str]): Referrer URL.++ Returns:+ Dict[str, Any]: Navigation result with final URL and title.+ """+ if isinstance(timeout, str):+ timeout = int(timeout)+ return await browser_manager.navigate(+ instance_id=instance_id,+ url=url,+ wait_until=wait_until,+ timeout=timeout,+ referrer=referrer,+ )++@section_tool("browser-management")+async def go_back(instance_id: str) -> bool:+ """+ Navigate back in history.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ bool: True if navigation was successful.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ await tab.back()+ return True++@section_tool("browser-management")+async def go_forward(instance_id: str) -> bool:+ """+ Navigate forward in history.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ bool: True if navigation was successful.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ await tab.forward()+ return True++@section_tool("browser-management")+async def reload_page(instance_id: str, ignore_cache: bool = False) -> bool:+ """+ Reload the current page.++ Args:+ instance_id (str): Browser instance ID.+ ignore_cache (bool): Whether to ignore cache when reloading.++ Returns:+ bool: True if reload was successful.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ await tab.reload(ignore_cache=ignore_cache)+ return True++@section_tool("element-interaction")+async def query_elements(+ instance_id: str,+ selector: str,+ text_filter: Optional[str] = None,+ visible_only: bool = True,+ limit: Optional[Any] = None+) -> List[Dict[str, Any]]:+ """+ Query DOM elements.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector or XPath (starts with '//').+ text_filter (Optional[str]): Filter by text content.+ visible_only (bool): Only return visible elements.+ limit (Optional[Any]): Maximum number of elements to return.++ Returns:+ List[Dict[str, Any]]: List of matching elements with their properties.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ debug_logger.log_info('Server', 'query_elements', f'Received limit parameter: {limit} (type: {type(limit)})')+ elements = await dom_handler.query_elements(+ tab, selector, text_filter, visible_only, limit+ )+ debug_logger.log_info('Server', 'query_elements', f'DOM handler returned {len(elements)} elements')+ result = []+ for i, elem in enumerate(elements):+ try:+ if hasattr(elem, 'model_dump'):+ elem_dict = elem.model_dump()+ else:+ elem_dict = elem.dict()+ result.append(elem_dict)+ debug_logger.log_info('Server', 'query_elements', f'Converted element {i+1} to dict: {list(elem_dict.keys())}')+ except Exception as e:+ debug_logger.log_error('Server', 'query_elements', e, {'element_index': i})+ debug_logger.log_info('Server', 'query_elements', f'Returning {len(result)} results to MCP client')+ return result if result else []++@section_tool("element-interaction")+async def click_element(+ instance_id: str,+ selector: str,+ text_match: Optional[str] = None,+ timeout: int = 10000+) -> bool:+ """+ Click an element.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector or XPath.+ text_match (Optional[str]): Click element with matching text.+ timeout (int): Timeout in milliseconds.++ Returns:+ bool: True if clicked successfully.+ """+ if isinstance(timeout, str):+ timeout = int(timeout)+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.click_element(tab, selector, text_match, timeout)++@section_tool("element-interaction")+async def type_text(+ instance_id: str,+ selector: str,+ text: str,+ clear_first: bool = True,+ delay_ms: int = 50,+ parse_newlines: bool = False,+ shift_enter: bool = False+) -> bool:+ """+ Type text into an input field with trusted keyboard events.++ Every character produces keydown, keypress, input, and keyup events.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector.+ text (str): Text to type.+ clear_first (bool): Clear field before typing.+ delay_ms (int): Delay between keystrokes in milliseconds.+ parse_newlines (bool): If True, press Enter for each \n instead of inserting a line break.+ shift_enter (bool): If True, press Shift+Enter for each \n (for chat apps).++ Returns:+ bool: True if typed successfully.+ """+ if isinstance(delay_ms, str):+ delay_ms = int(delay_ms)+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.type_text(tab, selector, text, clear_first, delay_ms, parse_newlines, shift_enter)++@section_tool("element-interaction")+async def paste_text(+ instance_id: str,+ selector: str,+ text: str,+ clear_first: bool = True+) -> bool:+ """+ Paste text instantly into an input field.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector or XPath.+ text (str): Text to paste.+ clear_first (bool): Clear field before pasting.++ Returns:+ bool: True if pasted successfully.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.paste_text(tab, selector, text, clear_first)++@section_tool("element-interaction")+async def press_key(+ instance_id: str,+ key: str,+ modifiers: Optional[List[str]] = None,+ selector: Optional[str] = None,+ count: int = 1,+ delay_ms: int = 50+) -> Dict[str, Any]:+ """+ Press a real key using trusted CDP key events.++ Sends Input.dispatchKeyEvent keyDown/keyUp pairs, so the page sees trusted+ events. Use this for control keys such as Enter, Tab, Escape, and the+ arrow keys in React-select, typeahead, and combobox widgets.++ Args:+ instance_id (str): Browser instance ID.+ key (str): DOM KeyboardEvent.key name (Enter, Tab, Escape, ArrowDown, ArrowUp, ArrowLeft, ArrowRight, Backspace, Delete, Home, End, PageUp, PageDown, Space, F1-F12) or a single printable character.+ modifiers (Optional[List[str]]): Modifier keys to hold: Shift, Control (Ctrl), Alt, Meta (Cmd).+ selector (Optional[str]): CSS selector to focus before pressing. Presses on the focused element when omitted.+ count (int): Number of times to press the key.+ delay_ms (int): Delay between presses in milliseconds.++ Returns:+ Dict[str, Any]: { success: True, key: str, count: int, modifiers: [str, ...] }+ """+ if isinstance(count, str):+ count = int(count)+ if isinstance(delay_ms, str):+ delay_ms = int(delay_ms)+ if isinstance(modifiers, str):+ modifiers = [modifiers]+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.press_key(tab, key, modifiers, selector, count, delay_ms)++@section_tool("element-interaction")+async def file_upload(+ instance_id: str,+ selector: str,+ paths: List[str]+) -> Dict[str, Any]:+ """+ Upload allowed local files to a <input type="file"> element via CDP DOM.setFileInputFiles.++ Resolves the actual file input even if the selector matches a wrapper element+ (Workday/Ashby/LinkedIn often hide the real input behind a styled button).+ Refuses multi-file upload when the input lacks the `multiple` attribute.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the file input or any ancestor that contains it.+ paths (List[str]): Absolute allowlisted local file paths to upload.++ Returns:+ Dict[str, Any]: { success: True, count: N, files: [basename, ...] }+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.file_upload(tab, selector, paths)++@section_tool("element-interaction")+async def select_option(+ instance_id: str,+ selector: str,+ value: Optional[str] = None,+ text: Optional[str] = None,+ index: Optional[Any] = None+) -> bool:+ """+ Select an option from a dropdown.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the select element.+ value (Optional[str]): Option value attribute.+ text (Optional[str]): Option text content.+ index (Optional[Any]): Option index (0-based). Can be string or int.++ Returns:+ bool: True if selected successfully.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")++ converted_index = None+ if index is not None:+ try:+ converted_index = int(index)+ except (ValueError, TypeError):+ raise Exception(f"Invalid index value: {index}. Must be a number.")++ return await dom_handler.select_option(tab, selector, value, text, converted_index)++@section_tool("element-interaction")+async def get_element_state(+ instance_id: str,+ selector: str+) -> Dict[str, Any]:+ """+ Get complete state of an element.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector or XPath.++ Returns:+ Dict[str, Any]: Element state including attributes, style, position, etc.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.get_element_state(tab, selector)++@section_tool("element-interaction")+async def wait_for_element(+ instance_id: str,+ selector: str,+ timeout: int = 30000,+ visible: bool = True,+ text_content: Optional[str] = None+) -> bool:+ """+ Wait for an element to appear.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector or XPath.+ timeout (int): Timeout in milliseconds.+ visible (bool): Wait for element to be visible.+ text_content (Optional[str]): Wait for specific text content.++ Returns:+ bool: True if element found.+ """+ if isinstance(timeout, str):+ timeout = int(timeout)+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.wait_for_element(tab, selector, timeout, visible, text_content)++@section_tool("element-interaction")+async def scroll_page(+ instance_id: str,+ direction: str = "down",+ amount: int = 500,+ smooth: bool = True+) -> bool:+ """+ Scroll the page.++ Args:+ instance_id (str): Browser instance ID.+ direction (str): 'down', 'up', 'left', 'right', 'top', or 'bottom'.+ amount (int): Pixels to scroll (ignored for 'top' and 'bottom').+ smooth (bool): Use smooth scrolling.++ Returns:+ bool: True if scrolled successfully.+ """+ if isinstance(amount, str):+ amount = int(amount)+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.scroll_page(tab, direction, amount, smooth)++@section_tool("element-interaction")+async def execute_script(+ instance_id: str,+ script: str,+ args: Optional[List[Any]] = None+) -> Dict[str, Any]:+ """+ Execute JavaScript in page context.++ Args:+ instance_id (str): Browser instance ID.+ script (str): JavaScript code to execute.+ args (Optional[List[Any]]): Arguments to pass to the script.++ Returns:+ Dict[str, Any]: Script execution result.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ try:+ result = await dom_handler.execute_script(tab, script, args)+ bounded = response_handler.handle_response(result, "script_result")+ return {+ "success": True,+ "result": bounded,+ "error": None+ }+ except Exception as e:+ return {+ "success": False,+ "result": None,+ "error": str(e)+ }++@section_tool("element-interaction")+async def get_page_content(+ instance_id: str,+ include_frames: bool = False+) -> Dict[str, Any]:+ """+ Get page HTML and text content.++ Args:+ instance_id (str): Browser instance ID.+ include_frames (bool): Include iframe information.++ Returns:+ Dict[str, Any]: Page content including HTML, text, and metadata.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ content = await dom_handler.get_page_content(tab, include_frames)++ return response_handler.handle_response(+ content,+ "page_content",+ {"instance_id": instance_id, "include_frames": include_frames}+ )++@section_tool("element-interaction")+async def take_screenshot(+ instance_id: str,+ full_page: bool = False,+ format: str = "png",+ file_path: Optional[str] = None,+ quality: int = DEFAULT_QUALITY,+ max_width: Optional[int] = None+) -> Union[str, ToolResult]:+ """+ Take a screenshot of the page.++ The image is returned as MCP image content with metadata. It is scaled+ down, keeping its aspect ratio, when it is wider than max_width, longer+ than 8000 pixels on a side, or too large to send inline. The metadata+ reports the original size and the scale used.++ Args:+ instance_id (str): Browser instance ID.+ full_page (bool): Capture full page (not just viewport).+ format (str): Image format ('png' or 'jpeg'). Use jpeg with a lower quality for smaller images.+ file_path (Optional[str]): Save the full-resolution screenshot to this path on the server instead.+ quality (int): JPEG quality from 1 to 100. PNG is lossless and ignores it.+ max_width (Optional[int]): Largest width in pixels to return, for a smaller image.++ Returns:+ Union[str, ToolResult]: Server file location when file_path is given, otherwise+ an MCP image with format, width, height, original size, scale, quality,+ byte count, and SHA-256 metadata.+ """+ format = validate_screenshot_options(format, quality, max_width)++ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")++ if file_path:+ save_path = Path(file_path)+ save_path.parent.mkdir(parents=True, exist_ok=True)+ await tab.save_screenshot(save_path, format=format, full_page=full_page)+ return f"Screenshot saved on the server: {str(save_path.absolute())}"++ with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:+ tmp_path = Path(tmp_file.name)++ try:+ await tab.save_screenshot(tmp_path, format="png", full_page=full_page)+ image_bytes, metadata = fit_screenshot(tmp_path, format, quality, max_width)+ return ToolResult(+ content=[ImageContent(+ type="image",+ mimeType=f"image/{format}",+ data=base64.b64encode(image_bytes).decode("ascii"),+ ), TextContent(type="text", text=json.dumps(metadata))],+ structured_content=metadata,+ )+ finally:+ if tmp_path.exists():+ os.unlink(tmp_path)+++@section_tool("network-debugging")+async def list_network_requests(+ instance_id: str,+ filter_type: Optional[str] = None+) -> Union[List[Dict[str, Any]], Dict[str, Any]]:+ """+ List captured network requests.++ Args:+ instance_id (str): Browser instance ID.+ filter_type (Optional[str]): Filter by resource type (e.g., 'image', 'script', 'xhr').++ Returns:+ Union[List[Dict[str, Any]], Dict[str, Any]]: List of network requests, or file metadata if response too large.+ """+ requests = await network_interceptor.list_requests(instance_id, filter_type)+ formatted_requests = [+ {+ "request_id": req.request_id,+ "url": req.url,+ "method": req.method,+ "resource_type": req.resource_type,+ "timestamp": req.timestamp.isoformat()+ }+ for req in requests+ ]++ return response_handler.handle_response(formatted_requests, "network_requests")+++@section_tool("network-debugging")+async def get_request_details(+ request_id: str+) -> Optional[Dict[str, Any]]:+ """+ Get detailed information about a network request.++ Args:+ request_id (str): Network request ID.++ Returns:+ Optional[Dict[str, Any]]: Request details including headers, cookies, and body.+ """+ request = await network_interceptor.get_request(request_id)+ if request:+ return request.dict()+ return None+++@section_tool("network-debugging")+async def get_response_details(+ request_id: str+) -> Optional[Dict[str, Any]]:+ """+ Get response details for a network request.++ Args:+ request_id (str): Network request ID.++ Returns:+ Optional[Dict[str, Any]]: Response details including status, headers, and metadata.+ """+ response = await network_interceptor.get_response(request_id)+ if response:+ return response.dict()+ return None+++@section_tool("network-debugging")+async def get_response_content(+ instance_id: str,+ request_id: str+) -> Optional[str]:+ """+ Get response body content.++ Args:+ instance_id (str): Browser instance ID.+ request_id (str): Network request ID.++ Returns:+ Optional[str]: Response body as text (base64 encoded for binary).+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ body = await network_interceptor.get_response_body(tab, request_id)+ if body:+ try:+ return body.decode('utf-8')+ except UnicodeDecodeError:+ return base64.b64encode(body).decode('utf-8')+ return None+++@section_tool("network-debugging")+async def search_network_requests(+ instance_id: str,+ url_pattern: Optional[str] = None,+ method: Optional[str] = None,+ status_code: Optional[int] = None,+ response_contains: Optional[str] = None,+ payload_contains: Optional[str] = None,+ resource_type: Optional[str] = None,+ limit: int = 100,+ offset: int = 0,+) -> Dict[str, Any]:+ """+ Search network requests with advanced filters and pagination.++ Args:+ instance_id (str): Browser instance ID.+ url_pattern (Optional[str]): Filter by URL pattern (substring match).+ method (Optional[str]): Filter by HTTP method.+ status_code (Optional[int]): Filter by response status code.+ response_contains (Optional[str]): Search in response body.+ payload_contains (Optional[str]): Search in request payload.+ resource_type (Optional[str]): Filter by resource type.+ limit (int): Max results per page.+ offset (int): Starting index for pagination.++ Returns:+ Dict[str, Any]: Paginated results with metadata.+ """+ return await network_interceptor.search_requests(+ instance_id,+ url_pattern,+ method,+ status_code,+ response_contains,+ payload_contains,+ resource_type,+ limit,+ offset,+ )+++@section_tool("network-debugging")+async def export_network_data(+ instance_id: str,+ filepath: str+) -> bool:+ """+ Export network data to JSON file.++ Args:+ instance_id (str): Browser instance ID.+ filepath (str): Path to save JSON file.++ Returns:+ bool: True if successful.+ """+ return await network_interceptor.export_to_json(instance_id, filepath)+++@section_tool("network-debugging")+async def import_network_data(+ instance_id: str,+ filepath: str+) -> bool:+ """+ Import network data from JSON file.++ Args:+ instance_id (str): Browser instance ID.+ filepath (str): Path to JSON file.++ Returns:+ bool: True if successful.+ """+ return await network_interceptor.import_from_json(instance_id, filepath)+++@section_tool("network-debugging")+async def set_network_capture_filters(+ instance_id: str,+ include_types: Optional[List[str]] = None,+ exclude_types: Optional[List[str]] = None,+) -> bool:+ """+ Set resource type filters for network capture to reduce memory usage.++ Args:+ instance_id (str): Browser instance ID.+ include_types (Optional[List[str]]): Only capture these types (e.g., ['XHR', 'Fetch', 'Document']).+ exclude_types (Optional[List[str]]): Exclude these types (e.g., ['Image', 'Stylesheet', 'Font', 'Script']).++ Common resource types: Document, Stylesheet, Image, Media, Font, Script, XHR, Fetch, WebSocket, Manifest, Other++ Returns:+ bool: True if successful.+ """+ await network_interceptor.set_capture_filters(instance_id, include_types, exclude_types)+ return True+++@section_tool("network-debugging")+async def get_network_capture_filters(+ instance_id: str+) -> Dict[str, List[str]]:+ """+ Get current network capture filters.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ Dict[str, List[str]]: Current filters with 'include' and 'exclude' lists.+ """+ return await network_interceptor.get_capture_filters(instance_id)+++@section_tool("network-debugging")+async def modify_headers(+ instance_id: str,+ headers: Dict[str, str]+) -> bool:+ """+ Modify request headers for future requests.++ Args:+ instance_id (str): Browser instance ID.+ headers (Dict[str, str]): Headers to add/modify.++ Returns:+ bool: True if modified successfully.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await network_interceptor.modify_headers(tab, headers)+++@section_tool("cookies-storage")+async def get_cookies(+ instance_id: str,+ urls: Optional[List[str]] = None+) -> List[Dict[str, Any]]:+ """+ Get cookies for current page or specific URLs.++ Args:+ instance_id (str): Browser instance ID.+ urls (Optional[List[str]]): Optional list of URLs to get cookies for.++ Returns:+ List[Dict[str, Any]]: List of cookies.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await network_interceptor.get_cookies(tab, urls)+++@section_tool("cookies-storage")+async def set_cookie(+ instance_id: str,+ name: str,+ value: str,+ url: Optional[str] = None,+ domain: Optional[str] = None,+ path: str = "/",+ secure: bool = False,+ http_only: bool = False,+ same_site: Optional[str] = None+) -> bool:+ """+ Set a cookie.++ Args:+ instance_id (str): Browser instance ID.+ name (str): Cookie name.+ value (str): Cookie value.+ url (Optional[str]): The request-URI to associate with the cookie.+ domain (Optional[str]): Cookie domain.+ path (str): Cookie path.+ secure (bool): Secure flag.+ http_only (bool): HttpOnly flag.+ same_site (Optional[str]): SameSite attribute ('Strict', 'Lax', or 'None').++ Returns:+ bool: True if set successfully.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")++ if not url and not domain:+ current_url = tab.url if hasattr(tab, 'url') else None+ if current_url:+ url = current_url+ else:+ raise Exception("At least one of 'url' or 'domain' must be specified")++ cookie = {+ "name": name,+ "value": value,+ "path": path,+ "secure": secure,+ "http_only": http_only+ }+ if url:+ cookie["url"] = url+ if domain:+ cookie["domain"] = domain+ if same_site:+ try:+ cookie["same_site"] = uc.cdp.network.CookieSameSite(same_site.capitalize())+ except ValueError:+ raise ValueError(f"Invalid same_site value: {same_site}. Use 'Strict', 'Lax', or 'None'.")+ return await network_interceptor.set_cookie(tab, cookie)+++@section_tool("cookies-storage")+async def clear_cookies(+ instance_id: str,+ url: Optional[str] = None+) -> bool:+ """+ Clear cookies.++ Args:+ instance_id (str): Browser instance ID.+ url (Optional[str]): Optional URL to clear cookies for (clears all if not specified).++ Returns:+ bool: True if cleared successfully.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await network_interceptor.clear_cookies(tab, url)++[email protected]("browser://{instance_id}/state")+async def get_browser_state_resource(instance_id: str) -> str:+ """+ Get current state of a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ str: JSON string of the browser state or error message.+ """+ state = await browser_manager.get_page_state(instance_id)+ if state:+ return to_json(state, indent=2).decode()+ return json.dumps({"error": "Instance not found"})++[email protected]("browser://{instance_id}/cookies")+async def get_cookies_resource(instance_id: str) -> str:+ """+ Get cookies for a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ str: JSON string of cookies or error message.+ """+ tab = await browser_manager.get_tab(instance_id)+ if tab:+ cookies = await network_interceptor.get_cookies(tab)+ return to_json(cookies, indent=2).decode()+ return json.dumps({"error": "Instance not found"})++[email protected]("browser://{instance_id}/network")+async def get_network_resource(instance_id: str) -> str:+ """+ Get network requests for a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ str: JSON string of network requests.+ """+ requests = await network_interceptor.list_requests(instance_id)+ return to_json(requests, indent=2).decode()++[email protected]("browser://{instance_id}/console")+async def get_console_resource(instance_id: str) -> str:+ """+ Get console logs for a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ str: JSON string of console logs or error message.+ """+ state = await browser_manager.get_page_state(instance_id)+ if state:+ return json.dumps(state.console_logs, indent=2)+ return json.dumps({"error": "Instance not found"})+++@section_tool("debugging")+async def get_debug_view(+ max_errors: int = 50,+ max_warnings: int = 50,+ max_info: int = 50,+ include_all: bool = False+) -> Dict[str, Any]:+ """+ Get comprehensive debug view with all logged errors and statistics.++ Args:+ max_errors (int): Maximum number of errors to include (default: 50).+ max_warnings (int): Maximum number of warnings to include (default: 50).+ max_info (int): Maximum number of info logs to include (default: 50).+ include_all (bool): Include all logs regardless of limits (default: False).++ Returns:+ Dict[str, Any]: Debug information including errors, warnings, and statistics.+ """+ debug_data = debug_logger.get_debug_view_paginated(+ max_errors=max_errors if not include_all else None,+ max_warnings=max_warnings if not include_all else None,+ max_info=max_info if not include_all else None+ )+ return debug_data+++@section_tool("debugging")+async def clear_debug_view() -> bool:+ """+ Clear all debug logs and statistics with timeout protection.++ Returns:+ bool: True if cleared successfully.+ """+ try:+ await asyncio.wait_for(+ asyncio.to_thread(debug_logger.clear_debug_view_safe),+ timeout=10.0+ )+ return True+ except asyncio.TimeoutError:+ return False+++@section_tool("debugging")+async def export_debug_logs(+ filename: str = "debug_log.json",+ max_errors: int = 100,+ max_warnings: int = 100,+ max_info: int = 100,+ include_all: bool = False,+ format: str = "auto"+) -> str:+ """+ Export debug logs to a file using the fastest available method with timeout protection.++ Args:+ filename (str): Name of the file to export to.+ max_errors (int): Maximum number of errors to export (default: 100).+ max_warnings (int): Maximum number of warnings to export (default: 100).+ max_info (int): Maximum number of info logs to export (default: 100).+ include_all (bool): Include all logs regardless of limits (default: False).+ format (str): Export format: 'json', 'pickle', 'gzip-pickle', 'auto' (default: 'auto').+ 'auto' chooses fastest format based on data size:+ - Small data (<100 items): JSON (human readable)+ - Medium data (100-1000 items): Pickle (fast binary)+ - Large data (>1000 items): Gzip-Pickle (fastest, compressed)++ Returns:+ str: Path to the exported file.+ """+ try:+ filepath = await asyncio.wait_for(+ asyncio.to_thread(+ debug_logger.export_to_file_paginated,+ filename,+ max_errors if not include_all else None,+ max_warnings if not include_all else None,+ max_info if not include_all else None,+ format+ ),+ timeout=30.0+ )+ return filepath+ except asyncio.TimeoutError:+ return "Export timeout - file too large. Try with smaller limits or 'gzip-pickle' format."+++@section_tool("debugging")+async def get_debug_lock_status() -> Dict[str, Any]:+ """+ Get current debug logger lock status for debugging hanging exports.++ Returns:+ Dict[str, Any]: Lock status information.+ """+ try:+ return debug_logger.get_lock_status()+ except Exception as e:+ return {"error": str(e)}+++@section_tool("tabs")+async def list_tabs(instance_id: str) -> List[Dict[str, str]]:+ """+ List all tabs for a browser instance.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ List[Dict[str, str]]: List of tabs with their details.+ """+ return await browser_manager.list_tabs(instance_id)+++@section_tool("tabs")+async def switch_tab(+ instance_id: str,+ tab_id: str+) -> bool:+ """+ Switch to a specific tab by bringing it to front.++ Args:+ instance_id (str): Browser instance ID.+ tab_id (str): Target tab ID to switch to.++ Returns:+ bool: True if switched successfully.+ """+ return await browser_manager.switch_to_tab(instance_id, tab_id)+++@section_tool("tabs")+async def close_tab(+ instance_id: str,+ tab_id: str+) -> bool:+ """+ Close a specific tab.++ Args:+ instance_id (str): Browser instance ID.+ tab_id (str): Tab ID to close.++ Returns:+ bool: True if closed successfully.+ """+ return await browser_manager.close_tab(instance_id, tab_id)+++@section_tool("tabs")+async def get_active_tab(instance_id: str) -> Dict[str, Any]:+ """+ Get information about the currently active tab.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ Dict[str, Any]: Active tab information.+ """+ tab = await browser_manager.get_active_tab(instance_id)+ if not tab:+ return {"error": "No active tab found"}+ browser = await browser_manager.get_browser(instance_id, touch_activity=False)+ if browser:+ try:+ await browser.update_targets()+ except Exception:+ pass+ return {+ "tab_id": str(tab.target.target_id),+ "url": getattr(tab, 'url', '') or '',+ "title": getattr(tab.target, 'title', '') or 'Untitled',+ "type": getattr(tab.target, 'type_', 'page')+ }+++@section_tool("tabs")+async def new_tab(+ instance_id: str,+ url: str = "about:blank"+) -> Dict[str, Any]:+ """+ Open a new tab in the browser instance.++ Args:+ instance_id (str): Browser instance ID.+ url (str): URL to open in the new tab.++ Returns:+ Dict[str, Any]: New tab information.+ """+ try:+ new_tab_obj = await browser_manager.open_tab(instance_id, url)+ return {+ "tab_id": str(new_tab_obj.target.target_id),+ "url": getattr(new_tab_obj, 'url', '') or url,+ "title": getattr(new_tab_obj.target, 'title', '') or 'New Tab',+ "type": getattr(new_tab_obj.target, 'type_', 'page')+ }+ except Exception as e:+ raise Exception(f"Failed to create new tab: {str(e)}")+++@section_tool("element-extraction")+async def extract_element_styles(+ instance_id: str,+ selector: str,+ include_computed: bool = True,+ include_css_rules: bool = True,+ include_pseudo: bool = True,+ include_inheritance: bool = False+) -> Dict[str, Any]:+ """+ Extract complete styling information from an element.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_computed (bool): Include computed styles.+ include_css_rules (bool): Include matching CSS rules.+ include_pseudo (bool): Include pseudo-element styles (::before, ::after).+ include_inheritance (bool): Include style inheritance chain.++ Returns:+ Dict[str, Any]: Complete styling data including computed styles, CSS rules, pseudo-elements.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await element_cloner.extract_element_styles(+ tab,+ selector=selector,+ include_computed=include_computed,+ include_css_rules=include_css_rules,+ include_pseudo=include_pseudo,+ include_inheritance=include_inheritance+ )+++@section_tool("element-extraction")+async def extract_element_structure(+ instance_id: str,+ selector: str,+ include_children: bool = False,+ include_attributes: bool = True,+ include_data_attributes: bool = True,+ max_depth: int = 3+) -> Dict[str, Any]:+ """+ Extract complete HTML structure and DOM information.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_children (bool): Include child elements.+ include_attributes (bool): Include all attributes.+ include_data_attributes (bool): Include data-* attributes specifically.+ max_depth (int): Maximum depth for children extraction.++ Returns:+ Dict[str, Any]: HTML structure, attributes, position, and children data.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await element_cloner.extract_element_structure(+ tab,+ selector=selector,+ include_children=include_children,+ include_attributes=include_attributes,+ include_data_attributes=include_data_attributes,+ max_depth=max_depth+ )+++@section_tool("element-extraction")+async def extract_element_events(+ instance_id: str,+ selector: str,+ include_inline: bool = True,+ include_listeners: bool = True,+ include_framework: bool = True,+ analyze_handlers: bool = False+) -> Dict[str, Any]:+ """+ Extract complete event listener and JavaScript handler information.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_inline (bool): Include inline event handlers (onclick, etc.).+ include_listeners (bool): Include addEventListener attached handlers.+ include_framework (bool): Include framework-specific handlers (React, Vue, etc.).+ analyze_handlers (bool): Analyze handler functions for full details (can be large).++ Returns:+ Dict[str, Any]: Event listeners, inline handlers, framework handlers, detected frameworks.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await element_cloner.extract_element_events(+ tab,+ selector=selector,+ include_inline=include_inline,+ include_listeners=include_listeners,+ include_framework=include_framework,+ analyze_handlers=analyze_handlers+ )+++@section_tool("element-extraction")+async def extract_element_animations(+ instance_id: str,+ selector: str,+ include_css_animations: bool = True,+ include_transitions: bool = True,+ include_transforms: bool = True,+ analyze_keyframes: bool = True+) -> Dict[str, Any]:+ """+ Extract CSS animations, transitions, and transforms.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_css_animations (bool): Include CSS @keyframes animations.+ include_transitions (bool): Include CSS transitions.+ include_transforms (bool): Include CSS transforms.+ analyze_keyframes (bool): Analyze keyframe rules.++ Returns:+ Dict[str, Any]: Animation data, transition data, transform data, keyframe rules.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await element_cloner.extract_element_animations(+ tab,+ selector=selector,+ include_css_animations=include_css_animations,+ include_transitions=include_transitions,+ include_transforms=include_transforms,+ analyze_keyframes=analyze_keyframes+ )+++@section_tool("element-extraction")+async def extract_element_assets(+ instance_id: str,+ selector: str,+ include_images: bool = True,+ include_backgrounds: bool = True,+ include_fonts: bool = True,+ fetch_external: bool = False+) -> Dict[str, Any]:+ """+ Extract all assets related to an element (images, fonts, etc.).++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_images (bool): Include img src and related images.+ include_backgrounds (bool): Include background images.+ include_fonts (bool): Include font information.+ fetch_external (bool): Whether to fetch external assets for analysis.++ Returns:+ Dict[str, Any]: Images, background images, fonts, icons, videos, audio assets.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ result = await element_cloner.extract_element_assets(+ tab,+ selector=selector,+ include_images=include_images,+ include_backgrounds=include_backgrounds,+ include_fonts=include_fonts,+ fetch_external=fetch_external+ )+ return response_handler.handle_response(result, f"element_assets_{instance_id}_{selector.replace(' ', '_')}")+++@section_tool("element-extraction")+async def extract_element_styles_cdp(+ instance_id: str,+ selector: str,+ include_computed: bool = True,+ include_css_rules: bool = True,+ include_pseudo: bool = True,+ include_inheritance: bool = False,+) -> Dict[str, Any]:+ """+ Extract element styles using direct CDP calls (no JavaScript evaluation).+ This prevents hanging issues by using nodriver's native CDP methods.++ Args:+ instance_id (str): Browser instance ID+ selector (str): CSS selector for the element+ include_computed (bool): Include computed styles+ include_css_rules (bool): Include matching CSS rules+ include_pseudo (bool): Include pseudo-element styles+ include_inheritance (bool): Include style inheritance chain++ Returns:+ Dict[str, Any]: Styling data extracted using CDP+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await element_cloner.extract_element_styles_cdp(+ tab,+ selector=selector,+ include_computed=include_computed,+ include_css_rules=include_css_rules,+ include_pseudo=include_pseudo,+ include_inheritance=include_inheritance+ )+++@section_tool("element-extraction")+async def extract_related_files(+ instance_id: str,+ analyze_css: bool = True,+ analyze_js: bool = True,+ follow_imports: bool = False,+ max_depth: int = 2+) -> Dict[str, Any]:+ """+ Discover and analyze related CSS/JS files for context.++ Args:+ instance_id (str): Browser instance ID.+ analyze_css (bool): Analyze linked CSS files.+ analyze_js (bool): Analyze linked JS files.+ follow_imports (bool): Follow @import and module imports (uses network).+ max_depth (int): Maximum depth for following imports.++ Returns:+ Dict[str, Any]: Stylesheets, scripts, imports, modules, framework detection.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ result = await element_cloner.extract_related_files(+ tab,+ analyze_css=analyze_css,+ analyze_js=analyze_js,+ follow_imports=follow_imports,+ max_depth=max_depth+ )+ return response_handler.handle_response(result, f"related_files_{instance_id}")+++@section_tool("element-extraction")+async def clone_element_complete(+ instance_id: str,+ selector: str,+ extraction_options: Optional[str] = None+) -> Dict[str, Any]:+ """+ Master function that extracts ALL element data using specialized functions.++ This is the ultimate element cloning tool that combines all extraction methods.+ Use this when you want complete element fidelity for recreation or analysis.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ extraction_options (Optional[str]): Dict specifying what to extract and options for each.+ Example: {+ 'styles': {'include_computed': True, 'include_pseudo': True},+ 'structure': {'include_children': True, 'max_depth': 2},+ 'events': {'include_framework': True, 'analyze_handlers': False},+ 'animations': {'analyze_keyframes': True},+ 'assets': {'fetch_external': False},+ 'related_files': {'follow_imports': True, 'max_depth': 1}+ }++ Returns:+ Dict[str, Any]: Complete element clone with styles, structure, events, animations, assets, related files.+ """+ parsed_options = None+ if extraction_options:+ try:+ parsed_options = json.loads(extraction_options)+ except json.JSONDecodeError:+ raise Exception(f"Invalid JSON in extraction_options: {extraction_options}")+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ result = await comprehensive_element_cloner.extract_complete_element(+ tab,+ selector=selector,+ include_children=parsed_options.get('structure', {}).get('include_children', True) if parsed_options else True+ )++ return response_handler.handle_response(+ result,+ fallback_filename_prefix="complete_clone",+ metadata={+ "selector": selector,+ "extraction_options": parsed_options,+ "url": getattr(tab, 'url', 'unknown')+ }+ )+++@section_tool("debugging")+async def hot_reload() -> str:+ """+ Hot reload all modules without restarting the server.++ Reloading replaces the browser manager and network interceptor, so it is+ refused while browser instances are open to avoid orphaning them.++ Returns:+ str: Status message.+ """+ global browser_manager, network_interceptor, dom_handler, debug_logger+ try:+ active_instances = await browser_manager.list_instances()+ if active_instances:+ return (+ f"Hot reload refused: {len(active_instances)} browser instance(s) are open. "+ "Close them first so they are not orphaned."+ )+ modules_to_reload = [+ 'browser_manager',+ 'network_interceptor',+ 'dom_handler',+ 'debug_logger',+ 'models'+ ]+ reloaded_modules = []+ for module_name in modules_to_reload:+ qualified_name = f"{__package__}.{module_name}"+ if qualified_name in sys.modules:+ module = importlib.reload(sys.modules[qualified_name])+ reloaded_modules.append(module_name)+ if module_name == 'browser_manager':+ await browser_manager.stop_idle_reaper()+ browser_manager = module.BrowserManager()+ _register_manager_listeners()+ await browser_manager.start_idle_reaper()+ elif module_name == 'network_interceptor':+ network_interceptor = module.NetworkInterceptor()+ elif module_name == 'dom_handler':+ dom_handler = module.DOMHandler()+ elif module_name == 'debug_logger':+ debug_logger = module.debug_logger+ return f"Hot reload completed. Reloaded modules: {', '.join(reloaded_modules)}"+ except Exception as e:+ return f"Hot reload failed: {str(e)}"+++@section_tool("debugging")+async def reload_status() -> str:+ """+ Check the status of loaded modules.++ Returns:+ str: Module status information.+ """+ try:+ modules_info = []+ modules_to_check = [+ 'browser_manager',+ 'network_interceptor',+ 'dom_handler',+ 'debug_logger',+ 'models',+ 'persistent_storage'+ ]+ for module_name in modules_to_check:+ qualified_name = f"{__package__}.{module_name}"+ if qualified_name in sys.modules:+ module = sys.modules[qualified_name]+ modules_info.append(f"[loaded] {module_name}: {getattr(module, '__file__', 'built-in')}")+ else:+ modules_info.append(f"[missing] {module_name}: Not loaded")+ return "\n".join(modules_info)+ except Exception as e:+ return f"Error checking module status: {str(e)}"+++@section_tool("debugging")+async def validate_browser_environment_tool() -> Dict[str, Any]:+ """+ Validate browser environment and diagnose potential issues.++ Returns:+ Dict[str, Any]: Environment validation results with platform info and recommendations+ """+ try:+ return validate_browser_environment()+ except Exception as e:+ return {+ "error": str(e),+ "platform_info": get_platform_info(),+ "is_ready": False,+ "issues": [f"Validation failed: {str(e)}"],+ "warnings": []+ }+++@section_tool("progressive-cloning")+async def clone_element_progressive(+ instance_id: str,+ selector: str,+ include_children: bool = True+) -> Dict[str, Any]:+ """+ Clone element progressively - returns lightweight base structure with element_id.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_children (bool): Whether to extract child elements.++ Returns:+ Dict[str, Any]: Base structure with element_id for progressive expansion.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await progressive_element_cloner.clone_element_progressive(tab, selector, include_children)+++@section_tool("progressive-cloning")+async def expand_styles(+ element_id: str,+ categories: Optional[List[str]] = None,+ properties: Optional[List[str]] = None+) -> Dict[str, Any]:+ """+ Expand styles data for a stored element.++ Args:+ element_id (str): Element ID from clone_element_progressive().+ categories (Optional[List[str]]): Style categories to include (layout, typography, colors, spacing, borders, backgrounds, effects, animation).+ properties (Optional[List[str]]): Specific CSS property names to include.++ Returns:+ Dict[str, Any]: Filtered styles data.+ """+ return progressive_element_cloner.expand_styles(element_id, categories, properties)+++@section_tool("progressive-cloning")+async def expand_events(+ element_id: str,+ event_types: Optional[List[str]] = None+) -> Dict[str, Any]:+ """+ Expand event listeners data for a stored element.++ Args:+ element_id (str): Element ID from clone_element_progressive().+ event_types (Optional[List[str]]): Event types or sources to include (click, react, inline, addEventListener).++ Returns:+ Dict[str, Any]: Filtered event listeners data.+ """+ return progressive_element_cloner.expand_events(element_id, event_types)+++@section_tool("progressive-cloning")+async def expand_children(+ element_id: str,+ depth_range: Optional[List] = None,+ max_count: Optional[Any] = None+) -> Dict[str, Any]:+ """+ Expand children data for a stored element.++ Args:+ element_id (str): Element ID from clone_element_progressive().+ depth_range (Optional[List]): [min_depth, max_depth] range to include.+ max_count (Optional[Any]): Maximum number of children to return.++ Returns:+ Dict[str, Any]: Filtered children data.+ """+ if isinstance(max_count, str):+ try:+ max_count = int(max_count) if max_count else None+ except ValueError:+ return {"error": f"Invalid max_count value: {max_count}"}++ if isinstance(depth_range, list):+ try:+ depth_range = [int(x) if isinstance(x, str) else x for x in depth_range]+ except ValueError:+ return {"error": f"Invalid depth_range values: {depth_range}"}++ depth_tuple = tuple(depth_range) if depth_range else None++ result = progressive_element_cloner.expand_children(element_id, depth_tuple, max_count)+ return response_handler.handle_response(result, f"expand_children_{element_id}")+++@section_tool("progressive-cloning")+async def expand_css_rules(+ element_id: str,+ source_types: Optional[List[str]] = None+) -> Dict[str, Any]:+ """+ Expand CSS rules data for a stored element.++ Args:+ element_id (str): Element ID from clone_element_progressive().+ source_types (Optional[List[str]]): CSS rule sources to include (inline, external stylesheet URLs).++ Returns:+ Dict[str, Any]: Filtered CSS rules data.+ """+ return progressive_element_cloner.expand_css_rules(element_id, source_types)+++@section_tool("progressive-cloning")+async def expand_pseudo_elements(+ element_id: str+) -> Dict[str, Any]:+ """+ Expand pseudo-elements data for a stored element.++ Args:+ element_id (str): Element ID from clone_element_progressive().++ Returns:+ Dict[str, Any]: Pseudo-elements data (::before, ::after, etc.).+ """+ return progressive_element_cloner.expand_pseudo_elements(element_id)+++@section_tool("progressive-cloning")+async def expand_animations(+ element_id: str+) -> Dict[str, Any]:+ """+ Expand animations and fonts data for a stored element.++ Args:+ element_id (str): Element ID from clone_element_progressive().++ Returns:+ Dict[str, Any]: Animations, transitions, and fonts data.+ """+ return progressive_element_cloner.expand_animations(element_id)+++@section_tool("progressive-cloning")+async def list_stored_elements() -> Dict[str, Any]:+ """+ List all stored elements with their basic info.++ Returns:+ Dict[str, Any]: List of stored elements with metadata.+ """+ return progressive_element_cloner.list_stored_elements()+++@section_tool("progressive-cloning")+async def clear_stored_element(+ element_id: str+) -> Dict[str, Any]:+ """+ Clear a specific stored element.++ Args:+ element_id (str): Element ID to clear.++ Returns:+ Dict[str, Any]: Success/error message.+ """+ return progressive_element_cloner.clear_stored_element(element_id)+++@section_tool("progressive-cloning")+async def clear_all_elements() -> Dict[str, Any]:+ """+ Clear all stored elements.++ Returns:+ Dict[str, Any]: Success message.+ """+ return progressive_element_cloner.clear_all_elements()+++@section_tool("file-extraction")+async def clone_element_to_file(+ instance_id: str,+ selector: str,+ extraction_options: Optional[str] = None+) -> Dict[str, Any]:+ """+ Clone element completely and save to file, returning file path instead of full data.++ This is ideal when you want complete element data but don't want to overwhelm+ the response with large JSON objects. The data is saved to a JSON file that+ can be read later.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ extraction_options (Optional[str]): JSON string with extraction options.++ Returns:+ Dict[str, Any]: File path and summary information about the cloned element.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ parsed_options = None+ if extraction_options:+ try:+ parsed_options = json.loads(extraction_options)+ except json.JSONDecodeError:+ return {"error": "Invalid extraction_options JSON"}+ return await file_based_element_cloner.clone_element_complete_to_file(+ tab, selector=selector, extraction_options=parsed_options+ )+++@section_tool("file-extraction")+async def extract_complete_element_to_file(+ instance_id: str,+ selector: str,+ include_children: bool = True+) -> Dict[str, Any]:+ """+ Extract complete element using working comprehensive cloner and save to file.++ This uses the proven comprehensive extraction logic that returns large amounts+ of data, but saves it to a file instead of overwhelming the response.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_children (bool): Whether to include child elements.++ Returns:+ Dict[str, Any]: File path and concise summary instead of massive data dump.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await file_based_element_cloner.extract_complete_element_to_file(+ tab, selector, include_children+ )+++@section_tool("element-extraction")+async def extract_complete_element_cdp(+ instance_id: str,+ selector: str,+ include_children: bool = True+) -> Dict[str, Any]:+ """+ Extract complete element using native CDP methods for 100% accuracy.++ This uses Chrome DevTools Protocol's native methods to extract:+ - Complete computed styles via CSS.getComputedStyleForNode+ - Matched CSS rules via CSS.getMatchedStylesForNode+ - Event listeners via DOMDebugger.getEventListeners+ - Complete DOM structure and attributes++ This provides the most accurate element cloning possible by bypassing+ JavaScript limitations and using CDP's direct browser access.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_children (bool): Whether to include child elements.++ Returns:+ Dict[str, Any]: Complete element data with 100% accuracy.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ cdp_cloner = CDPElementCloner()+ return await cdp_cloner.extract_complete_element_cdp(tab, selector, include_children)+++@section_tool("file-extraction")+async def extract_element_styles_to_file(+ instance_id: str,+ selector: str,+ include_computed: bool = True,+ include_css_rules: bool = True,+ include_pseudo: bool = True,+ include_inheritance: bool = False+) -> Dict[str, Any]:+ """+ Extract element styles and save to file, returning file path.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_computed (bool): Include computed styles.+ include_css_rules (bool): Include matching CSS rules.+ include_pseudo (bool): Include pseudo-element styles.+ include_inheritance (bool): Include style inheritance chain.++ Returns:+ Dict[str, Any]: File path and summary of extracted styles.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await file_based_element_cloner.extract_element_styles_to_file(+ tab,+ selector=selector,+ include_computed=include_computed,+ include_css_rules=include_css_rules,+ include_pseudo=include_pseudo,+ include_inheritance=include_inheritance+ )+++@section_tool("file-extraction")+async def extract_element_structure_to_file(+ instance_id: str,+ selector: str,+ include_children: bool = False,+ include_attributes: bool = True,+ include_data_attributes: bool = True,+ max_depth: int = 3+) -> Dict[str, Any]:+ """+ Extract element structure and save to file, returning file path.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_children (bool): Include child elements.+ include_attributes (bool): Include all attributes.+ include_data_attributes (bool): Include data-* attributes.+ max_depth (int): Maximum depth for children extraction.++ Returns:+ Dict[str, Any]: File path and summary of extracted structure.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await file_based_element_cloner.extract_element_structure_to_file(+ tab,+ selector=selector,+ include_children=include_children,+ include_attributes=include_attributes,+ include_data_attributes=include_data_attributes,+ max_depth=max_depth+ )+++@section_tool("file-extraction")+async def extract_element_events_to_file(+ instance_id: str,+ selector: str,+ include_inline: bool = True,+ include_listeners: bool = True,+ include_framework: bool = True,+ analyze_handlers: bool = True+) -> Dict[str, Any]:+ """+ Extract element events and save to file, returning file path.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_inline (bool): Include inline event handlers.+ include_listeners (bool): Include addEventListener handlers.+ include_framework (bool): Include framework-specific handlers.+ analyze_handlers (bool): Analyze handler functions.++ Returns:+ Dict[str, Any]: File path and summary of extracted events.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await file_based_element_cloner.extract_element_events_to_file(+ tab,+ selector=selector,+ include_inline=include_inline,+ include_listeners=include_listeners,+ include_framework=include_framework,+ analyze_handlers=analyze_handlers+ )+++@section_tool("file-extraction")+async def extract_element_animations_to_file(+ instance_id: str,+ selector: str,+ include_css_animations: bool = True,+ include_transitions: bool = True,+ include_transforms: bool = True,+ analyze_keyframes: bool = True+) -> Dict[str, Any]:+ """+ Extract element animations and save to file, returning file path.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_css_animations (bool): Include CSS animations.+ include_transitions (bool): Include CSS transitions.+ include_transforms (bool): Include CSS transforms.+ analyze_keyframes (bool): Analyze keyframe rules.++ Returns:+ Dict[str, Any]: File path and summary of extracted animations.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await file_based_element_cloner.extract_element_animations_to_file(+ tab,+ selector=selector,+ include_css_animations=include_css_animations,+ include_transitions=include_transitions,+ include_transforms=include_transforms,+ analyze_keyframes=analyze_keyframes+ )+++@section_tool("file-extraction")+async def extract_element_assets_to_file(+ instance_id: str,+ selector: str,+ include_images: bool = True,+ include_backgrounds: bool = True,+ include_fonts: bool = True,+ fetch_external: bool = False+) -> Dict[str, Any]:+ """+ Extract element assets and save to file, returning file path.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the element.+ include_images (bool): Include images.+ include_backgrounds (bool): Include background images.+ include_fonts (bool): Include font information.+ fetch_external (bool): Fetch external assets.++ Returns:+ Dict[str, Any]: File path and summary of extracted assets.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await file_based_element_cloner.extract_element_assets_to_file(+ tab,+ selector=selector,+ include_images=include_images,+ include_backgrounds=include_backgrounds,+ include_fonts=include_fonts,+ fetch_external=fetch_external+ )+++@section_tool("file-extraction")+async def list_clone_files() -> List[Dict[str, Any]]:+ """+ List all element clone files saved to disk.++ Returns:+ List[Dict[str, Any]]: List of clone files with metadata and file information.+ """+ return file_based_element_cloner.list_clone_files()+++@section_tool("file-extraction")+async def cleanup_clone_files(+ max_age_hours: int = 24+) -> Dict[str, int]:+ """+ Clean up old clone files to save disk space.++ Args:+ max_age_hours (int): Maximum age in hours for files to keep.++ Returns:+ Dict[str, int]: Number of files deleted.+ """+ deleted_count = file_based_element_cloner.cleanup_old_files(max_age_hours)+ return {"deleted_count": deleted_count}+++@section_tool("cdp-functions")+async def list_cdp_commands() -> List[str]:+ """+ List all available CDP Runtime commands for function execution.++ Returns:+ List[str]: List of available CDP command names.+ """+ return await cdp_function_executor.list_cdp_commands()+++@section_tool("cdp-functions")+async def execute_cdp_command(+ instance_id: str,+ command: str,+ params: Optional[Dict[str, Any]] = None+) -> Dict[str, Any]:+ """+ Execute any CDP Runtime command with given parameters.++ Args:+ instance_id (str): Browser instance ID.+ command (str): CDP command name (e.g., 'evaluate', 'callFunctionOn').+ params (Dict[str, Any], optional): Command parameters as a dictionary.+ IMPORTANT: Use snake_case parameter names (e.g., 'return_by_value')+ NOT camelCase ('returnByValue'). The nodriver library expects+ Python-style parameter names.++ Returns:+ Dict[str, Any]: Command execution result.++ Example:+ # Correct - use snake_case+ params = {"expression": "document.title", "return_by_value": True}++ params = {"expression": "document.title", "returnByValue": True}+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ return await cdp_function_executor.execute_cdp_command(tab, command, params or {})+++@section_tool("cdp-functions")+async def add_script_to_evaluate_on_new_document(+ instance_id: str,+ source: str,+ world_name: Optional[str] = None,+ include_command_line_api: Optional[bool] = None,+ run_immediately: bool = True,+) -> Dict[str, Any]:+ """+ Install JavaScript that runs before page scripts on each new document.++ Args:+ instance_id (str): Browser instance ID.+ source (str): JavaScript source to evaluate before page scripts.+ world_name (Optional[str]): Isolated world name, or None for the main world.+ include_command_line_api (Optional[bool]): Whether command line API is available.+ run_immediately (bool): Whether to run the script in existing contexts too.++ Returns:+ Dict[str, Any]: Result with success state and script identifier.+ """+ try:+ identifier = await browser_manager.add_init_script(+ instance_id,+ source,+ world_name=world_name,+ include_command_line_api=include_command_line_api,+ run_immediately=run_immediately,+ )+ return {"success": True, "identifier": identifier}+ except Exception as e:+ debug_logger.log_error("server", "add_script_to_evaluate_on_new_document", e)+ return {"success": False, "error": str(e)}+++@section_tool("cdp-functions")+async def get_execution_contexts(+ instance_id: str+) -> List[Dict[str, Any]]:+ """+ Get all available JavaScript execution contexts.++ Args:+ instance_id (str): Browser instance ID.++ Returns:+ List[Dict[str, Any]]: List of execution contexts with their details.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return []+ contexts = await cdp_function_executor.get_execution_contexts(tab)+ return [+ {+ "id": ctx.id,+ "name": ctx.name,+ "origin": ctx.origin,+ "unique_id": ctx.unique_id,+ "aux_data": ctx.aux_data+ }+ for ctx in contexts+ ]+++@section_tool("cdp-functions")+async def discover_global_functions(+ instance_id: str,+ context_id: Optional[str] = None+) -> List[Dict[str, Any]]:+ """+ Discover all global JavaScript functions available in the page.++ Args:+ instance_id (str): Browser instance ID.+ context_id (str, optional): Optional execution context ID.++ Returns:+ List[Dict[str, Any]]: List of discovered functions with their details.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return []+ functions = await cdp_function_executor.discover_global_functions(tab, context_id)+ result = [+ {+ "name": func.name,+ "path": func.path,+ "signature": func.signature,+ "description": func.description+ }+ for func in functions+ ]++ file_response = response_handler.handle_response(+ result,+ fallback_filename_prefix="global_functions",+ metadata={+ "context_id": context_id,+ "function_count": len(result),+ "url": getattr(tab, 'url', 'unknown')+ }+ )++ if isinstance(file_response, dict) and "file_path" in file_response:+ return [{+ "name": "LARGE_RESPONSE_SAVED_TO_FILE",+ "path": "file_storage",+ "signature": "automatic_file_fallback",+ "description": f"Response too large ({file_response['estimated_tokens']} tokens), saved to: {file_response['filename']}"+ }]++ return file_response+++@section_tool("cdp-functions")+async def discover_object_methods(+ instance_id: str,+ object_path: str+) -> List[Dict[str, Any]]:+ """+ Discover methods of a specific JavaScript object.++ Args:+ instance_id (str): Browser instance ID.+ object_path (str): Path to the object (e.g., 'document', 'window.localStorage').++ Returns:+ List[Dict[str, Any]]: List of discovered methods.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return []+ methods = await cdp_function_executor.discover_object_methods(tab, object_path)+ methods_data = [+ {+ "name": method.name,+ "path": method.path,+ "signature": method.signature,+ "description": method.description+ }+ for method in methods+ ]++ return response_handler.handle_response(+ methods_data,+ f"object_methods_{object_path.replace('.', '_')}"+ )+++@section_tool("cdp-functions")+async def call_javascript_function(+ instance_id: str,+ function_path: str,+ args: Optional[List[Any]] = None+) -> Dict[str, Any]:+ """+ Call a JavaScript function with arguments.++ Args:+ instance_id (str): Browser instance ID.+ function_path (str): Full path to the function (e.g., 'document.getElementById').+ args (List[Any], optional): List of arguments to pass to the function.++ Returns:+ Dict[str, Any]: Function call result.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ return await cdp_function_executor.call_discovered_function(tab, function_path, args or [])+++@section_tool("cdp-functions")+async def inspect_function_signature(+ instance_id: str,+ function_path: str+) -> Dict[str, Any]:+ """+ Inspect a JavaScript function's signature and details.++ Args:+ instance_id (str): Browser instance ID.+ function_path (str): Full path to the function.++ Returns:+ Dict[str, Any]: Function signature and details.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ return await cdp_function_executor.inspect_function_signature(tab, function_path)+++@section_tool("cdp-functions")+async def inject_and_execute_script(+ instance_id: str,+ script_code: str,+ context_id: Optional[str] = None+) -> Dict[str, Any]:+ """+ Inject and execute custom JavaScript code.++ Args:+ instance_id (str): Browser instance ID.+ script_code (str): JavaScript code to execute.+ context_id (str, optional): Optional execution context ID.++ Returns:+ Dict[str, Any]: Script execution result.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ return await cdp_function_executor.inject_and_execute_script(tab, script_code, context_id)+++@section_tool("cdp-functions")+async def create_persistent_function(+ instance_id: str,+ function_name: str,+ function_code: str+) -> Dict[str, Any]:+ """+ Create a persistent JavaScript function that survives page reloads.++ Args:+ instance_id (str): Browser instance ID.+ function_name (str): Name for the function.+ function_code (str): JavaScript function code.++ Returns:+ Dict[str, Any]: Function creation result.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ try:+ source = await cdp_function_executor.prepare_persistent_function(tab, function_name, function_code, instance_id)+ identifier = await browser_manager.add_init_script(instance_id, source)+ except Exception as e:+ return {"success": False, "error": str(e), "function_name": function_name}+ return {+ "success": True,+ "function_name": function_name,+ "available_as": f"window.{function_name}",+ "script_identifier": identifier,+ }+++@section_tool("cdp-functions")+async def execute_function_sequence(+ instance_id: str,+ function_calls: List[Dict[str, Any]]+) -> List[Dict[str, Any]]:+ """+ Execute a sequence of JavaScript function calls.++ Args:+ instance_id (str): Browser instance ID.+ function_calls (List[Dict[str, Any]]): List of function calls, each with 'function_path', 'args', and optional 'context_id'.++ Returns:+ List[Dict[str, Any]]: List of function call results.+ """+ from .cdp_function_executor import FunctionCall+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return [{"success": False, "error": f"Instance not found: {instance_id}"}]+ calls = []+ for call_data in function_calls:+ calls.append(FunctionCall(+ function_path=call_data['function_path'],+ args=call_data.get('args', []),+ context_id=call_data.get('context_id')+ ))+ return await cdp_function_executor.execute_function_sequence(tab, calls)+++@section_tool("cdp-functions")+async def create_page_binding(+ instance_id: str,+ binding_name: str,+ auto_resolve: bool = False,+ auto_response: Optional[Any] = None+) -> Dict[str, Any]:+ """+ Expose window.<binding_name>(...args) so page JavaScript can call back to you.++ Each call returns a Promise in the page and is queued here. Read calls with+ get_page_binding_calls and answer them with resolve_page_binding_call, or set+ auto_resolve to answer every call immediately with auto_response. Nothing+ runs on the server host. Arguments must be JSON, payloads are capped at+ 64 KB, and at most 500 calls are queued per instance. The binding is+ installed on every tab of the instance and survives navigation. Calls are+ delivered through Runtime events.++ Args:+ instance_id (str): Browser instance ID.+ binding_name (str): JavaScript identifier for the function.+ auto_resolve (bool): Resolve every call immediately with auto_response.+ auto_response (Optional[Any]): JSON value returned to the page when auto_resolve is set.++ Returns:+ Dict[str, Any]: Creation result.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ try:+ return await page_bindings.create(tab, instance_id, binding_name, auto_resolve, auto_response)+ except Exception as e:+ return {"success": False, "error": str(e)}+++@section_tool("cdp-functions")+async def get_page_binding_calls(+ instance_id: str,+ binding_name: Optional[str] = None,+ wait_seconds: float = 0,+ limit: int = 50+) -> Dict[str, Any]:+ """+ Read calls made by page JavaScript to page bindings.++ Pending calls stay queued until resolved. Auto-resolved calls are returned+ once and then removed.++ Args:+ instance_id (str): Browser instance ID.+ binding_name (Optional[str]): Only return calls to this binding.+ wait_seconds (float): Wait up to this many seconds (max 60) for a call when none is queued.+ limit (int): Maximum number of calls to return.++ Returns:+ Dict[str, Any]: Calls (call_id, binding, args, received_at, status) and binding names.+ """+ await browser_manager.touch_instance(instance_id)+ calls = await page_bindings.get_calls(instance_id, binding_name, wait_seconds, limit)+ return {"calls": calls, "bindings": page_bindings.list_bindings(instance_id)}+++@section_tool("cdp-functions")+async def resolve_page_binding_call(+ instance_id: str,+ call_id: str,+ result: Optional[Any] = None,+ error: Optional[str] = None+) -> Dict[str, Any]:+ """+ Answer a pending page binding call.++ Args:+ instance_id (str): Browser instance ID.+ call_id (str): Call ID from get_page_binding_calls.+ result (Optional[Any]): JSON value the page Promise resolves with.+ error (Optional[str]): Reject the page Promise with this message instead.++ Returns:+ Dict[str, Any]: Whether the answer reached the page.+ """+ await browser_manager.touch_instance(instance_id)+ try:+ return await page_bindings.resolve(instance_id, call_id, result, error)+ except Exception as e:+ return {"success": False, "error": str(e)}+++@section_tool("cdp-functions")+async def remove_page_binding(+ instance_id: str,+ binding_name: str+) -> Dict[str, Any]:+ """+ Remove a page binding from every tab and reject its pending calls.++ Args:+ instance_id (str): Browser instance ID.+ binding_name (str): Binding to remove.++ Returns:+ Dict[str, Any]: Removal result.+ """+ await browser_manager.touch_instance(instance_id)+ try:+ return await page_bindings.remove(instance_id, binding_name)+ except Exception as e:+ return {"success": False, "error": str(e)}+++@section_tool("cdp-functions")+async def execute_python_in_browser(+ instance_id: str,+ python_code: str+) -> Dict[str, Any]:+ """+ Execute Python code by translating it to JavaScript.++ Args:+ instance_id (str): Browser instance ID.+ python_code (str): Python code to translate and execute.++ Returns:+ Dict[str, Any]: Execution result.+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ return {"success": False, "error": f"Instance not found: {instance_id}"}+ return await cdp_function_executor.execute_python_in_browser(tab, python_code)+++@section_tool("cdp-functions")+async def get_function_executor_info(+ instance_id: Optional[str] = None+) -> Dict[str, Any]:+ """+ Get information about the CDP function executor state.++ Args:+ instance_id (str, optional): Optional browser instance ID for specific info.++ Returns:+ Dict[str, Any]: Function executor state and capabilities.+ """+ return await cdp_function_executor.get_function_executor_info(instance_id)+++@section_tool("dynamic-hooks")+async def create_dynamic_hook(+ name: str,+ requirements: Dict[str, Any],+ function_code: str,+ instance_ids: Optional[List[str]] = None,+ priority: int = 100+) -> Dict[str, Any]:+ """+ Create a new dynamic hook with AI-generated Python function.++ This is the new powerful hook system that allows AI to write custom Python functions+ that process network requests in real-time with no pending state.++ Args:+ name (str): Human-readable hook name+ requirements (Dict[str, Any]): Matching criteria (url_pattern, method, resource_type, custom_condition)+ function_code (str): Python function code that processes requests (must define process_request(request))+ instance_ids (Optional[List[str]]): Browser instances to apply hook to (all if None)+ priority (int): Hook priority (lower = higher priority)++ Returns:+ Dict[str, Any]: Hook creation result with hook_id++ Example function_code:+ ```python+ def process_request(request):+ if "example.com" in request["url"]:+ return HookAction(action="redirect", url="https://httpbin.org/get")+ return HookAction(action="continue")+ ```+ """+ return await dynamic_hook_ai.create_dynamic_hook(+ name=name,+ requirements=requirements,+ function_code=function_code,+ instance_ids=instance_ids,+ priority=priority+ )+++@section_tool("dynamic-hooks")+async def create_simple_dynamic_hook(+ name: str,+ url_pattern: str,+ action: str,+ target_url: Optional[str] = None,+ custom_headers: Optional[Dict[str, str]] = None,+ instance_ids: Optional[List[str]] = None+) -> Dict[str, Any]:+ """+ Create a simple dynamic hook using predefined templates (easier for AI).++ Args:+ name (str): Hook name+ url_pattern (str): URL pattern to match+ action (str): Action type - 'block', 'redirect', 'add_headers', or 'log'+ target_url (Optional[str]): Target URL for redirect action+ custom_headers (Optional[Dict[str, str]]): Headers to add for add_headers action+ instance_ids (Optional[List[str]]): Browser instances to apply hook to++ Returns:+ Dict[str, Any]: Hook creation result+ """+ return await dynamic_hook_ai.create_simple_hook(+ name=name,+ url_pattern=url_pattern,+ action=action,+ target_url=target_url,+ custom_headers=custom_headers,+ instance_ids=instance_ids+ )+++@section_tool("dynamic-hooks")+async def list_dynamic_hooks(instance_id: Optional[str] = None) -> Dict[str, Any]:+ """+ List all dynamic hooks.++ Args:+ instance_id (Optional[str]): Optional filter by browser instance++ Returns:+ Dict[str, Any]: List of hooks with details and statistics+ """+ return await dynamic_hook_ai.list_dynamic_hooks(instance_id=instance_id)+++@section_tool("dynamic-hooks")+async def get_dynamic_hook_details(hook_id: str) -> Dict[str, Any]:+ """+ Get detailed information about a specific dynamic hook.++ Args:+ hook_id (str): Hook identifier++ Returns:+ Dict[str, Any]: Detailed hook information including function code+ """+ return await dynamic_hook_ai.get_hook_details(hook_id=hook_id)+++@section_tool("dynamic-hooks")+async def remove_dynamic_hook(hook_id: str) -> Dict[str, Any]:+ """+ Remove a dynamic hook.++ Args:+ hook_id (str): Hook identifier to remove++ Returns:+ Dict[str, Any]: Removal status+ """+ return await dynamic_hook_ai.remove_dynamic_hook(hook_id=hook_id)+++@section_tool("dynamic-hooks")+def get_hook_documentation() -> Dict[str, Any]:+ """+ Get comprehensive documentation for creating hook functions (AI learning).++ Returns:+ Dict[str, Any]: Documentation of request object structure and HookAction types+ """+ return dynamic_hook_ai.get_request_documentation()+++@section_tool("dynamic-hooks")+def get_hook_examples() -> Dict[str, Any]:+ """+ Get example hook functions for AI learning.++ Returns:+ Dict[str, Any]: Collection of example hook functions with explanations+ """+ return dynamic_hook_ai.get_hook_examples()+++@section_tool("dynamic-hooks")+def get_hook_requirements_documentation() -> Dict[str, Any]:+ """+ Get documentation on hook requirements and matching criteria.++ Returns:+ Dict[str, Any]: Requirements documentation and best practices+ """+ return dynamic_hook_ai.get_requirements_documentation()+++@section_tool("dynamic-hooks")+def get_hook_common_patterns() -> Dict[str, Any]:+ """+ Get common hook patterns and use cases.++ Returns:+ Dict[str, Any]: Common patterns like ad blocking, API proxying, etc.+ """+ return dynamic_hook_ai.get_common_patterns()+++@section_tool("dynamic-hooks")+def validate_hook_function(function_code: str) -> Dict[str, Any]:+ """+ Validate hook function code for common issues before creating.++ Args:+ function_code (str): Python function code to validate++ Returns:+ Dict[str, Any]: Validation results with issues and warnings+ """+ return dynamic_hook_ai.validate_hook_function(function_code=function_code)+++if parse_bool_env("XPOOL_SAFE_MODE", default=False):+ DISABLED_SECTIONS.add("cdp-functions")+ apply_disabled_sections()+++def main(argv: Optional[List[str]] = None) -> None:+ """+ Parse command line options and run the MCP server.++ Args:+ argv (Optional[List[str]]): Arguments to parse instead of sys.argv.++ Returns:+ None+ """+ parser = argparse.ArgumentParser(+ description=f"Stealth Browser MCP Server with {sum(len(tools) for tools in SECTION_TOOLS.values())} tools"+ )+ parser.add_argument("--transport", choices=["stdio", "http"], default="stdio",+ help="Transport protocol to use")+ parser.add_argument("--port", type=int, default=int(os.getenv("PORT", 8000)),+ help="Port for HTTP transport")+ parser.add_argument("--host", default="127.0.0.1",+ help="Host for HTTP transport (use 0.0.0.0 only with an auth token on trusted networks)")++ parser.add_argument("--disable-browser-management", action="store_true",+ help="Disable browser management tools (spawn, navigate, close, etc.)")+ parser.add_argument("--disable-element-interaction", action="store_true",+ help="Disable element interaction tools (click, type, scroll, etc.)")+ parser.add_argument("--disable-element-extraction", action="store_true",+ help="Disable element extraction tools (styles, structure, events, etc.)")+ parser.add_argument("--disable-file-extraction", action="store_true",+ help="Disable file-based extraction tools")+ parser.add_argument("--disable-network-debugging", action="store_true",+ help="Disable network debugging and interception tools")+ parser.add_argument("--disable-cdp-functions", action="store_true",+ help="Disable CDP function execution tools")+ parser.add_argument("--disable-progressive-cloning", action="store_true",+ help="Disable progressive element cloning tools")+ parser.add_argument("--disable-cookies-storage", action="store_true",+ help="Disable cookie and storage management tools")+ parser.add_argument("--disable-tabs", action="store_true",+ help="Disable tab management tools")+ parser.add_argument("--disable-debugging", action="store_true",+ help="Disable debug and system tools")+ parser.add_argument("--disable-dynamic-hooks", action="store_true",+ help="Disable dynamic network hook system")++ parser.add_argument("--minimal", action="store_true",+ help="Enable only core browser management and element interaction (disable everything else)")+ parser.add_argument("--list-sections", action="store_true",+ help="List all available tool sections and exit")+ parser.add_argument(+ "--debug",+ action="store_true",+ default=DEBUG_LOGGING_ENABLED,+ help="Enable debug logging to stderr",+ )+ parser.add_argument(+ "--xpool-safe",+ action="store_true",+ default=parse_bool_env("XPOOL_SAFE_MODE", default=False),+ help="Enable xpool-safe surface (disables cdp-functions tools that trigger Runtime.enable)",+ )++ args = parser.parse_args(argv)++ if args.debug and not debug_logger._enabled:+ debug_logger.enable()++ if args.list_sections:+ print("Available tool sections:")+ for section, description in SECTION_DESCRIPTIONS.items():+ print(f" {section}: {description} ({len(SECTION_TOOLS.get(section, []))} tools)")+ print("\nUse --disable-<section-name> to disable specific sections")+ print("Use --minimal to enable only core functionality")+ sys.exit(0)++ if args.minimal:+ DISABLED_SECTIONS.update([+ "element-extraction", "file-extraction", "network-debugging",+ "cdp-functions", "progressive-cloning", "cookies-storage",+ "tabs", "debugging", "dynamic-hooks"+ ])++ if args.disable_browser_management:+ DISABLED_SECTIONS.add("browser-management")+ if args.disable_element_interaction:+ DISABLED_SECTIONS.add("element-interaction")+ if args.disable_element_extraction:+ DISABLED_SECTIONS.add("element-extraction")+ if args.disable_file_extraction:+ DISABLED_SECTIONS.add("file-extraction")+ if args.disable_network_debugging:+ DISABLED_SECTIONS.add("network-debugging")+ if args.disable_cdp_functions:+ DISABLED_SECTIONS.add("cdp-functions")+ if args.disable_progressive_cloning:+ DISABLED_SECTIONS.add("progressive-cloning")+ if args.disable_cookies_storage:+ DISABLED_SECTIONS.add("cookies-storage")+ if args.disable_tabs:+ DISABLED_SECTIONS.add("tabs")+ if args.disable_debugging:+ DISABLED_SECTIONS.add("debugging")+ if args.disable_dynamic_hooks:+ DISABLED_SECTIONS.add("dynamic-hooks")++ if args.xpool_safe:+ DISABLED_SECTIONS.add("cdp-functions")++ apply_disabled_sections()++ if DISABLED_SECTIONS:+ debug_logger.log_info(+ "server",+ "startup",+ f"Disabled tool sections: {', '.join(sorted(DISABLED_SECTIONS))}",+ )++ if args.transport == "http":+ if HTTP_AUTH_TOKEN is None and not is_loopback_host(args.host):+ print(+ f"WARNING: HTTP transport is listening on {args.host} without an auth token. "+ "Set STEALTH_BROWSER_MCP_AUTH_TOKEN before exposing the server beyond localhost.",+ file=sys.stderr,+ )+ mcp.run(transport="http", host=args.host, port=args.port)+ else:+ install_stdio_guard()+ mcp.run(transport="stdio")+++if __name__ == "__main__":+ main()diff --git a/src/stdio_guard.py b/src/stealth_browser_mcp/stdio_guard.pysimilarity index 100%rename from src/stdio_guard.pyrename to src/stealth_browser_mcp/stdio_guard.pydiff --git a/tests/test_dynamic_hook_cleanup.py b/tests/test_dynamic_hook_cleanup.pyindex f5f26b9..bdfbfdb 100644--- a/tests/test_dynamic_hook_cleanup.py+++ b/tests/test_dynamic_hook_cleanup.py@@ -8,7 +8,7 @@ sys.path.insert(0, str(ROOT / "src"))import nodriver as uc-from dynamic_hook_system import DynamicHookSystem+from stealth_browser_mcp.dynamic_hook_system import DynamicHookSystemclass FakeTab:diff --git a/tests/test_element_cloners.py b/tests/test_element_cloners.pyindex 447b713..e242e58 100644--- a/tests/test_element_cloners.py+++ b/tests/test_element_cloners.py@@ -9,10 +9,10 @@ from unittest import mockROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-from element_cloner import element_cloner-from file_based_element_cloner import FileBasedElementCloner-from progressive_element_cloner import MAX_STORED_ELEMENTS, progressive_element_cloner-from response_handler import ResponseHandler+from stealth_browser_mcp.element_cloner import element_cloner+from stealth_browser_mcp.file_based_element_cloner import FileBasedElementCloner+from stealth_browser_mcp.progressive_element_cloner import MAX_STORED_ELEMENTS, progressive_element_cloner+from stealth_browser_mcp.response_handler import ResponseHandlerSAMPLE_CLONE = {@@ -82,7 +82,7 @@ class ProgressiveClonerTests(unittest.TestCase):def test_store_is_capped_to_newest_entries(self):saved = {}data = {f"elem_{i}": {"full_data": {}, "timestamp": float(i)} for i in range(MAX_STORED_ELEMENTS + 5)}- with mock.patch("progressive_element_cloner.persistent_storage.set", side_effect=lambda key, value: saved.update(value)):+ with mock.patch("stealth_browser_mcp.progressive_element_cloner.persistent_storage.set", side_effect=lambda key, value: saved.update(value)):progressive_element_cloner._save_store(data)self.assertEqual(len(saved), MAX_STORED_ELEMENTS)self.assertNotIn("elem_0", saved)@@ -97,14 +97,14 @@ class FileBasedClonerTests(unittest.TestCase):def test_extraction_error_is_not_saved(self):error = {"error": "JavaScript error: boom"}- with mock.patch("file_based_element_cloner.element_cloner.extract_element_structure", mock.AsyncMock(return_value=error)):+ with mock.patch("stealth_browser_mcp.file_based_element_cloner.element_cloner.extract_element_structure", mock.AsyncMock(return_value=error)):result = asyncio.run(self.cloner.extract_element_structure_to_file(object(), selector="#x"))self.assertEqual(result, error)self.assertEqual(list(Path(self.tmp.name).iterdir()), [])def test_complete_summary_reads_nested_element(self):with mock.patch(- "file_based_element_cloner.comprehensive_element_cloner.extract_complete_element",+ "stealth_browser_mcp.file_based_element_cloner.comprehensive_element_cloner.extract_complete_element",mock.AsyncMock(return_value=json.loads(json.dumps(SAMPLE_CLONE))),):result = asyncio.run(self.cloner.extract_complete_element_to_file(object(), "#go"))@@ -117,7 +117,7 @@ class FileBasedClonerTests(unittest.TestCase):def test_styles_summary_keeps_components_key(self):styles = {"computed_styles": {"color": "red"}, "css_rules": []}- with mock.patch("file_based_element_cloner.element_cloner.extract_element_styles", mock.AsyncMock(return_value=styles)):+ with mock.patch("stealth_browser_mcp.file_based_element_cloner.element_cloner.extract_element_styles", mock.AsyncMock(return_value=styles)):result = asyncio.run(self.cloner.extract_element_styles_to_file(object(), "#go"))self.assertEqual(result["components"]["computed_styles_count"], 1)diff --git a/tests/test_headless_user_agent.py b/tests/test_headless_user_agent.pyindex 0b6a7a0..2cb1606 100644--- a/tests/test_headless_user_agent.py+++ b/tests/test_headless_user_agent.py@@ -7,8 +7,8 @@ from unittest import mockROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-import browser_manager as browser_manager_module-from browser_manager import BrowserManager+from stealth_browser_mcp import browser_manager as browser_manager_module+from stealth_browser_mcp.browser_manager import BrowserManagerHEADLESS_UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "diff --git a/tests/test_hooks_and_proxy.py b/tests/test_hooks_and_proxy.pyindex 3ad4d31..324cac5 100644--- a/tests/test_hooks_and_proxy.py+++ b/tests/test_hooks_and_proxy.py@@ -6,13 +6,13 @@ from pathlib import PathROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-from cdp_function_executor import CDPFunctionExecutor-from debug_logger import DebugLogger-from dynamic_hook_ai_interface import dynamic_hook_ai-from dynamic_hook_system import DynamicHook, RequestInfo, dynamic_hook_system-from http_security import EnvironmentBearerTokenVerifier-from proxy_forwarder import AuthenticatedProxyForwarder-from proxy_utils import ProxyConfigError, parse_proxy_config+from stealth_browser_mcp.cdp_function_executor import CDPFunctionExecutor+from stealth_browser_mcp.debug_logger import DebugLogger+from stealth_browser_mcp.dynamic_hook_ai_interface import dynamic_hook_ai+from stealth_browser_mcp.dynamic_hook_system import DynamicHook, RequestInfo, dynamic_hook_system+from stealth_browser_mcp.http_security import EnvironmentBearerTokenVerifier+from stealth_browser_mcp.proxy_forwarder import AuthenticatedProxyForwarder+from stealth_browser_mcp.proxy_utils import ProxyConfigError, parse_proxy_configBLOCK_CODE = 'def process_request(request):\n return HookAction(action="block")\n'diff --git a/tests/test_input_actions.py b/tests/test_input_actions.pyindex 495f4f4..bfa7dbd 100644--- a/tests/test_input_actions.py+++ b/tests/test_input_actions.py@@ -9,7 +9,7 @@ sys.path.insert(0, str(ROOT / "src"))import nodriver as uc-import input_actions+from stealth_browser_mcp import input_actionsclass RecordingTab:diff --git a/tests/test_js_values.py b/tests/test_js_values.pyindex 3d7b49f..1d6aafb 100644--- a/tests/test_js_values.py+++ b/tests/test_js_values.py@@ -8,7 +8,7 @@ sys.path.insert(0, str(ROOT / "src"))from nodriver import cdp-from js_values import evaluate_to_python, from_deep_serialized, to_python+from stealth_browser_mcp.js_values import evaluate_to_python, from_deep_serialized, to_pythonDEEP_OBJECT = {diff --git a/tests/test_network_capture.py b/tests/test_network_capture.pyindex d8ddf34..d9fccbe 100644--- a/tests/test_network_capture.py+++ b/tests/test_network_capture.py@@ -7,8 +7,8 @@ from types import SimpleNamespaceROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-import network_interceptor as network_module-from network_interceptor import NetworkInterceptor+from stealth_browser_mcp import network_interceptor as network_module+from stealth_browser_mcp.network_interceptor import NetworkInterceptorclass FakeTab:diff --git a/tests/test_packaging.py b/tests/test_packaging.pynew file mode 100644index 0000000..704940d--- /dev/null+++ b/tests/test_packaging.py@@ -0,0 +1,89 @@+import os+import re+import sys+import tempfile+import unittest+from pathlib import Path+from unittest import mock++ROOT = Path(__file__).resolve().parents[1]+sys.path.insert(0, str(ROOT / "src"))++from stealth_browser_mcp import element_cloner, file_upload_security, paths+from stealth_browser_mcp import server++try:+ import tomllib+except ImportError:+ tomllib = None+++class InstalledLayoutTests(unittest.TestCase):+ def setUp(self):+ self.directory = tempfile.TemporaryDirectory()+ self.addCleanup(self.directory.cleanup)+ self.base = Path(self.directory.name)+ package_dir = self.base / "site-packages" / "stealth_browser_mcp"+ package_dir.mkdir(parents=True)+ self.patches = [+ mock.patch.object(paths, "PACKAGE_DIR", package_dir),+ mock.patch.object(paths, "user_data_dir", return_value=self.base / "data"),+ mock.patch.dict(os.environ, {}, clear=False),+ ]+ for patch in self.patches:+ patch.start()+ self.addCleanup(patch.stop)+ os.environ.pop(paths.DATA_DIR_ENV, None)+ os.environ.pop(file_upload_security.FILE_UPLOAD_ALLOWED_DIRS_ENV, None)++ def test_installed_package_is_not_a_checkout(self):+ self.assertIsNone(paths.source_checkout_root())++ def test_clones_go_to_user_data_dir(self):+ self.assertEqual(paths.clone_output_dir(), self.base / "data" / "element_clones")++ def test_upload_root_is_created_in_user_data_dir(self):+ roots = file_upload_security.get_allowed_upload_roots()+ self.assertEqual(roots, [(self.base / "data" / "uploads").resolve()])+ self.assertTrue(roots[0].is_dir())++ def test_data_dir_env_overrides_default(self):+ target = self.base / "custom"+ with mock.patch.dict(os.environ, {paths.DATA_DIR_ENV: str(target)}):+ self.assertEqual(paths.data_dir(), target.resolve())+ self.assertEqual(paths.clone_output_dir(), target.resolve() / "element_clones")+++class CheckoutLayoutTests(unittest.TestCase):+ def test_checkout_keeps_repository_defaults(self):+ with mock.patch.dict(os.environ, {}, clear=False):+ os.environ.pop(paths.DATA_DIR_ENV, None)+ self.assertEqual(paths.source_checkout_root(), ROOT)+ self.assertEqual(paths.clone_output_dir(), ROOT / "element_clones")+ self.assertEqual(paths.default_upload_root(), ROOT)+++class PackageContentsTests(unittest.TestCase):+ def test_every_js_template_is_package_data(self):+ source = (ROOT / "src" / "stealth_browser_mcp" / "element_cloner.py").read_text(encoding="utf-8")+ templates = set(re.findall(r"'(\w+\.js)'", source))+ self.assertTrue(templates)+ for name in templates:+ self.assertTrue((element_cloner.JS_DIR / name).is_file(), name)++ def test_main_accepts_argv(self):+ with mock.patch("sys.stdout"), self.assertRaises(SystemExit) as raised:+ server.main(["--list-sections"])+ self.assertEqual(raised.exception.code, 0)++ @unittest.skipIf(tomllib is None, "tomllib needs Python 3.11")+ def test_console_script_points_at_main(self):+ project = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))+ self.assertEqual(project["project"]["scripts"]["stealth-browser-mcp"], "stealth_browser_mcp.server:main")+ self.assertEqual(project["tool"]["hatch"]["build"]["targets"]["wheel"]["packages"], ["src/stealth_browser_mcp"])+ for requirement in project["project"]["dependencies"]:+ self.assertNotIn("@", requirement, "PyPI rejects direct URL dependencies")+++if __name__ == "__main__":+ unittest.main()diff --git a/tests/test_page_bindings.py b/tests/test_page_bindings.pyindex 532fe44..cdc4de2 100644--- a/tests/test_page_bindings.py+++ b/tests/test_page_bindings.py@@ -10,8 +10,8 @@ sys.path.insert(0, str(ROOT / "src"))import nodriver as uc-import page_bindings as page_bindings_module-from page_bindings import MAX_PAYLOAD_BYTES, PageBindingManager+from stealth_browser_mcp import page_bindings as page_bindings_module+from stealth_browser_mcp.page_bindings import MAX_PAYLOAD_BYTES, PageBindingManagerclass FakeTab:diff --git a/tests/test_page_state.py b/tests/test_page_state.pyindex 8d08ddc..297418c 100644--- a/tests/test_page_state.py+++ b/tests/test_page_state.py@@ -9,8 +9,8 @@ sys.path.insert(0, str(ROOT / "src"))import nodriver as uc-from browser_manager import BrowserManager-from models import PageState+from stealth_browser_mcp.browser_manager import BrowserManager+from stealth_browser_mcp.models import PageStateRUN_BROWSER_TESTS = os.getenv("STEALTH_BROWSER_TESTS", "").strip().lower() in {"1",@@ -151,7 +151,7 @@ class GetPageStateTests(unittest.TestCase):)class GetPageStateBrowserTests(unittest.TestCase):def test_real_page_state(self):- from models import BrowserOptions+ from stealth_browser_mcp.models import BrowserOptionsasync def run():manager = BrowserManager()diff --git a/tests/test_platform_sandbox.py b/tests/test_platform_sandbox.pyindex 0d9fa38..8534881 100644--- a/tests/test_platform_sandbox.py+++ b/tests/test_platform_sandbox.py@@ -6,7 +6,7 @@ from unittest import mockROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-import platform_utils+from stealth_browser_mcp import platform_utilsdef environment(system, root, container):diff --git a/tests/test_press_key.py b/tests/test_press_key.pyindex 3c81906..31077d0 100644--- a/tests/test_press_key.py+++ b/tests/test_press_key.py@@ -8,8 +8,8 @@ from urllib.parse import quoteROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-from dom_handler import DOMHandler-from key_definitions import (+from stealth_browser_mcp.dom_handler import DOMHandler+from stealth_browser_mcp.key_definitions import (MODIFIER_ALT,MODIFIER_CONTROL,MODIFIER_META,@@ -217,8 +217,8 @@ class PressKeyDispatchTests(unittest.IsolatedAsyncioTestCase):)class PressKeyBrowserTests(unittest.IsolatedAsyncioTestCase):async def test_dispatched_events_are_trusted(self):- from browser_manager import BrowserManager- from models import BrowserOptions+ from stealth_browser_mcp.browser_manager import BrowserManager+ from stealth_browser_mcp.models import BrowserOptionsmanager = BrowserManager()instance = await manager.spawn_browser(diff --git a/tests/test_proxy_forwarder.py b/tests/test_proxy_forwarder.pyindex 0404976..d2ae3ff 100644--- a/tests/test_proxy_forwarder.py+++ b/tests/test_proxy_forwarder.py@@ -7,7 +7,7 @@ from pathlib import PathROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-from proxy_forwarder import AuthenticatedProxyForwarder+from stealth_browser_mcp.proxy_forwarder import AuthenticatedProxyForwarderRESPONSE = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"diff --git a/tests/test_screenshot_content.py b/tests/test_screenshot_content.pyindex cd45072..01297d6 100644--- a/tests/test_screenshot_content.py+++ b/tests/test_screenshot_content.py@@ -25,8 +25,8 @@ from PIL import ImageROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-import screenshot_encoder-from screenshot_encoder import DEFAULT_QUALITY, fit_screenshot, validate_options+from stealth_browser_mcp import screenshot_encoder+from stealth_browser_mcp.screenshot_encoder import DEFAULT_QUALITY, fit_screenshot, validate_optionsclass ScreenshotContentTests(unittest.TestCase):@@ -43,7 +43,7 @@ class ScreenshotContentTests(unittest.TestCase):def server(self):# Load the exact production function without importing server.py's global# cleanup/browser objects. Registration and client serialization are real.- tree = ast.parse((ROOT / "src/server.py").read_text())+ tree = ast.parse((ROOT / "src/stealth_browser_mcp/server.py").read_text())function = next(n for n in tree.body if isinstance(n, ast.AsyncFunctionDef)and n.name == "take_screenshot")mcp = FastMCP("screenshot-wire-test")@@ -70,7 +70,7 @@ class ScreenshotContentTests(unittest.TestCase):tempfile=tempfile, os=os, base64=base64, json=json, DEFAULT_QUALITY=DEFAULT_QUALITY,fit_screenshot=fit_screenshot, validate_screenshot_options=validate_options,)- exec(compile(ast.Module(body=[function], type_ignores=[]), str(ROOT / "src/server.py"), "exec"), namespace)+ exec(compile(ast.Module(body=[function], type_ignores=[]), str(ROOT / "src/stealth_browser_mcp/server.py"), "exec"), namespace)return mcpdef call(self, **kwargs):diff --git a/tests/test_stdio_guard.py b/tests/test_stdio_guard.pyindex 838084e..004484f 100644--- a/tests/test_stdio_guard.py+++ b/tests/test_stdio_guard.py@@ -6,8 +6,8 @@ from pathlib import PathROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "src"))-from response_handler import ResponseHandler-from stdio_guard import install_stdio_guard, is_guard_active, reset_stdio_guard_for_tests+from stealth_browser_mcp.response_handler import ResponseHandler+from stealth_browser_mcp.stdio_guard import install_stdio_guard, is_guard_active, reset_stdio_guard_for_testsclass FakeStdout:diff --git a/tests/test_tabs_and_contexts.py b/tests/test_tabs_and_contexts.pyindex 36df0f4..e492198 100644--- a/tests/test_tabs_and_contexts.py+++ b/tests/test_tabs_and_contexts.py@@ -9,9 +9,9 @@ sys.path.insert(0, str(ROOT / "src"))import nodriver as uc-from browser_manager import BrowserManager-from cdp_function_executor import CDPFunctionExecutor-from models import BrowserOptions+from stealth_browser_mcp.browser_manager import BrowserManager+from stealth_browser_mcp.cdp_function_executor import CDPFunctionExecutor+from stealth_browser_mcp.models import BrowserOptionsclass FakeTab:diff --git a/tests/test_tool_schemas.py b/tests/test_tool_schemas.pyindex 1ac096e..2d7df5b 100644--- a/tests/test_tool_schemas.py+++ b/tests/test_tool_schemas.py@@ -8,7 +8,7 @@ sys.path.insert(0, str(ROOT / "src"))from fastmcp import Client-import server+from stealth_browser_mcp import server# Tool parameters that default to None and must therefore accept an explicit null.NULL_DEFAULT_PARAMS = {