Skip to content
Merged
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
50 changes: 5 additions & 45 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ import {Locator} from './third_party/index.js';
import {PredefinedNetworkConditions} from './third_party/index.js';
import {listPages} from './tools/pages.js';
import {CLOSE_PAGE_ERROR} from './tools/ToolDefinition.js';
import type {
Context,
DevToolsData,
ContextPage,
} from './tools/ToolDefinition.js';
import type {Context, DevToolsData} from './tools/ToolDefinition.js';
import type {TraceResult} from './trace-processing/parse.js';
import type {
EmulationSettings,
Expand Down Expand Up @@ -116,7 +112,6 @@ export class McpContext implements Context {
#isRunningTrace = false;
#screenRecorderData: {recorder: ScreenRecorder; filePath: string} | null =
null;
#focusedPagePerContext = new Map<BrowserContext, Page>();

#nextPageId = 1;

Expand Down Expand Up @@ -302,10 +297,6 @@ export class McpContext implements Context {
page.dispose();
this.#mcpPages.delete(page.pptrPage);
}
const ctx = page.pptrPage.browserContext();
if (this.#focusedPagePerContext.get(ctx) === page.pptrPage) {
this.#focusedPagePerContext.delete(ctx);
}
await page.pptrPage.close({runBeforeUnload: false});
}

Expand Down Expand Up @@ -499,38 +490,9 @@ export class McpContext implements Context {
return this.#selectedPage?.pptrPage === page;
}

assertPageIsFocused(pageToCheck: Page | ContextPage): void {
const page = 'pptrPage' in pageToCheck ? pageToCheck.pptrPage : pageToCheck;
const ctx = page.browserContext();
const focused = this.#focusedPagePerContext.get(ctx);
if (focused && focused !== page) {
const targetId = this.#mcpPages.get(page)?.id ?? '?';
const focusedId = this.#mcpPages.get(focused)?.id ?? '?';
throw new Error(
`Page ${targetId} is not the active page in its browser context (page ${focusedId} is). ` +
`Call select_page with pageId ${targetId} first.`,
);
}
}

selectPage(newPage: McpPage): void {
const ctx = newPage.pptrPage.browserContext();
const oldFocused = this.#focusedPagePerContext.get(ctx);
if (
oldFocused &&
oldFocused !== newPage.pptrPage &&
!oldFocused.isClosed()
) {
void oldFocused.emulateFocusedPage(false).catch(error => {
this.logger('Error turning off focused page emulation', error);
});
}
this.#focusedPagePerContext.set(ctx, newPage.pptrPage);
this.#selectedPage = newPage;
this.#updateSelectedPageTimeouts();
void newPage.pptrPage.emulateFocusedPage(true).catch(error => {
this.logger('Error turning on focused page emulation', error);
});
}

#updateSelectedPageTimeouts() {
Expand Down Expand Up @@ -606,6 +568,10 @@ export class McpContext implements Context {
if (!mcpPage) {
mcpPage = new McpPage(page, this.#nextPageId++);
this.#mcpPages.set(page, mcpPage);
// We emulate a focused page for all pages to support multi-agent workflows.
void page.emulateFocusedPage(true).catch(error => {
this.logger('Error turning on focused page emulation', error);
});
}
mcpPage.isolatedContextName = isolatedContextNames.get(page);
}
Expand All @@ -618,12 +584,6 @@ export class McpContext implements Context {
this.#mcpPages.delete(page);
}
}
// Prune stale #focusedPagePerContext entries.
for (const [ctx, page] of this.#focusedPagePerContext) {
if (!currentPages.has(page)) {
this.#focusedPagePerContext.delete(ctx);
}
}

this.#pages = allPages.filter(page => {
return (
Expand Down
1 change: 0 additions & 1 deletion src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export type Context = Readonly<{
): Promise<ContextPage>;
closePage(pageId: number): Promise<void>;
selectPage(page: ContextPage): void;
assertPageIsFocused(page: Page): void;
restoreEmulation(page: ContextPage): Promise<void>;
emulate(
options: {
Expand Down
3 changes: 0 additions & 3 deletions src/tools/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export const clickAt = definePageTool({
},
handler: async (request, response, context) => {
const page = request.page;
context.assertPageIsFocused(page.pptrPage);
await context.waitForEventsAfterAction(async () => {
await page.pptrPage.mouse.click(request.params.x, request.params.y, {
clickCount: request.params.dblClick ? 2 : 1,
Expand Down Expand Up @@ -263,7 +262,6 @@ export const typeText = definePageTool({
},
handler: async (request, response, context) => {
const page = request.page;
context.assertPageIsFocused(page.pptrPage);
await context.waitForEventsAfterAction(async () => {
await page.pptrPage.keyboard.type(request.params.text);
if (request.params.submitKey) {
Expand Down Expand Up @@ -416,7 +414,6 @@ export const pressKey = definePageTool({
},
handler: async (request, response, context) => {
const page = request.page;
context.assertPageIsFocused(page.pptrPage);
const tokens = parseKey(request.params.key);
const [key, ...modifiers] = tokens;

Expand Down
Loading