Skip to content

Commit d9f77f8

Browse files
authored
fix: do not error if the dialog was already handled (#208)
users report that the MCP client gets stuck if the dialog was closed manually as the MCP state never updates.
1 parent 13613b4 commit d9f77f8

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/tools/pages.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import z from 'zod';
88

9+
import {logger} from '../logger.js';
10+
911
import {ToolCategories} from './categories.js';
1012
import {CLOSE_PAGE_ERROR, defineTool} from './ToolDefinition.js';
1113

@@ -195,12 +197,22 @@ export const handleDialog = defineTool({
195197

196198
switch (request.params.action) {
197199
case 'accept': {
198-
await dialog.accept(request.params.promptText);
200+
try {
201+
await dialog.accept(request.params.promptText);
202+
} catch (err) {
203+
// Likely already handled by the user outside of MCP.
204+
logger(err);
205+
}
199206
response.appendResponseLine('Successfully accepted the dialog');
200207
break;
201208
}
202209
case 'dismiss': {
203-
await dialog.dismiss();
210+
try {
211+
await dialog.dismiss();
212+
} catch (err) {
213+
// Likely already handled.
214+
logger(err);
215+
}
204216
response.appendResponseLine('Successfully dismissed the dialog');
205217
break;
206218
}

tests/tools/pages.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import assert from 'node:assert';
77
import {describe, it} from 'node:test';
88

9+
import type {Dialog} from 'puppeteer-core';
10+
911
import {
1012
listPages,
1113
newPage,
@@ -265,5 +267,34 @@ describe('pages', () => {
265267
);
266268
});
267269
});
270+
it('can dismiss alread dismissed dialog dialogs', async () => {
271+
await withBrowser(async (response, context) => {
272+
const page = context.getSelectedPage();
273+
const dialogPromise = new Promise<Dialog>(resolve => {
274+
page.on('dialog', dialog => {
275+
resolve(dialog);
276+
});
277+
});
278+
page.evaluate(() => {
279+
alert('test');
280+
});
281+
const dialog = await dialogPromise;
282+
await dialog.dismiss();
283+
await handleDialog.handler(
284+
{
285+
params: {
286+
action: 'dismiss',
287+
},
288+
},
289+
response,
290+
context,
291+
);
292+
assert.strictEqual(context.getDialog(), undefined);
293+
assert.strictEqual(
294+
response.responseLines[0],
295+
'Successfully dismissed the dialog',
296+
);
297+
});
298+
});
268299
});
269300
});

0 commit comments

Comments
 (0)