@@ -9,6 +9,7 @@ import type {WebMCPTool} from 'puppeteer-core';
99import type { ParsedArguments } from './bin/chrome-devtools-mcp-cli-options.js' ;
1010import { ConsoleFormatter } from './formatters/ConsoleFormatter.js' ;
1111import { HeapSnapshotFormatter } from './formatters/HeapSnapshotFormatter.js' ;
12+ import { isNodeLike } from './formatters/HeapSnapshotFormatter.js' ;
1213import { IssueFormatter } from './formatters/IssueFormatter.js' ;
1314import { NetworkFormatter } from './formatters/NetworkFormatter.js' ;
1415import { SnapshotFormatter } from './formatters/SnapshotFormatter.js' ;
@@ -202,6 +203,7 @@ export class McpResponse implements Response {
202203 #args: ParsedArguments ;
203204 #page?: McpPage ;
204205 #redactNetworkHeaders = true ;
206+ #error?: Error ;
205207
206208 constructor ( args : ParsedArguments ) {
207209 this . #args = args ;
@@ -306,6 +308,10 @@ export class McpResponse implements Response {
306308 } ;
307309 }
308310
311+ setError ( error : Error ) : void {
312+ this . #error = error ;
313+ }
314+
309315 attachNetworkRequest (
310316 reqId : number ,
311317 options ?: { requestFilePath ?: string ; responseFilePath ?: string } ,
@@ -374,6 +380,10 @@ export class McpResponse implements Response {
374380 return this . #consoleDataOptions?. types ;
375381 }
376382
383+ get error ( ) : Error | undefined {
384+ return this . #error;
385+ }
386+
377387 appendResponseLine ( value : string ) : void {
378388 this . #textResponseLines. push ( value ) ;
379389 }
@@ -661,6 +671,7 @@ export class McpResponse implements Response {
661671 lighthouseResult : this . #attachedLighthouseResult,
662672 inPageTools,
663673 webmcpTools,
674+ errorMessage : this . #error?. message ,
664675 } ) ;
665676 }
666677
@@ -679,6 +690,7 @@ export class McpResponse implements Response {
679690 lighthouseResult ?: LighthouseData ;
680691 inPageTools ?: ToolGroup < ToolDefinition > ;
681692 webmcpTools ?: WebMCPTool [ ] ;
693+ errorMessage ?: string ;
682694 } ,
683695 ) : { content : Array < TextContent | ImageContent > ; structuredContent : object } {
684696 const structuredContent : {
@@ -717,6 +729,7 @@ export class McpResponse implements Response {
717729 heapSnapshotNodes ?: readonly object [ ] ;
718730 extensionServiceWorkers ?: object [ ] ;
719731 extensionPages ?: object [ ] ;
732+ errorMessage ?: string ;
720733 } = { } ;
721734
722735 const response = [ ] ;
@@ -945,8 +958,12 @@ Call ${handleDialog.name} to handle it before continuing.`);
945958 }
946959 const nodes = this . #heapSnapshotOptions. nodes ;
947960 if ( nodes ) {
961+ const sortedItems = nodes . items
962+ . filter ( isNodeLike )
963+ . sort ( ( a , b ) => b . retainedSize - a . retainedSize ) ;
964+
948965 const paginationData = this . #dataWithPagination(
949- nodes . items ,
966+ sortedItems ,
950967 this . #heapSnapshotOptions. pagination ,
951968 ) ;
952969
@@ -1075,6 +1092,11 @@ Call ${handleDialog.name} to handle it before continuing.`);
10751092 }
10761093 }
10771094
1095+ if ( data . errorMessage ) {
1096+ response . push ( `Error: ${ data . errorMessage } ` ) ;
1097+ structuredContent . errorMessage = data . errorMessage ;
1098+ }
1099+
10781100 const text : TextContent = {
10791101 type : 'text' ,
10801102 text : response . join ( '\n' ) ,
0 commit comments