Skip to content

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

shared mcp runner + max_dim guard

Laith Weinberger133761dbbdf4
diff --git a/browser_use/cli.py b/browser_use/cli.py
index fce9f010c..30e795293 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -57,8 +57,10 @@ def _capture_via_harness(
pass
-def _run_mcp_server() -> None:
+def _run_mcp_stdio_server(module_name: str) -> None:
+ """Silence all logging"""
import asyncio
+ import importlib
import logging
import os
@@ -66,23 +68,16 @@ def _run_mcp_server() -> None:
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
logging.disable(logging.CRITICAL)
- from browser_use.mcp.server import main as mcp_main
-
- asyncio.run(mcp_main())
-
+ main = importlib.import_module(module_name).main
+ asyncio.run(main())
-def _run_cli_mcp_server() -> None:
- import asyncio
- import logging
- import os
- os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'critical'
- os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
- logging.disable(logging.CRITICAL)
+def _run_mcp_server() -> None:
+ _run_mcp_stdio_server('browser_use.mcp.server')
- from browser_use.mcp.cli_mcp import main as cli_mcp_main
- asyncio.run(cli_mcp_main())
+def _run_cli_mcp_server() -> None:
+ _run_mcp_stdio_server('browser_use.mcp.cli_mcp')
def _run_install_command(argv: list[str]) -> int:
diff --git a/browser_use/mcp/cli_mcp.py b/browser_use/mcp/cli_mcp.py
index 156df2c55..e1aa86105 100644
--- a/browser_use/mcp/cli_mcp.py
+++ b/browser_use/mcp/cli_mcp.py
@@ -67,6 +67,7 @@ class CLIMCPServer:
'full': {'type': 'boolean', 'description': 'Capture beyond the viewport (full page)', 'default': False},
'max_dim': {
'type': 'integer',
+ 'minimum': 1,
'description': 'Downscale so no side exceeds this many pixels (e.g. 1800 for 2x displays)',
},
},
@@ -90,8 +91,11 @@ class CLIMCPServer:
output = await asyncio.to_thread(self._execute, code)
return [types.TextContent(type='text', text=output or '(no output)')]
if name == 'browser_screenshot':
+ max_dim = arguments.get('max_dim')
+ if max_dim is not None and (isinstance(max_dim, bool) or not isinstance(max_dim, int) or max_dim < 1):
+ return [types.TextContent(type='text', text="Error: 'max_dim' must be a positive integer")]
async with self._exec_lock:
- png = await asyncio.to_thread(self._screenshot, bool(arguments.get('full', False)), arguments.get('max_dim'))
+ png = await asyncio.to_thread(self._screenshot, bool(arguments.get('full', False)), max_dim)
return [types.ImageContent(type='image', data=png, mimeType='image/png')]
return [types.TextContent(type='text', text=f'Unknown tool: {name}')]