Skip to content

feat(pages): add select_foreground_page tool to detect and select the visible browser tab#2356

Open
santhreal wants to merge 1 commit into
ChromeDevTools:mainfrom
santhreal:feat/select-foreground-page
Open

feat(pages): add select_foreground_page tool to detect and select the visible browser tab#2356
santhreal wants to merge 1 commit into
ChromeDevTools:mainfrom
santhreal:feat/select-foreground-page

Conversation

@santhreal

Copy link
Copy Markdown

Summary

Adds a new select_foreground_page tool that detects and selects the browser tab currently visible to the user.

Fixes #933

Problem

When connecting to an existing browser with --autoConnect, the MCP-selected page often doesn't match the tab the user is actually looking at. The [selected] tag in list_pages reflects MCP's internal selection (defaults to pages[0]), not the browser's foreground tab. Users have to:

  1. list_pages
  2. Manually identify which page they're looking at
  3. select_page with the correct ID

Solution

The new select_foreground_page tool queries document.visibilityState on every open page via page.evaluate(). If exactly one page reports "visible", it is automatically selected.

Why document.visibilityState?

  • It's a standard web API available on all pages
  • Puppeteer's bringToFront() correctly updates it
  • When a user switches tabs in Chrome, the foreground tab reports "visible" and background tabs report "hidden"
  • No experimental CDP APIs needed

Graceful fallback

If visibility can't be determined (e.g. all pages hidden, multiple visible, or evaluate() fails on chrome:// pages), the tool reports failure and suggests using select_page manually.

Changes

File Change
src/McpContext.ts Add selectForegroundPage() method
src/tools/pages.ts Add select_foreground_page tool definition
src/tools/ToolDefinition.ts Add selectForegroundPage to Context type
src/bin/chrome-devtools-cli-options.ts Add CLI entry
tests/tools/pages.test.ts Add tests for foreground detection and fallback

Example workflow

User: "take a snapshot of the page I'm looking at"
Agent calls: select_foreground_page
→ Selected page 3 (https://docs.example.com) as the foreground tab.
Agent calls: take_snapshot
→ Returns snapshot of the correct page

@google-cla

google-cla Bot commented Jul 13, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@santhreal
santhreal force-pushed the feat/select-foreground-page branch from 1bf9505 to 921194b Compare July 13, 2026 11:00
… visible browser tab (ChromeDevTools#933)

When connecting to an existing browser with --autoConnect, the MCP-selected
page may differ from the tab the user is actually looking at. The new
select_foreground_page tool queries document.visibilityState across all open
pages and selects the one that is currently in the foreground.

This avoids the need for the user to manually list_pages + select_page when
they simply want to work with whatever tab is currently open in the browser.
@santhreal
santhreal force-pushed the feat/select-foreground-page branch from 921194b to 2498c49 Compare July 13, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Get the currently selected browser tab

1 participant