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
2 changes: 1 addition & 1 deletion docs/tool-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@

### `close_page`

**Description:** Closes the page by its index.
**Description:** Closes the page by its index. The last open page cannot be closed.

**Parameters:**

Expand Down
10 changes: 10 additions & 0 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ export class McpContext implements Context {
this.#consoleCollector.addPage(page);
return page;
}
async closePage(pageIdx: number): Promise<void> {
if (this.#pages.length === 1) {
throw new Error(
'Unable to close the last page in the browser. It is fine to keep the last page open.',
);
}
const page = this.getPageByIdx(pageIdx);
this.setSelectedPageIdx(0);
await page.close({runBeforeUnload: false});
}

getNetworkRequestByUrl(url: string): HTTPRequest {
const requests = this.getNetworkRequests();
Expand Down
1 change: 1 addition & 0 deletions src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type Context = Readonly<{
clearDialog(): void;
getPageByIdx(idx: number): Page;
newPage(): Promise<Page>;
closePage(pageIdx: number): Promise<void>;
setSelectedPageIdx(idx: number): void;
getElementByUid(uid: string): Promise<ElementHandle<Element>>;
setNetworkConditions(conditions: string | null): void;
Expand Down
6 changes: 2 additions & 4 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const selectPage = defineTool({

export const closePage = defineTool({
name: 'close_page',
description: `Closes the page by its index.`,
description: `Closes the page by its index. The last open page cannot be closed.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: false,
Expand All @@ -58,9 +58,7 @@ export const closePage = defineTool({
),
},
handler: async (request, response, context) => {
const page = context.getPageByIdx(request.params.pageIdx);
context.setSelectedPageIdx(0);
await page.close({runBeforeUnload: false});
await context.closePage(request.params.pageIdx);
response.setIncludePages(true);
},
});
Expand Down
15 changes: 15 additions & 0 deletions tests/tools/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ describe('pages', () => {
assert.ok(response.includePages);
});
});
it('cannot close the last page', async () => {
await withBrowser(async (response, context) => {
const page = context.getSelectedPage();
try {
await closePage.handler({params: {pageIdx: 0}}, response, context);
assert.fail('not reached');
} catch (err) {
assert.strictEqual(
err.message,
'Unable to close the last page in the browser. It is fine to keep the last page open.',
);
}
assert.ok(!page.isClosed());
});
});
});
describe('browser_select_page', () => {
it('selects a page', async () => {
Expand Down
Loading