@@ -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,14 @@ 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+ } ;
171180 #networkRequestsOptions?: {
172181 include : boolean ;
173182 pagination ?: PaginationOptions ;
@@ -365,6 +374,20 @@ export class McpResponse implements Response {
365374 this . #textResponseLines. push ( value ) ;
366375 }
367376
377+ setHeapSnapshot (
378+ aggregates : Record <
379+ string ,
380+ DevTools . HeapSnapshotModel . HeapSnapshotModel . AggregatedInfo
381+ > ,
382+ options ?: PaginationOptions ,
383+ ) {
384+ this . #heapSnapshotOptions = {
385+ include : true ,
386+ aggregates,
387+ pagination : options ,
388+ } ;
389+ }
390+
368391 attachImage ( value : ImageContentData ) : void {
369392 this . #images. push ( value ) ;
370393 }
@@ -661,6 +684,7 @@ export class McpResponse implements Response {
661684 } ;
662685 pages ?: object [ ] ;
663686 pagination ?: object ;
687+ heapSnapshot ?: object [ ] ;
664688 extensionServiceWorkers ?: object [ ] ;
665689 extensionPages ?: object [ ] ;
666690 } = { } ;
@@ -857,6 +881,26 @@ Call ${handleDialog.name} to handle it before continuing.`);
857881 }
858882 }
859883
884+ if ( this . #heapSnapshotOptions?. include ) {
885+ const aggregates = this . #heapSnapshotOptions. aggregates ;
886+ const entries = Object . entries ( aggregates ) ;
887+ const sortedEntries = entries . sort ( ( a , b ) => b [ 1 ] . self - a [ 1 ] . self ) ;
888+
889+ const paginationData = this . #dataWithPagination(
890+ sortedEntries ,
891+ this . #heapSnapshotOptions. pagination ,
892+ ) ;
893+
894+ structuredContent . pagination = paginationData . pagination ;
895+ response . push ( ...paginationData . info ) ;
896+
897+ const paginatedRecord = Object . fromEntries ( paginationData . items ) ;
898+ const formatter = new HeapSnapshotFormatter ( paginatedRecord ) ;
899+
900+ response . push ( formatter . toString ( ) ) ;
901+ structuredContent . heapSnapshot = formatter . toJSON ( ) ;
902+ }
903+
860904 if ( data . detailedNetworkRequest ) {
861905 response . push ( data . detailedNetworkRequest . toStringDetailed ( ) ) ;
862906 structuredContent . networkRequest =
0 commit comments