@@ -37,7 +37,7 @@ import {PredefinedNetworkConditions} from './third_party/index.js';
3737import { listPages } from './tools/pages.js' ;
3838import { takeSnapshot } from './tools/snapshot.js' ;
3939import { CLOSE_PAGE_ERROR } from './tools/ToolDefinition.js' ;
40- import type { Context , DevToolsData } from './tools/ToolDefinition.js' ;
40+ import type { Context , DevToolsData , ToolPage } from './tools/ToolDefinition.js' ;
4141import type { TraceResult } from './trace-processing/parse.js' ;
4242import type {
4343 EmulationSettings ,
@@ -117,7 +117,7 @@ export class McpContext implements Context {
117117 null ;
118118 #focusedPagePerContext = new Map < BrowserContext , Page > ( ) ;
119119
120- #requestPage?: Page ;
120+ #requestPage?: ToolPage ;
121121
122122 #nextPageId = 1 ;
123123
@@ -196,12 +196,12 @@ export class McpContext implements Context {
196196 // TODO: Refactor away mutable request state (e.g. per-request facade,
197197 // per-request context object, or another approach). Once resolved, the
198198 // global toolMutex could become per-BrowserContext for parallel execution.
199- setRequestPage ( page ?: Page ) : void {
199+ setRequestPage ( page ?: ToolPage ) : void {
200200 this . #requestPage = page ;
201201 }
202202
203203 #resolveTargetPage( ) : Page {
204- return this . #requestPage ?? this . getSelectedPage ( ) ;
204+ return this . #requestPage?. page ?? this . getSelectedPage ( ) ;
205205 }
206206
207207 resolveCdpRequestId ( cdpRequestId : string ) : number | undefined {
@@ -283,7 +283,7 @@ export class McpContext implements Context {
283283 async newPage (
284284 background ?: boolean ,
285285 isolatedContextName ?: string ,
286- ) : Promise < Page > {
286+ ) : Promise < ToolPage > {
287287 let page : Page ;
288288 if ( isolatedContextName !== undefined ) {
289289 let ctx = this . #isolatedContexts. get ( isolatedContextName ) ;
@@ -299,7 +299,7 @@ export class McpContext implements Context {
299299 this . selectPage ( page ) ;
300300 this . #networkCollector. addPage ( page ) ;
301301 this . #consoleCollector. addPage ( page ) ;
302- return page ;
302+ return this . #getMcpPage ( page ) ;
303303 }
304304 async closePage ( pageId : number ) : Promise < void > {
305305 if ( this . #pages. length === 1 ) {
@@ -489,7 +489,7 @@ export class McpContext implements Context {
489489 }
490490
491491 getDialog ( page ?: Page ) : Dialog | undefined {
492- const targetPage = page ?? this . #requestPage ?? this . #selectedPage;
492+ const targetPage = page ?? this . #requestPage?. page ?? this . #selectedPage;
493493 if ( ! targetPage ) {
494494 return undefined ;
495495 }
@@ -516,11 +516,17 @@ export class McpContext implements Context {
516516 return page ;
517517 }
518518
519- resolvePageById ( pageId ?: number ) : Page {
519+ getSelectedMcpPage ( ) : McpPage {
520+ const page = this . getSelectedPage ( ) ;
521+ return this . #getMcpPage( page ) ;
522+ }
523+
524+ resolvePageById ( pageId ?: number ) : McpPage {
520525 if ( pageId === undefined ) {
521- return this . getSelectedPage ( ) ;
526+ return this . getSelectedMcpPage ( ) ;
522527 }
523- return this . getPageById ( pageId ) ;
528+ const page = this . getPageById ( pageId ) ;
529+ return this . #getMcpPage( page ) ;
524530 }
525531
526532 getPageById ( pageId : number ) : Page {
@@ -551,7 +557,8 @@ export class McpContext implements Context {
551557 return this . #selectedPage === page ;
552558 }
553559
554- assertPageIsFocused ( page : Page ) : void {
560+ assertPageIsFocused ( pageToCheck : Page | ToolPage ) : void {
561+ const page = 'page' in pageToCheck ? pageToCheck . page : pageToCheck ;
555562 const ctx = page . browserContext ( ) ;
556563 const focused = this . #focusedPagePerContext. get ( ctx ) ;
557564 if ( focused && focused !== page ) {
@@ -564,7 +571,8 @@ export class McpContext implements Context {
564571 }
565572 }
566573
567- selectPage ( newPage : Page ) : void {
574+ selectPage ( pageToSelect : Page | ToolPage ) : void {
575+ const newPage = 'page' in pageToSelect ? pageToSelect . page : pageToSelect ;
568576 const ctx = newPage . browserContext ( ) ;
569577 const oldFocused = this . #focusedPagePerContext. get ( ctx ) ;
570578 if ( oldFocused && oldFocused !== newPage && ! oldFocused . isClosed ( ) ) {
0 commit comments