Chrome DevTools for coding agents
fix: resolve page ids only among listed pages (#2332)
Thomas Bachemeb049513d272
diff --git a/src/McpContext.ts b/src/McpContext.tsindex 8ff0e7ba..efb8df1b 100644--- a/src/McpContext.ts+++ b/src/McpContext.ts@@ -509,9 +509,11 @@ export class McpContext implements Context {}getPageById(pageId: number): McpPage {- const page = Array.from(this.#mcpPages.values()).find(- mcpPage => mcpPage.id === pageId,- );+ // Resolve only among listed pages (`getPages()`), so an id that+ // `list_pages` never showed cannot be targeted (e.g. a `devtools://`+ // frontend, which the listing excludes unless `experimentalDevToolsDebugging`+ // is set).+ const page = this.getPages().find(mcpPage => mcpPage.id === pageId);if (!page) {throw new Error('No page found');}@@ -640,8 +642,7 @@ export class McpContext implements Context {// Only fall back when the selected page is actually gone. Gating on// `isClosed()` instead of `pages` membership avoids silently swapping a- // live page that is momentarily missing from the snapshot, e.g., a- // `devtools://` page, which is selectable but filtered out of `pages` above.+ // live page that is momentarily missing from the snapshot.this.#selectedPageFallback = undefined;if ((!this.#selectedPage || this.#selectedPage.pptrPage.isClosed()) &&diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.tsindex 365b3ddb..f2a965a5 100644--- a/tests/McpContext.test.ts+++ b/tests/McpContext.test.ts@@ -96,6 +96,26 @@ describe('McpContext', () => {const page = await context.newPage();await context.createPagesSnapshot();assert.ok(page.devToolsPage);++ // A devtools page is tracked but excluded from the listing, so its id+ // must not resolve through `getPageById()` (which backs `select_page`+ // and every other pageId tool). A second snapshot guarantees the+ // devtools page is enrolled.+ await context.createPagesSnapshot();+ const listed = context.getPages();+ assert.ok(+ listed.every(+ mcpPage => !mcpPage.pptrPage.url().startsWith('devtools://'),+ ),+ 'listing should exclude devtools pages',+ );+ const listedIds = new Set(listed.map(mcpPage => mcpPage.id));+ for (let id = 1; id < 30; id++) {+ if (listedIds.has(id)) {+ continue;+ }+ assert.throws(() => context.getPageById(id), /No page found/);+ }},{autoOpenDevTools: true,