44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ import { logger } from '../logger.js' ;
78import type { McpContext , TextSnapshotNode } from '../McpContext.js' ;
89import { zod } from '../third_party/index.js' ;
910import type { ElementHandle } from '../third_party/index.js' ;
@@ -22,6 +23,16 @@ const includeSnapshotSchema = zod
2223 . optional ( )
2324 . describe ( 'Whether to include a snapshot in the response. Default is false.' ) ;
2425
26+ function handleActionError ( error : unknown , uid : string ) {
27+ logger ( 'failed to act using a locator' , error ) ;
28+ throw new Error (
29+ `Failed to interact with the element with uid ${ uid } . The element did not become interactive within the configured timeout.` ,
30+ {
31+ cause : error ,
32+ } ,
33+ ) ;
34+ }
35+
2536export const click = defineTool ( {
2637 name : 'click' ,
2738 description : `Clicks on the provided element` ,
@@ -55,6 +66,8 @@ export const click = defineTool({
5566 if ( request . params . includeSnapshot ) {
5667 response . includeSnapshot ( ) ;
5768 }
69+ } catch ( error ) {
70+ handleActionError ( error , uid ) ;
5871 } finally {
5972 void handle . dispose ( ) ;
6073 }
@@ -119,6 +132,8 @@ export const hover = defineTool({
119132 if ( request . params . includeSnapshot ) {
120133 response . includeSnapshot ( ) ;
121134 }
135+ } catch ( error ) {
136+ handleActionError ( error , uid ) ;
122137 } finally {
123138 void handle . dispose ( ) ;
124139 }
@@ -180,6 +195,8 @@ async function fillFormElement(
180195 value . length * timeoutPerChar ;
181196 await handle . asLocator ( ) . setTimeout ( fillTimeout ) . fill ( value ) ;
182197 }
198+ } catch ( error ) {
199+ handleActionError ( error , uid ) ;
183200 } finally {
184201 void handle . dispose ( ) ;
185202 }
0 commit comments