mcp-browser-dev-tools 0.0.1-beta.1
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/README.md +141 -0
- package/cli.mjs +9 -0
- package/package.json +87 -0
- package/src/app.mjs +56 -0
- package/src/browser-adapter.mjs +10 -0
- package/src/browser-launcher.mjs +170 -0
- package/src/cdp-client.mjs +599 -0
- package/src/cli.mjs +199 -0
- package/src/config.mjs +86 -0
- package/src/doctor.mjs +88 -0
- package/src/firefox-bidi-client.mjs +645 -0
- package/src/index.mjs +31 -0
- package/src/json-rpc-stdio.mjs +56 -0
- package/src/mcp-server.mjs +506 -0
- package/src/package-info.mjs +5 -0
- package/src/server.mjs +5 -0
- package/src/session-events.mjs +76 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export const MAX_MESSAGE_BYTES = 1024 * 1024;
|
|
2
|
+
|
|
3
|
+
export function encodeMessage(message) {
|
|
4
|
+
const payload = Buffer.from(JSON.stringify(message), "utf8");
|
|
5
|
+
return Buffer.concat([
|
|
6
|
+
Buffer.from(`Content-Length: ${payload.length}\r\n\r\n`, "utf8"),
|
|
7
|
+
payload,
|
|
8
|
+
]);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class MessageBuffer {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.buffer = Buffer.alloc(0);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
push(chunk) {
|
|
17
|
+
const incoming = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
18
|
+
this.buffer = Buffer.concat([this.buffer, incoming]);
|
|
19
|
+
|
|
20
|
+
if (this.buffer.length > MAX_MESSAGE_BYTES) {
|
|
21
|
+
this.buffer = Buffer.alloc(0);
|
|
22
|
+
throw new Error("Message buffer exceeded maximum size");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const messages = [];
|
|
26
|
+
while (true) {
|
|
27
|
+
const headerEnd = this.buffer.indexOf("\r\n\r\n");
|
|
28
|
+
if (headerEnd === -1) {
|
|
29
|
+
return messages;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const header = this.buffer.slice(0, headerEnd).toString("utf8");
|
|
33
|
+
const match = header.match(/content-length:\s*(\d+)/i);
|
|
34
|
+
if (!match) {
|
|
35
|
+
this.buffer = Buffer.alloc(0);
|
|
36
|
+
throw new Error("Missing Content-Length header");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const bodyLength = Number.parseInt(match[1], 10);
|
|
40
|
+
if (bodyLength > MAX_MESSAGE_BYTES) {
|
|
41
|
+
this.buffer = Buffer.alloc(0);
|
|
42
|
+
throw new Error("Message exceeded maximum size");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const bodyStart = headerEnd + 4;
|
|
46
|
+
const bodyEnd = bodyStart + bodyLength;
|
|
47
|
+
if (this.buffer.length < bodyEnd) {
|
|
48
|
+
return messages;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const body = this.buffer.slice(bodyStart, bodyEnd).toString("utf8");
|
|
52
|
+
this.buffer = this.buffer.slice(bodyEnd);
|
|
53
|
+
messages.push(JSON.parse(body));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
import { encodeMessage, MessageBuffer } from "./json-rpc-stdio.mjs";
|
|
2
|
+
import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
|
|
3
|
+
|
|
4
|
+
const SERVER_NAME = PACKAGE_NAME;
|
|
5
|
+
const SERVER_VERSION = PACKAGE_VERSION;
|
|
6
|
+
|
|
7
|
+
function success(id, result) {
|
|
8
|
+
return {
|
|
9
|
+
jsonrpc: "2.0",
|
|
10
|
+
id,
|
|
11
|
+
result,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function failure(id, code, message) {
|
|
16
|
+
return {
|
|
17
|
+
jsonrpc: "2.0",
|
|
18
|
+
id,
|
|
19
|
+
error: {
|
|
20
|
+
code,
|
|
21
|
+
message,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function asToolResult(value) {
|
|
27
|
+
return {
|
|
28
|
+
content: [
|
|
29
|
+
{
|
|
30
|
+
type: "text",
|
|
31
|
+
text: JSON.stringify(value, null, 2),
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
structuredContent: value,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function emptyObjectSchema() {
|
|
39
|
+
return {
|
|
40
|
+
type: "object",
|
|
41
|
+
properties: {},
|
|
42
|
+
additionalProperties: false,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function validateValue(path, value, schema) {
|
|
47
|
+
if (schema.type === "object") {
|
|
48
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
49
|
+
throw new Error(`${path} must be an object`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const properties = schema.properties ?? {};
|
|
53
|
+
const required = schema.required ?? [];
|
|
54
|
+
for (const key of required) {
|
|
55
|
+
if (value[key] === undefined) {
|
|
56
|
+
throw new Error(`${path}.${key} is required`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (schema.additionalProperties === false) {
|
|
61
|
+
for (const key of Object.keys(value)) {
|
|
62
|
+
if (!(key in properties)) {
|
|
63
|
+
throw new Error(`${path}.${key} is not allowed`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
69
|
+
if (value[key] !== undefined) {
|
|
70
|
+
validateValue(`${path}.${key}`, value[key], propertySchema);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (schema.type === "string") {
|
|
78
|
+
if (typeof value !== "string") {
|
|
79
|
+
throw new Error(`${path} must be a string`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (schema.enum && !schema.enum.includes(value)) {
|
|
83
|
+
throw new Error(`${path} must be one of: ${schema.enum.join(", ")}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (schema.type === "boolean") {
|
|
90
|
+
if (typeof value !== "boolean") {
|
|
91
|
+
throw new Error(`${path} must be a boolean`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (schema.type === "integer") {
|
|
98
|
+
if (!Number.isInteger(value)) {
|
|
99
|
+
throw new Error(`${path} must be an integer`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
103
|
+
throw new Error(`${path} must be >= ${schema.minimum}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function screenshotFormatsFor(browserFamily) {
|
|
109
|
+
return browserFamily === "firefox"
|
|
110
|
+
? ["png", "jpeg"]
|
|
111
|
+
: ["png", "jpeg", "webp"];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function sessionWithLimitSchema(description) {
|
|
115
|
+
return {
|
|
116
|
+
type: "object",
|
|
117
|
+
properties: {
|
|
118
|
+
sessionId: {
|
|
119
|
+
type: "string",
|
|
120
|
+
},
|
|
121
|
+
limit: {
|
|
122
|
+
type: "integer",
|
|
123
|
+
minimum: 1,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
required: ["sessionId"],
|
|
127
|
+
additionalProperties: false,
|
|
128
|
+
description,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export class McpBrowserDevToolsServer {
|
|
133
|
+
constructor({
|
|
134
|
+
config,
|
|
135
|
+
browserAdapter,
|
|
136
|
+
input = process.stdin,
|
|
137
|
+
output = process.stdout,
|
|
138
|
+
errorOutput = process.stderr,
|
|
139
|
+
}) {
|
|
140
|
+
this.config = config;
|
|
141
|
+
this.browserAdapter = browserAdapter;
|
|
142
|
+
this.input = input;
|
|
143
|
+
this.output = output;
|
|
144
|
+
this.errorOutput = errorOutput;
|
|
145
|
+
this.messageBuffer = new MessageBuffer();
|
|
146
|
+
this.tools = this.createTools();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
createTools() {
|
|
150
|
+
const tools = [
|
|
151
|
+
[
|
|
152
|
+
"browser_status",
|
|
153
|
+
{
|
|
154
|
+
definition: {
|
|
155
|
+
name: "browser_status",
|
|
156
|
+
description:
|
|
157
|
+
"Report whether the configured browser endpoint is reachable and how many active sessions are attached.",
|
|
158
|
+
inputSchema: emptyObjectSchema(),
|
|
159
|
+
},
|
|
160
|
+
handler: async () => this.browserAdapter.getBrowserStatus(),
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
[
|
|
164
|
+
"list_tabs",
|
|
165
|
+
{
|
|
166
|
+
definition: {
|
|
167
|
+
name: "list_tabs",
|
|
168
|
+
description:
|
|
169
|
+
"List inspectable page targets exposed by the configured browser adapter.",
|
|
170
|
+
inputSchema: emptyObjectSchema(),
|
|
171
|
+
},
|
|
172
|
+
handler: async () => ({
|
|
173
|
+
tabs: await this.browserAdapter.listTargets(),
|
|
174
|
+
}),
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
[
|
|
178
|
+
"list_sessions",
|
|
179
|
+
{
|
|
180
|
+
definition: {
|
|
181
|
+
name: "list_sessions",
|
|
182
|
+
description:
|
|
183
|
+
"List active attached debugging sessions held by this broker.",
|
|
184
|
+
inputSchema: emptyObjectSchema(),
|
|
185
|
+
},
|
|
186
|
+
handler: async () => ({
|
|
187
|
+
sessions: this.browserAdapter.listSessions(),
|
|
188
|
+
}),
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
[
|
|
192
|
+
"attach_tab",
|
|
193
|
+
{
|
|
194
|
+
definition: {
|
|
195
|
+
name: "attach_tab",
|
|
196
|
+
description:
|
|
197
|
+
"Attach to a page target and start buffering console, log, and network events.",
|
|
198
|
+
inputSchema: {
|
|
199
|
+
type: "object",
|
|
200
|
+
properties: {
|
|
201
|
+
targetId: {
|
|
202
|
+
type: "string",
|
|
203
|
+
description: "The target id returned by list_tabs.",
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
required: ["targetId"],
|
|
207
|
+
additionalProperties: false,
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
handler: async (args) =>
|
|
211
|
+
this.browserAdapter.attachToTarget(args.targetId),
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
[
|
|
215
|
+
"detach_tab",
|
|
216
|
+
{
|
|
217
|
+
definition: {
|
|
218
|
+
name: "detach_tab",
|
|
219
|
+
description: "Close an attached debugging session.",
|
|
220
|
+
inputSchema: {
|
|
221
|
+
type: "object",
|
|
222
|
+
properties: {
|
|
223
|
+
sessionId: {
|
|
224
|
+
type: "string",
|
|
225
|
+
description: "The session id returned by attach_tab.",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
required: ["sessionId"],
|
|
229
|
+
additionalProperties: false,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
handler: async (args) =>
|
|
233
|
+
this.browserAdapter.detachSession(args.sessionId),
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
"get_console_messages",
|
|
238
|
+
{
|
|
239
|
+
definition: {
|
|
240
|
+
name: "get_console_messages",
|
|
241
|
+
description:
|
|
242
|
+
"Read buffered console, log, and exception messages for an attached session.",
|
|
243
|
+
inputSchema: sessionWithLimitSchema(),
|
|
244
|
+
},
|
|
245
|
+
handler: async (args) => ({
|
|
246
|
+
messages: this.browserAdapter.getConsoleMessages(
|
|
247
|
+
args.sessionId,
|
|
248
|
+
args.limit ?? 50,
|
|
249
|
+
),
|
|
250
|
+
}),
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
[
|
|
254
|
+
"get_network_requests",
|
|
255
|
+
{
|
|
256
|
+
definition: {
|
|
257
|
+
name: "get_network_requests",
|
|
258
|
+
description:
|
|
259
|
+
"Summarize buffered network requests for an attached session in a DevTools-network-tab style view.",
|
|
260
|
+
inputSchema: sessionWithLimitSchema(),
|
|
261
|
+
},
|
|
262
|
+
handler: async (args) => ({
|
|
263
|
+
requests: this.browserAdapter.getNetworkRequests(
|
|
264
|
+
args.sessionId,
|
|
265
|
+
args.limit ?? 50,
|
|
266
|
+
),
|
|
267
|
+
}),
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
[
|
|
271
|
+
"get_document",
|
|
272
|
+
{
|
|
273
|
+
definition: {
|
|
274
|
+
name: "get_document",
|
|
275
|
+
description: "Fetch the DOM document tree for an attached page.",
|
|
276
|
+
inputSchema: {
|
|
277
|
+
type: "object",
|
|
278
|
+
properties: {
|
|
279
|
+
sessionId: {
|
|
280
|
+
type: "string",
|
|
281
|
+
},
|
|
282
|
+
depth: {
|
|
283
|
+
type: "integer",
|
|
284
|
+
minimum: 1,
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
required: ["sessionId"],
|
|
288
|
+
additionalProperties: false,
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
handler: async (args) =>
|
|
292
|
+
this.browserAdapter.getDocument(args.sessionId, args.depth ?? 2),
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
[
|
|
296
|
+
"inspect_element",
|
|
297
|
+
{
|
|
298
|
+
definition: {
|
|
299
|
+
name: "inspect_element",
|
|
300
|
+
description:
|
|
301
|
+
"Inspect a single DOM element with a CSS selector and return normalized element details.",
|
|
302
|
+
inputSchema: {
|
|
303
|
+
type: "object",
|
|
304
|
+
properties: {
|
|
305
|
+
sessionId: {
|
|
306
|
+
type: "string",
|
|
307
|
+
},
|
|
308
|
+
selector: {
|
|
309
|
+
type: "string",
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
required: ["sessionId", "selector"],
|
|
313
|
+
additionalProperties: false,
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
handler: async (args) =>
|
|
317
|
+
this.browserAdapter.inspectElement(args.sessionId, args.selector),
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
[
|
|
321
|
+
"take_screenshot",
|
|
322
|
+
{
|
|
323
|
+
definition: {
|
|
324
|
+
name: "take_screenshot",
|
|
325
|
+
description: "Capture a screenshot from an attached page.",
|
|
326
|
+
inputSchema: {
|
|
327
|
+
type: "object",
|
|
328
|
+
properties: {
|
|
329
|
+
sessionId: {
|
|
330
|
+
type: "string",
|
|
331
|
+
},
|
|
332
|
+
format: {
|
|
333
|
+
type: "string",
|
|
334
|
+
enum: screenshotFormatsFor(this.config.browserFamily),
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
required: ["sessionId"],
|
|
338
|
+
additionalProperties: false,
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
handler: async (args) =>
|
|
342
|
+
this.browserAdapter.takeScreenshot(
|
|
343
|
+
args.sessionId,
|
|
344
|
+
args.format ?? "png",
|
|
345
|
+
),
|
|
346
|
+
},
|
|
347
|
+
],
|
|
348
|
+
[
|
|
349
|
+
"get_events",
|
|
350
|
+
{
|
|
351
|
+
definition: {
|
|
352
|
+
name: "get_events",
|
|
353
|
+
description:
|
|
354
|
+
"Read buffered console, log, exception, and network events for an attached session.",
|
|
355
|
+
inputSchema: {
|
|
356
|
+
type: "object",
|
|
357
|
+
properties: {
|
|
358
|
+
sessionId: {
|
|
359
|
+
type: "string",
|
|
360
|
+
},
|
|
361
|
+
limit: {
|
|
362
|
+
type: "integer",
|
|
363
|
+
minimum: 1,
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
required: ["sessionId"],
|
|
367
|
+
additionalProperties: false,
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
handler: async (args) => ({
|
|
371
|
+
events: this.browserAdapter.getEvents(
|
|
372
|
+
args.sessionId,
|
|
373
|
+
args.limit ?? 50,
|
|
374
|
+
),
|
|
375
|
+
}),
|
|
376
|
+
},
|
|
377
|
+
],
|
|
378
|
+
];
|
|
379
|
+
|
|
380
|
+
if (this.config.enableEvaluate) {
|
|
381
|
+
tools.push([
|
|
382
|
+
"evaluate_js",
|
|
383
|
+
{
|
|
384
|
+
definition: {
|
|
385
|
+
name: "evaluate_js",
|
|
386
|
+
description: "Evaluate JavaScript in the attached page context.",
|
|
387
|
+
inputSchema: {
|
|
388
|
+
type: "object",
|
|
389
|
+
properties: {
|
|
390
|
+
sessionId: {
|
|
391
|
+
type: "string",
|
|
392
|
+
},
|
|
393
|
+
expression: {
|
|
394
|
+
type: "string",
|
|
395
|
+
},
|
|
396
|
+
awaitPromise: {
|
|
397
|
+
type: "boolean",
|
|
398
|
+
},
|
|
399
|
+
returnByValue: {
|
|
400
|
+
type: "boolean",
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
required: ["sessionId", "expression"],
|
|
404
|
+
additionalProperties: false,
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
handler: async (args) =>
|
|
408
|
+
this.browserAdapter.evaluate(args.sessionId, args.expression, {
|
|
409
|
+
awaitPromise: args.awaitPromise,
|
|
410
|
+
returnByValue: args.returnByValue,
|
|
411
|
+
}),
|
|
412
|
+
},
|
|
413
|
+
]);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return new Map(tools);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
start() {
|
|
420
|
+
this.input.on("data", (chunk) => {
|
|
421
|
+
let messages;
|
|
422
|
+
try {
|
|
423
|
+
messages = this.messageBuffer.push(chunk);
|
|
424
|
+
} catch (error) {
|
|
425
|
+
this.log(`failed to parse incoming message: ${error.message}`);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
for (const message of messages) {
|
|
430
|
+
void this.dispatch(message);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
this.log(
|
|
435
|
+
`listening on stdio for MCP messages; browser=${this.config.browserFamily}`,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
log(message) {
|
|
440
|
+
this.errorOutput.write(`[${SERVER_NAME}] ${message}\n`);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async dispatch(message) {
|
|
444
|
+
const response = await this.handleRequest(message);
|
|
445
|
+
if (!response) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
this.output.write(encodeMessage(response));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async handleRequest(message) {
|
|
453
|
+
const id = message?.id ?? null;
|
|
454
|
+
|
|
455
|
+
if (message?.jsonrpc !== "2.0") {
|
|
456
|
+
return failure(id, -32600, "Invalid Request");
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
switch (message.method) {
|
|
461
|
+
case "initialize":
|
|
462
|
+
return success(id, {
|
|
463
|
+
protocolVersion: this.config.protocolVersion,
|
|
464
|
+
capabilities: {
|
|
465
|
+
tools: {},
|
|
466
|
+
},
|
|
467
|
+
serverInfo: {
|
|
468
|
+
name: SERVER_NAME,
|
|
469
|
+
version: SERVER_VERSION,
|
|
470
|
+
},
|
|
471
|
+
instructions:
|
|
472
|
+
"Use the browser tools to inspect tabs, console output, network activity, DOM structure, and specific elements across Chromium CDP or Firefox BiDi.",
|
|
473
|
+
});
|
|
474
|
+
case "notifications/initialized":
|
|
475
|
+
return null;
|
|
476
|
+
case "ping":
|
|
477
|
+
return success(id, {});
|
|
478
|
+
case "tools/list":
|
|
479
|
+
return success(id, {
|
|
480
|
+
tools: Array.from(this.tools.values(), (tool) => tool.definition),
|
|
481
|
+
});
|
|
482
|
+
case "tools/call":
|
|
483
|
+
return success(id, await this.callTool(message.params));
|
|
484
|
+
default:
|
|
485
|
+
if (message.id === undefined) {
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
return failure(id, -32601, `Method not found: ${message.method}`);
|
|
489
|
+
}
|
|
490
|
+
} catch (error) {
|
|
491
|
+
return failure(id, -32000, error.message);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async callTool(params = {}) {
|
|
496
|
+
const tool = this.tools.get(params.name);
|
|
497
|
+
if (!tool) {
|
|
498
|
+
throw new Error(`Unknown tool: ${params.name}`);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const args = params.arguments ?? {};
|
|
502
|
+
validateValue("arguments", args, tool.definition.inputSchema);
|
|
503
|
+
const result = await tool.handler(args);
|
|
504
|
+
return asToolResult(result);
|
|
505
|
+
}
|
|
506
|
+
}
|
package/src/server.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const CONSOLE_EVENT_KINDS = new Set(["console", "log", "exception"]);
|
|
2
|
+
|
|
3
|
+
function normalizeLimit(limit, fallback = 50) {
|
|
4
|
+
return Number.isInteger(limit) && limit > 0 ? limit : fallback;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function filterConsoleMessages(events = [], limit = 50) {
|
|
8
|
+
const safeLimit = normalizeLimit(limit);
|
|
9
|
+
return events
|
|
10
|
+
.filter((event) => CONSOLE_EVENT_KINDS.has(event.kind))
|
|
11
|
+
.slice(-safeLimit);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function createEmptyNetworkRequest(requestId) {
|
|
15
|
+
return {
|
|
16
|
+
requestId,
|
|
17
|
+
url: null,
|
|
18
|
+
method: null,
|
|
19
|
+
resourceType: null,
|
|
20
|
+
status: null,
|
|
21
|
+
statusText: null,
|
|
22
|
+
mimeType: null,
|
|
23
|
+
startedAt: null,
|
|
24
|
+
completedAt: null,
|
|
25
|
+
updatedAt: null,
|
|
26
|
+
finished: false,
|
|
27
|
+
failed: false,
|
|
28
|
+
canceled: false,
|
|
29
|
+
errorText: null,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function summarizeNetworkRequests(events = [], limit = 50) {
|
|
34
|
+
const safeLimit = normalizeLimit(limit);
|
|
35
|
+
const requests = new Map();
|
|
36
|
+
|
|
37
|
+
for (const event of events) {
|
|
38
|
+
if (event?.kind !== "network") {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const requestId = event.requestId ?? `network-${requests.size + 1}`;
|
|
43
|
+
const timestamp = event.timestamp ?? event.capturedAt ?? null;
|
|
44
|
+
const next = {
|
|
45
|
+
...createEmptyNetworkRequest(requestId),
|
|
46
|
+
...(requests.get(requestId) ?? {}),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
next.url = event.url ?? next.url;
|
|
50
|
+
next.method = event.method ?? next.method;
|
|
51
|
+
next.resourceType = event.resourceType ?? next.resourceType;
|
|
52
|
+
next.status = event.status ?? next.status;
|
|
53
|
+
next.statusText = event.statusText ?? next.statusText;
|
|
54
|
+
next.mimeType = event.mimeType ?? next.mimeType;
|
|
55
|
+
next.updatedAt = timestamp ?? next.updatedAt;
|
|
56
|
+
|
|
57
|
+
if (!next.startedAt || event.phase === "request") {
|
|
58
|
+
next.startedAt = next.startedAt ?? timestamp;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (event.completed) {
|
|
62
|
+
next.completedAt = timestamp ?? next.completedAt;
|
|
63
|
+
next.finished = !event.failed;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (event.failed) {
|
|
67
|
+
next.failed = true;
|
|
68
|
+
next.canceled = event.canceled ?? next.canceled;
|
|
69
|
+
next.errorText = event.errorText ?? next.errorText;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
requests.set(requestId, next);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return Array.from(requests.values()).slice(-safeLimit);
|
|
76
|
+
}
|