@@ -33,15 +33,18 @@ export class McpResponse implements Response {
3333 #includePages = false ;
3434 #includeSnapshot = false ;
3535 #attachedNetworkRequestData?: NetworkRequestData ;
36- #includeConsoleData = false ;
3736 #textResponseLines: string [ ] = [ ] ;
38- #formattedConsoleData?: string [ ] ;
3937 #images: ImageContentData [ ] = [ ] ;
4038 #networkRequestsOptions?: {
4139 include : boolean ;
4240 pagination ?: PaginationOptions ;
4341 resourceTypes ?: ResourceType [ ] ;
4442 } ;
43+ #consoleDataOptions?: {
44+ include : boolean ;
45+ pagination ?: PaginationOptions ;
46+ types ?: string [ ] ;
47+ } ;
4548
4649 setIncludePages ( value : boolean ) : void {
4750 this . #includePages = value ;
@@ -77,8 +80,30 @@ export class McpResponse implements Response {
7780 } ;
7881 }
7982
80- setIncludeConsoleData ( value : boolean ) : void {
81- this . #includeConsoleData = value ;
83+ setIncludeConsoleData (
84+ value : boolean ,
85+ options ?: {
86+ pageSize ?: number ;
87+ pageIdx ?: number ;
88+ types ?: string [ ] ;
89+ } ,
90+ ) : void {
91+ if ( ! value ) {
92+ this . #consoleDataOptions = undefined ;
93+ return ;
94+ }
95+
96+ this . #consoleDataOptions = {
97+ include : value ,
98+ pagination :
99+ options ?. pageSize || options ?. pageIdx
100+ ? {
101+ pageSize : options . pageSize ,
102+ pageIdx : options . pageIdx ,
103+ }
104+ : undefined ,
105+ types : options ?. types ,
106+ } ;
82107 }
83108
84109 attachNetworkRequest ( url : string ) : void {
@@ -96,14 +121,20 @@ export class McpResponse implements Response {
96121 }
97122
98123 get includeConsoleData ( ) : boolean {
99- return this . #includeConsoleData ;
124+ return this . #consoleDataOptions ?. include ?? false ;
100125 }
101126 get attachedNetworkRequestUrl ( ) : string | undefined {
102127 return this . #attachedNetworkRequestData?. networkRequestUrl ;
103128 }
104129 get networkRequestsPageIdx ( ) : number | undefined {
105130 return this . #networkRequestsOptions?. pagination ?. pageIdx ;
106131 }
132+ get consoleMessagesPageIdx ( ) : number | undefined {
133+ return this . #consoleDataOptions?. pagination ?. pageIdx ;
134+ }
135+ get consoleMessagesTypes ( ) : string [ ] | undefined {
136+ return this . #consoleDataOptions?. types ;
137+ }
107138
108139 appendResponseLine ( value : string ) : void {
109140 this . #textResponseLines. push ( value ) ;
@@ -136,8 +167,6 @@ export class McpResponse implements Response {
136167 await context . createTextSnapshot ( ) ;
137168 }
138169
139- let formattedConsoleMessages : string [ ] ;
140-
141170 if ( this . #attachedNetworkRequestData?. networkRequestUrl ) {
142171 const request = context . getNetworkRequestByUrl (
143172 this . #attachedNetworkRequestData. networkRequestUrl ,
@@ -153,23 +182,13 @@ export class McpResponse implements Response {
153182 }
154183 }
155184
156- if ( this . #includeConsoleData) {
157- const consoleMessages = context . getConsoleData ( ) ;
158- if ( consoleMessages ) {
159- formattedConsoleMessages = await Promise . all (
160- consoleMessages . map ( message => formatConsoleEvent ( message ) ) ,
161- ) ;
162- this . #formattedConsoleData = formattedConsoleMessages ;
163- }
164- }
165-
166- return this . format ( toolName , context ) ;
185+ return await this . format ( toolName , context ) ;
167186 }
168187
169- format (
188+ async format (
170189 toolName : string ,
171190 context : McpContext ,
172- ) : Array < TextContent | ImageContent > {
191+ ) : Promise < Array < TextContent | ImageContent > > {
173192 const response = [ `# ${ toolName } response` ] ;
174193 for ( const line of this . #textResponseLines) {
175194 response . push ( line ) ;
@@ -253,10 +272,32 @@ Call ${handleDialog.name} to handle it before continuing.`);
253272 }
254273 }
255274
256- if ( this . #includeConsoleData && this . #formattedConsoleData) {
275+ if ( this . #consoleDataOptions?. include ) {
276+ let messages = context . getConsoleData ( ) ;
277+
278+ if ( this . #consoleDataOptions. types ?. length ) {
279+ const normalizedTypes = new Set ( this . #consoleDataOptions. types ) ;
280+ messages = messages . filter ( message => {
281+ if ( ! ( 'type' in message ) ) {
282+ return normalizedTypes . has ( 'error' ) ;
283+ }
284+ const type = message . type ( ) ;
285+ return normalizedTypes . has ( type ) ;
286+ } ) ;
287+ }
288+
257289 response . push ( '## Console messages' ) ;
258- if ( this . #formattedConsoleData. length ) {
259- response . push ( ...this . #formattedConsoleData) ;
290+ if ( messages . length ) {
291+ const data = this . #dataWithPagination(
292+ messages ,
293+ this . #consoleDataOptions. pagination ,
294+ ) ;
295+ response . push ( ...data . info ) ;
296+ response . push (
297+ ...( await Promise . all (
298+ data . items . map ( message => formatConsoleEvent ( message ) ) ,
299+ ) ) ,
300+ ) ;
260301 } else {
261302 response . push ( '<no console messages found>' ) ;
262303 }
0 commit comments