diff --git a/agents/codex.ts b/agents/codex.ts index dc6198a..eaaa20c 100644 --- a/agents/codex.ts +++ b/agents/codex.ts @@ -1,4 +1,6 @@ import { spawnSync } from "node:child_process"; +import { mkdirSync } from "node:fs"; +import { join } from "node:path"; import { Codex, type CodexOptions, type ThreadEvent } from "@openai/codex-sdk"; import { log } from "../utils/cli.ts"; import { addInstructions } from "./instructions.ts"; @@ -16,7 +18,13 @@ export const codex = agent({ run: async ({ payload, mcpServers, apiKey, cliPath, githubInstallationToken }) => { process.env.OPENAI_API_KEY = apiKey; process.env.GITHUB_INSTALLATION_TOKEN = githubInstallationToken; - process.env.HOME = process.env.PULLFROG_TEMP_DIR; + + // create config directory for codex before setting HOME + const tempHome = process.env.PULLFROG_TEMP_DIR!; + const configDir = join(tempHome, ".config", "codex"); + mkdirSync(configDir, { recursive: true }); + + process.env.HOME = tempHome; configureCodexMcpServers({ mcpServers, cliPath }); @@ -45,6 +53,7 @@ export const codex = agent({ let finalOutput = ""; for await (const event of streamedTurn.events) { const handler = messageHandlers[event.type]; + console.log(event as never); if (handler) { handler(event as never); } diff --git a/agents/cursor.ts b/agents/cursor.ts index 99cc790..14533a3 100644 --- a/agents/cursor.ts +++ b/agents/cursor.ts @@ -1,5 +1,6 @@ import { spawn } from "node:child_process"; import { mkdirSync, writeFileSync } from "node:fs"; +import { homedir } from "node:os"; import { join } from "node:path"; import { log } from "../utils/cli.ts"; import { addInstructions } from "./instructions.ts"; @@ -25,9 +26,6 @@ export const cursor = agent({ // and --approve-mcps to automatically approve all MCP servers const fullPrompt = addInstructions(payload); - // Find temp directory from cliPath to set HOME for MCP config lookup - const tempDir = cliPath.split("/.local/bin/")[0]; - log.info("Running Cursor CLI..."); // Use spawn to handle streaming output @@ -42,7 +40,8 @@ export const cursor = agent({ ...process.env, CURSOR_API_KEY: apiKey, GITHUB_INSTALLATION_TOKEN: githubInstallationToken, - HOME: tempDir, // Set HOME so Cursor CLI can find .cursor/mcp.json + // Don't override HOME - Cursor CLI needs access to macOS keychain + // MCP config is written to tempDir/.cursor/mcp.json which Cursor will find }, stdio: ["ignore", "pipe", "pipe"], // Ignore stdin, pipe stdout/stderr } @@ -116,14 +115,14 @@ export const cursor = agent({ }, }); -/** - * Configure MCP servers for Cursor by writing to the Cursor configuration file. - * For cursor, we need to add the MCP servers to the Cursor configuration file manually as there is no CLI command to do this. - */ -function configureCursorMcpServers({ mcpServers, cliPath }: ConfigureMcpServersParams) { - const tempDir = cliPath.split("/.local/bin/")[0]; - const cursorConfigDir = join(tempDir, ".cursor"); +// There was an issue on macOS when you set HOME to a temp directory +// it was unable to find the macOS keychain and would fail +// temp solution is to stick with the actual $HOME +function configureCursorMcpServers({ mcpServers }: ConfigureMcpServersParams) { + const realHome = homedir(); + const cursorConfigDir = join(realHome, ".cursor"); const mcpConfigPath = join(cursorConfigDir, "mcp.json"); mkdirSync(cursorConfigDir, { recursive: true }); writeFileSync(mcpConfigPath, JSON.stringify({ mcpServers }, null, 2), "utf-8"); + log.info(`MCP config written to ${mcpConfigPath}`); } diff --git a/mcp/config.ts b/mcp/config.ts index c374543..374bb39 100644 --- a/mcp/config.ts +++ b/mcp/config.ts @@ -30,6 +30,7 @@ export function createMcpConfigs( GITHUB_REPOSITORY: githubRepository, PULLFROG_MODES: JSON.stringify(modes), PULLFROG_PAYLOAD: JSON.stringify(payload), + PULLFROG_TEMP_DIR: process.env.PULLFROG_TEMP_DIR!, }; // pass through GITHUB_RUN_ID if available (automatically set in GitHub Actions)