@@ -3,10 +3,10 @@ import type {
3
3
TextContent ,
4
4
ImageContent ,
5
5
} from "@modelcontextprotocol/sdk/types.js" ;
6
+ import type { Locator , PageScreenshotOptions } from "playwright-core" ;
6
7
7
8
import { defineTool , type ToolResult , } from "./tool.js" ;
8
9
import type { Context , ToolActionResult } from "../context.js" ;
9
- import type { Page , Locator } from "playwright-core" ;
10
10
import { PageSnapshot } from "../pageSnapshot.js" ;
11
11
import { outputFile } from "../config.js" ;
12
12
@@ -358,27 +358,23 @@ const selectOption = defineTool<typeof selectOptionSchema>({
358
358
} ) ;
359
359
360
360
// --- Tool: Screenshot (Adapted Handle, Example Action) ---
361
- const screenshotSchema = z
362
- . object ( {
363
- raw : z
364
- . boolean ( )
365
- . optional ( )
366
- . describe (
367
- "Whether to return without compression (PNG). Default is false (JPEG)."
368
- ) ,
369
- element : z
370
- . string ( )
371
- . optional ( )
372
- . describe ( "Human-readable element description." ) ,
373
- ref : z
374
- . string ( )
375
- . optional ( )
376
- . describe ( "Exact target element reference from the page snapshot." ) ,
377
- } )
378
- . refine ( ( data ) => ! ! data . element === ! ! data . ref , {
379
- message : "Both element and ref must be provided or neither." ,
380
- path : [ "ref" , "element" ] ,
381
- } ) ;
361
+ const screenshotSchema = z . object ( {
362
+ raw : z
363
+ . boolean ( )
364
+ . optional ( )
365
+ . describe (
366
+ "Whether to return without compression (PNG). Default is false (JPEG)."
367
+ ) ,
368
+ element : z
369
+ . string ( )
370
+ . optional ( )
371
+ . describe ( "Human-readable element description." ) ,
372
+ ref : z
373
+ . string ( )
374
+ . optional ( )
375
+ . describe ( "Exact target element reference from the page snapshot." )
376
+ } ) ;
377
+
382
378
type ScreenshotInput = z . infer < typeof screenshotSchema > ;
383
379
384
380
const screenshot = defineTool < typeof screenshotSchema > ( {
@@ -392,6 +388,10 @@ const screenshot = defineTool<typeof screenshotSchema>({
392
388
context : Context ,
393
389
params : ScreenshotInput
394
390
) : Promise < ToolResult > => {
391
+ if ( ! ! params . element !== ! ! params . ref ) {
392
+ throw new Error ( "Both element and ref must be provided or neither." ) ;
393
+ }
394
+
395
395
const page = await context . getActivePage ( ) ;
396
396
if ( ! page ) {
397
397
throw new Error ( "No active page found for screenshot" ) ;
@@ -407,15 +407,12 @@ const screenshot = defineTool<typeof screenshotSchema>({
407
407
`screenshot-${ Date . now ( ) } .${ fileType } `
408
408
) ;
409
409
410
- const baseOptions : Omit <
411
- Parameters < Page [ "screenshot" ] > [ 0 ] ,
412
- "type" | "quality" | "path"
413
- > = {
410
+ const baseOptions : PageScreenshotOptions = {
414
411
scale : "css" ,
415
412
timeout : 15000 , // Kept existing timeout
416
413
} ;
417
414
418
- let options : Parameters < Page [ "screenshot" ] > [ 0 ] ;
415
+ let options : PageScreenshotOptions ;
419
416
420
417
if ( fileType === "jpeg" ) {
421
418
options = {
@@ -499,4 +496,4 @@ export async function generateLocator(locator: Locator): Promise<string> {
499
496
return ( locator as any ) . _generateLocatorString ( ) ;
500
497
}
501
498
502
- export default [ snapshot , click , drag , hover , type , selectOption , screenshot ] ;
499
+ export default [ snapshot , click , drag , hover , type , selectOption , screenshot ] ;
0 commit comments