diff --git a/src/WaitForHelper.ts b/src/WaitForHelper.ts index f41ca84cc..6a67224c3 100644 --- a/src/WaitForHelper.ts +++ b/src/WaitForHelper.ts @@ -128,8 +128,10 @@ export class WaitForHelper { action: () => Promise, options?: {timeout?: number; handleDialog?: 'accept' | 'dismiss' | string}, ): Promise { + let dialogOpened = false; if (options?.handleDialog) { const dialogHandler = (dialog: Pick) => { + dialogOpened = true; if (options.handleDialog === 'dismiss') { void dialog.dismiss(); } else if (options.handleDialog === 'accept') { @@ -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(); diff --git a/src/index.ts b/src/index.ts index 9f522f63b..2bb9abb7c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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.