Skip to content

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

fix: redirect logging to stderr so MCP stdio doesn't break (#2748)

Laith Weinberger4d05218d7d5c
diff --git a/browser_use/cli.py b/browser_use/cli.py
index 7ad1e88fd..5b32f1779 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -169,7 +169,9 @@ try:
from textual.containers import Container, HorizontalGroup, VerticalScroll
from textual.widgets import Footer, Header, Input, Label, Link, RichLog, Static
except ImportError:
- print('⚠️ CLI addon is not installed. Please install it with: `pip install "browser-use[cli]"` and try again.')
+ print(
+ '⚠️ CLI addon is not installed. Please install it with: `pip install "browser-use[cli]"` and try again.', file=sys.stderr
+ )
sys.exit(1)
diff --git a/browser_use/logging_config.py b/browser_use/logging_config.py
index 863ce6f17..950d0479f 100644
--- a/browser_use/logging_config.py
+++ b/browser_use/logging_config.py
@@ -112,7 +112,7 @@ def setup_logging(stream=None, log_level=None, force_setup=False, debug_log_file
return super().format(record)
# Setup single handler for all loggers
- console = logging.StreamHandler(stream or sys.stdout)
+ console = logging.StreamHandler(stream or sys.stderr)
# Determine the log level to use first
if log_type == 'result':
@@ -184,7 +184,7 @@ def setup_logging(stream=None, log_level=None, force_setup=False, debug_log_file
# Use the CDP-specific logging level
setup_cdp_logging(
level=cdp_level,
- stream=stream or sys.stdout,
+ stream=stream or sys.stderr,
format_string='%(levelname)-8s [%(name)s] %(message)s' if log_type != 'result' else '%(message)s',
)
except ImportError:
diff --git a/browser_use/mcp/server.py b/browser_use/mcp/server.py
index f76669376..680a65584 100644
--- a/browser_use/mcp/server.py
+++ b/browser_use/mcp/server.py
@@ -26,12 +26,12 @@ Or as an MCP server in Claude Desktop or other MCP clients:
import os
import sys
-from browser_use.llm import ChatAWSBedrock
-
# Set environment variables BEFORE any browser_use imports to prevent early logging
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'critical'
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
+from browser_use.llm import ChatAWSBedrock
+
import asyncio
import json
import logging