File tree Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ import {
1313 SerializedAXNode ,
1414 PredefinedNetworkConditions ,
1515} from 'puppeteer-core' ;
16- import { Context } from './tools/ToolDefinition.js' ;
16+ import { CLOSE_PAGE_ERROR , Context } from './tools/ToolDefinition.js' ;
1717import { Debugger } from 'debug' ;
1818import { NetworkCollector , PageCollector } from './PageCollector.js' ;
1919import fs from 'node:fs/promises' ;
@@ -133,9 +133,7 @@ export class McpContext implements Context {
133133 }
134134 async closePage ( pageIdx : number ) : Promise < void > {
135135 if ( this . #pages. length === 1 ) {
136- throw new Error (
137- 'Unable to close the last page in the browser. It is fine to keep the last page open.' ,
138- ) ;
136+ throw new Error ( CLOSE_PAGE_ERROR ) ;
139137 }
140138 const page = this . getPageByIdx ( pageIdx ) ;
141139 this . setSelectedPageIdx ( 0 ) ;
Original file line number Diff line number Diff line change @@ -79,3 +79,6 @@ export function defineTool<Schema extends Zod.ZodRawShape>(
7979) {
8080 return definition ;
8181}
82+
83+ export const CLOSE_PAGE_ERROR =
84+ 'The last open page cannot be closed. It is fine to keep it open.' ;
Original file line number Diff line number Diff line change 55 */
66
77import z from 'zod' ;
8- import { defineTool } from './ToolDefinition.js' ;
8+ import { CLOSE_PAGE_ERROR , defineTool } from './ToolDefinition.js' ;
99import { ToolCategories } from './categories.js' ;
1010
1111export const listPages = defineTool ( {
@@ -58,7 +58,15 @@ export const closePage = defineTool({
5858 ) ,
5959 } ,
6060 handler : async ( request , response , context ) => {
61- await context . closePage ( request . params . pageIdx ) ;
61+ try {
62+ await context . closePage ( request . params . pageIdx ) ;
63+ } catch ( err ) {
64+ if ( err . message === CLOSE_PAGE_ERROR ) {
65+ response . appendResponseLine ( err . message ) ;
66+ } else {
67+ throw err ;
68+ }
69+ }
6270 response . setIncludePages ( true ) ;
6371 } ,
6472} ) ;
Original file line number Diff line number Diff line change @@ -56,15 +56,12 @@ describe('pages', () => {
5656 it ( 'cannot close the last page' , async ( ) => {
5757 await withBrowser ( async ( response , context ) => {
5858 const page = context . getSelectedPage ( ) ;
59- try {
60- await closePage . handler ( { params : { pageIdx : 0 } } , response , context ) ;
61- assert . fail ( 'not reached' ) ;
62- } catch ( err ) {
63- assert . strictEqual (
64- err . message ,
65- 'Unable to close the last page in the browser. It is fine to keep the last page open.' ,
66- ) ;
67- }
59+ await closePage . handler ( { params : { pageIdx : 0 } } , response , context ) ;
60+ assert . deepStrictEqual (
61+ response . responseLines [ 0 ] ,
62+ `The last open page cannot be closed. It is fine to keep it open.` ,
63+ ) ;
64+ assert . ok ( response . includePages ) ;
6865 assert . ok ( ! page . isClosed ( ) ) ;
6966 } ) ;
7067 } ) ;
You can’t perform that action at this time.
0 commit comments