Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type McpLaunchOptions = {
userDataDir?: string;
headless: boolean;
isolated: boolean;
logFile?: fs.WriteStream;
};

export async function launch(options: McpLaunchOptions): Promise<Browser> {
Expand Down Expand Up @@ -100,7 +101,7 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
}

try {
return await puppeteer.launch({
const browser = await puppeteer.launch({
...connectOptions,
channel: puppeterChannel,
executablePath,
Expand All @@ -110,6 +111,13 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
headless,
args,
});
if (options.logFile) {
// FIXME: we are probably subscribing too late to catch startup logs. We
// should expose the process earlier or expose the getRecentLogs() getter.
browser.process()?.stderr?.pipe(options.logFile);
browser.process()?.stdout?.pipe(options.logFile);
}
return browser;
} catch (error) {
// TODO: check browser logs for `Failed to create a ProcessSingleton for
// your profile directory` instead.
Expand Down Expand Up @@ -145,6 +153,7 @@ export async function resolveBrowser(options: {
channel?: Channel;
headless: boolean;
isolated: boolean;
logFile?: fs.WriteStream;
}) {
const browser = options.browserUrl
? await ensureBrowserConnected(options.browserUrl)
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ export const args = yargsInstance
.help()
.parseSync();

if (args.logFile) {
saveLogsToFile(args.logFile);
}
const logFile = args.logFile ? saveLogsToFile(args.logFile) : undefined;

function readPackageJson(): {version?: string} {
const currentDir = import.meta.dirname;
Expand Down Expand Up @@ -155,6 +153,7 @@ async function getContext(): Promise<McpContext> {
customDevTools: args.customDevtools,
channel: args.channel as Channel,
isolated: args.isolated,
logFile,
});
if (context?.browser !== browser) {
context = await McpContext.from(browser, logger);
Expand Down
3 changes: 2 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const namespacesToEnable = [
...(process.env['DEBUG'] ? [process.env['DEBUG']] : []),
];

export function saveLogsToFile(fileName: string) {
export function saveLogsToFile(fileName: string): fs.WriteStream {
// Enable overrides everything so we need to add them
debug.enable(namespacesToEnable.join(','));

Expand All @@ -27,6 +27,7 @@ export function saveLogsToFile(fileName: string) {
logFile.end();
process.exit(1);
});
return logFile;
}

export const logger = debug(mcpDebugNamespace);