@@ -24,14 +24,17 @@ import type {
2424 TextContent ,
2525} from './third_party/index.js' ;
2626import { handleDialog } from './tools/pages.js' ;
27- import type { ImageContentData , Response } from './tools/ToolDefinition.js' ;
27+ import type {
28+ ImageContentData ,
29+ Response ,
30+ SnapshotParams ,
31+ } from './tools/ToolDefinition.js' ;
2832import { paginate } from './utils/pagination.js' ;
2933import type { PaginationOptions } from './utils/types.js' ;
3034
3135export class McpResponse implements Response {
3236 #includePages = false ;
33- #includeSnapshot = false ;
34- #includeVerboseSnapshot = false ;
37+ #snapshotParams?: SnapshotParams ;
3538 #attachedNetworkRequestId?: number ;
3639 #attachedConsoleMessageId?: number ;
3740 #textResponseLines: string [ ] = [ ] ;
@@ -53,9 +56,8 @@ export class McpResponse implements Response {
5356 this . #includePages = value ;
5457 }
5558
56- setIncludeSnapshot ( value : boolean , verbose = false ) : void {
57- this . #includeSnapshot = value ;
58- this . #includeVerboseSnapshot = verbose ;
59+ includeSnapshot ( params ?: SnapshotParams ) : void {
60+ this . #snapshotParams = params ;
5961 }
6062
6163 setIncludeNetworkRequests (
@@ -158,12 +160,8 @@ export class McpResponse implements Response {
158160 return this . #images;
159161 }
160162
161- get includeSnapshot ( ) : boolean {
162- return this . #includeSnapshot;
163- }
164-
165- get includeVersboseSnapshot ( ) : boolean {
166- return this . #includeVerboseSnapshot;
163+ get snapshotParams ( ) : SnapshotParams | undefined {
164+ return this . #snapshotParams;
167165 }
168166
169167 async handle (
@@ -173,8 +171,22 @@ export class McpResponse implements Response {
173171 if ( this . #includePages) {
174172 await context . createPagesSnapshot ( ) ;
175173 }
176- if ( this . #includeSnapshot) {
177- await context . createTextSnapshot ( this . #includeVerboseSnapshot) ;
174+
175+ let formattedSnapshot : string | undefined ;
176+ if ( this . #snapshotParams) {
177+ await context . createTextSnapshot ( this . #snapshotParams. verbose ) ;
178+ const snapshot = context . getTextSnapshot ( ) ;
179+ if ( snapshot ) {
180+ if ( this . #snapshotParams. filePath ) {
181+ await context . saveFile (
182+ new TextEncoder ( ) . encode ( formatA11ySnapshot ( snapshot . root ) ) ,
183+ this . #snapshotParams. filePath ,
184+ ) ;
185+ formattedSnapshot = `Saved screenshot to ${ this . #snapshotParams. filePath } .` ;
186+ } else {
187+ formattedSnapshot = formatA11ySnapshot ( snapshot . root ) ;
188+ }
189+ }
178190 }
179191
180192 const bodies : {
@@ -281,6 +293,7 @@ export class McpResponse implements Response {
281293 bodies,
282294 consoleData,
283295 consoleListData,
296+ formattedSnapshot,
284297 } ) ;
285298 }
286299
@@ -294,6 +307,7 @@ export class McpResponse implements Response {
294307 } ;
295308 consoleData : ConsoleMessageData | undefined ;
296309 consoleListData : ConsoleMessageData [ ] | undefined ;
310+ formattedSnapshot : string | undefined ;
297311 } ,
298312 ) : Array < TextContent | ImageContent > {
299313 const response = [ `# ${ toolName } response` ] ;
@@ -339,13 +353,9 @@ Call ${handleDialog.name} to handle it before continuing.`);
339353 response . push ( ...parts ) ;
340354 }
341355
342- if ( this . #includeSnapshot) {
343- const snapshot = context . getTextSnapshot ( ) ;
344- if ( snapshot ) {
345- const formattedSnapshot = formatA11ySnapshot ( snapshot . root ) ;
346- response . push ( '## Page content' ) ;
347- response . push ( formattedSnapshot ) ;
348- }
356+ if ( data . formattedSnapshot ) {
357+ response . push ( '## Page content' ) ;
358+ response . push ( data . formattedSnapshot ) ;
349359 }
350360
351361 response . push ( ...this . #formatNetworkRequestData( context , data . bodies ) ) ;
0 commit comments