Skip to content

Commit cf57a11

Browse files
iliakanIlya Kantor
authored andcommitted
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 cf57a11

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/puppeteer/index.ts

Lines changed: 12 additions & 10 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);

0 commit comments

Comments
 (0)