Skip to content

Chrome DevTools for coding agents

fix: block evaluate_script while a dialog is open (#3196)

patrickson545bcbb8c66b
diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts
index ab14a67f..3c2f5271 100644
--- a/src/tools/ToolDefinition.ts
+++ b/src/tools/ToolDefinition.ts
@@ -121,6 +121,11 @@ export interface ToolDefinition<
Schema extends zod.ZodRawShape = zod.ZodRawShape,
> extends BaseToolDefinition<Schema> {
schema: Schema;
+ /**
+ * Only page-scoped tools are checked for open dialogs by ToolHandler, so a
+ * non-page tool must handle dialogs itself.
+ */
+ blockedByDialog: false;
handler(
request: Request<Schema>,
response: Response,
@@ -545,6 +550,11 @@ export interface BrowserlessToolDefinition<
Schema extends zod.ZodRawShape = zod.ZodRawShape,
> extends BaseToolDefinition<Schema> {
browserless: true;
+ /**
+ * Browserless tools have no page, so ToolHandler never checks them for open
+ * dialogs.
+ */
+ blockedByDialog: false;
handler(
request: Request<Schema>,
response: Response,
diff --git a/src/tools/script.ts b/src/tools/script.ts
index 3b2232f5..989f2f59 100644
--- a/src/tools/script.ts
+++ b/src/tools/script.ts
@@ -100,7 +100,7 @@ Example with arguments: \`(el) => el.innerText\`
}
: {}),
},
- blockedByDialog: true,
+ blockedByDialog: false,
verifyFilesSchema: {
filePath: true,
// sourcePath may be an http(s): URL the server fetches, so allow URLs
@@ -164,6 +164,12 @@ Example with arguments: \`(el) => el.innerText\`
? context.getPageById(request.params.pageId)
: context.getSelectedMcpPage();
await mcpPage.init();
+ // evaluate_script is not page-scoped, so ToolHandler does not run the
+ // blockedByDialog check for it. Without this, an evaluation started
+ // while a dialog is open waits until the protocol timeout.
+ // TODO: include the "# Open dialog ... Call handle_dialog" hint once
+ // Response can attach the page for non-page-scoped tools.
+ mcpPage.throwIfDialogOpen();
const page: Page = mcpPage.pptrPage;
const args: Array<JSHandle<unknown>> = [];
diff --git a/tests/index.test.ts b/tests/index.test.ts
index a3c70933..dfa8de3c 100644
--- a/tests/index.test.ts
+++ b/tests/index.test.ts
@@ -419,5 +419,27 @@ describe('e2e', {concurrency: true}, () => {
t.assert.snapshot(JSON.stringify(result));
});
});
+
+ it('when dialog is open, evaluate_script returns an error', async () => {
+ await withClient(async client => {
+ await createNewPageAndTriggerDialog(client);
+ const result = await client.callTool(
+ {
+ name: 'evaluate_script',
+ arguments: {
+ pageId: 2,
+ function: '() => 1',
+ },
+ },
+ {timeout: 10_000},
+ );
+
+ assert.strictEqual(result.isError, true);
+ assert.match(
+ getFirstText(result),
+ /A dialog is open \(alert: test dialog\)\./,
+ );
+ });
+ });
});
});
diff --git a/tests/tools/script.test.ts b/tests/tools/script.test.ts
index 0625fb3d..f9964163 100644
--- a/tests/tools/script.test.ts
+++ b/tests/tools/script.test.ts
@@ -228,6 +228,23 @@ describe('script', () => {
pathToFileURL(sourcePath).href,
);
});
+ it('throws without evaluating if a dialog is already open', async () => {
+ const {page, response, context, args} = createHandlerMocks();
+ page.throwIfDialogOpen.throws(
+ new Error('A dialog is open (alert: hello).'),
+ );
+ await assert.rejects(
+ evaluateScript(args).handler(
+ {params: {function: '() => 1'}},
+ response,
+ context,
+ ),
+ {message: 'A dialog is open (alert: hello).'},
+ );
+ sinon.assert.calledOnceWithExactly(page.throwIfDialogOpen);
+ sinon.assert.callOrder(page.init, page.throwIfDialogOpen);
+ sinon.assert.notCalled(page.waitForEventsAfterAction);
+ });
it('skips the stable DOM wait when waitForStableDom is false', async () => {
await withMcpContext(async (response, context, args) => {
const spy = sinon.spy(WaitForHelper.prototype, 'waitForStableDom');