Skip to content

feat: report navigated URL in action responses#1954

Open
masamaru0513 wants to merge 1 commit intoChromeDevTools:mainfrom
masamaru0513:feat/report-navigated-url
Open

feat: report navigated URL in action responses#1954
masamaru0513 wants to merge 1 commit intoChromeDevTools:mainfrom
masamaru0513:feat/report-navigated-url

Conversation

@masamaru0513
Copy link
Copy Markdown
Contributor

@masamaru0513 masamaru0513 commented Apr 24, 2026

Summary

Report the new page URL when an action triggers a navigation, so there is no need to re-query the pages list.

Fixes #243

Note: I am aware of the existing #1853 which addresses the same issue. This PR incorporates the review feedback from #1853 (by @Lightning00Blade) — specifically using navigatedToUrl naming, extracting a shared appendNavigatedToUrl helper in WaitForHelper, and applying it to evaluate_script as well.

Changes

  • McpPage.waitForEventsAfterAction now compares the page URL before and after the action, returning {navigatedToUrl} when a navigation occurred
  • Added appendNavigatedToUrl helper in WaitForHelper.ts for consistent URL reporting across tools
  • All input tools (click, click_at, hover, fill, fill_form, type_text, drag, press_key) use the helper to append Navigated to <URL> when a navigation is detected
  • evaluate_script also reports navigated URLs using the same helper
  • navigate_page (url type) now reports the final URL after redirects instead of the requested URL
  • new_page now reports the navigated URL in the response message

Output example

Before:

Successfully clicked on the element

After:

Successfully clicked on the element
Navigated to https://www.iana.org/help/example-domains

Testing

  • Unit tests added for:
    • Click triggering navigation → reports URL
    • Click without navigation → no "Navigated to" in response
    • navigate_page reports final URL
  • Manual verification:
    • click on a link → reports navigated URL ✅
    • press_key Enter on a focused link → reports navigated URL ✅
    • evaluate_script with window.location.href change → reports navigated URL ✅
    • evaluate_script without navigation → no "Navigated to" ✅
    • navigate_page reports final URL (with trailing slash after redirect) ✅
    • Hash-only change (anchor link) → no "Navigated to" (same-document navigations are excluded by WaitForHelper) ✅
    • navigate_page back/forward → existing behavior preserved ✅

@masamaru0513 masamaru0513 force-pushed the feat/report-navigated-url branch from d58c34c to ccf84a8 Compare April 24, 2026 13:05
Report the new page URL when an action (click, fill, press_key, etc.)
triggers a navigation. The URL is detected by comparing the page URL
before and after waitForEventsAfterAction.

Fixes ChromeDevTools#243
Comment thread src/tools/pages.ts
request.params.timeout,
);

response.appendResponseLine(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_page already returns a list of open pages, no need to add anything

Comment thread src/tools/pages.ts
const finalUrl = page.pptrPage.url();
response.appendResponseLine(
`Successfully navigated to ${request.params.url}.`,
`Successfully navigated to ${finalUrl}.`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For redirects (e.g. from foo to bar), the tool currently returns: "Successfully navigated to foo.

Pages:

1: bar"
I think this is already working as intended as it gives more info than just reporting the final URL twice.

Comment thread src/McpPage.ts
return helper.waitForEventsAfterAction(action, options);
await helper.waitForEventsAfterAction(action, options);
const urlAfter = this.pptrPage.url();
if (urlAfter !== urlBefore) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could be shortened to
return (urlAfter === urlBefore) ? {} : {navigatedToUrl: urlAfter};

Comment thread tests/tools/input.test.ts
);
assert.ok(
response.responseLines[1]?.includes('/nav-target'),
`Expected URL to contain /nav-target but got: ${response.responseLines[1]}`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of comparing the line in 2 (imprecise) parts, could we construct the whole response line (with server.getRoute) and check for equality?

@wolfib wolfib requested a review from Lightning00Blade April 29, 2026 16:49
@wolfib
Copy link
Copy Markdown
Contributor

wolfib commented Apr 29, 2026

Thanks! This mostly looks good to me, added a few comments.
@Lightning00Blade PTAL since you reviewed the previous PR

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.

Actions that result in a navigation should report the new page URL in the response

2 participants