diff --git a/src/McpResponse.ts b/src/McpResponse.ts index 43a26b68a..a00f2887f 100644 --- a/src/McpResponse.ts +++ b/src/McpResponse.ts @@ -337,7 +337,7 @@ export class McpResponse implements Response { const devTools = context.getDevToolsUniverse(); return await ConsoleFormatter.from(consoleMessage, { id: consoleMessageStableId, - fetchDetailedData: true, + fetchDetailedData: false, devTools: devTools ?? undefined, }); } diff --git a/src/formatters/ConsoleFormatter.ts b/src/formatters/ConsoleFormatter.ts index 366f1a234..4e0fa74f3 100644 --- a/src/formatters/ConsoleFormatter.ts +++ b/src/formatters/ConsoleFormatter.ts @@ -50,14 +50,24 @@ export class ConsoleFormatter { } this.#resolvedArgs = await Promise.all( - this.#msg.args().map(arg => arg.jsonValue()), + this.#msg.args().map(async (arg, i) => { + try { + return await arg.jsonValue(); + } catch { + return ``; + } + }), ); if (devTools) { - this.#resolvedStackTrace = await createStackTraceForConsoleMessage( - devTools, - this.#msg, - ); + try { + this.#resolvedStackTrace = await createStackTraceForConsoleMessage( + devTools, + this.#msg, + ); + } catch { + // ignore + } } } diff --git a/tests/formatters/ConsoleFormatter.test.js.snapshot b/tests/formatters/ConsoleFormatter.test.js.snapshot index 76bbb0a74..00520af77 100644 --- a/tests/formatters/ConsoleFormatter.test.js.snapshot +++ b/tests/formatters/ConsoleFormatter.test.js.snapshot @@ -44,3 +44,10 @@ Message: log> Processing file: ### Arguments Arg #0: file.txt `; + +exports[`ConsoleFormatter > toStringDetailed > handles \"Execution context is not available\" error in args 1`] = ` +ID: 6 +Message: log> Processing file: +### Arguments +Arg #0: +`; diff --git a/tests/formatters/ConsoleFormatter.test.ts b/tests/formatters/ConsoleFormatter.test.ts index 985bf013e..4720ff613 100644 --- a/tests/formatters/ConsoleFormatter.test.ts +++ b/tests/formatters/ConsoleFormatter.test.ts @@ -163,6 +163,27 @@ describe('ConsoleFormatter', () => { ).toStringDetailed(); t.assert.snapshot?.(result); }); + + it('handles "Execution context is not available" error in args', async t => { + const message = createMockMessage({ + type: () => 'log', + text: () => 'Processing file:', + args: () => [ + { + jsonValue: async () => { + throw new Error('Execution context is not available'); + }, + }, + ], + }); + const formatter = await ConsoleFormatter.from(message, { + id: 6, + fetchDetailedData: true, + }); + const result = formatter.toStringDetailed(); + t.assert.snapshot?.(result); + assert.ok(result.includes('')); + }); }); describe('toJSON', () => { it('formats a console.log message', async () => {