Skip to content

Commit c841124

Browse files
author
A.R.
committed
fix(#5.2): Phase 2 uses persistent context + selective vault cookie injection
Phase 1 (headed bootstrap) wrote fresh cf_clearance to disk via `launchPersistentContext(browserData)`, but Phase 2 used `chromium.launch()` + `getOrCreateContext()` — a non-persistent context — so the fresh clearance was never loaded. Only stale vault cookies were injected. Fix: Phase 2 now uses `launchPersistentContext(activePaths.browserData)` so it inherits the disk cookies from Phase 1 automatically. To avoid overwriting the fresh cf_clearance with the stale vault copy, inject vault cookies ONLY for cookie names not already present on disk. Remove now-unused `getOrCreateContext` import from client.ts. Refs issue #5
1 parent 0c935a5 commit c841124

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

packages/mcp-server/src/client.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
findBrowser,
2020
findChromeExecutable,
2121
resolveBrowserExecutable,
22-
getOrCreateContext,
2322
getSavedCookies,
2423
type BrowserChannel,
2524
type ASIFile,
@@ -914,25 +913,33 @@ export class PerplexityClient {
914913
}
915914

916915
// Phase 2: Headless browser for search operations.
917-
console.error("[perplexity-mcp] Launching headless browser...");
916+
// Use the SAME persistent browserData directory as Phase 1 so that
917+
// any cf_clearance cookie acquired during the headed bootstrap is
918+
// already on disk and loaded automatically. This fixes the bug where
919+
// Phase 2 used a non-persistent context and only had stale vault
920+
// cookies (issue #5).
921+
console.error("[perplexity-mcp] Launching headless persistent browser...");
918922
const launchOpts = buildLaunchOptions(true);
919-
this.browser = await chromium.launch({
920-
headless: launchOpts.headless,
921-
args: launchOpts.args,
922-
...(launchOpts.executablePath ? { executablePath: launchOpts.executablePath } : {}),
923-
...(launchOpts.channel ? { channel: launchOpts.channel } : {}),
924-
ignoreDefaultArgs: launchOpts.ignoreDefaultArgs,
925-
});
926-
this.context = await getOrCreateContext(this.browser, {
927-
viewport: launchOpts.viewport,
928-
userAgent: launchOpts.userAgent,
929-
});
923+
this.context = await chromium.launchPersistentContext(
924+
activePaths.browserData,
925+
launchOpts,
926+
);
927+
this.browser = this.context.browser();
930928

931-
// Inject saved cookies (session + cf_clearance from login)
929+
// Inject vault cookies only for cookies not already present on disk.
930+
// The headed bootstrap may have refreshed cf_clearance; we must not
931+
// overwrite the fresh disk cookie with the stale vault copy.
932932
const saved = await getSavedCookies();
933933
if (saved.length > 0) {
934-
await this.context.addCookies(saved);
935-
console.error(`[perplexity-mcp] Injected ${saved.length} saved cookies into browser context.`);
934+
const current = await this.context.cookies();
935+
const currentNames = new Set(current.map((c) => c.name));
936+
const toInject = saved.filter((c) => !currentNames.has(c.name));
937+
if (toInject.length > 0) {
938+
await this.context.addCookies(toInject);
939+
console.error(`[perplexity-mcp] Injected ${toInject.length} missing cookies from vault.`);
940+
} else {
941+
console.error("[perplexity-mcp] All vault cookies already present on disk; skipping injection.");
942+
}
936943
}
937944

938945
this.page = await this.context.newPage();

0 commit comments

Comments
 (0)