@@ -10,6 +10,7 @@ import {zod} from '../third_party/index.js';
1010import type { ElementHandle , KeyInput } from '../third_party/index.js' ;
1111import type { TextSnapshotNode } from '../types.js' ;
1212import { parseKey } from '../utils/keyboard.js' ;
13+ import { appendNavigatedToUrl } from '../WaitForHelper.js' ;
1314
1415import { ToolCategory } from './categories.js' ;
1516import type { ContextPage } from './ToolDefinition.js' ;
@@ -109,7 +110,7 @@ export const click = definePageTool({
109110 const shouldSelectNativeOption =
110111 ! request . params . dblClick && aXNode ?. role === 'option' ;
111112 try {
112- await request . page . waitForEventsAfterAction ( async ( ) => {
113+ const result = await request . page . waitForEventsAfterAction ( async ( ) => {
113114 if (
114115 shouldSelectNativeOption &&
115116 ( await selectNativeSelectOption ( handle ) )
@@ -126,6 +127,7 @@ export const click = definePageTool({
126127 ? `Successfully double clicked on the element`
127128 : `Successfully clicked on the element` ,
128129 ) ;
130+ appendNavigatedToUrl ( response , result ) ;
129131 if ( request . params . includeSnapshot ) {
130132 response . includeSnapshot ( ) ;
131133 }
@@ -154,7 +156,7 @@ export const clickAt = definePageTool({
154156 blockedByDialog : true ,
155157 handler : async ( request , response ) => {
156158 const page = request . page ;
157- await page . waitForEventsAfterAction ( async ( ) => {
159+ const result = await page . waitForEventsAfterAction ( async ( ) => {
158160 await page . pptrPage . mouse . click ( request . params . x , request . params . y , {
159161 clickCount : request . params . dblClick ? 2 : 1 ,
160162 } ) ;
@@ -164,6 +166,7 @@ export const clickAt = definePageTool({
164166 ? `Successfully double clicked at the coordinates`
165167 : `Successfully clicked at the coordinates` ,
166168 ) ;
169+ appendNavigatedToUrl ( response , result ) ;
167170 if ( request . params . includeSnapshot ) {
168171 response . includeSnapshot ( ) ;
169172 }
@@ -190,10 +193,11 @@ export const hover = definePageTool({
190193 const uid = request . params . uid ;
191194 const handle = await request . page . getElementByUid ( uid ) ;
192195 try {
193- await request . page . waitForEventsAfterAction ( async ( ) => {
196+ const result = await request . page . waitForEventsAfterAction ( async ( ) => {
194197 await handle . asLocator ( ) . hover ( ) ;
195198 } ) ;
196199 response . appendResponseLine ( `Successfully hovered over the element` ) ;
200+ appendNavigatedToUrl ( response , result ) ;
197201 if ( request . params . includeSnapshot ) {
198202 response . includeSnapshot ( ) ;
199203 }
@@ -292,7 +296,7 @@ export const fill = definePageTool({
292296 blockedByDialog : true ,
293297 handler : async ( request , response , context ) => {
294298 const page = request . page ;
295- await page . waitForEventsAfterAction ( async ( ) => {
299+ const result = await page . waitForEventsAfterAction ( async ( ) => {
296300 await fillFormElement (
297301 request . params . uid ,
298302 request . params . value ,
@@ -301,6 +305,7 @@ export const fill = definePageTool({
301305 ) ;
302306 } ) ;
303307 response . appendResponseLine ( `Successfully filled out the element` ) ;
308+ appendNavigatedToUrl ( response , result ) ;
304309 if ( request . params . includeSnapshot ) {
305310 response . includeSnapshot ( ) ;
306311 }
@@ -321,7 +326,7 @@ export const typeText = definePageTool({
321326 blockedByDialog : true ,
322327 handler : async ( request , response ) => {
323328 const page = request . page ;
324- await page . waitForEventsAfterAction ( async ( ) => {
329+ const result = await page . waitForEventsAfterAction ( async ( ) => {
325330 await page . pptrPage . keyboard . type ( request . params . text ) ;
326331 if ( request . params . submitKey ) {
327332 await page . pptrPage . keyboard . press (
@@ -332,6 +337,7 @@ export const typeText = definePageTool({
332337 response . appendResponseLine (
333338 `Typed text "${ request . params . text } ${ request . params . submitKey ? ` + ${ request . params . submitKey } ` : '' } "` ,
334339 ) ;
340+ appendNavigatedToUrl ( response , result ) ;
335341 } ,
336342} ) ;
337343
@@ -354,12 +360,13 @@ export const drag = definePageTool({
354360 ) ;
355361 const toHandle = await request . page . getElementByUid ( request . params . to_uid ) ;
356362 try {
357- await request . page . waitForEventsAfterAction ( async ( ) => {
363+ const result = await request . page . waitForEventsAfterAction ( async ( ) => {
358364 await fromHandle . drag ( toHandle ) ;
359365 await new Promise ( resolve => setTimeout ( resolve , 50 ) ) ;
360366 await toHandle . drop ( fromHandle ) ;
361367 } ) ;
362368 response . appendResponseLine ( `Successfully dragged an element` ) ;
369+ appendNavigatedToUrl ( response , result ) ;
363370 if ( request . params . includeSnapshot ) {
364371 response . includeSnapshot ( ) ;
365372 }
@@ -392,17 +399,22 @@ export const fillForm = definePageTool({
392399 blockedByDialog : true ,
393400 handler : async ( request , response , context ) => {
394401 const page = request . page ;
402+ let lastResult : { navigatedToUrl ?: string } = { } ;
395403 for ( const element of request . params . elements ) {
396- await page . waitForEventsAfterAction ( async ( ) => {
404+ const result = await page . waitForEventsAfterAction ( async ( ) => {
397405 await fillFormElement (
398406 element . uid ,
399407 element . value ,
400408 context as McpContext ,
401409 page ,
402410 ) ;
403411 } ) ;
412+ if ( result . navigatedToUrl ) {
413+ lastResult = result ;
414+ }
404415 }
405416 response . appendResponseLine ( `Successfully filled out the form` ) ;
417+ appendNavigatedToUrl ( response , lastResult ) ;
406418 if ( request . params . includeSnapshot ) {
407419 response . includeSnapshot ( ) ;
408420 }
@@ -482,7 +494,7 @@ export const pressKey = definePageTool({
482494 const tokens = parseKey ( request . params . key ) ;
483495 const [ key , ...modifiers ] = tokens ;
484496
485- await page . waitForEventsAfterAction ( async ( ) => {
497+ const result = await page . waitForEventsAfterAction ( async ( ) => {
486498 for ( const modifier of modifiers ) {
487499 await page . pptrPage . keyboard . down ( modifier ) ;
488500 }
@@ -495,6 +507,7 @@ export const pressKey = definePageTool({
495507 response . appendResponseLine (
496508 `Successfully pressed key: ${ request . params . key } ` ,
497509 ) ;
510+ appendNavigatedToUrl ( response , result ) ;
498511 if ( request . params . includeSnapshot ) {
499512 response . includeSnapshot ( ) ;
500513 }
0 commit comments