@@ -17,7 +17,7 @@ import {
1717 getShortDescriptionForRequest ,
1818 getStatusFromRequest ,
1919} from './formatters/networkFormatter.js' ;
20- import { formatSnapshotNode } from './formatters/snapshotFormatter .js' ;
20+ import { SnapshotFormatter } from './formatters/SnapshotFormatter .js' ;
2121import type { McpContext } from './McpContext.js' ;
2222import { DevTools } from './third_party/index.js' ;
2323import type {
@@ -57,11 +57,16 @@ export class McpResponse implements Response {
5757 includePreservedMessages ?: boolean ;
5858 } ;
5959 #devToolsData?: DevToolsData ;
60+ #tabId?: string ;
6061
6162 attachDevToolsData ( data : DevToolsData ) : void {
6263 this . #devToolsData = data ;
6364 }
6465
66+ setTabId ( tabId : string ) : void {
67+ this . #tabId = tabId ;
68+ }
69+
6570 setIncludePages ( value : boolean ) : void {
6671 this . #includePages = value ;
6772 }
@@ -181,29 +186,31 @@ export class McpResponse implements Response {
181186 async handle (
182187 toolName : string ,
183188 context : McpContext ,
184- ) : Promise < Array < TextContent | ImageContent > > {
189+ ) : Promise < {
190+ content : Array < TextContent | ImageContent > ;
191+ structuredContent : object ;
192+ } > {
185193 if ( this . #includePages) {
186194 await context . createPagesSnapshot ( ) ;
187195 }
188196
189- let formattedSnapshot : string | undefined ;
197+ let snapshot : SnapshotFormatter | string | undefined ;
190198 if ( this . #snapshotParams) {
191199 await context . createTextSnapshot (
192200 this . #snapshotParams. verbose ,
193201 this . #devToolsData,
194202 ) ;
195- const snapshot = context . getTextSnapshot ( ) ;
196- if ( snapshot ) {
203+ const textSnapshot = context . getTextSnapshot ( ) ;
204+ if ( textSnapshot ) {
205+ const formatter = new SnapshotFormatter ( textSnapshot ) ;
197206 if ( this . #snapshotParams. filePath ) {
198207 await context . saveFile (
199- new TextEncoder ( ) . encode (
200- formatSnapshotNode ( snapshot . root , snapshot ) ,
201- ) ,
208+ new TextEncoder ( ) . encode ( formatter . toString ( ) ) ,
202209 this . #snapshotParams. filePath ,
203210 ) ;
204- formattedSnapshot = `Saved snapshot to ${ this . #snapshotParams. filePath } .` ;
211+ snapshot = this . #snapshotParams. filePath ;
205212 } else {
206- formattedSnapshot = formatSnapshotNode ( snapshot . root , snapshot ) ;
213+ snapshot = formatter ;
207214 }
208215 }
209216 }
@@ -335,7 +342,7 @@ export class McpResponse implements Response {
335342 bodies,
336343 consoleData,
337344 consoleListData,
338- formattedSnapshot ,
345+ snapshot ,
339346 } ) ;
340347 }
341348
@@ -349,9 +356,9 @@ export class McpResponse implements Response {
349356 } ;
350357 consoleData : ConsoleMessageData | undefined ;
351358 consoleListData : ConsoleMessageData [ ] | undefined ;
352- formattedSnapshot : string | undefined ;
359+ snapshot : SnapshotFormatter | string | undefined ;
353360 } ,
354- ) : Array < TextContent | ImageContent > {
361+ ) : { content : Array < TextContent | ImageContent > ; structuredContent : object } {
355362 const response = [ `# ${ toolName } response` ] ;
356363 for ( const line of this . #textResponseLines) {
357364 response . push ( line ) ;
@@ -393,9 +400,25 @@ Call ${handleDialog.name} to handle it before continuing.`);
393400 response . push ( ...parts ) ;
394401 }
395402
396- if ( data . formattedSnapshot ) {
397- response . push ( '## Latest page snapshot' ) ;
398- response . push ( data . formattedSnapshot ) ;
403+ const structuredContent : {
404+ snapshot ?: object ;
405+ snapshotFilePath ?: string ;
406+ tabId ?: string ;
407+ } = { } ;
408+
409+ if ( this . #tabId) {
410+ structuredContent . tabId = this . #tabId;
411+ }
412+
413+ if ( data . snapshot ) {
414+ if ( typeof data . snapshot === 'string' ) {
415+ response . push ( `Saved snapshot to ${ data . snapshot } .` ) ;
416+ structuredContent . snapshotFilePath = data . snapshot ;
417+ } else {
418+ response . push ( '## Latest page snapshot' ) ;
419+ response . push ( data . snapshot . toString ( ) ) ;
420+ structuredContent . snapshot = data . snapshot . toJSON ( ) ;
421+ }
399422 }
400423
401424 response . push ( ...this . #formatNetworkRequestData( context , data . bodies ) ) ;
@@ -468,7 +491,10 @@ Call ${handleDialog.name} to handle it before continuing.`);
468491 } as const ;
469492 } ) ;
470493
471- return [ text , ...images ] ;
494+ return {
495+ content : [ text , ...images ] ,
496+ structuredContent,
497+ } ;
472498 }
473499
474500 #dataWithPagination< T > ( data : T [ ] , pagination ?: PaginationOptions ) {
0 commit comments