Skip to content

Commit 949de38

Browse files
committed
perf: improve options for sharing content
1 parent c7733a8 commit 949de38

File tree

5 files changed

+142
-115
lines changed

5 files changed

+142
-115
lines changed

src/tools/ToolDefinition.ts

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {Dialog, ElementHandle, Page} from 'puppeteer-core';
7+
import type {ConsoleMessageType, Dialog, ElementHandle, Page, ResourceType} from 'puppeteer-core';
88

99
import type {TextSnapshotNode} from '../McpContext.js';
1010
import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';
@@ -115,3 +115,109 @@ export const timeoutSchema = {
115115
return value && value <= 0 ? undefined : value;
116116
}),
117117
};
118+
119+
export const snapshotSchema = {
120+
verbose: zod
121+
.boolean()
122+
.optional()
123+
.describe(
124+
'Whether to include all possible information available in the full a11y tree. Default is false.',
125+
),
126+
};
127+
128+
const FILTERABLE_MESSAGE_TYPES: readonly [
129+
ConsoleMessageType,
130+
...ConsoleMessageType[],
131+
] = [
132+
'log',
133+
'debug',
134+
'info',
135+
'error',
136+
'warn',
137+
'dir',
138+
'dirxml',
139+
'table',
140+
'trace',
141+
'clear',
142+
'startGroup',
143+
'startGroupCollapsed',
144+
'endGroup',
145+
'assert',
146+
'profile',
147+
'profileEnd',
148+
'count',
149+
'timeEnd',
150+
'verbose',
151+
]
152+
153+
export const consoleMessagesSchema = {
154+
pageSize: zod
155+
.number()
156+
.int()
157+
.positive()
158+
.optional()
159+
.describe(
160+
'Maximum number of messages to return. When omitted, returns all requests.',
161+
),
162+
pageIdx: zod
163+
.number()
164+
.int()
165+
.min(0)
166+
.optional()
167+
.describe(
168+
'Page number to return (0-based). When omitted, returns the first page.',
169+
),
170+
types: zod
171+
.array(zod.enum(FILTERABLE_MESSAGE_TYPES))
172+
.optional()
173+
.describe(
174+
'Filter messages to only return messages of the specified resource types. When omitted or empty, returns all messages.',
175+
),
176+
};
177+
178+
const FILTERABLE_RESOURCE_TYPES: readonly [ResourceType, ...ResourceType[]] = [
179+
'document',
180+
'stylesheet',
181+
'image',
182+
'media',
183+
'font',
184+
'script',
185+
'texttrack',
186+
'xhr',
187+
'fetch',
188+
'prefetch',
189+
'eventsource',
190+
'websocket',
191+
'manifest',
192+
'signedexchange',
193+
'ping',
194+
'cspviolationreport',
195+
'preflight',
196+
'fedcm',
197+
'other',
198+
];
199+
200+
export const networkRequestsSchema = {
201+
pageSize: zod
202+
.number()
203+
.int()
204+
.positive()
205+
.optional()
206+
.describe(
207+
'Maximum number of requests to return. When omitted, returns all requests.',
208+
),
209+
pageIdx: zod
210+
.number()
211+
.int()
212+
.min(0)
213+
.optional()
214+
.describe(
215+
'Page number to return (0-based). When omitted, returns the first page.',
216+
),
217+
resourceTypes: zod
218+
.array(zod.enum(FILTERABLE_RESOURCE_TYPES))
219+
.optional()
220+
.describe(
221+
'Filter requests to only return requests of the specified resource types. When omitted or empty, returns all requests.',
222+
),
223+
}

src/tools/console.ts

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {ConsoleMessageType} from 'puppeteer-core';
8-
9-
import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';
10-
117
import {ToolCategories} from './categories.js';
12-
import {defineTool} from './ToolDefinition.js';
13-
14-
const FILTERABLE_MESSAGE_TYPES: readonly [
15-
ConsoleMessageType,
16-
...ConsoleMessageType[],
17-
] = [
18-
'log',
19-
'debug',
20-
'info',
21-
'error',
22-
'warn',
23-
'dir',
24-
'dirxml',
25-
'table',
26-
'trace',
27-
'clear',
28-
'startGroup',
29-
'startGroupCollapsed',
30-
'endGroup',
31-
'assert',
32-
'profile',
33-
'profileEnd',
34-
'count',
35-
'timeEnd',
36-
'verbose',
37-
];
8+
import {consoleMessagesSchema, defineTool} from './ToolDefinition.js';
389

