@@ -3,10 +3,10 @@ import type {
33 TextContent ,
44 ImageContent ,
55} from "@modelcontextprotocol/sdk/types.js" ;
6+ import type { Locator , PageScreenshotOptions } from "playwright-core" ;
67
78import { defineTool , type ToolResult , } from "./tool.js" ;
89import type { Context , ToolActionResult } from "../context.js" ;
9- import type { Page , Locator } from "playwright-core" ;
1010import { PageSnapshot } from "../pageSnapshot.js" ;
1111import { outputFile } from "../config.js" ;
1212
@@ -358,27 +358,23 @@ const selectOption = defineTool<typeof selectOptionSchema>({
358358} ) ;
359359
360360// --- 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+
382378type ScreenshotInput = z . infer < typeof screenshotSchema > ;
383379
384380const screenshot = defineTool < typeof screenshotSchema > ( {
@@ -392,6 +388,10 @@ const screenshot = defineTool<typeof screenshotSchema>({
392388 context : Context ,
393389 params : ScreenshotInput
394390 ) : Promise < ToolResult > => {
391+ if ( ! ! params . element !== ! ! params . ref ) {
392+ throw new Error ( "Both element and ref must be provided or neither." ) ;
393+ }
394+
395395 const page = await context . getActivePage ( ) ;
396396 if ( ! page ) {
397397 throw new Error ( "No active page found for screenshot" ) ;
@@ -407,15 +407,12 @@ const screenshot = defineTool<typeof screenshotSchema>({
407407 `screenshot-${ Date . now ( ) } .${ fileType } `
408408 ) ;
409409
410- const baseOptions : Omit <
411- Parameters < Page [ "screenshot" ] > [ 0 ] ,
412- "type" | "quality" | "path"
413- > = {
410+ const baseOptions : PageScreenshotOptions = {
414411 scale : "css" ,
415412 timeout : 15000 , // Kept existing timeout
416413 } ;
417414
418- let options : Parameters < Page [ "screenshot" ] > [ 0 ] ;
415+ let options : PageScreenshotOptions ;
419416
420417 if ( fileType === "jpeg" ) {
421418 options = {
@@ -499,4 +496,4 @@ export async function generateLocator(locator: Locator): Promise<string> {
499496 return ( locator as any ) . _generateLocatorString ( ) ;
500497}
501498
502- export default [ snapshot , click , drag , hover , type , selectOption , screenshot ] ;
499+ export default [ snapshot , click , drag , hover , type , selectOption , screenshot ] ;
0 commit comments