Skip to content

cursor-sdk-agent.ts

Source file: 02-agents/templates/cursor-sdk-agent.ts. Copy it from the repository, or use the copy button on the code block.

cursor-sdk-agent.ts
/**
* Cursor SDK agent template: summarise a PR diff into summary.md.
*
* Setup: npm install @cursor/sdk (Node >= 22.13)
* Auth: CURSOR_API_KEY (Cursor Dashboard → Integrations, or a team service account)
* Run: npx tsx cursor-sdk-agent.ts
*
* Exit codes: 0 = finished, 1 = agent never started (auth/config/network), 2 = run failed.
*/
import { Agent, CursorAgentError } from "@cursor/sdk";
const PROMPT = `
Summarise the changes in \`git diff origin/main...HEAD\`.
Write the result to summary.md using exactly these headings:
## What changed
## Why it matters
## Risks
Keep it under 200 words. Do not modify any other file.
`;
async function main(): Promise<number> {
try {
await using agent = await Agent.create({
apiKey: process.env.CURSOR_API_KEY!,
model: { id: "composer-2.5" },
local: { cwd: process.cwd() }, // set explicitly; use `cloud: { repos: [...] }` for Cloud Agents
});
const run = await agent.send(PROMPT);
console.error(`agent=${agent.agentId} run=${run.id}`);
for await (const event of run.stream()) {
if (event.type === "assistant") {
for (const block of event.message.content) {
if (block.type === "text") process.stdout.write(block.text);
}
}
}
const result = await run.wait();
if (result.status === "error") {
console.error(`run failed: ${result.id}`);
return 2;
}
return 0;
} catch (err) {
if (err instanceof CursorAgentError) {
console.error(`startup failed: ${err.message} (retryable=${err.isRetryable})`);
return 1;
}
throw err;
}
}
process.exit(await main());