Skip to content

Commit 4160e27

Browse files
committed
chore: reverting evaluateScript to use defineTool
1 parent f10f417 commit 4160e27

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/McpContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import type {
3434
} from './third_party/index.js';
3535
import {Locator} from './third_party/index.js';
3636
import {PredefinedNetworkConditions} from './third_party/index.js';
37+
import {listPages} from './tools/pages.js';
3738
import {CLOSE_PAGE_ERROR} from './tools/ToolDefinition.js';
3839
import type {Context, DevToolsData} from './tools/ToolDefinition.js';
3940
import type {TraceResult} from './trace-processing/parse.js';
@@ -508,7 +509,7 @@ export class McpContext implements Context {
508509
}
509510
if (page.isClosed()) {
510511
throw new Error(
511-
`The selected page has been closed. Call list_pages to see open pages.`,
512+
`The selected page has been closed. Call ${listPages().name} to see open pages.`,
512513
);
513514
}
514515
return page;

src/tools/ToolDefinition.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export type Context = Readonly<{
142142
getDialog(page?: Page): Dialog | undefined;
143143
clearDialog(page?: Page): void;
144144
getPageById(pageId: number): Page;
145+
resolvePageById(pageId?: number): Page;
145146
newPage(background?: boolean, isolatedContextName?: string): Promise<Page>;
146147
closePage(pageId: number): Promise<void>;
147148
selectPage(page: Page): void;

src/tools/script.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import type {Frame, JSHandle, Page, WebWorker} from '../third_party/index.js';
1010

1111
import {ToolCategory} from './categories.js';
1212
import type {Context} from './ToolDefinition.js';
13-
import {definePageTool} from './ToolDefinition.js';
13+
import {defineTool, pageIdSchema} from './ToolDefinition.js';
1414

1515
export type Evaluatable = Page | Frame | WebWorker;
1616

17-
export const evaluateScript = definePageTool(cliArgs => {
17+
export const evaluateScript = defineTool(cliArgs => {
1818
return {
1919
name: 'evaluate_script',
2020
description: `Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON,
@@ -48,6 +48,7 @@ Example with arguments: \`(el) => {
4848
)
4949
.optional()
5050
.describe(`An optional list of arguments to pass to the function.`),
51+
...(cliArgs?.experimentalPageIdRouting ? pageIdSchema : {}),
5152
...(cliArgs?.categoryExtensions
5253
? {
5354
serviceWorkerId: zod
@@ -60,17 +61,22 @@ Example with arguments: \`(el) => {
6061
: {}),
6162
},
6263
handler: async (request, response, context) => {
64+
const page: Page = cliArgs?.experimentalPageIdRouting
65+
? context.resolvePageById(request.params.pageId)
66+
: context.getSelectedPage();
67+
6368
const args: Array<JSHandle<unknown>> = [];
6469
try {
6570
const frames = new Set<Frame>();
6671
for (const el of request.params.args ?? []) {
67-
const handle = await context.getElementByUid(el.uid, request.page);
72+
const handle = await context.getElementByUid(el.uid, page);
6873
frames.add(handle.frame);
6974
args.push(handle);
7075
}
7176

7277
const evaluatable = await getEvaluatable(
7378
context,
79+
page,
7480
frames,
7581
cliArgs?.categoryExtensions,
7682
request.params.serviceWorkerId as string | undefined,
@@ -103,18 +109,20 @@ Example with arguments: \`(el) => {
103109

104110
const getEvaluatable = async (
105111
context: Context,
112+
page: Page,
106113
frames: Set<Frame>,
107114
enableExtensions?: boolean,
108115
serviceWorkerId?: string,
109116
): Promise<Evaluatable> => {
110117
if (enableExtensions && serviceWorkerId) {
111118
return getWebWorker(context, serviceWorkerId);
112119
}
113-
return getPageOrFrame(context, frames);
120+
return getPageOrFrame(context, page, frames);
114121
};
115122

116123
const getPageOrFrame = async (
117124
context: Context,
125+
page: Page,
118126
frames: Set<Frame>,
119127
): Promise<Page | Frame> => {
120128
let pageOrFrame: Page | Frame;
@@ -124,7 +132,7 @@ const getPageOrFrame = async (
124132
"Elements from different frames can't be evaluated together.",
125133
);
126134
} else {
127-
pageOrFrame = [...frames.values()][0] ?? context.getSelectedPage();
135+
pageOrFrame = [...frames.values()][0] ?? page;
128136
}
129137

130138
return pageOrFrame;

tests/tools/script.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ describe('script', () => {
3030
await evaluateScript().handler(
3131
{
3232
params: {function: String(() => 2 * 5)},
33-
page: context.getSelectedPage(),
3433
},
3534
response,
3635
context,
@@ -44,7 +43,6 @@ describe('script', () => {
4443
await evaluateScript().handler(
4544
{
4645
params: {function: String(() => document.title)},
47-
page: context.getSelectedPage(),
4846
},
4947
response,
5048
context,
@@ -64,7 +62,6 @@ describe('script', () => {
6462
await evaluateScript().handler(
6563
{
6664
params: {function: String(() => document.title)},
67-
page: context.getSelectedPage(),
6865
},
6966
response,
7067
context,
@@ -92,7 +89,6 @@ describe('script', () => {
9289
return {scripts};
9390
}),
9491
},
95-
page: context.getSelectedPage(),
9692
},
9793
response,
9894
context,
@@ -118,7 +114,6 @@ describe('script', () => {
118114
return 'Works';
119115
}),
120116
},
121-
page: context.getSelectedPage(),
122117
},
123118
response,
124119
context,
@@ -144,7 +139,6 @@ describe('script', () => {
144139
}),
145140
args: [{uid: '1_1'}],
146141
},
147-
page: context.getSelectedPage(),
148142
},
149143
response,
150144
context,
@@ -170,7 +164,6 @@ describe('script', () => {
170164
}),
171165
args: [{uid: '1_0'}, {uid: '1_1'}],
172166
},
173-
page: context.getSelectedPage(),
174167
},
175168
response,
176169
context,
@@ -199,7 +192,6 @@ describe('script', () => {
199192
}),
200193
args: [{uid: '1_3'}],
201194
},
202-
page: context.getSelectedPage(),
203195
},
204196
response,
205197
context,
@@ -243,7 +235,6 @@ describe('script', () => {
243235
}),
244236
serviceWorkerId: swId,
245237
},
246-
page: context.getSelectedPage(),
247238
},
248239
response,
249240
context,

0 commit comments

Comments
 (0)