Skip to content

Commit d756888

Browse files
domdomeggOrKoN
andauthored
feat: add background parameter to new_page tool (#837)
## Summary - Add `background` boolean parameter to the `new_page` tool - When set to `true`, the new page opens in the background without bringing it to the front - Uses Puppeteer's existing `background` option for `browser.newPage()` Fixes #826 --------- Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com> Co-authored-by: Alex Rudenko <alexrudenko@chromium.org>
1 parent 849c590 commit d756888

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

docs/tool-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
**Parameters:**
171171

172172
- **url** (string) **(required)**: URL to load in a new page.
173+
- **background** (boolean) _(optional)_: Whether to open the page in the background without bringing it to the front. Default is false (foreground).
173174
- **timeout** (integer) _(optional)_: Maximum wait time in milliseconds. If set to 0, the default timeout will be used.
174175

175176
---

src/McpContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ export class McpContext implements Context {
265265
return this.#consoleCollector.getById(this.getSelectedPage(), id);
266266
}
267267

268-
async newPage(): Promise<Page> {
269-
const page = await this.browser.newPage();
268+
async newPage(background?: boolean): Promise<Page> {
269+
const page = await this.browser.newPage({background});
270270
await this.createPagesSnapshot();
271271
this.selectPage(page);
272272
this.#networkCollector.addPage(page);

src/tools/ToolDefinition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export type Context = Readonly<{
110110
getPageById(pageId: number): Page;
111111
getPageId(page: Page): number | undefined;
112112
isPageSelected(page: Page): boolean;
113-
newPage(): Promise<Page>;
113+
newPage(background?: boolean): Promise<Page>;
114114
closePage(pageId: number): Promise<void>;
115115
selectPage(page: Page): void;
116116
getElementByUid(uid: string): Promise<ElementHandle<Element>>;

src/tools/pages.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ export const newPage = defineTool({
8787
},
8888
schema: {
8989
url: zod.string().describe('URL to load in a new page.'),
90+
background: zod
91+
.boolean()
92+
.optional()
93+
.describe(
94+
'Whether to open the page in the background without bringing it to the front. Default is false (foreground).',
95+
),
9096
...timeoutSchema,
9197
},
9298
handler: async (request, response, context) => {
93-
const page = await context.newPage();
99+
const page = await context.newPage(request.params.background);
94100

95101
await context.waitForEventsAfterAction(async () => {
96102
await page.goto(request.params.url, {

tests/tools/pages.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ describe('pages', () => {
4343
assert.ok(response.includePages);
4444
});
4545
});
46+
it('create a page in the background', async () => {
47+
await withMcpContext(async (response, context) => {
48+
const originalPage = context.getPageById(1);
49+
assert.strictEqual(originalPage, context.getSelectedPage());
50+
// Ensure original page has focus
51+
await originalPage.bringToFront();
52+
assert.strictEqual(
53+
await originalPage.evaluate(() => document.hasFocus()),
54+
true,
55+
);
56+
await newPage.handler(
57+
{params: {url: 'about:blank', background: true}},
58+
response,
59+
context,
60+
);
61+
// New page should be selected but original should retain focus
62+
assert.strictEqual(context.getPageById(2), context.getSelectedPage());
63+
assert.strictEqual(
64+
await originalPage.evaluate(() => document.hasFocus()),
65+
true,
66+
);
67+
assert.ok(response.includePages);
68+
});
69+
});
4670
});
4771
describe('close_page', () => {
4872
it('closes a page', async () => {

0 commit comments

Comments
 (0)