@darbotlabs/darbot-browser-mcp 0.2.0 → 1.3.0
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.
- package/LICENSE +1 -1
- package/README.md +222 -161
- package/cli.js +1 -1
- package/config.d.ts +77 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/ai/context.js +150 -0
- package/lib/ai/guardrails.js +382 -0
- package/lib/ai/integration.js +397 -0
- package/lib/ai/intent.js +237 -0
- package/lib/ai/manualPromise.js +111 -0
- package/lib/ai/memory.js +273 -0
- package/lib/ai/ml-scorer.js +265 -0
- package/lib/ai/orchestrator-tools.js +292 -0
- package/lib/ai/orchestrator.js +473 -0
- package/lib/ai/planner.js +300 -0
- package/lib/ai/reporter.js +493 -0
- package/lib/ai/workflow.js +407 -0
- package/lib/auth/apiKeyAuth.js +46 -0
- package/lib/auth/entraAuth.js +110 -0
- package/lib/auth/entraJwtVerifier.js +117 -0
- package/lib/auth/index.js +210 -0
- package/lib/auth/managedIdentityAuth.js +175 -0
- package/lib/auth/mcpOAuthProvider.js +186 -0
- package/lib/auth/tunnelAuth.js +120 -0
- package/lib/browserContextFactory.js +1 -1
- package/lib/browserServer.js +1 -1
- package/lib/cdpRelay.js +2 -2
- package/lib/common.js +68 -0
- package/lib/config.js +62 -3
- package/lib/connection.js +1 -1
- package/lib/context.js +1 -1
- package/lib/fileUtils.js +1 -1
- package/lib/guardrails.js +382 -0
- package/lib/health.js +178 -0
- package/lib/httpServer.js +1 -1
- package/lib/index.js +1 -1
- package/lib/javascript.js +1 -1
- package/lib/manualPromise.js +1 -1
- package/lib/memory.js +273 -0
- package/lib/openapi.js +373 -0
- package/lib/orchestrator.js +473 -0
- package/lib/package.js +1 -1
- package/lib/pageSnapshot.js +17 -2
- package/lib/planner.js +302 -0
- package/lib/program.js +17 -5
- package/lib/reporter.js +493 -0
- package/lib/resources/resource.js +1 -1
- package/lib/server.js +5 -3
- package/lib/tab.js +1 -1
- package/lib/tools/ai-native.js +298 -0
- package/lib/tools/autonomous.js +147 -0
- package/lib/tools/clock.js +183 -0
- package/lib/tools/common.js +1 -1
- package/lib/tools/console.js +1 -1
- package/lib/tools/diagnostics.js +132 -0
- package/lib/tools/dialogs.js +1 -1
- package/lib/tools/emulation.js +155 -0
- package/lib/tools/files.js +1 -1
- package/lib/tools/install.js +1 -1
- package/lib/tools/keyboard.js +1 -1
- package/lib/tools/navigate.js +1 -1
- package/lib/tools/network.js +1 -1
- package/lib/tools/pageSnapshot.js +58 -0
- package/lib/tools/pdf.js +1 -1
- package/lib/tools/profiles.js +76 -25
- package/lib/tools/screenshot.js +1 -1
- package/lib/tools/scroll.js +93 -0
- package/lib/tools/snapshot.js +1 -1
- package/lib/tools/storage.js +328 -0
- package/lib/tools/tab.js +16 -0
- package/lib/tools/tabs.js +1 -1
- package/lib/tools/testing.js +1 -1
- package/lib/tools/tool.js +1 -1
- package/lib/tools/utils.js +1 -1
- package/lib/tools/vision.js +1 -1
- package/lib/tools/wait.js +1 -1
- package/lib/tools.js +22 -1
- package/lib/transport.js +251 -31
- package/package.json +28 -22
package/lib/planner.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) DarbotLabs.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import debug from 'debug';
|
|
17
|
+
const log = debug('darbot:planner');
|
|
18
|
+
/**
|
|
19
|
+
* BFS Planner for autonomous site crawling
|
|
20
|
+
*/
|
|
21
|
+
export class BFSPlanner {
|
|
22
|
+
config;
|
|
23
|
+
memory;
|
|
24
|
+
visitQueue;
|
|
25
|
+
visited;
|
|
26
|
+
currentDepth = 0;
|
|
27
|
+
pagesVisited = 0;
|
|
28
|
+
constructor(config, memory) {
|
|
29
|
+
this.config = {
|
|
30
|
+
...config,
|
|
31
|
+
// Set default strategy if not provided
|
|
32
|
+
strategy: config.strategy || 'bfs'
|
|
33
|
+
};
|
|
34
|
+
this.memory = memory;
|
|
35
|
+
this.visitQueue = [];
|
|
36
|
+
this.visited = new Set();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Plan the next action based on current observation and goal
|
|
40
|
+
*/
|
|
41
|
+
async planNextAction(observation) {
|
|
42
|
+
try {
|
|
43
|
+
log('Planning next action for:', observation.url);
|
|
44
|
+
// Check if we should finish crawling
|
|
45
|
+
if (this.shouldFinish(observation)) {
|
|
46
|
+
return {
|
|
47
|
+
type: 'finish',
|
|
48
|
+
reason: this.getFinishReason(),
|
|
49
|
+
priority: 1
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
// Mark current URL as visited
|
|
53
|
+
this.visited.add(observation.url);
|
|
54
|
+
this.pagesVisited++;
|
|
55
|
+
// Store current state in memory
|
|
56
|
+
if (this.memory.enabled) {
|
|
57
|
+
await this.memory.storeState(observation.url, observation.title, observation.domSnapshot, undefined, // Screenshots handled separately
|
|
58
|
+
observation.links.map(link => link.href));
|
|
59
|
+
}
|
|
60
|
+
// Extract and queue new links
|
|
61
|
+
await this.extractAndQueueLinks(observation);
|
|
62
|
+
// Get next URL to visit
|
|
63
|
+
const nextTarget = this.getNextTarget();
|
|
64
|
+
if (nextTarget) {
|
|
65
|
+
return {
|
|
66
|
+
type: 'navigate',
|
|
67
|
+
url: nextTarget.url,
|
|
68
|
+
reason: `BFS navigation to depth ${nextTarget.depth}: ${nextTarget.url}`,
|
|
69
|
+
priority: this.calculatePriority(nextTarget.url)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
// If no more URLs to visit, check for clickable elements on current page
|
|
73
|
+
const clickTarget = this.findBestClickTarget(observation);
|
|
74
|
+
if (clickTarget) {
|
|
75
|
+
return {
|
|
76
|
+
type: 'click',
|
|
77
|
+
target: clickTarget.selector,
|
|
78
|
+
reason: `Clicking "${clickTarget.text}" to discover new content`,
|
|
79
|
+
priority: 2
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// No more actions possible
|
|
83
|
+
return {
|
|
84
|
+
type: 'finish',
|
|
85
|
+
reason: 'No more discoverable content within constraints',
|
|
86
|
+
priority: 1
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
log('Error in planning:', error);
|
|
91
|
+
return {
|
|
92
|
+
type: 'finish',
|
|
93
|
+
reason: `Planning error: ${error}`,
|
|
94
|
+
priority: 1
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Initialize the planner with a starting URL
|
|
100
|
+
*/
|
|
101
|
+
async initialize(startUrl) {
|
|
102
|
+
this.visitQueue.push({ url: startUrl, depth: 0 });
|
|
103
|
+
log('Initialized BFS planner with start URL:', startUrl);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Extract links from observation and add to queue
|
|
107
|
+
*/
|
|
108
|
+
async extractAndQueueLinks(observation) {
|
|
109
|
+
const currentDepth = this.getCurrentDepth(observation.url);
|
|
110
|
+
for (const link of observation.links) {
|
|
111
|
+
const normalizedUrl = this.normalizeUrl(link.href, observation.url);
|
|
112
|
+
if (this.shouldVisitUrl(normalizedUrl, currentDepth + 1)) {
|
|
113
|
+
if (!this.visited.has(normalizedUrl) && !this.isQueued(normalizedUrl)) {
|
|
114
|
+
this.visitQueue.push({
|
|
115
|
+
url: normalizedUrl,
|
|
116
|
+
depth: currentDepth + 1,
|
|
117
|
+
parent: observation.url
|
|
118
|
+
});
|
|
119
|
+
log('Queued URL:', normalizedUrl, 'at depth', currentDepth + 1);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Sort queue by priority (breadth-first)
|
|
124
|
+
this.visitQueue.sort((a, b) => {
|
|
125
|
+
if (a.depth !== b.depth)
|
|
126
|
+
return a.depth - b.depth;
|
|
127
|
+
return this.calculatePriority(b.url) - this.calculatePriority(a.url);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get the next target URL from the queue
|
|
132
|
+
*/
|
|
133
|
+
getNextTarget() {
|
|
134
|
+
while (this.visitQueue.length > 0) {
|
|
135
|
+
const target = this.visitQueue.shift();
|
|
136
|
+
if (!this.visited.has(target.url) && this.shouldVisitUrl(target.url, target.depth))
|
|
137
|
+
return target;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Find the best clickable element to interact with
|
|
143
|
+
*/
|
|
144
|
+
findBestClickTarget(observation) {
|
|
145
|
+
const clickableElements = observation.clickableElements
|
|
146
|
+
.filter(el => this.isInterestingElement(el))
|
|
147
|
+
.sort((a, b) => this.calculateElementPriority(b) - this.calculateElementPriority(a));
|
|
148
|
+
return clickableElements.length > 0 ? clickableElements[0] : null;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Check if an element is interesting to click
|
|
152
|
+
*/
|
|
153
|
+
isInterestingElement(element) {
|
|
154
|
+
const text = element.text.toLowerCase();
|
|
155
|
+
const tag = element.tag.toLowerCase();
|
|
156
|
+
// Avoid navigation elements we might get stuck in
|
|
157
|
+
const avoidPatterns = [
|
|
158
|
+
/back/i, /previous/i, /close/i, /cancel/i, /logout/i, /sign.?out/i,
|
|
159
|
+
/advertisement/i, /ad/i, /sponsor/i, /cookie/i, /privacy/i
|
|
160
|
+
];
|
|
161
|
+
if (avoidPatterns.some(pattern => pattern.test(text)))
|
|
162
|
+
return false;
|
|
163
|
+
// Prefer buttons and links with meaningful text
|
|
164
|
+
if (['button', 'a', 'input'].includes(tag) && text.length > 2)
|
|
165
|
+
return true;
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Calculate priority for clicking an element
|
|
170
|
+
*/
|
|
171
|
+
calculateElementPriority(element) {
|
|
172
|
+
let priority = 0;
|
|
173
|
+
const text = element.text.toLowerCase();
|
|
174
|
+
// Higher priority for content-related actions
|
|
175
|
+
const highPriorityPatterns = [
|
|
176
|
+
/more/i, /show/i, /view/i, /read/i, /details/i, /expand/i,
|
|
177
|
+
/next/i, /continue/i, /load/i, /see.?all/i
|
|
178
|
+
];
|
|
179
|
+
if (highPriorityPatterns.some(pattern => pattern.test(text)))
|
|
180
|
+
priority += 10;
|
|
181
|
+
// Medium priority for navigation
|
|
182
|
+
const mediumPriorityPatterns = [
|
|
183
|
+
/menu/i, /navigation/i, /search/i, /filter/i, /category/i
|
|
184
|
+
];
|
|
185
|
+
if (mediumPriorityPatterns.some(pattern => pattern.test(text)))
|
|
186
|
+
priority += 5;
|
|
187
|
+
return priority;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Calculate priority for visiting a URL
|
|
191
|
+
*/
|
|
192
|
+
calculatePriority(url) {
|
|
193
|
+
let priority = 0;
|
|
194
|
+
// Higher priority for pages with interesting content indicators
|
|
195
|
+
const highPriorityPatterns = [
|
|
196
|
+
/article/i, /post/i, /blog/i, /news/i, /content/i,
|
|
197
|
+
/product/i, /item/i, /detail/i, /page/i
|
|
198
|
+
];
|
|
199
|
+
const mediumPriorityPatterns = [
|
|
200
|
+
/category/i, /section/i, /topic/i, /tag/i, /archive/i
|
|
201
|
+
];
|
|
202
|
+
const lowPriorityPatterns = [
|
|
203
|
+
/admin/i, /login/i, /register/i, /account/i, /profile/i,
|
|
204
|
+
/terms/i, /privacy/i, /legal/i, /contact/i, /about/i
|
|
205
|
+
];
|
|
206
|
+
if (highPriorityPatterns.some(pattern => pattern.test(url)))
|
|
207
|
+
priority += 10;
|
|
208
|
+
else if (mediumPriorityPatterns.some(pattern => pattern.test(url)))
|
|
209
|
+
priority += 5;
|
|
210
|
+
else if (lowPriorityPatterns.some(pattern => pattern.test(url)))
|
|
211
|
+
priority -= 5;
|
|
212
|
+
return priority;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Check if we should visit a URL
|
|
216
|
+
*/
|
|
217
|
+
shouldVisitUrl(url, depth) {
|
|
218
|
+
if (depth > this.config.maxDepth)
|
|
219
|
+
return false;
|
|
220
|
+
if (this.pagesVisited >= this.config.maxPages)
|
|
221
|
+
return false;
|
|
222
|
+
// Check allowed domains
|
|
223
|
+
if (this.config.allowedDomains && this.config.allowedDomains.length > 0) {
|
|
224
|
+
try {
|
|
225
|
+
const urlObj = new URL(url);
|
|
226
|
+
const allowed = this.config.allowedDomains.some(domain => urlObj.hostname === domain || urlObj.hostname.endsWith('.' + domain));
|
|
227
|
+
if (!allowed)
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Check exclude patterns
|
|
235
|
+
if (this.config.excludePatterns) {
|
|
236
|
+
if (this.config.excludePatterns.some(pattern => pattern.test(url)))
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
// Exclude common file types and non-HTML resources
|
|
240
|
+
const fileExtensionPattern = /\.(pdf|doc|docx|xls|xlsx|ppt|pptx|zip|rar|exe|dmg|mp4|mp3|jpg|jpeg|png|gif|svg|css|js|json|xml)$/i;
|
|
241
|
+
if (fileExtensionPattern.test(url))
|
|
242
|
+
return false;
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Check if URL is already queued
|
|
247
|
+
*/
|
|
248
|
+
isQueued(url) {
|
|
249
|
+
return this.visitQueue.some(item => item.url === url);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Get current depth for a URL
|
|
253
|
+
*/
|
|
254
|
+
getCurrentDepth(url) {
|
|
255
|
+
const queueItem = this.visitQueue.find(item => item.url === url);
|
|
256
|
+
return queueItem ? queueItem.depth : this.currentDepth;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Normalize URL relative to base URL
|
|
260
|
+
*/
|
|
261
|
+
normalizeUrl(href, baseUrl) {
|
|
262
|
+
try {
|
|
263
|
+
return new URL(href, baseUrl).href;
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
return href;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Check if we should finish crawling
|
|
271
|
+
*/
|
|
272
|
+
shouldFinish(observation) {
|
|
273
|
+
if (this.pagesVisited >= this.config.maxPages)
|
|
274
|
+
return true;
|
|
275
|
+
if (this.visitQueue.length === 0 && observation.clickableElements.length === 0)
|
|
276
|
+
return true;
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Get reason for finishing
|
|
281
|
+
*/
|
|
282
|
+
getFinishReason() {
|
|
283
|
+
if (this.pagesVisited >= this.config.maxPages)
|
|
284
|
+
return `Reached maximum page limit (${this.config.maxPages})`;
|
|
285
|
+
if (this.visitQueue.length === 0)
|
|
286
|
+
return 'No more URLs to visit in the queue';
|
|
287
|
+
return 'Crawling complete';
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Get crawling statistics
|
|
291
|
+
*/
|
|
292
|
+
getStats() {
|
|
293
|
+
return {
|
|
294
|
+
pagesVisited: this.pagesVisited,
|
|
295
|
+
queueSize: this.visitQueue.length,
|
|
296
|
+
visitedUrls: Array.from(this.visited),
|
|
297
|
+
currentDepth: this.currentDepth,
|
|
298
|
+
maxDepth: this.config.maxDepth,
|
|
299
|
+
maxPages: this.config.maxPages
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
package/lib/program.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) DarbotLabs.
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -50,10 +50,19 @@ program
|
|
|
50
50
|
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
|
|
51
51
|
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"')
|
|
52
52
|
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
|
53
|
+
.option('--edge-profile <name>', 'Edge profile name to use (e.g., "Profile 1", "Default"). This is recorded in saved session states.')
|
|
54
|
+
.option('--edge-profile-email <email>', 'Email associated with the Edge profile. This is recorded in saved session states for context.')
|
|
55
|
+
.option('--auto-sign-in', 'Auto sign in with work/school account (Edge profile preference)')
|
|
56
|
+
.option('--profile-switching', 'Enable automatic profile switching based on site (Edge profile preference)')
|
|
57
|
+
.option('--intranet-switch', 'Automatically switch to work profile for intranet sites (Edge profile preference)')
|
|
58
|
+
.option('--ie-mode-switch', 'Automatically switch profile for IE mode sites (Edge profile preference)')
|
|
59
|
+
.option('--default-profile <name>', 'Default Edge profile for external links (Edge profile preference)')
|
|
53
60
|
.addOption(new Option('--extension', 'Allow connecting to a running browser instance (Edge/Chrome only). Requires the \'Darbot Browser MCP\' browser extension to be installed.').hideHelp())
|
|
54
61
|
.action(async (options) => {
|
|
55
62
|
const config = await resolveCLIConfig(options);
|
|
56
|
-
const
|
|
63
|
+
const httpResult = config.server.port !== undefined ? await startHttpServer(config.server) : undefined;
|
|
64
|
+
const httpServer = httpResult?.httpServer;
|
|
65
|
+
const expressApp = httpResult?.app;
|
|
57
66
|
if (config.extension) {
|
|
58
67
|
if (!httpServer)
|
|
59
68
|
throw new Error('--port parameter is required for extension mode');
|
|
@@ -61,9 +70,12 @@ program
|
|
|
61
70
|
config.browser.cdpEndpoint = await startCDPRelayServer(httpServer);
|
|
62
71
|
}
|
|
63
72
|
const server = new Server(config);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
// Use http mode when port is specified to prevent exit on stdin close
|
|
74
|
+
server.setupExitWatchdog(httpServer ? 'http' : 'stdio');
|
|
75
|
+
if (httpServer && expressApp)
|
|
76
|
+
await startHttpTransport(httpServer, server, expressApp);
|
|
77
|
+
else if (httpServer)
|
|
78
|
+
throw new Error('Express app not initialized');
|
|
67
79
|
else
|
|
68
80
|
await startStdioTransport(server);
|
|
69
81
|
if (config.saveTrace) {
|