Authorize Reddit Account
authorizeAuthorize your Reddit account once via browser to grant permanent access. No API keys or passwords needed.
Instructions
One-time authorization: opens Reddit in your browser to connect your account. You click 'Allow', and reddirect gets permanent access. No API keys or passwords stored. Only needs to be done once.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/auth.ts:64-98 (handler)The handler function for the 'authorize' tool. It calls client.authorize() and returns the authenticated username.
async () => { try { const username = await client.authorize(); return { content: [ { type: "text" as const, text: JSON.stringify( { success: true, username, message: `Connected as u/${username}. You now have full read/write access. This authorization persists — you won't need to do this again.`, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : String(error), }, null, 2), }, ], isError: true, }; } } ); - src/tools/auth.ts:57-63 (schema)The registration call for 'authorize' with title, description, and inputSchema (empty object — no inputs).
"authorize", { title: "Authorize Reddit Account", description: "One-time authorization: opens Reddit in your browser to connect your account. You click 'Allow', and reddirect gets permanent access. No API keys or passwords stored. Only needs to be done once.", inputSchema: z.object({}), }, - src/tools/auth.ts:56-99 (registration)Registration of the 'authorize' tool via server.registerTool() inside the register() function exported from src/tools/auth.ts.
server.registerTool( "authorize", { title: "Authorize Reddit Account", description: "One-time authorization: opens Reddit in your browser to connect your account. You click 'Allow', and reddirect gets permanent access. No API keys or passwords stored. Only needs to be done once.", inputSchema: z.object({}), }, async () => { try { const username = await client.authorize(); return { content: [ { type: "text" as const, text: JSON.stringify( { success: true, username, message: `Connected as u/${username}. You now have full read/write access. This authorization persists — you won't need to do this again.`, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : String(error), }, null, 2), }, ], isError: true, }; } } ); } - src/index.ts:26-33 (registration)The root registration: registerAuthTools(server, client) is called, which registers the 'authorize' tool.
registerAuthTools(server, client); registerBrowseTools(server, client); registerSearchTools(server, client); registerPostTools(server, client); registerVoteTools(server, client); registerSaveTools(server, client); registerInboxTools(server, client); registerSubscriptionTools(server, client); - src/reddit/client.ts:113-128 (helper)The RedditClient.authorize() method that is called by the handler. It delegates to runOneTimeAuth() from auth-flow.ts.
async authorize(): Promise<string> { console.error("[reddirect] Starting one-time authorization..."); const result = await runOneTimeAuth(); this.session = { accessToken: result.accessToken, refreshToken: null, expiresAt: Date.now() + 23 * 60 * 60 * 1000, // token_v2 lasts ~24hrs username: result.username, scope: "*", }; await this.saveSession(); console.error(`[reddirect] Authorized as ${result.username}`); return result.username; } - src/reddit/auth-flow.ts:18-83 (helper)The runOneTimeAuth() function that opens Chrome, waits for Reddit login, extracts the token_v2 cookie, and returns the access token + username.
export async function runOneTimeAuth(): Promise<AuthResult> { const chromePath = await findChrome(); const tempProfile = path.join(os.tmpdir(), "reddirect-auth-profile"); await fs.mkdir(tempProfile, { recursive: true }); console.error("[reddirect] Opening Chrome for Reddit login..."); console.error( "[reddirect] Log into your Reddit account in the browser window.\n" ); // Launch Chrome with a temp profile and debug port // Use 'start' on Windows to ensure the window is visible const args = [ `--remote-debugging-port=${CDP_PORT}`, `--user-data-dir=${tempProfile}`, "--no-first-run", "--no-default-browser-check", "--window-size=800,700", "https://www.reddit.com/login/", ]; let chromeProcess: ReturnType<typeof spawn>; if (process.platform === "win32") { chromeProcess = spawn("cmd", ["/c", "start", "", chromePath, ...args], { stdio: "ignore", shell: false, }); } else { chromeProcess = spawn(chromePath, args, { stdio: "ignore", detached: true, }); } try { await waitForCDP(); const token = await pollForLogin(); console.error("[reddirect] Login detected! Extracting session..."); const username = await fetchUsername(token); return { accessToken: token, username }; } finally { // Kill Chrome and the debug port if (process.platform === "win32") { // Find and kill Chrome on our debug port spawn("cmd", ["/c", "taskkill", "/F", "/IM", "chrome.exe", "/FI", `WINDOWTITLE eq *reddirect*`], { stdio: "ignore", }); // More reliable: kill by the temp profile directory try { const { execSync } = await import("node:child_process"); execSync( `wmic process where "CommandLine like '%reddirect-auth-profile%'" call terminate`, { stdio: "ignore" } ); } catch { // Best effort } } else { chromeProcess.kill(); } // Clean up temp profile await fs.rm(tempProfile, { recursive: true, force: true }).catch(() => {}); } }