Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/WaitForHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ export class WaitForHelper {
action: () => Promise<unknown>,
options?: {timeout?: number; handleDialog?: 'accept' | 'dismiss' | string},
): Promise<void> {
let dialogOpened = false;
if (options?.handleDialog) {
const dialogHandler = (dialog: Pick<Dialog, 'accept' | 'dismiss'>) => {
dialogOpened = true;
if (options.handleDialog === 'dismiss') {
void dialog.dismiss();
} else if (options.handleDialog === 'accept') {
Expand Down Expand Up @@ -167,6 +169,10 @@ export class WaitForHelper {
try {
await navigationFinished;

if (dialogOpened) {
return;
}

// Wait for stable dom after navigation so we execute in
// the correct context
await this.waitForStableDom();
Expand Down
27 changes: 19 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,25 @@ export async function createMcpServer(
? context.getPageById(params.pageId)
: context.getSelectedMcpPage();
response.setPage(page);
await tool.handler(
{
params,
page,
},
response,
context,
);
try {
await tool.handler(
{
params,
page,
},
response,
context,
);
} catch (error) {
const dialog = page.getDialog();
if (dialog) {
response.appendResponseLine(
`[Blocked] Action triggered a dialog.`,
);
} else {
throw error;
}
}
} else {
await tool.handler(
// @ts-expect-error types do not match.
Expand Down
32 changes: 32 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,38 @@ describe('e2e', () => {
},
);
});

it('returns blocked message when dialog is opened during tool execution', async () => {
await withClient(async client => {
// Navigate to a page with a button that triggers a dialog on click
await client.callTool({
name: 'new_page',
arguments: {
url: `data:text/html,<button id="test" onclick="alert('test dialog')">Click me</button>`,
},
});

const snapshotResult = await client.callTool({
name: 'take_snapshot',
arguments: {},
});

const snapshotText = (snapshotResult.content as TextContent[])[0].text;
const match = snapshotText.match(/uid=(\d+_\d+)\s+button "Click me"/);
const uid = match ? match[1] : '1_1';

// Trigger the dialog
const result = await client.callTool({
name: 'click',
arguments: {
uid,
},
});

const content = result.content as TextContent[];
assert.match(content[0].text, /\[Blocked\] Action triggered a dialog\./);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we assert complete response via a snapshot assertion? I want to make sure there is no duplication in the full response

});
});
});

async function getToolsWithFilteredCategories(
Expand Down
Loading