Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Bridge BrowserProfile CDP headers

Magnus Müllera747f4c36858
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index e9b92b070..b9a23e718 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -181,10 +181,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- The terminal core applies compatible cookies with CDP `Storage.setCookies` and installs origin-scoped localStorage/sessionStorage init scripts with `Page.addScriptToEvaluateOnNewDocument`.
- Proof: `test_rust_agent_translates_browser_profile_storage_state` and `browser_storage_state_env_parses_cookies_and_storage_scripts`.
+35. BrowserProfile CDP headers bridge
+ - `BrowserProfile.headers` is serialized into `BU_CDP_HEADERS`.
+ - The terminal core applies those headers when resolving HTTP CDP endpoints through `/json/version` and when opening CDP WebSocket connections.
+ - Proof: `test_rust_agent_translates_browser_profile_cdp_headers` and `cdp_headers_env_builds_websocket_request_headers`.
+
## Current Verification
- `python3 -m py_compile browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (44 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (45 tests)
- `cargo build -q -p browser-use-cli`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`
@@ -193,6 +198,7 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_download_behavior_env_ -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_viewport_env_ -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser browser_storage_state_env_parses_cookies_and_storage_scripts -- --nocapture`
+- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-browser cdp_headers_env_builds_websocket_request_headers -- --nocapture`
- Managed-headless end-to-end:
- `BROWSER_USE_TERMINAL_BINARY=/home/exedev/Developer/terminal/target/debug/browser-use-terminal BROWSER_USE_RUST_BROWSER_MODE=managed-headless BU_TASK='Open https://example.com and report the page title only.' BU_MAX_STEPS=12 timeout 300 uv run python examples/rust_agent/basic.py`
- Output: `Example Domain`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 6a64da53a..dcbc43504 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -229,6 +229,20 @@ def _managed_browser_env(browser_session: BrowserSession | None, browser_profile
return env
+def _extract_cdp_headers(browser_session: BrowserSession | None, browser_profile: BrowserProfile | None) -> dict[str, str]:
+ headers: dict[str, str] = {}
+ session_profile = getattr(browser_session, 'browser_profile', None)
+ for profile in (session_profile, browser_profile, browser_session):
+ raw_headers = getattr(profile, 'headers', None)
+ if not isinstance(raw_headers, dict):
+ continue
+ for key, value in raw_headers.items():
+ header_value = _env_value_to_str(value)
+ if isinstance(key, str) and key and header_value is not None:
+ headers[key] = header_value
+ return headers
+
+
def _extract_profile_permissions(browser_session: BrowserSession | None, browser_profile: BrowserProfile | None) -> list[str]:
values: list[str] = []
seen: set[str] = set()
@@ -877,6 +891,7 @@ class Agent(Generic[AgentStructuredOutput]):
self.managed_browser_profile_dir = _managed_browser_profile_dir(self.browser_session, self.browser_profile)
self.managed_browser_executable_path = _managed_browser_executable_path(self.browser_session, self.browser_profile)
self.managed_browser_env = _managed_browser_env(self.browser_session, self.browser_profile)
+ self.cdp_headers = _extract_cdp_headers(self.browser_session, self.browser_profile)
self.browser_permissions = _extract_profile_permissions(self.browser_session, self.browser_profile)
self.browser_accept_downloads, self.browser_downloads_path = _extract_browser_downloads(
self.browser_session, self.browser_profile
@@ -1330,6 +1345,8 @@ class Agent(Generic[AgentStructuredOutput]):
cdp_url = _extract_cdp_url(self.browser_session) or _extract_profile_cdp_url(self.browser_profile)
if cdp_url:
env['BU_CDP_URL'] = cdp_url
+ if self.cdp_headers:
+ env['BU_CDP_HEADERS'] = json.dumps(self.cdp_headers)
if self.browser_permissions:
env['BU_BROWSER_PERMISSIONS'] = json.dumps(self.browser_permissions)
if self.browser_accept_downloads is not None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index a94afd684..093dcb6d9 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -180,6 +180,27 @@ def test_rust_agent_translates_browser_profile_cdp_url(monkeypatch):
assert env['LLM_BROWSER_BROWSER_MODE'] == 'remote-cdp'
+def test_rust_agent_translates_browser_profile_cdp_headers(monkeypatch):
+ from browser_use.rust import Agent
+
+ class BrowserProfile:
+ cdp_url = 'http://127.0.0.1:9222'
+ headers = {'Authorization': 'Bearer test-token', 'X-Browser-Use': 'rust', 'Retries': 3}
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+
+ agent = Agent(task='report title', browser_profile=BrowserProfile())
+ env = agent._run_env()
+
+ assert env['LLM_BROWSER_BROWSER_MODE'] == 'remote-cdp'
+ assert agent.cdp_headers == {
+ 'Authorization': 'Bearer test-token',
+ 'X-Browser-Use': 'rust',
+ 'Retries': '3',
+ }
+ assert json.loads(env['BU_CDP_HEADERS']) == agent.cdp_headers
+
+
def test_rust_agent_translates_browser_profile_headless(monkeypatch):
from browser_use.rust import Agent