Learn how to run Cursor's headless CLI agent inside a Trigger.dev task, parsing NDJSON stdout into a Realtime Stream that renders live in a browser terminal.
- Next.js – App Router frontend with server actions to trigger runs
- Cursor CLI – Headless AI coding agent spawned as a child process
- Trigger.dev – Background task orchestration with real-time streaming to the frontend, observability, and deployment
background-cursor.mp4
-
Install dependencies
pnpm install
-
Configure environment variables
cp env.local.example .env.local
TRIGGER_SECRET_KEY– From Trigger.dev dashboard (starts withtr_dev_ortr_)TRIGGER_PROJECT_REF– Your project ref (starts withproj_)CURSOR_API_KEY– Your Cursor API key for headless CLI access
-
Start development servers
# Terminal 1: Next.js pnpm dev # Terminal 2: Trigger.dev npx trigger.dev@latest dev
-
Open http://localhost:3000 in your browser to see the demo
- Build extensions – Installs
cursor-agentinto the task container image viaaddLayer, so any system binary can ship with your task - Realtime Streams v2 – NDJSON from a child process stdout is parsed and piped directly to the browser using
streams.define()and.pipe() - Live terminal rendering – Each cursor event (system, assistant, tool_call, result) renders as a distinct row with auto-scroll
- Long-running tasks – cursor-agent runs for minutes; Trigger.dev handles lifecycle, timeouts, and retries
- Machine selection –
medium-2xpreset for resource-intensive CLI tools - Model picker – Switch between Claude models from the UI before triggering a run
- Bundled binary support – Spawns cursor-agent's own node binary for native module ABI compatibility
- extensions/cursor-cli.ts – Build extension + spawn helper that returns a typed NDJSON stream and
waitUntilExit() - trigger/cursor-agent.ts – The task: spawns the CLI, pipes the stream, waits for exit
- trigger/cursor-stream.ts – Realtime Streams v2 stream definition
- components/terminal.tsx – Realtime terminal UI with
useRealtimeRunWithStreams - lib/cursor-events.ts – TypeScript types and parsers for cursor NDJSON events
- trigger.config.ts – Trigger.dev config with the cursor CLI build extension