@ejazullah/browser-mcp 0.0.56
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 +860 -0
- package/cli.js +19 -0
- package/index.d.ts +23 -0
- package/index.js +1061 -0
- package/lib/auth.js +82 -0
- package/lib/browserContextFactory.js +205 -0
- package/lib/browserServerBackend.js +125 -0
- package/lib/config.js +266 -0
- package/lib/context.js +232 -0
- package/lib/databaseLogger.js +264 -0
- package/lib/extension/cdpRelay.js +346 -0
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/extension/main.js +26 -0
- package/lib/fileUtils.js +32 -0
- package/lib/httpServer.js +39 -0
- package/lib/index.js +39 -0
- package/lib/javascript.js +49 -0
- package/lib/log.js +21 -0
- package/lib/loop/loop.js +69 -0
- package/lib/loop/loopClaude.js +152 -0
- package/lib/loop/loopOpenAI.js +143 -0
- package/lib/loop/main.js +60 -0
- package/lib/loopTools/context.js +66 -0
- package/lib/loopTools/main.js +49 -0
- package/lib/loopTools/perform.js +32 -0
- package/lib/loopTools/snapshot.js +29 -0
- package/lib/loopTools/tool.js +18 -0
- package/lib/manualPromise.js +111 -0
- package/lib/mcp/inProcessTransport.js +72 -0
- package/lib/mcp/server.js +93 -0
- package/lib/mcp/transport.js +217 -0
- package/lib/mongoDBLogger.js +252 -0
- package/lib/package.js +20 -0
- package/lib/program.js +113 -0
- package/lib/response.js +172 -0
- package/lib/sessionLog.js +156 -0
- package/lib/tab.js +266 -0
- package/lib/tools/cdp.js +169 -0
- package/lib/tools/common.js +55 -0
- package/lib/tools/console.js +33 -0
- package/lib/tools/dialogs.js +47 -0
- package/lib/tools/evaluate.js +53 -0
- package/lib/tools/extraction.js +217 -0
- package/lib/tools/files.js +44 -0
- package/lib/tools/forms.js +180 -0
- package/lib/tools/getext.js +99 -0
- package/lib/tools/install.js +53 -0
- package/lib/tools/interactions.js +191 -0
- package/lib/tools/keyboard.js +86 -0
- package/lib/tools/mouse.js +99 -0
- package/lib/tools/navigate.js +70 -0
- package/lib/tools/network.js +41 -0
- package/lib/tools/pdf.js +40 -0
- package/lib/tools/screenshot.js +75 -0
- package/lib/tools/selectors.js +233 -0
- package/lib/tools/snapshot.js +169 -0
- package/lib/tools/states.js +147 -0
- package/lib/tools/tabs.js +87 -0
- package/lib/tools/tool.js +33 -0
- package/lib/tools/utils.js +74 -0
- package/lib/tools/wait.js +56 -0
- package/lib/tools.js +64 -0
- package/lib/utils.js +26 -0
- package/openapi.json +683 -0
- package/package.json +92 -0
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
export class InProcessTransport {
|
|
17
|
+
_server;
|
|
18
|
+
_serverTransport;
|
|
19
|
+
_connected = false;
|
|
20
|
+
constructor(server) {
|
|
21
|
+
this._server = server;
|
|
22
|
+
this._serverTransport = new InProcessServerTransport(this);
|
|
23
|
+
}
|
|
24
|
+
async start() {
|
|
25
|
+
if (this._connected)
|
|
26
|
+
throw new Error('InprocessTransport already started!');
|
|
27
|
+
await this._server.connect(this._serverTransport);
|
|
28
|
+
this._connected = true;
|
|
29
|
+
}
|
|
30
|
+
async send(message, options) {
|
|
31
|
+
if (!this._connected)
|
|
32
|
+
throw new Error('Transport not connected');
|
|
33
|
+
this._serverTransport._receiveFromClient(message);
|
|
34
|
+
}
|
|
35
|
+
async close() {
|
|
36
|
+
if (this._connected) {
|
|
37
|
+
this._connected = false;
|
|
38
|
+
this.onclose?.();
|
|
39
|
+
this._serverTransport.onclose?.();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
onclose;
|
|
43
|
+
onerror;
|
|
44
|
+
onmessage;
|
|
45
|
+
sessionId;
|
|
46
|
+
setProtocolVersion;
|
|
47
|
+
_receiveFromServer(message, extra) {
|
|
48
|
+
this.onmessage?.(message, extra);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class InProcessServerTransport {
|
|
52
|
+
_clientTransport;
|
|
53
|
+
constructor(clientTransport) {
|
|
54
|
+
this._clientTransport = clientTransport;
|
|
55
|
+
}
|
|
56
|
+
async start() {
|
|
57
|
+
}
|
|
58
|
+
async send(message, options) {
|
|
59
|
+
this._clientTransport._receiveFromServer(message);
|
|
60
|
+
}
|
|
61
|
+
async close() {
|
|
62
|
+
this.onclose?.();
|
|
63
|
+
}
|
|
64
|
+
onclose;
|
|
65
|
+
onerror;
|
|
66
|
+
onmessage;
|
|
67
|
+
sessionId;
|
|
68
|
+
setProtocolVersion;
|
|
69
|
+
_receiveFromClient(message) {
|
|
70
|
+
this.onmessage?.(message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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 { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
17
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
18
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
19
|
+
import { ManualPromise } from '../manualPromise.js';
|
|
20
|
+
import { logUnhandledError } from '../log.js';
|
|
21
|
+
export async function connect(serverBackendFactory, transport, runHeartbeat) {
|
|
22
|
+
const backend = serverBackendFactory();
|
|
23
|
+
const server = createServer(backend, runHeartbeat);
|
|
24
|
+
await server.connect(transport);
|
|
25
|
+
}
|
|
26
|
+
export function createServer(backend, runHeartbeat) {
|
|
27
|
+
const initializedPromise = new ManualPromise();
|
|
28
|
+
const server = new Server({ name: backend.name, version: backend.version }, {
|
|
29
|
+
capabilities: {
|
|
30
|
+
tools: {},
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const tools = backend.tools();
|
|
34
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
35
|
+
return { tools: tools.map(tool => ({
|
|
36
|
+
name: tool.name,
|
|
37
|
+
description: tool.description,
|
|
38
|
+
inputSchema: zodToJsonSchema(tool.inputSchema),
|
|
39
|
+
annotations: {
|
|
40
|
+
title: tool.title,
|
|
41
|
+
readOnlyHint: tool.type === 'readOnly',
|
|
42
|
+
destructiveHint: tool.type === 'destructive',
|
|
43
|
+
openWorldHint: true,
|
|
44
|
+
},
|
|
45
|
+
})) };
|
|
46
|
+
});
|
|
47
|
+
let heartbeatRunning = false;
|
|
48
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
49
|
+
await initializedPromise;
|
|
50
|
+
if (runHeartbeat && !heartbeatRunning) {
|
|
51
|
+
heartbeatRunning = true;
|
|
52
|
+
startHeartbeat(server);
|
|
53
|
+
}
|
|
54
|
+
const errorResult = (...messages) => ({
|
|
55
|
+
content: [{ type: 'text', text: '### Result\n' + messages.join('\n') }],
|
|
56
|
+
isError: true,
|
|
57
|
+
});
|
|
58
|
+
const tool = tools.find(tool => tool.name === request.params.name);
|
|
59
|
+
if (!tool)
|
|
60
|
+
return errorResult(`Error: Tool "${request.params.name}" not found`);
|
|
61
|
+
try {
|
|
62
|
+
return await backend.callTool(tool, tool.inputSchema.parse(request.params.arguments || {}));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return errorResult(String(error));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
addServerListener(server, 'initialized', () => {
|
|
69
|
+
backend.initialize?.(server).then(() => initializedPromise.resolve()).catch(logUnhandledError);
|
|
70
|
+
});
|
|
71
|
+
addServerListener(server, 'close', () => backend.serverClosed?.());
|
|
72
|
+
return server;
|
|
73
|
+
}
|
|
74
|
+
const startHeartbeat = (server) => {
|
|
75
|
+
const beat = () => {
|
|
76
|
+
Promise.race([
|
|
77
|
+
server.ping(),
|
|
78
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('ping timeout')), 5000)),
|
|
79
|
+
]).then(() => {
|
|
80
|
+
setTimeout(beat, 3000);
|
|
81
|
+
}).catch(() => {
|
|
82
|
+
void server.close();
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
beat();
|
|
86
|
+
};
|
|
87
|
+
function addServerListener(server, event, listener) {
|
|
88
|
+
const oldListener = server[`on${event}`];
|
|
89
|
+
server[`on${event}`] = () => {
|
|
90
|
+
oldListener?.();
|
|
91
|
+
listener();
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
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 crypto from 'crypto';
|
|
17
|
+
import debug from 'debug';
|
|
18
|
+
import cors from 'cors';
|
|
19
|
+
import { readFileSync } from 'fs';
|
|
20
|
+
import { fileURLToPath } from 'url';
|
|
21
|
+
import { dirname, join } from 'path';
|
|
22
|
+
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
23
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
24
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
25
|
+
import { httpAddressToString, startHttpServer } from '../httpServer.js';
|
|
26
|
+
import * as mcpServer from './server.js';
|
|
27
|
+
import { AuthManager, defaultAuthConfig } from '../auth.js';
|
|
28
|
+
export async function start(serverBackendFactory, options, authConfig) {
|
|
29
|
+
if (options.port !== undefined) {
|
|
30
|
+
const httpServer = await startHttpServer(options);
|
|
31
|
+
const auth = new AuthManager(authConfig ?? defaultAuthConfig());
|
|
32
|
+
startHttpTransport(httpServer, serverBackendFactory, auth);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
await startStdioTransport(serverBackendFactory);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function startStdioTransport(serverBackendFactory) {
|
|
39
|
+
await mcpServer.connect(serverBackendFactory, new StdioServerTransport(), false);
|
|
40
|
+
}
|
|
41
|
+
const testDebug = debug('pw:mcp:test');
|
|
42
|
+
// Get the current file's directory
|
|
43
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
44
|
+
const __dirname = dirname(__filename);
|
|
45
|
+
// Embedded OpenAPI specification as fallback
|
|
46
|
+
const embeddedOpenApiSpec = `{
|
|
47
|
+
"openapi": "3.0.3",
|
|
48
|
+
"info": {
|
|
49
|
+
"title": "MCP Playwright Browser Automation API",
|
|
50
|
+
"description": "Enhanced Playwright Tools for Model Context Protocol (MCP) with CDP Support. Provides browser automation capabilities including navigation, interaction, extraction, and testing.",
|
|
51
|
+
"version": "0.0.36",
|
|
52
|
+
"contact": {
|
|
53
|
+
"name": "Ejaz Ullah",
|
|
54
|
+
"email": "ejazullah@example.com",
|
|
55
|
+
"url": "https://github.com/ejazullah/mcp-playwright"
|
|
56
|
+
},
|
|
57
|
+
"license": {
|
|
58
|
+
"name": "Apache-2.0",
|
|
59
|
+
"url": "https://www.apache.org/licenses/LICENSE-2.0"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"servers": [
|
|
63
|
+
{
|
|
64
|
+
"url": "https://mcp.doingerp.com",
|
|
65
|
+
"description": "Production MCP Playwright Server"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"url": "http://localhost:8931",
|
|
69
|
+
"description": "Local Development Server"
|
|
70
|
+
}
|
|
71
|
+
],
|
|
72
|
+
"info": {
|
|
73
|
+
"description": "This API provides browser automation tools through MCP (Model Context Protocol). All endpoints accept POST requests with JSON payloads and return structured responses."
|
|
74
|
+
}
|
|
75
|
+
}`;
|
|
76
|
+
async function handleOpenAPI(req, res) {
|
|
77
|
+
if (req.method !== 'GET') {
|
|
78
|
+
res.statusCode = 405;
|
|
79
|
+
res.end('Method not allowed');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
// Try to read the OpenAPI spec from file first
|
|
84
|
+
const openApiPath = join(__dirname, '../../openapi.json');
|
|
85
|
+
let openApiSpec;
|
|
86
|
+
try {
|
|
87
|
+
openApiSpec = readFileSync(openApiPath, 'utf8');
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// Fallback to embedded spec if file not found
|
|
91
|
+
openApiSpec = embeddedOpenApiSpec;
|
|
92
|
+
}
|
|
93
|
+
res.setHeader('Content-Type', 'application/json');
|
|
94
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
95
|
+
res.statusCode = 200;
|
|
96
|
+
res.end(openApiSpec);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
res.statusCode = 500;
|
|
100
|
+
res.end('OpenAPI specification not found');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async function handleSSE(serverBackendFactory, req, res, url, sessions) {
|
|
104
|
+
if (req.method === 'POST') {
|
|
105
|
+
const sessionId = url.searchParams.get('sessionId');
|
|
106
|
+
if (!sessionId) {
|
|
107
|
+
res.statusCode = 400;
|
|
108
|
+
return res.end('Missing sessionId');
|
|
109
|
+
}
|
|
110
|
+
const transport = sessions.get(sessionId);
|
|
111
|
+
if (!transport) {
|
|
112
|
+
res.statusCode = 404;
|
|
113
|
+
return res.end('Session not found');
|
|
114
|
+
}
|
|
115
|
+
return await transport.handlePostMessage(req, res);
|
|
116
|
+
}
|
|
117
|
+
else if (req.method === 'GET') {
|
|
118
|
+
const transport = new SSEServerTransport('/sse', res);
|
|
119
|
+
sessions.set(transport.sessionId, transport);
|
|
120
|
+
testDebug(`create SSE session: ${transport.sessionId}`);
|
|
121
|
+
await mcpServer.connect(serverBackendFactory, transport, false);
|
|
122
|
+
res.on('close', () => {
|
|
123
|
+
testDebug(`delete SSE session: ${transport.sessionId}`);
|
|
124
|
+
sessions.delete(transport.sessionId);
|
|
125
|
+
});
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
res.statusCode = 405;
|
|
129
|
+
res.end('Method not allowed');
|
|
130
|
+
}
|
|
131
|
+
async function handleStreamable(serverBackendFactory, req, res, sessions) {
|
|
132
|
+
const sessionId = req.headers['mcp-session-id'];
|
|
133
|
+
if (sessionId) {
|
|
134
|
+
const transport = sessions.get(sessionId);
|
|
135
|
+
if (!transport) {
|
|
136
|
+
res.statusCode = 404;
|
|
137
|
+
res.end('Session not found');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
return await transport.handleRequest(req, res);
|
|
141
|
+
}
|
|
142
|
+
if (req.method === 'POST') {
|
|
143
|
+
const transport = new StreamableHTTPServerTransport({
|
|
144
|
+
sessionIdGenerator: () => crypto.randomUUID(),
|
|
145
|
+
onsessioninitialized: async (sessionId) => {
|
|
146
|
+
testDebug(`create http session: ${transport.sessionId}`);
|
|
147
|
+
await mcpServer.connect(serverBackendFactory, transport, true);
|
|
148
|
+
sessions.set(sessionId, transport);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
transport.onclose = () => {
|
|
152
|
+
if (!transport.sessionId)
|
|
153
|
+
return;
|
|
154
|
+
sessions.delete(transport.sessionId);
|
|
155
|
+
testDebug(`delete http session: ${transport.sessionId}`);
|
|
156
|
+
};
|
|
157
|
+
await transport.handleRequest(req, res);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
res.statusCode = 400;
|
|
161
|
+
res.end('Invalid request');
|
|
162
|
+
}
|
|
163
|
+
function startHttpTransport(httpServer, serverBackendFactory, auth) {
|
|
164
|
+
const sseSessions = new Map();
|
|
165
|
+
const streamableSessions = new Map();
|
|
166
|
+
// Configure CORS with permissive settings for MCP tools
|
|
167
|
+
const corsHandler = cors({
|
|
168
|
+
origin: true, // Allow all origins
|
|
169
|
+
methods: ['GET', 'POST', 'OPTIONS'],
|
|
170
|
+
allowedHeaders: ['Content-Type', 'Authorization', 'mcp-session-id', 'X-API-Key'],
|
|
171
|
+
credentials: true,
|
|
172
|
+
preflightContinue: false,
|
|
173
|
+
optionsSuccessStatus: 204
|
|
174
|
+
});
|
|
175
|
+
httpServer.on('request', async (req, res) => {
|
|
176
|
+
// Handle CORS first
|
|
177
|
+
corsHandler(req, res, async () => {
|
|
178
|
+
const url = new URL(`http://localhost${req.url}`);
|
|
179
|
+
// Handle OpenAPI specification endpoint (public)
|
|
180
|
+
if (url.pathname === '/openapi.json') {
|
|
181
|
+
await handleOpenAPI(req, res);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
// Authenticate all other requests when auth is enabled
|
|
185
|
+
if (await auth.authenticateRequest(req, res))
|
|
186
|
+
return;
|
|
187
|
+
if (url.pathname.startsWith('/sse'))
|
|
188
|
+
await handleSSE(serverBackendFactory, req, res, url, sseSessions);
|
|
189
|
+
else
|
|
190
|
+
await handleStreamable(serverBackendFactory, req, res, streamableSessions);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
const url = httpAddressToString(httpServer.address());
|
|
194
|
+
const authStatus = auth.enabled ? ' (auth enabled)' : '';
|
|
195
|
+
const message = [
|
|
196
|
+
`Listening on ${url}${authStatus}`,
|
|
197
|
+
'Put this in your client config:',
|
|
198
|
+
JSON.stringify({
|
|
199
|
+
'mcpServers': {
|
|
200
|
+
'playwright': {
|
|
201
|
+
'url': `${url}/mcp`
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}, undefined, 2),
|
|
205
|
+
'For legacy SSE transport support, you can use the /sse endpoint instead.',
|
|
206
|
+
...(auth.enabled ? [
|
|
207
|
+
'',
|
|
208
|
+
'Auth endpoints:',
|
|
209
|
+
` POST ${url}/oauth/token - Get access token`,
|
|
210
|
+
` GET ${url}/oauth/authorize - Authorization code flow`,
|
|
211
|
+
` GET ${url}/oauth/token-info - Introspect token`,
|
|
212
|
+
` GET ${url}/.well-known/oauth-authorization-server - OAuth metadata`,
|
|
213
|
+
] : []),
|
|
214
|
+
].join('\n');
|
|
215
|
+
// eslint-disable-next-line no-console
|
|
216
|
+
console.error(message);
|
|
217
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
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 { MongoClient } from 'mongodb';
|
|
17
|
+
import { logUnhandledError } from './log.js';
|
|
18
|
+
export class MongoDBLogger {
|
|
19
|
+
_client = null;
|
|
20
|
+
_db = null;
|
|
21
|
+
_collection = null;
|
|
22
|
+
_mongoUrl;
|
|
23
|
+
_dbName;
|
|
24
|
+
_collectionName;
|
|
25
|
+
_sessionId;
|
|
26
|
+
_connected = false;
|
|
27
|
+
_pendingWrites = [];
|
|
28
|
+
_flushTimeout;
|
|
29
|
+
constructor(config = {}) {
|
|
30
|
+
this._mongoUrl = config.mongoUrl || process.env.MONGODB_URL || 'mongodb://localhost:27017';
|
|
31
|
+
this._dbName = config.dbName || process.env.MONGODB_DB_NAME || 'playwright_mcp';
|
|
32
|
+
this._collectionName = config.collectionName || process.env.MONGODB_COLLECTION || 'element_interactions';
|
|
33
|
+
this._sessionId = config.sessionId || `session_${Date.now()}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Connect to MongoDB
|
|
37
|
+
*/
|
|
38
|
+
async connect() {
|
|
39
|
+
if (this._connected)
|
|
40
|
+
return true;
|
|
41
|
+
try {
|
|
42
|
+
this._client = new MongoClient(this._mongoUrl);
|
|
43
|
+
await this._client.connect();
|
|
44
|
+
this._db = this._client.db(this._dbName);
|
|
45
|
+
this._collection = this._db.collection(this._collectionName);
|
|
46
|
+
// Create indexes for better query performance
|
|
47
|
+
await this._collection.createIndex({ sessionId: 1, timestamp: 1 });
|
|
48
|
+
await this._collection.createIndex({ toolName: 1 });
|
|
49
|
+
await this._collection.createIndex({ elementRef: 1 });
|
|
50
|
+
await this._collection.createIndex({ url: 1 });
|
|
51
|
+
this._connected = true;
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
console.error(`✓ MongoDB connected: ${this._dbName}.${this._collectionName}`);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// eslint-disable-next-line no-console
|
|
58
|
+
console.error(`✗ MongoDB connection failed: ${error}`);
|
|
59
|
+
logUnhandledError(error);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Disconnect from MongoDB
|
|
65
|
+
*/
|
|
66
|
+
async disconnect() {
|
|
67
|
+
await this.flushSync();
|
|
68
|
+
if (this._client) {
|
|
69
|
+
await this._client.close();
|
|
70
|
+
this._client = null;
|
|
71
|
+
this._db = null;
|
|
72
|
+
this._collection = null;
|
|
73
|
+
this._connected = false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if connected to MongoDB
|
|
78
|
+
*/
|
|
79
|
+
isConnected() {
|
|
80
|
+
return this._connected;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get the session ID
|
|
84
|
+
*/
|
|
85
|
+
getSessionId() {
|
|
86
|
+
return this._sessionId;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Log an element interaction to MongoDB
|
|
90
|
+
*/
|
|
91
|
+
async logInteraction(interaction) {
|
|
92
|
+
const fullInteraction = {
|
|
93
|
+
...interaction,
|
|
94
|
+
sessionId: this._sessionId,
|
|
95
|
+
};
|
|
96
|
+
if (!this._connected) {
|
|
97
|
+
// Queue for later if not connected
|
|
98
|
+
this._pendingWrites.push(fullInteraction);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this._pendingWrites.push(fullInteraction);
|
|
102
|
+
// Debounce writes
|
|
103
|
+
if (this._flushTimeout)
|
|
104
|
+
clearTimeout(this._flushTimeout);
|
|
105
|
+
this._flushTimeout = setTimeout(() => this._flush(), 500);
|
|
106
|
+
}
|
|
107
|
+
async _flush() {
|
|
108
|
+
if (this._pendingWrites.length === 0 || !this._collection)
|
|
109
|
+
return;
|
|
110
|
+
const interactions = [...this._pendingWrites];
|
|
111
|
+
this._pendingWrites = [];
|
|
112
|
+
try {
|
|
113
|
+
if (interactions.length === 1) {
|
|
114
|
+
await this._collection.insertOne(interactions[0]);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
await this._collection.insertMany(interactions);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
// eslint-disable-next-line no-console
|
|
122
|
+
console.error('Failed to write to MongoDB:', error);
|
|
123
|
+
logUnhandledError(error);
|
|
124
|
+
// Re-queue failed writes
|
|
125
|
+
this._pendingWrites.unshift(...interactions);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Flush all pending writes immediately
|
|
130
|
+
*/
|
|
131
|
+
async flushSync() {
|
|
132
|
+
if (this._flushTimeout) {
|
|
133
|
+
clearTimeout(this._flushTimeout);
|
|
134
|
+
this._flushTimeout = undefined;
|
|
135
|
+
}
|
|
136
|
+
await this._flush();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Query interactions by tool name
|
|
140
|
+
*/
|
|
141
|
+
async queryByToolName(toolName, sessionId) {
|
|
142
|
+
if (!this._collection)
|
|
143
|
+
return [];
|
|
144
|
+
await this.flushSync();
|
|
145
|
+
const query = { toolName };
|
|
146
|
+
if (sessionId)
|
|
147
|
+
query.sessionId = sessionId;
|
|
148
|
+
else
|
|
149
|
+
query.sessionId = this._sessionId;
|
|
150
|
+
return await this._collection.find(query).sort({ timestamp: 1 }).toArray();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Query interactions by element ref
|
|
154
|
+
*/
|
|
155
|
+
async queryByElementRef(ref, sessionId) {
|
|
156
|
+
if (!this._collection)
|
|
157
|
+
return [];
|
|
158
|
+
await this.flushSync();
|
|
159
|
+
const query = { elementRef: ref };
|
|
160
|
+
if (sessionId)
|
|
161
|
+
query.sessionId = sessionId;
|
|
162
|
+
else
|
|
163
|
+
query.sessionId = this._sessionId;
|
|
164
|
+
return await this._collection.find(query).sort({ timestamp: 1 }).toArray();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Query interactions by URL
|
|
168
|
+
*/
|
|
169
|
+
async queryByUrl(url, sessionId) {
|
|
170
|
+
if (!this._collection)
|
|
171
|
+
return [];
|
|
172
|
+
await this.flushSync();
|
|
173
|
+
const query = { url: { $regex: url, $options: 'i' } };
|
|
174
|
+
if (sessionId)
|
|
175
|
+
query.sessionId = sessionId;
|
|
176
|
+
else
|
|
177
|
+
query.sessionId = this._sessionId;
|
|
178
|
+
return await this._collection.find(query).sort({ timestamp: 1 }).toArray();
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get all interactions for current session
|
|
182
|
+
*/
|
|
183
|
+
async getAllInteractions(sessionId) {
|
|
184
|
+
if (!this._collection)
|
|
185
|
+
return [];
|
|
186
|
+
await this.flushSync();
|
|
187
|
+
const query = { sessionId: sessionId || this._sessionId };
|
|
188
|
+
return await this._collection.find(query).sort({ timestamp: 1 }).toArray();
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get all sessions
|
|
192
|
+
*/
|
|
193
|
+
async getAllSessions() {
|
|
194
|
+
if (!this._collection)
|
|
195
|
+
return [];
|
|
196
|
+
const sessions = await this._collection.distinct('sessionId');
|
|
197
|
+
return sessions;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get interaction statistics
|
|
201
|
+
*/
|
|
202
|
+
async getStatistics(sessionId) {
|
|
203
|
+
if (!this._collection)
|
|
204
|
+
return { totalInteractions: 0, toolCounts: {}, elementCounts: {}, urlCounts: {} };
|
|
205
|
+
await this.flushSync();
|
|
206
|
+
const query = { sessionId: sessionId || this._sessionId };
|
|
207
|
+
const interactions = await this._collection.find(query).toArray();
|
|
208
|
+
const toolCounts = {};
|
|
209
|
+
const elementCounts = {};
|
|
210
|
+
const urlCounts = {};
|
|
211
|
+
interactions.forEach(interaction => {
|
|
212
|
+
toolCounts[interaction.toolName] = (toolCounts[interaction.toolName] || 0) + 1;
|
|
213
|
+
elementCounts[interaction.elementRef] = (elementCounts[interaction.elementRef] || 0) + 1;
|
|
214
|
+
urlCounts[interaction.url] = (urlCounts[interaction.url] || 0) + 1;
|
|
215
|
+
});
|
|
216
|
+
return {
|
|
217
|
+
totalInteractions: interactions.length,
|
|
218
|
+
toolCounts,
|
|
219
|
+
elementCounts,
|
|
220
|
+
urlCounts,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Delete all interactions for a session
|
|
225
|
+
*/
|
|
226
|
+
async deleteSession(sessionId) {
|
|
227
|
+
if (!this._collection)
|
|
228
|
+
return 0;
|
|
229
|
+
await this.flushSync();
|
|
230
|
+
const result = await this._collection.deleteMany({
|
|
231
|
+
sessionId: sessionId || this._sessionId
|
|
232
|
+
});
|
|
233
|
+
return result.deletedCount;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Clear all data from the collection (use with caution!)
|
|
237
|
+
*/
|
|
238
|
+
async clearAll() {
|
|
239
|
+
if (!this._collection)
|
|
240
|
+
return;
|
|
241
|
+
await this.flushSync();
|
|
242
|
+
await this._collection.deleteMany({});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Create a MongoDB logger instance
|
|
247
|
+
*/
|
|
248
|
+
export async function createMongoDBLogger(config = {}) {
|
|
249
|
+
const logger = new MongoDBLogger(config);
|
|
250
|
+
await logger.connect();
|
|
251
|
+
return logger;
|
|
252
|
+
}
|
package/lib/package.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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 fs from 'fs';
|
|
17
|
+
import path from 'path';
|
|
18
|
+
import url from 'url';
|
|
19
|
+
const __filename = url.fileURLToPath(import.meta.url);
|
|
20
|
+
export const packageJSON = JSON.parse(fs.readFileSync(path.join(path.dirname(__filename), '..', 'package.json'), 'utf8'));
|