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
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,22 @@ export async function createMcpServer(
logger(`${tool.name} request: ${JSON.stringify(params, null, ' ')}`);
const context = await getContext();
logger(`${tool.name} context: resolved`);
await context.detectOpenDevToolsWindows();
const page =
serverArgs.experimentalPageIdRouting &&
params.pageId &&
!serverArgs.slim
? context.getPageById(params.pageId)
: context.getSelectedMcpPage();
// If there is a dialog open, we will skip devtools detection
if (!page.getDialog()) {
await context.detectOpenDevToolsWindows();
}
const response = serverArgs.slim
? new SlimMcpResponse(serverArgs)
: new McpResponse(serverArgs);

response.setRedactNetworkHeaders(serverArgs.redactNetworkHeaders);
if ('pageScoped' in tool && tool.pageScoped) {
const page =
serverArgs.experimentalPageIdRouting &&
params.pageId &&
!serverArgs.slim
? context.getPageById(params.pageId)
: context.getSelectedMcpPage();
response.setPage(page);
await tool.handler(
{
Expand Down
12 changes: 12 additions & 0 deletions src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ export function definePageTool<
return {
...definition,
pageScoped: true,
handler: async (request, response, context) => {
try {
await definition.handler(request, response, context);
} catch (error) {
const dialog = request.page.getDialog();
if (dialog) {
response.appendResponseLine(`[Blocked] Action triggered a dialog.`);
return;
}
throw error;
}
},
} as DefinedPageTool<Schema>;
}

Expand Down
Loading