login
Check your authentication status on Perplexity.ai and open a browser to log in if not authenticated.
Instructions
Check if you are authenticated on Perplexity.ai. If not, opens a browser window so you can log in.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:60-75 (registration)Registration of the 'login' tool via mcp.addTool(), including its schema (no parameters), description, and execute handler.
mcp.addTool({ name: "login", description: "Check if you are authenticated on Perplexity.ai. If not, opens a browser window so you can log in.", parameters: z.object({}), execute: async () => { await ensureBrowser(); const page = await getFirstPage(); const authenticated = await checkSession(page); if (authenticated) { return "Already authenticated on Perplexity.ai."; } await ensureAuthenticated(); return "Login successful. You are now authenticated on Perplexity.ai."; }, }); - src/index.ts:65-74 (handler)The execute handler for the 'login' tool: checks session, and if not authenticated, calls ensureAuthenticated() to open browser for login.
execute: async () => { await ensureBrowser(); const page = await getFirstPage(); const authenticated = await checkSession(page); if (authenticated) { return "Already authenticated on Perplexity.ai."; } await ensureAuthenticated(); return "Login successful. You are now authenticated on Perplexity.ai."; }, - src/auth.ts:15-27 (helper)checkSession(): Helper that calls Perplexity's auth API endpoint to check if the user is logged in.
export async function checkSession(page: Page): Promise<boolean> { try { const response = await page.evaluate(async (url: string) => { const res = await fetch(url, { credentials: "include" }); return res.json(); }, AUTH_CHECK_URL); const session = response as Session; return !!session?.user?.id; } catch { return false; } } - src/auth.ts:29-43 (helper)ensureAuthenticated(): Navigates to Perplexity home, checks session, and waits for user to log in via the browser window.
export async function ensureAuthenticated(): Promise<void> { await ensureBrowser(); const page = await getFirstPage(); await page.goto(PERPLEXITY_HOME, { waitUntil: "domcontentloaded" }); const authenticated = await checkSession(page); if (authenticated) { log("Session active."); return; } log("No active session. Please log in to Perplexity in the browser window..."); await waitForLogin(page); log("Login detected."); } - src/auth.ts:45-53 (helper)waitForLogin(): Polls every 2 seconds for up to 5 minutes until a valid session is detected.
async function waitForLogin(page: Page): Promise<void> { const deadline = Date.now() + POLL_TIMEOUT_MS; while (Date.now() < deadline) { await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS)); const ok = await checkSession(page); if (ok) return; } throw new Error("Login timeout after 5 minutes."); }