Skip to content

Commit faf55ab

Browse files
authored
Fix bug with stack overflow on console calls
Fixed a bug with the multiple tool call within a single window. It was overwriting console with its own methods, so that `console.log` was causing a stack overflow due to calling itself.
1 parent 7a0eef8 commit faf55ab

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/puppeteer/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,19 @@ async function handleToolCall(name: string, args: any): Promise<CallToolResult>
351351
case "puppeteer_evaluate":
352352
try {
353353
await page.evaluate(() => {
354-
window.mcpHelper = {
355-
logs: [],
356-
originalConsole: { ...console },
357-
};
358-
359-
['log', 'info', 'warn', 'error'].forEach(method => {
360-
(console as any)[method] = (...args: any[]) => {
361-
window.mcpHelper.logs.push(`[${method}] ${args.join(' ')}`);
362-
(window.mcpHelper.originalConsole as any)[method](...args);
354+
if (!window.mcpHelper) {
355+
window.mcpHelper = {
356+
logs: [],
357+
originalConsole: { ...console },
363358
};
364-
});
359+
360+
['log', 'info', 'warn', 'error'].forEach(method => {
361+
(console as any)[method] = (...args: any[]) => {
362+
window.mcpHelper.logs.push(`[${method}] ${args.join(' ')}`);
363+
(window.mcpHelper.originalConsole as any)[method](...args);
364+
};
365+
});
366+
}
365367
});
366368

367369
const result = await page.evaluate(args.script);
@@ -481,4 +483,4 @@ runServer().catch(console.error);
481483
process.stdin.on("close", () => {
482484
console.error("Puppeteer MCP Server closed");
483485
server.close();
484-
});
486+
});

0 commit comments

Comments
 (0)