@@ -8,6 +8,7 @@ import type {WebMCPTool} from 'puppeteer-core';
88
99import type { ParsedArguments } from './bin/chrome-devtools-mcp-cli-options.js' ;
1010import { ConsoleFormatter } from './formatters/ConsoleFormatter.js' ;
11+ import { HeapSnapshotFormatter } from './formatters/HeapSnapshotFormatter.js' ;
1112import { IssueFormatter } from './formatters/IssueFormatter.js' ;
1213import { NetworkFormatter } from './formatters/NetworkFormatter.js' ;
1314import { SnapshotFormatter } from './formatters/SnapshotFormatter.js' ;
@@ -168,6 +169,16 @@ export class McpResponse implements Response {
168169 #attachedLighthouseResult?: LighthouseData ;
169170 #textResponseLines: string [ ] = [ ] ;
170171 #images: ImageContentData [ ] = [ ] ;
172+ #heapSnapshotOptions?: {
173+ include : boolean ;
174+ aggregates ?: Record <
175+ string ,
176+ DevTools . HeapSnapshotModel . HeapSnapshotModel . AggregatedInfo
177+ > ;
178+ pagination ?: PaginationOptions ;
179+ stats ?: DevTools . HeapSnapshotModel . HeapSnapshotModel . Statistics ;
180+ staticData ?: DevTools . HeapSnapshotModel . HeapSnapshotModel . StaticData | null ;
181+ } ;
171182 #networkRequestsOptions?: {
172183 include : boolean ;
173184 pagination ?: PaginationOptions ;
@@ -365,6 +376,33 @@ export class McpResponse implements Response {
365376 this . #textResponseLines. push ( value ) ;
366377 }
367378
379+ setHeapSnapshotAggregates (
380+ aggregates : Record <
381+ string ,
382+ DevTools . HeapSnapshotModel . HeapSnapshotModel . AggregatedInfo
383+ > ,
384+ options ?: PaginationOptions ,
385+ ) {
386+ this . #heapSnapshotOptions = {
387+ ...this . #heapSnapshotOptions,
388+ include : true ,
389+ aggregates,
390+ pagination : options ,
391+ } ;
392+ }
393+
394+ setHeapSnapshotStats (
395+ stats : DevTools . HeapSnapshotModel . HeapSnapshotModel . Statistics ,
396+ staticData : DevTools . HeapSnapshotModel . HeapSnapshotModel . StaticData | null ,
397+ ) {
398+ this . #heapSnapshotOptions = {
399+ ...this . #heapSnapshotOptions,
400+ include : true ,
401+ stats,
402+ staticData,
403+ } ;
404+ }
405+
368406 attachImage ( value : ImageContentData ) : void {
369407 this . #images. push ( value ) ;
370408 }
@@ -661,6 +699,11 @@ export class McpResponse implements Response {
661699 } ;
662700 pages ?: object [ ] ;
663701 pagination ?: object ;
702+ heapSnapshot ?: {
703+ stats ?: object ;
704+ staticData ?: object ;
705+ } ;
706+ heapSnapshotData ?: object [ ] ;
664707 extensionServiceWorkers ?: object [ ] ;
665708 extensionPages ?: object [ ] ;
666709 } = { } ;
@@ -857,6 +900,40 @@ Call ${handleDialog.name} to handle it before continuing.`);
857900 }
858901 }
859902
903+ if ( this . #heapSnapshotOptions?. include ) {
904+ response . push ( '## Heap Snapshot Data' ) ;
905+ const stats = this . #heapSnapshotOptions. stats ;
906+ const staticData = this . #heapSnapshotOptions. staticData ;
907+ if ( stats ) {
908+ response . push ( `Statistics: ${ JSON . stringify ( stats , null , 2 ) } ` ) ;
909+ structuredContent . heapSnapshot = structuredContent . heapSnapshot || { } ;
910+ structuredContent . heapSnapshot . stats = stats ;
911+ }
912+ if ( staticData ) {
913+ response . push ( `Static Data: ${ JSON . stringify ( staticData , null , 2 ) } ` ) ;
914+ structuredContent . heapSnapshot = structuredContent . heapSnapshot || { } ;
915+ structuredContent . heapSnapshot . staticData = staticData ;
916+ }
917+ const aggregates = this . #heapSnapshotOptions. aggregates ;
918+ if ( aggregates ) {
919+ const sortedEntries = HeapSnapshotFormatter . sort ( aggregates ) ;
920+
921+ const paginationData = this . #dataWithPagination(
922+ sortedEntries ,
923+ this . #heapSnapshotOptions. pagination ,
924+ ) ;
925+
926+ structuredContent . pagination = paginationData . pagination ;
927+ response . push ( ...paginationData . info ) ;
928+
929+ const paginatedRecord = Object . fromEntries ( paginationData . items ) ;
930+ const formatter = new HeapSnapshotFormatter ( paginatedRecord ) ;
931+
932+ response . push ( formatter . toString ( ) ) ;
933+ structuredContent . heapSnapshotData = formatter . toJSON ( ) ;
934+ }
935+ }
936+
860937 if ( data . detailedNetworkRequest ) {
861938 response . push ( data . detailedNetworkRequest . toStringDetailed ( ) ) ;
862939 structuredContent . networkRequest =
0 commit comments