@executeautomation/playwright-mcp-server
Version:
Model Context Protocol servers for Playwright
66 lines (65 loc) • 1.98 kB
JavaScript
import { BrowserToolBase } from './base.js';
import { createSuccessResponse } from '../common/types.js';
/**
* Tool for retrieving and filtering console logs from the browser
*/
export class ConsoleLogsTool extends BrowserToolBase {
constructor() {
super(...arguments);
this.consoleLogs = [];
}
/**
* Register a console message
* @param type The type of console message
* @param text The text content of the message
*/
registerConsoleMessage(type, text) {
this.consoleLogs.push(`[${type}] ${text}`);
}
/**
* Execute the console logs tool
*/
async execute(args, context) {
// No need to use safeExecute here as we don't need to interact with the page
// We're just filtering and returning logs that are already stored
let logs = [...this.consoleLogs];
// Filter by type if specified
if (args.type && args.type !== 'all') {
logs = logs.filter(log => log.startsWith(`[${args.type}]`));
}
// Filter by search text if specified
if (args.search) {
logs = logs.filter(log => log.includes(args.search));
}
// Limit the number of logs if specified
if (args.limit && args.limit > 0) {
logs = logs.slice(-args.limit);
}
// Clear logs if requested
if (args.clear) {
this.consoleLogs = [];
}
// Format the response
if (logs.length === 0) {
return createSuccessResponse("No console logs matching the criteria");
}
else {
return createSuccessResponse([
`Retrieved ${logs.length} console log(s):`,
...logs
]);
}
}
/**
* Get all console logs
*/
getConsoleLogs() {
return this.consoleLogs;
}
/**
* Clear all console logs
*/
clearConsoleLogs() {
this.consoleLogs = [];
}
}