3910
export const consoleTool = defineTool({
4011
name: 'list_console_messages',
@@ -45,28 +16,7 @@ export const consoleTool = defineTool({
4516
readOnlyHint: true,
4617
},
4718
schema: {
48-
pageSize: zod
49-
.number()
50-
.int()
51-
.positive()
52-
.optional()
53-
.describe(
54-
'Maximum number of messages to return. When omitted, returns all requests.',
55-
),
56-
pageIdx: zod
57-
.number()
58-
.int()
59-
.min(0)
60-
.optional()
61-
.describe(
62-
'Page number to return (0-based). When omitted, returns the first page.',
63-
),
64-
types: zod
65-
.array(zod.enum(FILTERABLE_MESSAGE_TYPES))
66-
.optional()
67-
.describe(
68-
'Filter messages to only return messages of the specified resource types. When omitted or empty, returns all messages.',
69-
),
19+
...consoleMessagesSchema,
7020
},
7121
handler: async (request, response) => {
7222
response.setIncludeConsoleData(true, {

src/tools/input.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {McpContext, TextSnapshotNode} from '../McpContext.js';
1010
import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';
1111

1212
import {ToolCategories} from './categories.js';
13-
import {defineTool} from './ToolDefinition.js';
13+
import {defineTool, snapshotSchema} from './ToolDefinition.js';
1414

1515
export const click = defineTool({
1616
name: 'click',
@@ -29,6 +29,9 @@ export const click = defineTool({
2929
.boolean()
3030
.optional()
3131
.describe('Set to true for double clicks. Default is false.'),
32+
snapshot: zod.object({
33+
...snapshotSchema,
34+
}).optional().describe('Options for the snapshot included in the response'),
3235
},
3336
handler: async (request, response, context) => {
3437
const uid = request.params.uid;
@@ -44,7 +47,7 @@ export const click = defineTool({
4447
? `Successfully double clicked on the element`
4548
: `Successfully clicked on the element`,
4649
);
47-
response.setIncludeSnapshot(true);
50+
response.setIncludeSnapshot(true, request.params.snapshot?.verbose ?? false);
4851
} finally {
4952
void handle.dispose();
5053
}
@@ -64,6 +67,9 @@ export const hover = defineTool({
6467
.describe(
6568
'The uid of an element on the page from the page content snapshot',
6669
),
70+
snapshot: zod.object({
71+
...snapshotSchema,
72+
}).optional().describe('Options for the snapshot included in the response'),
6773
},
6874
handler: async (request, response, context) => {
6975
const uid = request.params.uid;
@@ -73,7 +79,7 @@ export const hover = defineTool({
7379
await handle.asLocator().hover();
7480
});
7581
response.appendResponseLine(`Successfully hovered over the element`);
76-
response.setIncludeSnapshot(true);
82+
response.setIncludeSnapshot(true, request.params.snapshot?.verbose ?? false);
7783
} finally {
7884
void handle.dispose();
7985
}
@@ -149,6 +155,9 @@ export const fill = defineTool({
149155
'The uid of an element on the page from the page content snapshot',
150156
),
151157
value: zod.string().describe('The value to fill in'),
158+
snapshot: zod.object({
159+
...snapshotSchema,
160+
}).optional().describe('Options for the snapshot included in the response'),
152161
},
153162
handler: async (request, response, context) => {
154163
await context.waitForEventsAfterAction(async () => {
@@ -159,7 +168,7 @@ export const fill = defineTool({
159168
);
160169
});
161170
response.appendResponseLine(`Successfully filled out the element`);
162-
response.setIncludeSnapshot(true);
171+
response.setIncludeSnapshot(true, request.params.snapshot?.verbose ?? false);
163172
},
164173
});
165174

@@ -173,6 +182,9 @@ export const drag = defineTool({
173182
schema: {
174183
from_uid: zod.string().describe('The uid of the element to drag'),
175184
to_uid: zod.string().describe('The uid of the element to drop into'),
185+
snapshot: zod.object({
186+
...snapshotSchema,
187+
}).optional().describe('Options for the snapshot included in the response'),
176188
},
177189
handler: async (request, response, context) => {
178190
const fromHandle = await context.getElementByUid(request.params.from_uid);
@@ -184,7 +196,7 @@ export const drag = defineTool({
184196
await toHandle.drop(fromHandle);
185197
});
186198
response.appendResponseLine(`Successfully dragged an element`);
187-
response.setIncludeSnapshot(true);
199+
response.setIncludeSnapshot(true, request.params.snapshot?.verbose ?? false);
188200
} finally {
189201
void fromHandle.dispose();
190202
void toHandle.dispose();
@@ -208,6 +220,9 @@ export const fillForm = defineTool({
208220
}),
209221
)
210222
.describe('Elements from snapshot to fill out.'),
223+
snapshot: zod.object({
224+
...snapshotSchema,
225+
}).optional().describe('Options for the snapshot included in the response'),
211226
},
212227
handler: async (request, response, context) => {
213228
for (const element of request.params.elements) {
@@ -220,7 +235,7 @@ export const fillForm = defineTool({
220235
});
221236
}
222237
response.appendResponseLine(`Successfully filled out the form`);
223-
response.setIncludeSnapshot(true);
238+
response.setIncludeSnapshot(true, request.params.snapshot?.verbose ?? false);
224239
},
225240
});
226241

@@ -238,6 +253,9 @@ export const uploadFile = defineTool({
238253
'The uid of the file input element or an element that will open file chooser on the page from the page content snapshot',
239254
),
240255
filePath: zod.string().describe('The local path of the file to upload'),
256+
snapshot: zod.object({
257+
...snapshotSchema,
258+
}).optional().describe('Options for the snapshot included in the response'),
241259
},
242260
handler: async (request, response, context) => {
243261
const {uid, filePath} = request.params;
@@ -264,7 +282,7 @@ export const uploadFile = defineTool({
264282
);
265283
}
266284
}
267-
response.setIncludeSnapshot(true);
285+
response.setIncludeSnapshot(true, request.params.snapshot?.verbose ?? false);
268286
response.appendResponseLine(`File uploaded from ${filePath}.`);
269287
} finally {
270288
void handle.dispose();

src/tools/network.ts

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,10 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {ResourceType} from 'puppeteer-core';
8-
97
import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';
108

119
import {ToolCategories} from './categories.js';
12-
import {defineTool} from './ToolDefinition.js';
13-
14-
const FILTERABLE_RESOURCE_TYPES: readonly [ResourceType, ...ResourceType[]] = [
15-
'document',
16-
'stylesheet',
17-
'image',
18-
'media',
19-
'font',
20-
'script',
21-
'texttrack',
22-
'xhr',
23-
'fetch',
24-
'prefetch',
25-
'eventsource',
26-
'websocket',
27-
'manifest',
28-
'signedexchange',
29-
'ping',
30-
'cspviolationreport',
31-
'preflight',
32-
'fedcm',
33-
'other',
34-
];
10+
import {defineTool, networkRequestsSchema} from './ToolDefinition.js';
3511

3612
export const listNetworkRequests = defineTool({
3713
name: 'list_network_requests',
@@ -41,28 +17,7 @@ export const listNetworkRequests = defineTool({
4117
readOnlyHint: true,
4218
},
4319
schema: {
44-
pageSize: zod
45-
.number()
46-
.int()
47-
.positive()
48-
.optional()
49-
.describe(
50-
'Maximum number of requests to return. When omitted, returns all requests.',
51-
),
52-
pageIdx: zod
53-
.number()
54-
.int()
55-
.min(0)
56-
.optional()
57-
.describe(
58-
'Page number to return (0-based). When omitted, returns the first page.',
59-
),
60-
resourceTypes: zod
61-
.array(zod.enum(FILTERABLE_RESOURCE_TYPES))
62-
.optional()
63-
.describe(
64-
'Filter requests to only return requests of the specified resource types. When omitted or empty, returns all requests.',
65-
),
20+
...networkRequestsSchema,
6621
},
6722
handler: async (request, response) => {
6823
response.setIncludeNetworkRequests(true, {

0 commit comments

Comments
 (0)