optexity-browser-use 0.9.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- browser_use/__init__.py +157 -0
- browser_use/actor/__init__.py +11 -0
- browser_use/actor/element.py +1175 -0
- browser_use/actor/mouse.py +134 -0
- browser_use/actor/page.py +561 -0
- browser_use/actor/playground/flights.py +41 -0
- browser_use/actor/playground/mixed_automation.py +54 -0
- browser_use/actor/playground/playground.py +236 -0
- browser_use/actor/utils.py +176 -0
- browser_use/agent/cloud_events.py +282 -0
- browser_use/agent/gif.py +424 -0
- browser_use/agent/judge.py +170 -0
- browser_use/agent/message_manager/service.py +473 -0
- browser_use/agent/message_manager/utils.py +52 -0
- browser_use/agent/message_manager/views.py +98 -0
- browser_use/agent/prompts.py +413 -0
- browser_use/agent/service.py +2316 -0
- browser_use/agent/system_prompt.md +185 -0
- browser_use/agent/system_prompt_flash.md +10 -0
- browser_use/agent/system_prompt_no_thinking.md +183 -0
- browser_use/agent/views.py +743 -0
- browser_use/browser/__init__.py +41 -0
- browser_use/browser/cloud/cloud.py +203 -0
- browser_use/browser/cloud/views.py +89 -0
- browser_use/browser/events.py +578 -0
- browser_use/browser/profile.py +1158 -0
- browser_use/browser/python_highlights.py +548 -0
- browser_use/browser/session.py +3225 -0
- browser_use/browser/session_manager.py +399 -0
- browser_use/browser/video_recorder.py +162 -0
- browser_use/browser/views.py +200 -0
- browser_use/browser/watchdog_base.py +260 -0
- browser_use/browser/watchdogs/__init__.py +0 -0
- browser_use/browser/watchdogs/aboutblank_watchdog.py +253 -0
- browser_use/browser/watchdogs/crash_watchdog.py +335 -0
- browser_use/browser/watchdogs/default_action_watchdog.py +2729 -0
- browser_use/browser/watchdogs/dom_watchdog.py +817 -0
- browser_use/browser/watchdogs/downloads_watchdog.py +1277 -0
- browser_use/browser/watchdogs/local_browser_watchdog.py +461 -0
- browser_use/browser/watchdogs/permissions_watchdog.py +43 -0
- browser_use/browser/watchdogs/popups_watchdog.py +143 -0
- browser_use/browser/watchdogs/recording_watchdog.py +126 -0
- browser_use/browser/watchdogs/screenshot_watchdog.py +62 -0
- browser_use/browser/watchdogs/security_watchdog.py +280 -0
- browser_use/browser/watchdogs/storage_state_watchdog.py +335 -0
- browser_use/cli.py +2359 -0
- browser_use/code_use/__init__.py +16 -0
- browser_use/code_use/formatting.py +192 -0
- browser_use/code_use/namespace.py +665 -0
- browser_use/code_use/notebook_export.py +276 -0
- browser_use/code_use/service.py +1340 -0
- browser_use/code_use/system_prompt.md +574 -0
- browser_use/code_use/utils.py +150 -0
- browser_use/code_use/views.py +171 -0
- browser_use/config.py +505 -0
- browser_use/controller/__init__.py +3 -0
- browser_use/dom/enhanced_snapshot.py +161 -0
- browser_use/dom/markdown_extractor.py +169 -0
- browser_use/dom/playground/extraction.py +312 -0
- browser_use/dom/playground/multi_act.py +32 -0
- browser_use/dom/serializer/clickable_elements.py +200 -0
- browser_use/dom/serializer/code_use_serializer.py +287 -0
- browser_use/dom/serializer/eval_serializer.py +478 -0
- browser_use/dom/serializer/html_serializer.py +212 -0
- browser_use/dom/serializer/paint_order.py +197 -0
- browser_use/dom/serializer/serializer.py +1170 -0
- browser_use/dom/service.py +825 -0
- browser_use/dom/utils.py +129 -0
- browser_use/dom/views.py +906 -0
- browser_use/exceptions.py +5 -0
- browser_use/filesystem/__init__.py +0 -0
- browser_use/filesystem/file_system.py +619 -0
- browser_use/init_cmd.py +376 -0
- browser_use/integrations/gmail/__init__.py +24 -0
- browser_use/integrations/gmail/actions.py +115 -0
- browser_use/integrations/gmail/service.py +225 -0
- browser_use/llm/__init__.py +155 -0
- browser_use/llm/anthropic/chat.py +242 -0
- browser_use/llm/anthropic/serializer.py +312 -0
- browser_use/llm/aws/__init__.py +36 -0
- browser_use/llm/aws/chat_anthropic.py +242 -0
- browser_use/llm/aws/chat_bedrock.py +289 -0
- browser_use/llm/aws/serializer.py +257 -0
- browser_use/llm/azure/chat.py +91 -0
- browser_use/llm/base.py +57 -0
- browser_use/llm/browser_use/__init__.py +3 -0
- browser_use/llm/browser_use/chat.py +201 -0
- browser_use/llm/cerebras/chat.py +193 -0
- browser_use/llm/cerebras/serializer.py +109 -0
- browser_use/llm/deepseek/chat.py +212 -0
- browser_use/llm/deepseek/serializer.py +109 -0
- browser_use/llm/exceptions.py +29 -0
- browser_use/llm/google/__init__.py +3 -0
- browser_use/llm/google/chat.py +542 -0
- browser_use/llm/google/serializer.py +120 -0
- browser_use/llm/groq/chat.py +229 -0
- browser_use/llm/groq/parser.py +158 -0
- browser_use/llm/groq/serializer.py +159 -0
- browser_use/llm/messages.py +238 -0
- browser_use/llm/models.py +271 -0
- browser_use/llm/oci_raw/__init__.py +10 -0
- browser_use/llm/oci_raw/chat.py +443 -0
- browser_use/llm/oci_raw/serializer.py +229 -0
- browser_use/llm/ollama/chat.py +97 -0
- browser_use/llm/ollama/serializer.py +143 -0
- browser_use/llm/openai/chat.py +264 -0
- browser_use/llm/openai/like.py +15 -0
- browser_use/llm/openai/serializer.py +165 -0
- browser_use/llm/openrouter/chat.py +211 -0
- browser_use/llm/openrouter/serializer.py +26 -0
- browser_use/llm/schema.py +176 -0
- browser_use/llm/views.py +48 -0
- browser_use/logging_config.py +330 -0
- browser_use/mcp/__init__.py +18 -0
- browser_use/mcp/__main__.py +12 -0
- browser_use/mcp/client.py +544 -0
- browser_use/mcp/controller.py +264 -0
- browser_use/mcp/server.py +1114 -0
- browser_use/observability.py +204 -0
- browser_use/py.typed +0 -0
- browser_use/sandbox/__init__.py +41 -0
- browser_use/sandbox/sandbox.py +637 -0
- browser_use/sandbox/views.py +132 -0
- browser_use/screenshots/__init__.py +1 -0
- browser_use/screenshots/service.py +52 -0
- browser_use/sync/__init__.py +6 -0
- browser_use/sync/auth.py +357 -0
- browser_use/sync/service.py +161 -0
- browser_use/telemetry/__init__.py +51 -0
- browser_use/telemetry/service.py +112 -0
- browser_use/telemetry/views.py +101 -0
- browser_use/tokens/__init__.py +0 -0
- browser_use/tokens/custom_pricing.py +24 -0
- browser_use/tokens/mappings.py +4 -0
- browser_use/tokens/service.py +580 -0
- browser_use/tokens/views.py +108 -0
- browser_use/tools/registry/service.py +572 -0
- browser_use/tools/registry/views.py +174 -0
- browser_use/tools/service.py +1675 -0
- browser_use/tools/utils.py +82 -0
- browser_use/tools/views.py +100 -0
- browser_use/utils.py +670 -0
- optexity_browser_use-0.9.5.dist-info/METADATA +344 -0
- optexity_browser_use-0.9.5.dist-info/RECORD +147 -0
- optexity_browser_use-0.9.5.dist-info/WHEEL +4 -0
- optexity_browser_use-0.9.5.dist-info/entry_points.txt +3 -0
- optexity_browser_use-0.9.5.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,1114 @@
|
|
|
1
|
+
"""MCP Server for browser-use - exposes browser automation capabilities via Model Context Protocol.
|
|
2
|
+
|
|
3
|
+
This server provides tools for:
|
|
4
|
+
- Running autonomous browser tasks with an AI agent
|
|
5
|
+
- Direct browser control (navigation, clicking, typing, etc.)
|
|
6
|
+
- Content extraction from web pages
|
|
7
|
+
- File system operations
|
|
8
|
+
|
|
9
|
+
Usage:
|
|
10
|
+
uvx browser-use --mcp
|
|
11
|
+
|
|
12
|
+
Or as an MCP server in Claude Desktop or other MCP clients:
|
|
13
|
+
{
|
|
14
|
+
"mcpServers": {
|
|
15
|
+
"browser-use": {
|
|
16
|
+
"command": "uvx",
|
|
17
|
+
"args": ["browser-use[cli]", "--mcp"],
|
|
18
|
+
"env": {
|
|
19
|
+
"OPENAI_API_KEY": "sk-proj-1234567890",
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
import os
|
|
27
|
+
import sys
|
|
28
|
+
|
|
29
|
+
from browser_use.llm import ChatAWSBedrock
|
|
30
|
+
|
|
31
|
+
# Set environment variables BEFORE any browser_use imports to prevent early logging
|
|
32
|
+
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'critical'
|
|
33
|
+
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
|
|
34
|
+
|
|
35
|
+
import asyncio
|
|
36
|
+
import json
|
|
37
|
+
import logging
|
|
38
|
+
import time
|
|
39
|
+
from pathlib import Path
|
|
40
|
+
from typing import Any
|
|
41
|
+
|
|
42
|
+
# Configure logging for MCP mode - redirect to stderr but preserve critical diagnostics
|
|
43
|
+
logging.basicConfig(
|
|
44
|
+
stream=sys.stderr, level=logging.WARNING, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', force=True
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
import psutil
|
|
49
|
+
|
|
50
|
+
PSUTIL_AVAILABLE = True
|
|
51
|
+
except ImportError:
|
|
52
|
+
PSUTIL_AVAILABLE = False
|
|
53
|
+
|
|
54
|
+
# Add browser-use to path if running from source
|
|
55
|
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
56
|
+
|
|
57
|
+
# Import and configure logging to use stderr before other imports
|
|
58
|
+
from browser_use.logging_config import setup_logging
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _configure_mcp_server_logging():
|
|
62
|
+
"""Configure logging for MCP server mode - redirect all logs to stderr to prevent JSON RPC interference."""
|
|
63
|
+
# Set environment to suppress browser-use logging during server mode
|
|
64
|
+
os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'warning'
|
|
65
|
+
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false' # Prevent automatic logging setup
|
|
66
|
+
|
|
67
|
+
# Configure logging to stderr for MCP mode - preserve warnings and above for troubleshooting
|
|
68
|
+
setup_logging(stream=sys.stderr, log_level='warning', force_setup=True)
|
|
69
|
+
|
|
70
|
+
# Also configure the root logger and all existing loggers to use stderr
|
|
71
|
+
logging.root.handlers = []
|
|
72
|
+
stderr_handler = logging.StreamHandler(sys.stderr)
|
|
73
|
+
stderr_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
|
74
|
+
logging.root.addHandler(stderr_handler)
|
|
75
|
+
logging.root.setLevel(logging.CRITICAL)
|
|
76
|
+
|
|
77
|
+
# Configure all existing loggers to use stderr and CRITICAL level
|
|
78
|
+
for name in list(logging.root.manager.loggerDict.keys()):
|
|
79
|
+
logger_obj = logging.getLogger(name)
|
|
80
|
+
logger_obj.handlers = []
|
|
81
|
+
logger_obj.setLevel(logging.CRITICAL)
|
|
82
|
+
logger_obj.addHandler(stderr_handler)
|
|
83
|
+
logger_obj.propagate = False
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# Configure MCP server logging before any browser_use imports to capture early log lines
|
|
87
|
+
_configure_mcp_server_logging()
|
|
88
|
+
|
|
89
|
+
# Additional suppression - disable all logging completely for MCP mode
|
|
90
|
+
logging.disable(logging.CRITICAL)
|
|
91
|
+
|
|
92
|
+
# Import browser_use modules
|
|
93
|
+
from browser_use import ActionModel, Agent
|
|
94
|
+
from browser_use.browser import BrowserProfile, BrowserSession
|
|
95
|
+
from browser_use.config import get_default_llm, get_default_profile, load_browser_use_config
|
|
96
|
+
from browser_use.filesystem.file_system import FileSystem
|
|
97
|
+
from browser_use.llm.openai.chat import ChatOpenAI
|
|
98
|
+
from browser_use.tools.service import Tools
|
|
99
|
+
|
|
100
|
+
logger = logging.getLogger(__name__)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _ensure_all_loggers_use_stderr():
|
|
104
|
+
"""Ensure ALL loggers only output to stderr, not stdout."""
|
|
105
|
+
# Get the stderr handler
|
|
106
|
+
stderr_handler = None
|
|
107
|
+
for handler in logging.root.handlers:
|
|
108
|
+
if hasattr(handler, 'stream') and handler.stream == sys.stderr: # type: ignore
|
|
109
|
+
stderr_handler = handler
|
|
110
|
+
break
|
|
111
|
+
|
|
112
|
+
if not stderr_handler:
|
|
113
|
+
stderr_handler = logging.StreamHandler(sys.stderr)
|
|
114
|
+
stderr_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
|
115
|
+
|
|
116
|
+
# Configure root logger
|
|
117
|
+
logging.root.handlers = [stderr_handler]
|
|
118
|
+
logging.root.setLevel(logging.CRITICAL)
|
|
119
|
+
|
|
120
|
+
# Configure all existing loggers
|
|
121
|
+
for name in list(logging.root.manager.loggerDict.keys()):
|
|
122
|
+
logger_obj = logging.getLogger(name)
|
|
123
|
+
logger_obj.handlers = [stderr_handler]
|
|
124
|
+
logger_obj.setLevel(logging.CRITICAL)
|
|
125
|
+
logger_obj.propagate = False
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# Ensure stderr logging after all imports
|
|
129
|
+
_ensure_all_loggers_use_stderr()
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# Try to import MCP SDK
|
|
133
|
+
try:
|
|
134
|
+
import mcp.server.stdio
|
|
135
|
+
import mcp.types as types
|
|
136
|
+
from mcp.server import NotificationOptions, Server
|
|
137
|
+
from mcp.server.models import InitializationOptions
|
|
138
|
+
|
|
139
|
+
MCP_AVAILABLE = True
|
|
140
|
+
|
|
141
|
+
# Configure MCP SDK logging to stderr as well
|
|
142
|
+
mcp_logger = logging.getLogger('mcp')
|
|
143
|
+
mcp_logger.handlers = []
|
|
144
|
+
mcp_logger.addHandler(logging.root.handlers[0] if logging.root.handlers else logging.StreamHandler(sys.stderr))
|
|
145
|
+
mcp_logger.setLevel(logging.ERROR)
|
|
146
|
+
mcp_logger.propagate = False
|
|
147
|
+
except ImportError:
|
|
148
|
+
MCP_AVAILABLE = False
|
|
149
|
+
logger.error('MCP SDK not installed. Install with: pip install mcp')
|
|
150
|
+
sys.exit(1)
|
|
151
|
+
|
|
152
|
+
from browser_use.telemetry import MCPServerTelemetryEvent, ProductTelemetry
|
|
153
|
+
from browser_use.utils import get_browser_use_version
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def get_parent_process_cmdline() -> str | None:
|
|
157
|
+
"""Get the command line of all parent processes up the chain."""
|
|
158
|
+
if not PSUTIL_AVAILABLE:
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
try:
|
|
162
|
+
cmdlines = []
|
|
163
|
+
current_process = psutil.Process()
|
|
164
|
+
parent = current_process.parent()
|
|
165
|
+
|
|
166
|
+
while parent:
|
|
167
|
+
try:
|
|
168
|
+
cmdline = parent.cmdline()
|
|
169
|
+
if cmdline:
|
|
170
|
+
cmdlines.append(' '.join(cmdline))
|
|
171
|
+
except (psutil.AccessDenied, psutil.NoSuchProcess):
|
|
172
|
+
# Skip processes we can't access (like system processes)
|
|
173
|
+
pass
|
|
174
|
+
|
|
175
|
+
try:
|
|
176
|
+
parent = parent.parent()
|
|
177
|
+
except (psutil.AccessDenied, psutil.NoSuchProcess):
|
|
178
|
+
# Can't go further up the chain
|
|
179
|
+
break
|
|
180
|
+
|
|
181
|
+
return ';'.join(cmdlines) if cmdlines else None
|
|
182
|
+
except Exception:
|
|
183
|
+
# If we can't get parent process info, just return None
|
|
184
|
+
return None
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class BrowserUseServer:
|
|
188
|
+
"""MCP Server for browser-use capabilities."""
|
|
189
|
+
|
|
190
|
+
def __init__(self, session_timeout_minutes: int = 10):
|
|
191
|
+
# Ensure all logging goes to stderr (in case new loggers were created)
|
|
192
|
+
_ensure_all_loggers_use_stderr()
|
|
193
|
+
|
|
194
|
+
self.server = Server('browser-use')
|
|
195
|
+
self.config = load_browser_use_config()
|
|
196
|
+
self.agent: Agent | None = None
|
|
197
|
+
self.browser_session: BrowserSession | None = None
|
|
198
|
+
self.tools: Tools | None = None
|
|
199
|
+
self.llm: ChatOpenAI | None = None
|
|
200
|
+
self.file_system: FileSystem | None = None
|
|
201
|
+
self._telemetry = ProductTelemetry()
|
|
202
|
+
self._start_time = time.time()
|
|
203
|
+
|
|
204
|
+
# Session management
|
|
205
|
+
self.active_sessions: dict[str, dict[str, Any]] = {} # session_id -> session info
|
|
206
|
+
self.session_timeout_minutes = session_timeout_minutes
|
|
207
|
+
self._cleanup_task: Any = None
|
|
208
|
+
|
|
209
|
+
# Setup handlers
|
|
210
|
+
self._setup_handlers()
|
|
211
|
+
|
|
212
|
+
def _setup_handlers(self):
|
|
213
|
+
"""Setup MCP server handlers."""
|
|
214
|
+
|
|
215
|
+
@self.server.list_tools()
|
|
216
|
+
async def handle_list_tools() -> list[types.Tool]:
|
|
217
|
+
"""List all available browser-use tools."""
|
|
218
|
+
return [
|
|
219
|
+
# Agent tools
|
|
220
|
+
# Direct browser control tools
|
|
221
|
+
types.Tool(
|
|
222
|
+
name='browser_navigate',
|
|
223
|
+
description='Navigate to a URL in the browser',
|
|
224
|
+
inputSchema={
|
|
225
|
+
'type': 'object',
|
|
226
|
+
'properties': {
|
|
227
|
+
'url': {'type': 'string', 'description': 'The URL to navigate to'},
|
|
228
|
+
'new_tab': {'type': 'boolean', 'description': 'Whether to open in a new tab', 'default': False},
|
|
229
|
+
},
|
|
230
|
+
'required': ['url'],
|
|
231
|
+
},
|
|
232
|
+
),
|
|
233
|
+
types.Tool(
|
|
234
|
+
name='browser_click',
|
|
235
|
+
description='Click an element on the page by its index',
|
|
236
|
+
inputSchema={
|
|
237
|
+
'type': 'object',
|
|
238
|
+
'properties': {
|
|
239
|
+
'index': {
|
|
240
|
+
'type': 'integer',
|
|
241
|
+
'description': 'The index of the link or element to click (from browser_get_state)',
|
|
242
|
+
},
|
|
243
|
+
'new_tab': {
|
|
244
|
+
'type': 'boolean',
|
|
245
|
+
'description': 'Whether to open any resulting navigation in a new tab',
|
|
246
|
+
'default': False,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
'required': ['index'],
|
|
250
|
+
},
|
|
251
|
+
),
|
|
252
|
+
types.Tool(
|
|
253
|
+
name='browser_type',
|
|
254
|
+
description='Type text into an input field',
|
|
255
|
+
inputSchema={
|
|
256
|
+
'type': 'object',
|
|
257
|
+
'properties': {
|
|
258
|
+
'index': {
|
|
259
|
+
'type': 'integer',
|
|
260
|
+
'description': 'The index of the input element (from browser_get_state)',
|
|
261
|
+
},
|
|
262
|
+
'text': {'type': 'string', 'description': 'The text to type'},
|
|
263
|
+
},
|
|
264
|
+
'required': ['index', 'text'],
|
|
265
|
+
},
|
|
266
|
+
),
|
|
267
|
+
types.Tool(
|
|
268
|
+
name='browser_get_state',
|
|
269
|
+
description='Get the current state of the page including all interactive elements',
|
|
270
|
+
inputSchema={
|
|
271
|
+
'type': 'object',
|
|
272
|
+
'properties': {
|
|
273
|
+
'include_screenshot': {
|
|
274
|
+
'type': 'boolean',
|
|
275
|
+
'description': 'Whether to include a screenshot of the current page',
|
|
276
|
+
'default': False,
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
),
|
|
281
|
+
types.Tool(
|
|
282
|
+
name='browser_extract_content',
|
|
283
|
+
description='Extract structured content from the current page based on a query',
|
|
284
|
+
inputSchema={
|
|
285
|
+
'type': 'object',
|
|
286
|
+
'properties': {
|
|
287
|
+
'query': {'type': 'string', 'description': 'What information to extract from the page'},
|
|
288
|
+
'extract_links': {
|
|
289
|
+
'type': 'boolean',
|
|
290
|
+
'description': 'Whether to include links in the extraction',
|
|
291
|
+
'default': False,
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
'required': ['query'],
|
|
295
|
+
},
|
|
296
|
+
),
|
|
297
|
+
types.Tool(
|
|
298
|
+
name='browser_scroll',
|
|
299
|
+
description='Scroll the page',
|
|
300
|
+
inputSchema={
|
|
301
|
+
'type': 'object',
|
|
302
|
+
'properties': {
|
|
303
|
+
'direction': {
|
|
304
|
+
'type': 'string',
|
|
305
|
+
'enum': ['up', 'down'],
|
|
306
|
+
'description': 'Direction to scroll',
|
|
307
|
+
'default': 'down',
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
),
|
|
312
|
+
types.Tool(
|
|
313
|
+
name='browser_go_back',
|
|
314
|
+
description='Go back to the previous page',
|
|
315
|
+
inputSchema={'type': 'object', 'properties': {}},
|
|
316
|
+
),
|
|
317
|
+
# Tab management
|
|
318
|
+
types.Tool(
|
|
319
|
+
name='browser_list_tabs', description='List all open tabs', inputSchema={'type': 'object', 'properties': {}}
|
|
320
|
+
),
|
|
321
|
+
types.Tool(
|
|
322
|
+
name='browser_switch_tab',
|
|
323
|
+
description='Switch to a different tab',
|
|
324
|
+
inputSchema={
|
|
325
|
+
'type': 'object',
|
|
326
|
+
'properties': {'tab_id': {'type': 'string', 'description': '4 Character Tab ID of the tab to switch to'}},
|
|
327
|
+
'required': ['tab_id'],
|
|
328
|
+
},
|
|
329
|
+
),
|
|
330
|
+
types.Tool(
|
|
331
|
+
name='browser_close_tab',
|
|
332
|
+
description='Close a tab',
|
|
333
|
+
inputSchema={
|
|
334
|
+
'type': 'object',
|
|
335
|
+
'properties': {'tab_id': {'type': 'string', 'description': '4 Character Tab ID of the tab to close'}},
|
|
336
|
+
'required': ['tab_id'],
|
|
337
|
+
},
|
|
338
|
+
),
|
|
339
|
+
# types.Tool(
|
|
340
|
+
# name="browser_close",
|
|
341
|
+
# description="Close the browser session",
|
|
342
|
+
# inputSchema={
|
|
343
|
+
# "type": "object",
|
|
344
|
+
# "properties": {}
|
|
345
|
+
# }
|
|
346
|
+
# ),
|
|
347
|
+
types.Tool(
|
|
348
|
+
name='retry_with_browser_use_agent',
|
|
349
|
+
description='Retry a task using the browser-use agent. Only use this as a last resort if you fail to interact with a page multiple times.',
|
|
350
|
+
inputSchema={
|
|
351
|
+
'type': 'object',
|
|
352
|
+
'properties': {
|
|
353
|
+
'task': {
|
|
354
|
+
'type': 'string',
|
|
355
|
+
'description': 'The high-level goal and detailed step-by-step description of the task the AI browser agent needs to attempt, along with any relevant data needed to complete the task and info about previous attempts.',
|
|
356
|
+
},
|
|
357
|
+
'max_steps': {
|
|
358
|
+
'type': 'integer',
|
|
359
|
+
'description': 'Maximum number of steps an agent can take.',
|
|
360
|
+
'default': 100,
|
|
361
|
+
},
|
|
362
|
+
'model': {
|
|
363
|
+
'type': 'string',
|
|
364
|
+
'description': 'LLM model to use (e.g., gpt-4o, claude-3-opus-20240229)',
|
|
365
|
+
'default': 'gpt-4o',
|
|
366
|
+
},
|
|
367
|
+
'allowed_domains': {
|
|
368
|
+
'type': 'array',
|
|
369
|
+
'items': {'type': 'string'},
|
|
370
|
+
'description': 'List of domains the agent is allowed to visit (security feature)',
|
|
371
|
+
'default': [],
|
|
372
|
+
},
|
|
373
|
+
'use_vision': {
|
|
374
|
+
'type': 'boolean',
|
|
375
|
+
'description': 'Whether to use vision capabilities (screenshots) for the agent',
|
|
376
|
+
'default': True,
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
'required': ['task'],
|
|
380
|
+
},
|
|
381
|
+
),
|
|
382
|
+
# Browser session management tools
|
|
383
|
+
types.Tool(
|
|
384
|
+
name='browser_list_sessions',
|
|
385
|
+
description='List all active browser sessions with their details and last activity time',
|
|
386
|
+
inputSchema={'type': 'object', 'properties': {}},
|
|
387
|
+
),
|
|
388
|
+
types.Tool(
|
|
389
|
+
name='browser_close_session',
|
|
390
|
+
description='Close a specific browser session by its ID',
|
|
391
|
+
inputSchema={
|
|
392
|
+
'type': 'object',
|
|
393
|
+
'properties': {
|
|
394
|
+
'session_id': {
|
|
395
|
+
'type': 'string',
|
|
396
|
+
'description': 'The browser session ID to close (get from browser_list_sessions)',
|
|
397
|
+
}
|
|
398
|
+
},
|
|
399
|
+
'required': ['session_id'],
|
|
400
|
+
},
|
|
401
|
+
),
|
|
402
|
+
types.Tool(
|
|
403
|
+
name='browser_close_all',
|
|
404
|
+
description='Close all active browser sessions and clean up resources',
|
|
405
|
+
inputSchema={'type': 'object', 'properties': {}},
|
|
406
|
+
),
|
|
407
|
+
]
|
|
408
|
+
|
|
409
|
+
@self.server.list_resources()
|
|
410
|
+
async def handle_list_resources() -> list[types.Resource]:
|
|
411
|
+
"""List available resources (none for browser-use)."""
|
|
412
|
+
return []
|
|
413
|
+
|
|
414
|
+
@self.server.list_prompts()
|
|
415
|
+
async def handle_list_prompts() -> list[types.Prompt]:
|
|
416
|
+
"""List available prompts (none for browser-use)."""
|
|
417
|
+
return []
|
|
418
|
+
|
|
419
|
+
@self.server.call_tool()
|
|
420
|
+
async def handle_call_tool(name: str, arguments: dict[str, Any] | None) -> list[types.TextContent]:
|
|
421
|
+
"""Handle tool execution."""
|
|
422
|
+
start_time = time.time()
|
|
423
|
+
error_msg = None
|
|
424
|
+
try:
|
|
425
|
+
result = await self._execute_tool(name, arguments or {})
|
|
426
|
+
return [types.TextContent(type='text', text=result)]
|
|
427
|
+
except Exception as e:
|
|
428
|
+
error_msg = str(e)
|
|
429
|
+
logger.error(f'Tool execution failed: {e}', exc_info=True)
|
|
430
|
+
return [types.TextContent(type='text', text=f'Error: {str(e)}')]
|
|
431
|
+
finally:
|
|
432
|
+
# Capture telemetry for tool calls
|
|
433
|
+
duration = time.time() - start_time
|
|
434
|
+
self._telemetry.capture(
|
|
435
|
+
MCPServerTelemetryEvent(
|
|
436
|
+
version=get_browser_use_version(),
|
|
437
|
+
action='tool_call',
|
|
438
|
+
tool_name=name,
|
|
439
|
+
duration_seconds=duration,
|
|
440
|
+
error_message=error_msg,
|
|
441
|
+
)
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
async def _execute_tool(self, tool_name: str, arguments: dict[str, Any]) -> str:
|
|
445
|
+
"""Execute a browser-use tool."""
|
|
446
|
+
|
|
447
|
+
# Agent-based tools
|
|
448
|
+
if tool_name == 'retry_with_browser_use_agent':
|
|
449
|
+
return await self._retry_with_browser_use_agent(
|
|
450
|
+
task=arguments['task'],
|
|
451
|
+
max_steps=arguments.get('max_steps', 100),
|
|
452
|
+
model=arguments.get('model', 'gpt-4o'),
|
|
453
|
+
allowed_domains=arguments.get('allowed_domains', []),
|
|
454
|
+
use_vision=arguments.get('use_vision', True),
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
# Browser session management tools (don't require active session)
|
|
458
|
+
if tool_name == 'browser_list_sessions':
|
|
459
|
+
return await self._list_sessions()
|
|
460
|
+
|
|
461
|
+
elif tool_name == 'browser_close_session':
|
|
462
|
+
return await self._close_session(arguments['session_id'])
|
|
463
|
+
|
|
464
|
+
elif tool_name == 'browser_close_all':
|
|
465
|
+
return await self._close_all_sessions()
|
|
466
|
+
|
|
467
|
+
# Direct browser control tools (require active session)
|
|
468
|
+
elif tool_name.startswith('browser_'):
|
|
469
|
+
# Ensure browser session exists
|
|
470
|
+
if not self.browser_session:
|
|
471
|
+
await self._init_browser_session()
|
|
472
|
+
|
|
473
|
+
if tool_name == 'browser_navigate':
|
|
474
|
+
return await self._navigate(arguments['url'], arguments.get('new_tab', False))
|
|
475
|
+
|
|
476
|
+
elif tool_name == 'browser_click':
|
|
477
|
+
return await self._click(arguments['index'], arguments.get('new_tab', False))
|
|
478
|
+
|
|
479
|
+
elif tool_name == 'browser_type':
|
|
480
|
+
return await self._type_text(arguments['index'], arguments['text'])
|
|
481
|
+
|
|
482
|
+
elif tool_name == 'browser_get_state':
|
|
483
|
+
return await self._get_browser_state(arguments.get('include_screenshot', False))
|
|
484
|
+
|
|
485
|
+
elif tool_name == 'browser_extract_content':
|
|
486
|
+
return await self._extract_content(arguments['query'], arguments.get('extract_links', False))
|
|
487
|
+
|
|
488
|
+
elif tool_name == 'browser_scroll':
|
|
489
|
+
return await self._scroll(arguments.get('direction', 'down'))
|
|
490
|
+
|
|
491
|
+
elif tool_name == 'browser_go_back':
|
|
492
|
+
return await self._go_back()
|
|
493
|
+
|
|
494
|
+
elif tool_name == 'browser_close':
|
|
495
|
+
return await self._close_browser()
|
|
496
|
+
|
|
497
|
+
elif tool_name == 'browser_list_tabs':
|
|
498
|
+
return await self._list_tabs()
|
|
499
|
+
|
|
500
|
+
elif tool_name == 'browser_switch_tab':
|
|
501
|
+
return await self._switch_tab(arguments['tab_id'])
|
|
502
|
+
|
|
503
|
+
elif tool_name == 'browser_close_tab':
|
|
504
|
+
return await self._close_tab(arguments['tab_id'])
|
|
505
|
+
|
|
506
|
+
return f'Unknown tool: {tool_name}'
|
|
507
|
+
|
|
508
|
+
async def _init_browser_session(self, allowed_domains: list[str] | None = None, **kwargs):
|
|
509
|
+
"""Initialize browser session using config"""
|
|
510
|
+
if self.browser_session:
|
|
511
|
+
return
|
|
512
|
+
|
|
513
|
+
# Ensure all logging goes to stderr before browser initialization
|
|
514
|
+
_ensure_all_loggers_use_stderr()
|
|
515
|
+
|
|
516
|
+
logger.debug('Initializing browser session...')
|
|
517
|
+
|
|
518
|
+
# Get profile config
|
|
519
|
+
profile_config = get_default_profile(self.config)
|
|
520
|
+
|
|
521
|
+
# Merge profile config with defaults and overrides
|
|
522
|
+
profile_data = {
|
|
523
|
+
'downloads_path': str(Path.home() / 'Downloads' / 'browser-use-mcp'),
|
|
524
|
+
'wait_between_actions': 0.5,
|
|
525
|
+
'keep_alive': True,
|
|
526
|
+
'user_data_dir': '~/.config/browseruse/profiles/default',
|
|
527
|
+
'device_scale_factor': 1.0,
|
|
528
|
+
'disable_security': False,
|
|
529
|
+
'headless': False,
|
|
530
|
+
**profile_config, # Config values override defaults
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
# Tool parameter overrides (highest priority)
|
|
534
|
+
if allowed_domains is not None:
|
|
535
|
+
profile_data['allowed_domains'] = allowed_domains
|
|
536
|
+
|
|
537
|
+
# Merge any additional kwargs that are valid BrowserProfile fields
|
|
538
|
+
for key, value in kwargs.items():
|
|
539
|
+
profile_data[key] = value
|
|
540
|
+
|
|
541
|
+
# Create browser profile
|
|
542
|
+
profile = BrowserProfile(**profile_data)
|
|
543
|
+
|
|
544
|
+
# Create browser session
|
|
545
|
+
self.browser_session = BrowserSession(browser_profile=profile)
|
|
546
|
+
await self.browser_session.start()
|
|
547
|
+
|
|
548
|
+
# Track the session for management
|
|
549
|
+
self._track_session(self.browser_session)
|
|
550
|
+
|
|
551
|
+
# Create tools for direct actions
|
|
552
|
+
self.tools = Tools()
|
|
553
|
+
|
|
554
|
+
# Initialize LLM from config
|
|
555
|
+
llm_config = get_default_llm(self.config)
|
|
556
|
+
if api_key := llm_config.get('api_key'):
|
|
557
|
+
self.llm = ChatOpenAI(
|
|
558
|
+
model=llm_config.get('model', 'gpt-4o-mini'),
|
|
559
|
+
api_key=api_key,
|
|
560
|
+
temperature=llm_config.get('temperature', 0.7),
|
|
561
|
+
# max_tokens=llm_config.get('max_tokens'),
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
# Initialize FileSystem for extraction actions
|
|
565
|
+
file_system_path = profile_config.get('file_system_path', '~/.browser-use-mcp')
|
|
566
|
+
self.file_system = FileSystem(base_dir=Path(file_system_path).expanduser())
|
|
567
|
+
|
|
568
|
+
logger.debug('Browser session initialized')
|
|
569
|
+
|
|
570
|
+
async def _retry_with_browser_use_agent(
|
|
571
|
+
self,
|
|
572
|
+
task: str,
|
|
573
|
+
max_steps: int = 100,
|
|
574
|
+
model: str = 'gpt-4o',
|
|
575
|
+
allowed_domains: list[str] | None = None,
|
|
576
|
+
use_vision: bool = True,
|
|
577
|
+
) -> str:
|
|
578
|
+
"""Run an autonomous agent task."""
|
|
579
|
+
logger.debug(f'Running agent task: {task}')
|
|
580
|
+
|
|
581
|
+
# Get LLM config
|
|
582
|
+
llm_config = get_default_llm(self.config)
|
|
583
|
+
|
|
584
|
+
# Get LLM provider
|
|
585
|
+
model_provider = llm_config.get('model_provider') or os.getenv('MODEL_PROVIDER')
|
|
586
|
+
|
|
587
|
+
# 如果model_provider不等于空,且等Bedrock
|
|
588
|
+
if model_provider and model_provider.lower() == 'bedrock':
|
|
589
|
+
llm_model = llm_config.get('model') or os.getenv('MODEL') or 'us.anthropic.claude-sonnet-4-20250514-v1:0'
|
|
590
|
+
aws_region = llm_config.get('region') or os.getenv('REGION')
|
|
591
|
+
if not aws_region:
|
|
592
|
+
aws_region = 'us-east-1'
|
|
593
|
+
llm = ChatAWSBedrock(
|
|
594
|
+
model=llm_model, # or any Bedrock model
|
|
595
|
+
aws_region=aws_region,
|
|
596
|
+
aws_sso_auth=True,
|
|
597
|
+
)
|
|
598
|
+
else:
|
|
599
|
+
api_key = llm_config.get('api_key') or os.getenv('OPENAI_API_KEY')
|
|
600
|
+
if not api_key:
|
|
601
|
+
return 'Error: OPENAI_API_KEY not set in config or environment'
|
|
602
|
+
|
|
603
|
+
# Override model if provided in tool call
|
|
604
|
+
if model != llm_config.get('model', 'gpt-4o'):
|
|
605
|
+
llm_model = model
|
|
606
|
+
else:
|
|
607
|
+
llm_model = llm_config.get('model', 'gpt-4o')
|
|
608
|
+
|
|
609
|
+
llm = ChatOpenAI(
|
|
610
|
+
model=llm_model,
|
|
611
|
+
api_key=api_key,
|
|
612
|
+
temperature=llm_config.get('temperature', 0.7),
|
|
613
|
+
)
|
|
614
|
+
|
|
615
|
+
# Get profile config and merge with tool parameters
|
|
616
|
+
profile_config = get_default_profile(self.config)
|
|
617
|
+
|
|
618
|
+
# Override allowed_domains if provided in tool call
|
|
619
|
+
if allowed_domains is not None:
|
|
620
|
+
profile_config['allowed_domains'] = allowed_domains
|
|
621
|
+
|
|
622
|
+
# Create browser profile using config
|
|
623
|
+
profile = BrowserProfile(**profile_config)
|
|
624
|
+
|
|
625
|
+
# Create and run agent
|
|
626
|
+
agent = Agent(
|
|
627
|
+
task=task,
|
|
628
|
+
llm=llm,
|
|
629
|
+
browser_profile=profile,
|
|
630
|
+
use_vision=use_vision,
|
|
631
|
+
)
|
|
632
|
+
|
|
633
|
+
try:
|
|
634
|
+
history = await agent.run(max_steps=max_steps)
|
|
635
|
+
|
|
636
|
+
# Format results
|
|
637
|
+
results = []
|
|
638
|
+
results.append(f'Task completed in {len(history.history)} steps')
|
|
639
|
+
results.append(f'Success: {history.is_successful()}')
|
|
640
|
+
|
|
641
|
+
# Get final result if available
|
|
642
|
+
final_result = history.final_result()
|
|
643
|
+
if final_result:
|
|
644
|
+
results.append(f'\nFinal result:\n{final_result}')
|
|
645
|
+
|
|
646
|
+
# Include any errors
|
|
647
|
+
errors = history.errors()
|
|
648
|
+
if errors:
|
|
649
|
+
results.append(f'\nErrors encountered:\n{json.dumps(errors, indent=2)}')
|
|
650
|
+
|
|
651
|
+
# Include URLs visited
|
|
652
|
+
urls = history.urls()
|
|
653
|
+
if urls:
|
|
654
|
+
# Filter out None values and convert to strings
|
|
655
|
+
valid_urls = [str(url) for url in urls if url is not None]
|
|
656
|
+
if valid_urls:
|
|
657
|
+
results.append(f'\nURLs visited: {", ".join(valid_urls)}')
|
|
658
|
+
|
|
659
|
+
return '\n'.join(results)
|
|
660
|
+
|
|
661
|
+
except Exception as e:
|
|
662
|
+
logger.error(f'Agent task failed: {e}', exc_info=True)
|
|
663
|
+
return f'Agent task failed: {str(e)}'
|
|
664
|
+
finally:
|
|
665
|
+
# Clean up
|
|
666
|
+
await agent.close()
|
|
667
|
+
|
|
668
|
+
async def _navigate(self, url: str, new_tab: bool = False) -> str:
|
|
669
|
+
"""Navigate to a URL."""
|
|
670
|
+
if not self.browser_session:
|
|
671
|
+
return 'Error: No browser session active'
|
|
672
|
+
|
|
673
|
+
# Update session activity
|
|
674
|
+
self._update_session_activity(self.browser_session.id)
|
|
675
|
+
|
|
676
|
+
from browser_use.browser.events import NavigateToUrlEvent
|
|
677
|
+
|
|
678
|
+
if new_tab:
|
|
679
|
+
event = self.browser_session.event_bus.dispatch(NavigateToUrlEvent(url=url, new_tab=True))
|
|
680
|
+
await event
|
|
681
|
+
return f'Opened new tab with URL: {url}'
|
|
682
|
+
else:
|
|
683
|
+
event = self.browser_session.event_bus.dispatch(NavigateToUrlEvent(url=url))
|
|
684
|
+
await event
|
|
685
|
+
return f'Navigated to: {url}'
|
|
686
|
+
|
|
687
|
+
async def _click(self, index: int, new_tab: bool = False) -> str:
|
|
688
|
+
"""Click an element by index."""
|
|
689
|
+
if not self.browser_session:
|
|
690
|
+
return 'Error: No browser session active'
|
|
691
|
+
|
|
692
|
+
# Update session activity
|
|
693
|
+
self._update_session_activity(self.browser_session.id)
|
|
694
|
+
|
|
695
|
+
# Get the element
|
|
696
|
+
element = await self.browser_session.get_dom_element_by_index(index)
|
|
697
|
+
if not element:
|
|
698
|
+
return f'Element with index {index} not found'
|
|
699
|
+
|
|
700
|
+
if new_tab:
|
|
701
|
+
# For links, extract href and open in new tab
|
|
702
|
+
href = element.attributes.get('href')
|
|
703
|
+
if href:
|
|
704
|
+
# Convert relative href to absolute URL
|
|
705
|
+
state = await self.browser_session.get_browser_state_summary()
|
|
706
|
+
current_url = state.url
|
|
707
|
+
if href.startswith('/'):
|
|
708
|
+
# Relative URL - construct full URL
|
|
709
|
+
from urllib.parse import urlparse
|
|
710
|
+
|
|
711
|
+
parsed = urlparse(current_url)
|
|
712
|
+
full_url = f'{parsed.scheme}://{parsed.netloc}{href}'
|
|
713
|
+
else:
|
|
714
|
+
full_url = href
|
|
715
|
+
|
|
716
|
+
# Open link in new tab
|
|
717
|
+
from browser_use.browser.events import NavigateToUrlEvent
|
|
718
|
+
|
|
719
|
+
event = self.browser_session.event_bus.dispatch(NavigateToUrlEvent(url=full_url, new_tab=True))
|
|
720
|
+
await event
|
|
721
|
+
return f'Clicked element {index} and opened in new tab {full_url[:20]}...'
|
|
722
|
+
else:
|
|
723
|
+
# For non-link elements, just do a normal click
|
|
724
|
+
# Opening in new tab without href is not reliably supported
|
|
725
|
+
from browser_use.browser.events import ClickElementEvent
|
|
726
|
+
|
|
727
|
+
event = self.browser_session.event_bus.dispatch(ClickElementEvent(node=element))
|
|
728
|
+
await event
|
|
729
|
+
return f'Clicked element {index} (new tab not supported for non-link elements)'
|
|
730
|
+
else:
|
|
731
|
+
# Normal click
|
|
732
|
+
from browser_use.browser.events import ClickElementEvent
|
|
733
|
+
|
|
734
|
+
event = self.browser_session.event_bus.dispatch(ClickElementEvent(node=element))
|
|
735
|
+
await event
|
|
736
|
+
return f'Clicked element {index}'
|
|
737
|
+
|
|
738
|
+
async def _type_text(self, index: int, text: str) -> str:
|
|
739
|
+
"""Type text into an element."""
|
|
740
|
+
if not self.browser_session:
|
|
741
|
+
return 'Error: No browser session active'
|
|
742
|
+
|
|
743
|
+
element = await self.browser_session.get_dom_element_by_index(index)
|
|
744
|
+
if not element:
|
|
745
|
+
return f'Element with index {index} not found'
|
|
746
|
+
|
|
747
|
+
from browser_use.browser.events import TypeTextEvent
|
|
748
|
+
|
|
749
|
+
# Conservative heuristic to detect potentially sensitive data
|
|
750
|
+
# Only flag very obvious patterns to minimize false positives
|
|
751
|
+
is_potentially_sensitive = len(text) >= 6 and (
|
|
752
|
+
# Email pattern: contains @ and a domain-like suffix
|
|
753
|
+
('@' in text and '.' in text.split('@')[-1] if '@' in text else False)
|
|
754
|
+
# Mixed alphanumeric with reasonable complexity (likely API keys/tokens)
|
|
755
|
+
or (
|
|
756
|
+
len(text) >= 16
|
|
757
|
+
and any(char.isdigit() for char in text)
|
|
758
|
+
and any(char.isalpha() for char in text)
|
|
759
|
+
and any(char in '.-_' for char in text)
|
|
760
|
+
)
|
|
761
|
+
)
|
|
762
|
+
|
|
763
|
+
# Use generic key names to avoid information leakage about detection patterns
|
|
764
|
+
sensitive_key_name = None
|
|
765
|
+
if is_potentially_sensitive:
|
|
766
|
+
if '@' in text and '.' in text.split('@')[-1]:
|
|
767
|
+
sensitive_key_name = 'email'
|
|
768
|
+
else:
|
|
769
|
+
sensitive_key_name = 'credential'
|
|
770
|
+
|
|
771
|
+
event = self.browser_session.event_bus.dispatch(
|
|
772
|
+
TypeTextEvent(node=element, text=text, is_sensitive=is_potentially_sensitive, sensitive_key_name=sensitive_key_name)
|
|
773
|
+
)
|
|
774
|
+
await event
|
|
775
|
+
|
|
776
|
+
if is_potentially_sensitive:
|
|
777
|
+
if sensitive_key_name:
|
|
778
|
+
return f'Typed <{sensitive_key_name}> into element {index}'
|
|
779
|
+
else:
|
|
780
|
+
return f'Typed <sensitive> into element {index}'
|
|
781
|
+
else:
|
|
782
|
+
return f"Typed '{text}' into element {index}"
|
|
783
|
+
|
|
784
|
+
async def _get_browser_state(self, include_screenshot: bool = False) -> str:
|
|
785
|
+
"""Get current browser state."""
|
|
786
|
+
if not self.browser_session:
|
|
787
|
+
return 'Error: No browser session active'
|
|
788
|
+
|
|
789
|
+
state = await self.browser_session.get_browser_state_summary()
|
|
790
|
+
|
|
791
|
+
result = {
|
|
792
|
+
'url': state.url,
|
|
793
|
+
'title': state.title,
|
|
794
|
+
'tabs': [{'url': tab.url, 'title': tab.title} for tab in state.tabs],
|
|
795
|
+
'interactive_elements': [],
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
# Add interactive elements with their indices
|
|
799
|
+
for index, element in state.dom_state.selector_map.items():
|
|
800
|
+
elem_info = {
|
|
801
|
+
'index': index,
|
|
802
|
+
'tag': element.tag_name,
|
|
803
|
+
'text': element.get_all_children_text(max_depth=2)[:100],
|
|
804
|
+
}
|
|
805
|
+
if element.attributes.get('placeholder'):
|
|
806
|
+
elem_info['placeholder'] = element.attributes['placeholder']
|
|
807
|
+
if element.attributes.get('href'):
|
|
808
|
+
elem_info['href'] = element.attributes['href']
|
|
809
|
+
result['interactive_elements'].append(elem_info)
|
|
810
|
+
|
|
811
|
+
if include_screenshot and state.screenshot:
|
|
812
|
+
result['screenshot'] = state.screenshot
|
|
813
|
+
|
|
814
|
+
return json.dumps(result, indent=2)
|
|
815
|
+
|
|
816
|
+
async def _extract_content(self, query: str, extract_links: bool = False) -> str:
|
|
817
|
+
"""Extract content from current page."""
|
|
818
|
+
if not self.llm:
|
|
819
|
+
return 'Error: LLM not initialized (set OPENAI_API_KEY)'
|
|
820
|
+
|
|
821
|
+
if not self.file_system:
|
|
822
|
+
return 'Error: FileSystem not initialized'
|
|
823
|
+
|
|
824
|
+
if not self.browser_session:
|
|
825
|
+
return 'Error: No browser session active'
|
|
826
|
+
|
|
827
|
+
if not self.tools:
|
|
828
|
+
return 'Error: Tools not initialized'
|
|
829
|
+
|
|
830
|
+
state = await self.browser_session.get_browser_state_summary()
|
|
831
|
+
|
|
832
|
+
# Use the extract action
|
|
833
|
+
# Create a dynamic action model that matches the tools's expectations
|
|
834
|
+
from pydantic import create_model
|
|
835
|
+
|
|
836
|
+
# Create action model dynamically
|
|
837
|
+
ExtractAction = create_model(
|
|
838
|
+
'ExtractAction',
|
|
839
|
+
__base__=ActionModel,
|
|
840
|
+
extract=dict[str, Any],
|
|
841
|
+
)
|
|
842
|
+
|
|
843
|
+
# Use model_validate because Pyright does not understand the dynamic model
|
|
844
|
+
action = ExtractAction.model_validate(
|
|
845
|
+
{
|
|
846
|
+
'extract': {'query': query, 'extract_links': extract_links},
|
|
847
|
+
}
|
|
848
|
+
)
|
|
849
|
+
action_result = await self.tools.act(
|
|
850
|
+
action=action,
|
|
851
|
+
browser_session=self.browser_session,
|
|
852
|
+
page_extraction_llm=self.llm,
|
|
853
|
+
file_system=self.file_system,
|
|
854
|
+
)
|
|
855
|
+
|
|
856
|
+
return action_result.extracted_content or 'No content extracted'
|
|
857
|
+
|
|
858
|
+
async def _scroll(self, direction: str = 'down') -> str:
|
|
859
|
+
"""Scroll the page."""
|
|
860
|
+
if not self.browser_session:
|
|
861
|
+
return 'Error: No browser session active'
|
|
862
|
+
|
|
863
|
+
from browser_use.browser.events import ScrollEvent
|
|
864
|
+
|
|
865
|
+
# Scroll by a standard amount (500 pixels)
|
|
866
|
+
event = self.browser_session.event_bus.dispatch(
|
|
867
|
+
ScrollEvent(
|
|
868
|
+
direction=direction, # type: ignore
|
|
869
|
+
amount=500,
|
|
870
|
+
)
|
|
871
|
+
)
|
|
872
|
+
await event
|
|
873
|
+
return f'Scrolled {direction}'
|
|
874
|
+
|
|
875
|
+
async def _go_back(self) -> str:
|
|
876
|
+
"""Go back in browser history."""
|
|
877
|
+
if not self.browser_session:
|
|
878
|
+
return 'Error: No browser session active'
|
|
879
|
+
|
|
880
|
+
from browser_use.browser.events import GoBackEvent
|
|
881
|
+
|
|
882
|
+
event = self.browser_session.event_bus.dispatch(GoBackEvent())
|
|
883
|
+
await event
|
|
884
|
+
return 'Navigated back'
|
|
885
|
+
|
|
886
|
+
async def _close_browser(self) -> str:
|
|
887
|
+
"""Close the browser session."""
|
|
888
|
+
if self.browser_session:
|
|
889
|
+
from browser_use.browser.events import BrowserStopEvent
|
|
890
|
+
|
|
891
|
+
event = self.browser_session.event_bus.dispatch(BrowserStopEvent())
|
|
892
|
+
await event
|
|
893
|
+
self.browser_session = None
|
|
894
|
+
self.tools = None
|
|
895
|
+
return 'Browser closed'
|
|
896
|
+
return 'No browser session to close'
|
|
897
|
+
|
|
898
|
+
async def _list_tabs(self) -> str:
|
|
899
|
+
"""List all open tabs."""
|
|
900
|
+
if not self.browser_session:
|
|
901
|
+
return 'Error: No browser session active'
|
|
902
|
+
|
|
903
|
+
tabs_info = await self.browser_session.get_tabs()
|
|
904
|
+
tabs = []
|
|
905
|
+
for i, tab in enumerate(tabs_info):
|
|
906
|
+
tabs.append({'tab_id': tab.target_id[-4:], 'url': tab.url, 'title': tab.title or ''})
|
|
907
|
+
return json.dumps(tabs, indent=2)
|
|
908
|
+
|
|
909
|
+
async def _switch_tab(self, tab_id: str) -> str:
|
|
910
|
+
"""Switch to a different tab."""
|
|
911
|
+
if not self.browser_session:
|
|
912
|
+
return 'Error: No browser session active'
|
|
913
|
+
|
|
914
|
+
from browser_use.browser.events import SwitchTabEvent
|
|
915
|
+
|
|
916
|
+
target_id = await self.browser_session.get_target_id_from_tab_id(tab_id)
|
|
917
|
+
event = self.browser_session.event_bus.dispatch(SwitchTabEvent(target_id=target_id))
|
|
918
|
+
await event
|
|
919
|
+
state = await self.browser_session.get_browser_state_summary()
|
|
920
|
+
return f'Switched to tab {tab_id}: {state.url}'
|
|
921
|
+
|
|
922
|
+
async def _close_tab(self, tab_id: str) -> str:
|
|
923
|
+
"""Close a specific tab."""
|
|
924
|
+
if not self.browser_session:
|
|
925
|
+
return 'Error: No browser session active'
|
|
926
|
+
|
|
927
|
+
from browser_use.browser.events import CloseTabEvent
|
|
928
|
+
|
|
929
|
+
target_id = await self.browser_session.get_target_id_from_tab_id(tab_id)
|
|
930
|
+
event = self.browser_session.event_bus.dispatch(CloseTabEvent(target_id=target_id))
|
|
931
|
+
await event
|
|
932
|
+
current_url = await self.browser_session.get_current_page_url()
|
|
933
|
+
return f'Closed tab # {tab_id}, now on {current_url}'
|
|
934
|
+
|
|
935
|
+
def _track_session(self, session: BrowserSession) -> None:
|
|
936
|
+
"""Track a browser session for management."""
|
|
937
|
+
self.active_sessions[session.id] = {
|
|
938
|
+
'session': session,
|
|
939
|
+
'created_at': time.time(),
|
|
940
|
+
'last_activity': time.time(),
|
|
941
|
+
'url': getattr(session, 'current_url', None),
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
def _update_session_activity(self, session_id: str) -> None:
|
|
945
|
+
"""Update the last activity time for a session."""
|
|
946
|
+
if session_id in self.active_sessions:
|
|
947
|
+
self.active_sessions[session_id]['last_activity'] = time.time()
|
|
948
|
+
|
|
949
|
+
async def _list_sessions(self) -> str:
|
|
950
|
+
"""List all active browser sessions."""
|
|
951
|
+
if not self.active_sessions:
|
|
952
|
+
return 'No active browser sessions'
|
|
953
|
+
|
|
954
|
+
sessions_info = []
|
|
955
|
+
for session_id, session_data in self.active_sessions.items():
|
|
956
|
+
session = session_data['session']
|
|
957
|
+
created_at = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(session_data['created_at']))
|
|
958
|
+
last_activity = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(session_data['last_activity']))
|
|
959
|
+
|
|
960
|
+
# Check if session is still active
|
|
961
|
+
is_active = hasattr(session, 'cdp_client') and session.cdp_client is not None
|
|
962
|
+
|
|
963
|
+
sessions_info.append(
|
|
964
|
+
{
|
|
965
|
+
'session_id': session_id,
|
|
966
|
+
'created_at': created_at,
|
|
967
|
+
'last_activity': last_activity,
|
|
968
|
+
'active': is_active,
|
|
969
|
+
'current_url': session_data.get('url', 'Unknown'),
|
|
970
|
+
'age_minutes': (time.time() - session_data['created_at']) / 60,
|
|
971
|
+
}
|
|
972
|
+
)
|
|
973
|
+
|
|
974
|
+
return json.dumps(sessions_info, indent=2)
|
|
975
|
+
|
|
976
|
+
async def _close_session(self, session_id: str) -> str:
|
|
977
|
+
"""Close a specific browser session."""
|
|
978
|
+
if session_id not in self.active_sessions:
|
|
979
|
+
return f'Session {session_id} not found'
|
|
980
|
+
|
|
981
|
+
session_data = self.active_sessions[session_id]
|
|
982
|
+
session = session_data['session']
|
|
983
|
+
|
|
984
|
+
try:
|
|
985
|
+
# Close the session
|
|
986
|
+
if hasattr(session, 'kill'):
|
|
987
|
+
await session.kill()
|
|
988
|
+
elif hasattr(session, 'close'):
|
|
989
|
+
await session.close()
|
|
990
|
+
|
|
991
|
+
# Remove from tracking
|
|
992
|
+
del self.active_sessions[session_id]
|
|
993
|
+
|
|
994
|
+
# If this was the current session, clear it
|
|
995
|
+
if self.browser_session and self.browser_session.id == session_id:
|
|
996
|
+
self.browser_session = None
|
|
997
|
+
self.tools = None
|
|
998
|
+
|
|
999
|
+
return f'Successfully closed session {session_id}'
|
|
1000
|
+
except Exception as e:
|
|
1001
|
+
return f'Error closing session {session_id}: {str(e)}'
|
|
1002
|
+
|
|
1003
|
+
async def _close_all_sessions(self) -> str:
|
|
1004
|
+
"""Close all active browser sessions."""
|
|
1005
|
+
if not self.active_sessions:
|
|
1006
|
+
return 'No active sessions to close'
|
|
1007
|
+
|
|
1008
|
+
closed_count = 0
|
|
1009
|
+
errors = []
|
|
1010
|
+
|
|
1011
|
+
for session_id in list(self.active_sessions.keys()):
|
|
1012
|
+
try:
|
|
1013
|
+
result = await self._close_session(session_id)
|
|
1014
|
+
if 'Successfully closed' in result:
|
|
1015
|
+
closed_count += 1
|
|
1016
|
+
else:
|
|
1017
|
+
errors.append(f'{session_id}: {result}')
|
|
1018
|
+
except Exception as e:
|
|
1019
|
+
errors.append(f'{session_id}: {str(e)}')
|
|
1020
|
+
|
|
1021
|
+
# Clear current session references
|
|
1022
|
+
self.browser_session = None
|
|
1023
|
+
self.tools = None
|
|
1024
|
+
|
|
1025
|
+
result = f'Closed {closed_count} sessions'
|
|
1026
|
+
if errors:
|
|
1027
|
+
result += f'. Errors: {"; ".join(errors)}'
|
|
1028
|
+
|
|
1029
|
+
return result
|
|
1030
|
+
|
|
1031
|
+
async def _cleanup_expired_sessions(self) -> None:
|
|
1032
|
+
"""Background task to clean up expired sessions."""
|
|
1033
|
+
current_time = time.time()
|
|
1034
|
+
timeout_seconds = self.session_timeout_minutes * 60
|
|
1035
|
+
|
|
1036
|
+
expired_sessions = []
|
|
1037
|
+
for session_id, session_data in self.active_sessions.items():
|
|
1038
|
+
last_activity = session_data['last_activity']
|
|
1039
|
+
if current_time - last_activity > timeout_seconds:
|
|
1040
|
+
expired_sessions.append(session_id)
|
|
1041
|
+
|
|
1042
|
+
for session_id in expired_sessions:
|
|
1043
|
+
try:
|
|
1044
|
+
await self._close_session(session_id)
|
|
1045
|
+
logger.info(f'Auto-closed expired session {session_id}')
|
|
1046
|
+
except Exception as e:
|
|
1047
|
+
logger.error(f'Error auto-closing session {session_id}: {e}')
|
|
1048
|
+
|
|
1049
|
+
async def _start_cleanup_task(self) -> None:
|
|
1050
|
+
"""Start the background cleanup task."""
|
|
1051
|
+
|
|
1052
|
+
async def cleanup_loop():
|
|
1053
|
+
while True:
|
|
1054
|
+
try:
|
|
1055
|
+
await self._cleanup_expired_sessions()
|
|
1056
|
+
# Check every 2 minutes
|
|
1057
|
+
await asyncio.sleep(120)
|
|
1058
|
+
except Exception as e:
|
|
1059
|
+
logger.error(f'Error in cleanup task: {e}')
|
|
1060
|
+
await asyncio.sleep(120)
|
|
1061
|
+
|
|
1062
|
+
self._cleanup_task = asyncio.create_task(cleanup_loop())
|
|
1063
|
+
|
|
1064
|
+
async def run(self):
|
|
1065
|
+
"""Run the MCP server."""
|
|
1066
|
+
# Start the cleanup task
|
|
1067
|
+
await self._start_cleanup_task()
|
|
1068
|
+
|
|
1069
|
+
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
|
|
1070
|
+
await self.server.run(
|
|
1071
|
+
read_stream,
|
|
1072
|
+
write_stream,
|
|
1073
|
+
InitializationOptions(
|
|
1074
|
+
server_name='browser-use',
|
|
1075
|
+
server_version='0.1.0',
|
|
1076
|
+
capabilities=self.server.get_capabilities(
|
|
1077
|
+
notification_options=NotificationOptions(),
|
|
1078
|
+
experimental_capabilities={},
|
|
1079
|
+
),
|
|
1080
|
+
),
|
|
1081
|
+
)
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
async def main(session_timeout_minutes: int = 10):
|
|
1085
|
+
if not MCP_AVAILABLE:
|
|
1086
|
+
print('MCP SDK is required. Install with: pip install mcp', file=sys.stderr)
|
|
1087
|
+
sys.exit(1)
|
|
1088
|
+
|
|
1089
|
+
server = BrowserUseServer(session_timeout_minutes=session_timeout_minutes)
|
|
1090
|
+
server._telemetry.capture(
|
|
1091
|
+
MCPServerTelemetryEvent(
|
|
1092
|
+
version=get_browser_use_version(),
|
|
1093
|
+
action='start',
|
|
1094
|
+
parent_process_cmdline=get_parent_process_cmdline(),
|
|
1095
|
+
)
|
|
1096
|
+
)
|
|
1097
|
+
|
|
1098
|
+
try:
|
|
1099
|
+
await server.run()
|
|
1100
|
+
finally:
|
|
1101
|
+
duration = time.time() - server._start_time
|
|
1102
|
+
server._telemetry.capture(
|
|
1103
|
+
MCPServerTelemetryEvent(
|
|
1104
|
+
version=get_browser_use_version(),
|
|
1105
|
+
action='stop',
|
|
1106
|
+
duration_seconds=duration,
|
|
1107
|
+
parent_process_cmdline=get_parent_process_cmdline(),
|
|
1108
|
+
)
|
|
1109
|
+
)
|
|
1110
|
+
server._telemetry.flush()
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
if __name__ == '__main__':
|
|
1114
|
+
asyncio.run(main())
|