@darbotlabs/darbot-browser-mcp 0.1.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 +202 -0
- package/README.md +973 -0
- package/cli.js +18 -0
- package/config.d.ts +128 -0
- package/index.d.ts +28 -0
- package/index.js +19 -0
- package/lib/browserContextFactory.js +227 -0
- package/lib/browserServer.js +151 -0
- package/lib/cdpRelay.js +275 -0
- package/lib/config.js +204 -0
- package/lib/connection.js +84 -0
- package/lib/context.js +291 -0
- package/lib/fileUtils.js +32 -0
- package/lib/httpServer.js +201 -0
- package/lib/index.js +36 -0
- package/lib/javascript.js +49 -0
- package/lib/manualPromise.js +111 -0
- package/lib/package.js +20 -0
- package/lib/pageSnapshot.js +43 -0
- package/lib/program.js +80 -0
- package/lib/resources/resource.js +16 -0
- package/lib/server.js +48 -0
- package/lib/tab.js +123 -0
- package/lib/tools/common.js +68 -0
- package/lib/tools/console.js +44 -0
- package/lib/tools/dialogs.js +52 -0
- package/lib/tools/files.js +51 -0
- package/lib/tools/install.js +57 -0
- package/lib/tools/keyboard.js +46 -0
- package/lib/tools/navigate.js +93 -0
- package/lib/tools/network.js +51 -0
- package/lib/tools/pdf.js +49 -0
- package/lib/tools/profiles.js +271 -0
- package/lib/tools/screenshot.js +77 -0
- package/lib/tools/snapshot.js +204 -0
- package/lib/tools/tabs.js +118 -0
- package/lib/tools/testing.js +60 -0
- package/lib/tools/tool.js +18 -0
- package/lib/tools/utils.js +81 -0
- package/lib/tools/vision.js +189 -0
- package/lib/tools/wait.js +59 -0
- package/lib/tools.js +70 -0
- package/lib/transport.js +133 -0
- package/package.json +67 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
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 { z } from 'zod';
|
|
17
|
+
import { defineTool } from './tool.js';
|
|
18
|
+
const wait = captureSnapshot => defineTool({
|
|
19
|
+
capability: 'wait',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_wait_for',
|
|
22
|
+
title: 'Wait for',
|
|
23
|
+
description: 'Wait for text to appear or disappear or a specified time to pass',
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
time: z.number().optional().describe('The time to wait in seconds'),
|
|
26
|
+
text: z.string().optional().describe('The text to wait for'),
|
|
27
|
+
textGone: z.string().optional().describe('The text to wait for to disappear'),
|
|
28
|
+
}),
|
|
29
|
+
type: 'readOnly',
|
|
30
|
+
},
|
|
31
|
+
handle: async (context, params) => {
|
|
32
|
+
if (!params.text && !params.textGone && !params.time)
|
|
33
|
+
throw new Error('Either time, text or textGone must be provided');
|
|
34
|
+
const code = [];
|
|
35
|
+
if (params.time) {
|
|
36
|
+
code.push(`await new Promise(f => setTimeout(f, ${params.time} * 1000));`);
|
|
37
|
+
await new Promise(f => setTimeout(f, Math.min(10000, params.time * 1000)));
|
|
38
|
+
}
|
|
39
|
+
const tab = context.currentTabOrDie();
|
|
40
|
+
const locator = params.text ? tab.page.getByText(params.text).first() : undefined;
|
|
41
|
+
const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : undefined;
|
|
42
|
+
if (goneLocator) {
|
|
43
|
+
code.push(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`);
|
|
44
|
+
await goneLocator.waitFor({ state: 'hidden' });
|
|
45
|
+
}
|
|
46
|
+
if (locator) {
|
|
47
|
+
code.push(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`);
|
|
48
|
+
await locator.waitFor({ state: 'visible' });
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
code,
|
|
52
|
+
captureSnapshot,
|
|
53
|
+
waitForNetwork: false,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
export default (captureSnapshot) => [
|
|
58
|
+
wait(captureSnapshot),
|
|
59
|
+
];
|
package/lib/tools.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
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 common from './tools/common.js';
|
|
17
|
+
import console from './tools/console.js';
|
|
18
|
+
import dialogs from './tools/dialogs.js';
|
|
19
|
+
import files from './tools/files.js';
|
|
20
|
+
import install from './tools/install.js';
|
|
21
|
+
import keyboard from './tools/keyboard.js';
|
|
22
|
+
import navigate from './tools/navigate.js';
|
|
23
|
+
import network from './tools/network.js';
|
|
24
|
+
import pdf from './tools/pdf.js';
|
|
25
|
+
import * as profiles from './tools/profiles.js';
|
|
26
|
+
import snapshot from './tools/snapshot.js';
|
|
27
|
+
import tabs from './tools/tabs.js';
|
|
28
|
+
import screenshot from './tools/screenshot.js';
|
|
29
|
+
import testing from './tools/testing.js';
|
|
30
|
+
import vision from './tools/vision.js';
|
|
31
|
+
import wait from './tools/wait.js';
|
|
32
|
+
export const snapshotTools = [
|
|
33
|
+
...common(true),
|
|
34
|
+
...console,
|
|
35
|
+
...dialogs(true),
|
|
36
|
+
...files(true),
|
|
37
|
+
...install,
|
|
38
|
+
...keyboard(true),
|
|
39
|
+
...navigate(true),
|
|
40
|
+
...network,
|
|
41
|
+
...pdf,
|
|
42
|
+
profiles.browserSaveProfile,
|
|
43
|
+
profiles.browserSwitchProfile,
|
|
44
|
+
profiles.browserListProfiles,
|
|
45
|
+
profiles.browserDeleteProfile,
|
|
46
|
+
...screenshot,
|
|
47
|
+
...snapshot,
|
|
48
|
+
...tabs(true),
|
|
49
|
+
...testing,
|
|
50
|
+
...wait(true),
|
|
51
|
+
];
|
|
52
|
+
export const visionTools = [
|
|
53
|
+
...common(false),
|
|
54
|
+
...console,
|
|
55
|
+
...dialogs(false),
|
|
56
|
+
...files(false),
|
|
57
|
+
...install,
|
|
58
|
+
...keyboard(false),
|
|
59
|
+
...navigate(false),
|
|
60
|
+
...network,
|
|
61
|
+
...pdf,
|
|
62
|
+
profiles.browserSaveProfile,
|
|
63
|
+
profiles.browserSwitchProfile,
|
|
64
|
+
profiles.browserListProfiles,
|
|
65
|
+
profiles.browserDeleteProfile,
|
|
66
|
+
...tabs(false),
|
|
67
|
+
...testing,
|
|
68
|
+
...vision,
|
|
69
|
+
...wait(false),
|
|
70
|
+
];
|
package/lib/transport.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
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 http from 'node:http';
|
|
17
|
+
import assert from 'node:assert';
|
|
18
|
+
import crypto from 'node:crypto';
|
|
19
|
+
import debug from 'debug';
|
|
20
|
+
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
21
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
22
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
23
|
+
export async function startStdioTransport(server) {
|
|
24
|
+
await server.createConnection(new StdioServerTransport());
|
|
25
|
+
}
|
|
26
|
+
const testDebug = debug('pw:mcp:test');
|
|
27
|
+
async function handleSSE(server, req, res, url, sessions) {
|
|
28
|
+
if (req.method === 'POST') {
|
|
29
|
+
const sessionId = url.searchParams.get('sessionId');
|
|
30
|
+
if (!sessionId) {
|
|
31
|
+
res.statusCode = 400;
|
|
32
|
+
return res.end('Missing sessionId');
|
|
33
|
+
}
|
|
34
|
+
const transport = sessions.get(sessionId);
|
|
35
|
+
if (!transport) {
|
|
36
|
+
res.statusCode = 404;
|
|
37
|
+
return res.end('Session not found');
|
|
38
|
+
}
|
|
39
|
+
return await transport.handlePostMessage(req, res);
|
|
40
|
+
}
|
|
41
|
+
else if (req.method === 'GET') {
|
|
42
|
+
const transport = new SSEServerTransport('/sse', res);
|
|
43
|
+
sessions.set(transport.sessionId, transport);
|
|
44
|
+
testDebug(`create SSE session: ${transport.sessionId}`);
|
|
45
|
+
const connection = await server.createConnection(transport);
|
|
46
|
+
res.on('close', () => {
|
|
47
|
+
testDebug(`delete SSE session: ${transport.sessionId}`);
|
|
48
|
+
sessions.delete(transport.sessionId);
|
|
49
|
+
// eslint-disable-next-line no-console
|
|
50
|
+
void connection.close().catch(e => console.error(e));
|
|
51
|
+
});
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
res.statusCode = 405;
|
|
55
|
+
res.end('Method not allowed');
|
|
56
|
+
}
|
|
57
|
+
async function handleStreamable(server, req, res, sessions) {
|
|
58
|
+
const sessionId = req.headers['mcp-session-id'];
|
|
59
|
+
if (sessionId) {
|
|
60
|
+
const transport = sessions.get(sessionId);
|
|
61
|
+
if (!transport) {
|
|
62
|
+
res.statusCode = 404;
|
|
63
|
+
res.end('Session not found');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
return await transport.handleRequest(req, res);
|
|
67
|
+
}
|
|
68
|
+
if (req.method === 'POST') {
|
|
69
|
+
const transport = new StreamableHTTPServerTransport({
|
|
70
|
+
sessionIdGenerator: () => crypto.randomUUID(),
|
|
71
|
+
onsessioninitialized: sessionId => {
|
|
72
|
+
sessions.set(sessionId, transport);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
transport.onclose = () => {
|
|
76
|
+
if (transport.sessionId)
|
|
77
|
+
sessions.delete(transport.sessionId);
|
|
78
|
+
};
|
|
79
|
+
await server.createConnection(transport);
|
|
80
|
+
await transport.handleRequest(req, res);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
res.statusCode = 400;
|
|
84
|
+
res.end('Invalid request');
|
|
85
|
+
}
|
|
86
|
+
export async function startHttpServer(config) {
|
|
87
|
+
const { host, port } = config;
|
|
88
|
+
const httpServer = http.createServer();
|
|
89
|
+
await new Promise((resolve, reject) => {
|
|
90
|
+
httpServer.on('error', reject);
|
|
91
|
+
httpServer.listen(port, host, () => {
|
|
92
|
+
resolve();
|
|
93
|
+
httpServer.removeListener('error', reject);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
return httpServer;
|
|
97
|
+
}
|
|
98
|
+
export function startHttpTransport(httpServer, mcpServer) {
|
|
99
|
+
const sseSessions = new Map();
|
|
100
|
+
const streamableSessions = new Map();
|
|
101
|
+
httpServer.on('request', async (req, res) => {
|
|
102
|
+
const url = new URL(`http://localhost${req.url}`);
|
|
103
|
+
if (url.pathname.startsWith('/mcp'))
|
|
104
|
+
await handleStreamable(mcpServer, req, res, streamableSessions);
|
|
105
|
+
else
|
|
106
|
+
await handleSSE(mcpServer, req, res, url, sseSessions);
|
|
107
|
+
});
|
|
108
|
+
const url = httpAddressToString(httpServer.address());
|
|
109
|
+
const message = [
|
|
110
|
+
`Listening on ${url}`,
|
|
111
|
+
'Put this in your client config:',
|
|
112
|
+
JSON.stringify({
|
|
113
|
+
'mcpServers': {
|
|
114
|
+
'playwright': {
|
|
115
|
+
'url': `${url}/sse`
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}, undefined, 2),
|
|
119
|
+
'If your client supports streamable HTTP, you can use the /mcp endpoint instead.',
|
|
120
|
+
].join('\n');
|
|
121
|
+
// eslint-disable-next-line no-console
|
|
122
|
+
console.error(message);
|
|
123
|
+
}
|
|
124
|
+
export function httpAddressToString(address) {
|
|
125
|
+
assert(address, 'Could not bind server socket');
|
|
126
|
+
if (typeof address === 'string')
|
|
127
|
+
return address;
|
|
128
|
+
const resolvedPort = address.port;
|
|
129
|
+
let resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
|
|
130
|
+
if (resolvedHost === '0.0.0.0' || resolvedHost === '[::]')
|
|
131
|
+
resolvedHost = 'localhost';
|
|
132
|
+
return `http://${resolvedHost}:${resolvedPort}`;
|
|
133
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@darbotlabs/darbot-browser-mcp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "🤖 Your Autonomous Browser Companion - 29 AI-driven browser tools with work profile support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/darbotlabs/darbot-browser-mcp.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/darbotlabs/darbot-browser-mcp",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18"
|
|
13
|
+
},
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Microsoft Corporation"
|
|
16
|
+
},
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"lint": "npm run update-readme && eslint . && tsc --noEmit",
|
|
21
|
+
"update-readme": "node utils/update-readme.js",
|
|
22
|
+
"watch": "tsc --watch",
|
|
23
|
+
"test": "npx @playwright/test",
|
|
24
|
+
"test:msedge": "npx @playwright/test --project=msedge",
|
|
25
|
+
"run-server": "node cli.js",
|
|
26
|
+
"run-darbot": "node cli.js",
|
|
27
|
+
"clean": "rmdir /s /q lib 2>nul || echo Clean completed",
|
|
28
|
+
"npm-publish": "npm run clean && npm run build && npm run test && npm publish"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
"./package.json": "./package.json",
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./index.d.ts",
|
|
34
|
+
"default": "./index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@modelcontextprotocol/sdk": "^1.11.0",
|
|
39
|
+
"commander": "^13.1.0",
|
|
40
|
+
"debug": "^4.4.1",
|
|
41
|
+
"mime": "^4.0.7",
|
|
42
|
+
"playwright": "1.55.0-alpha-1752540053000",
|
|
43
|
+
"playwright-core": "1.55.0-alpha-1752540053000",
|
|
44
|
+
"ws": "^8.18.1",
|
|
45
|
+
"zod-to-json-schema": "^3.24.4"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@eslint/eslintrc": "^3.2.0",
|
|
49
|
+
"@eslint/js": "^9.19.0",
|
|
50
|
+
"@playwright/test": "1.55.0-alpha-1752540053000",
|
|
51
|
+
"@stylistic/eslint-plugin": "^3.0.1",
|
|
52
|
+
"@types/chrome": "^0.0.315",
|
|
53
|
+
"@types/debug": "^4.1.12",
|
|
54
|
+
"@types/node": "^22.13.10",
|
|
55
|
+
"@types/ws": "^8.18.1",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
57
|
+
"@typescript-eslint/parser": "^8.26.1",
|
|
58
|
+
"@typescript-eslint/utils": "^8.26.1",
|
|
59
|
+
"eslint": "^9.19.0",
|
|
60
|
+
"eslint-plugin-import": "^2.31.0",
|
|
61
|
+
"eslint-plugin-notice": "^1.0.0",
|
|
62
|
+
"typescript": "^5.8.2"
|
|
63
|
+
},
|
|
64
|
+
"bin": {
|
|
65
|
+
"darbot-browser-mcp": "cli.js"
|
|
66
|
+
}
|
|
67
|
+
}
|