cursor-delegate-mcp 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/acp-client.js +1 -1
- package/src/delegate.js +69 -9
- package/src/jsonrpc.js +7 -1
- package/src/server.js +52 -23
package/package.json
CHANGED
package/src/acp-client.js
CHANGED
|
@@ -81,7 +81,7 @@ export class AcpClient extends EventEmitter {
|
|
|
81
81
|
async newSession(cwd) { return this._captureSession(await this.peer.request("session/new", { cwd, mcpServers: [] })); }
|
|
82
82
|
async loadSession(sessionId, cwd) { return this._captureSession(await this.peer.request("session/load", { sessionId, cwd, mcpServers: [] })); }
|
|
83
83
|
setModel(sessionId, modelId) { return this.peer.request("session/set_model", { sessionId, modelId }); }
|
|
84
|
-
|
|
84
|
+
setConfigOption(sessionId, configId, value) { return this.peer.request("session/set_config_option", { sessionId, configId, value: String(value) }); } // configId, not optionId; ACP wants a string value
|
|
85
85
|
setMode(sessionId, modeId) { return this.peer.request("session/set_mode", { sessionId, modeId }); }
|
|
86
86
|
prompt(sessionId, text) { return this.peer.request("session/prompt", { sessionId, prompt: [{ type: "text", text }] }); }
|
|
87
87
|
cancel(sessionId) { this.peer.notify("session/cancel", { sessionId }); return Promise.resolve(); }
|
package/src/delegate.js
CHANGED
|
@@ -37,10 +37,12 @@ async function openSession(client, resumeSessionId, workspace) {
|
|
|
37
37
|
try {
|
|
38
38
|
await client.loadSession(resumeSessionId, workspace);
|
|
39
39
|
return { sessionId: resumeSessionId }; // load does not echo sessionId
|
|
40
|
-
} catch {
|
|
41
|
-
//
|
|
40
|
+
} catch (err) {
|
|
41
|
+
// Starting fresh is right, but silently doing so leaves the caller unable to tell a
|
|
42
|
+
// stale id from a typo'd one — keep why the load failed.
|
|
43
|
+
const fresh = await client.newSession(workspace);
|
|
44
|
+
return { ...fresh, resumeError: err?.message || String(err) };
|
|
42
45
|
}
|
|
43
|
-
return client.newSession(workspace);
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
// session/new reports the models the agent actually offers. Reject an unknown id here,
|
|
@@ -56,15 +58,25 @@ function assertKnownModel(client, model) {
|
|
|
56
58
|
throw err;
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
// Which config options a model carries is not discoverable up front: session/new reports
|
|
62
|
+
// configOptions for the default model and set_model returns nothing. So ask, and read the
|
|
63
|
+
// rejection as the answer. Two distinct -32602s, both measured: "Unknown model config
|
|
64
|
+
// option: X" means this model has no such knob — report it and carry on. "Invalid value
|
|
65
|
+
// for X: Y" means the caller named a value the model rejects, which must not be swallowed.
|
|
66
|
+
// Returns true when the option is unsupported.
|
|
67
|
+
async function applyConfig(client, sessionId, configId, value) {
|
|
68
|
+
try {
|
|
69
|
+
await client.setConfigOption(sessionId, configId, value);
|
|
70
|
+
return false;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
if (err?.code !== -32602 || !/unknown model config option/i.test(err?.message || "")) throw err;
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
export async function runDelegate({
|
|
66
78
|
spec, mode = "agent", resumeSessionId, workspace,
|
|
67
|
-
model = DEFAULT_MODEL, fast = false, clientFactory, onElicit,
|
|
79
|
+
model = DEFAULT_MODEL, fast = false, reasoning, context, clientFactory, onElicit,
|
|
68
80
|
idleMs, handshakeMs, hardCapMs, timeoutMs,
|
|
69
81
|
onSessionReady, onProgress, progressThrottleMs = 2000,
|
|
70
82
|
heartbeatMs = DEFAULT_HEARTBEAT_MS,
|
|
@@ -284,6 +296,9 @@ export async function runDelegate({
|
|
|
284
296
|
// The bridge cannot see inside a running shell command, so a long silence is reported
|
|
285
297
|
// rather than acted on: the caller gets elapsed time and frame age and can decide.
|
|
286
298
|
let lastToolLabel = null;
|
|
299
|
+
let modeChanged;
|
|
300
|
+
let sessionTitle;
|
|
301
|
+
let promptInFlight = false;
|
|
287
302
|
let heartbeat = null;
|
|
288
303
|
const startHeartbeat = () => {
|
|
289
304
|
if (!(heartbeatMs > 0)) return;
|
|
@@ -301,11 +316,27 @@ export async function runDelegate({
|
|
|
301
316
|
heartbeat.unref?.();
|
|
302
317
|
};
|
|
303
318
|
|
|
319
|
+
// session/load replays the previous turn as ordinary session/update frames, including
|
|
320
|
+
// tool_call and diff blocks. The reset before the prompt clears the state they touch,
|
|
321
|
+
// but it cannot unsend the progress notifications they already emitted — a resume
|
|
322
|
+
// otherwise reports the previous turn's tool calls and edits as if they were happening.
|
|
304
323
|
client.on("update", (u) => {
|
|
324
|
+
if (!promptInFlight) return;
|
|
305
325
|
const up = u?.update || {};
|
|
306
326
|
if (up.sessionUpdate === "plan") {
|
|
307
327
|
planEntries = up.entries || [];
|
|
308
328
|
}
|
|
329
|
+
// A plan-mode run that switches itself to agent mode becomes write-capable while the
|
|
330
|
+
// caller still believes nothing can change. No drift has been observed; report it so
|
|
331
|
+
// we find out rather than assume.
|
|
332
|
+
if (up.sessionUpdate === "current_mode_update" && up.currentModeId && up.currentModeId !== mode) {
|
|
333
|
+
modeChanged = { from: mode, to: up.currentModeId };
|
|
334
|
+
}
|
|
335
|
+
// The agent names the turn a beat after the prompt lands ("File Creator"). Cheap
|
|
336
|
+
// label for a caller juggling several delegations.
|
|
337
|
+
if (up.sessionUpdate === "session_info_update" && typeof up.title === "string" && up.title) {
|
|
338
|
+
sessionTitle = up.title;
|
|
339
|
+
}
|
|
309
340
|
if (up.sessionUpdate === "agent_thought_chunk" && up.content?.text) {
|
|
310
341
|
thoughtProgress.push(up.content.text);
|
|
311
342
|
}
|
|
@@ -339,17 +370,26 @@ export async function runDelegate({
|
|
|
339
370
|
});
|
|
340
371
|
|
|
341
372
|
let sessionId;
|
|
373
|
+
let resumeError;
|
|
374
|
+
const unsupportedOptions = [];
|
|
342
375
|
try {
|
|
343
376
|
const res = await supervisor.supervise(async () => {
|
|
344
377
|
await client.start();
|
|
345
378
|
await client.initialize();
|
|
346
379
|
const sess = await openSession(client, resumeSessionId, workspace);
|
|
347
380
|
sessionId = sess.sessionId;
|
|
381
|
+
resumeError = sess.resumeError;
|
|
348
382
|
supervisor.setSessionId(sessionId);
|
|
349
383
|
onSessionReady?.(sessionId, client);
|
|
350
384
|
assertKnownModel(client, model);
|
|
351
385
|
await client.setModel(sessionId, model);
|
|
352
|
-
|
|
386
|
+
// fast is always sent: a resumed session may already have it on, so false is a real
|
|
387
|
+
// instruction. reasoning and context are only sent when the caller named one.
|
|
388
|
+
if (await applyConfig(client, sessionId, "fast", fast) && fast) unsupportedOptions.push("fast");
|
|
389
|
+
for (const [id, value] of [["reasoning", reasoning], ["context", context]]) {
|
|
390
|
+
if (value === undefined) continue;
|
|
391
|
+
if (await applyConfig(client, sessionId, id, value)) unsupportedOptions.push(id);
|
|
392
|
+
}
|
|
353
393
|
await client.setMode(sessionId, mode);
|
|
354
394
|
resetResult();
|
|
355
395
|
sawToolCall = false;
|
|
@@ -364,8 +404,11 @@ export async function runDelegate({
|
|
|
364
404
|
sawTodoFrame = false;
|
|
365
405
|
touched.clear();
|
|
366
406
|
lastToolLabel = null;
|
|
407
|
+
modeChanged = undefined;
|
|
408
|
+
sessionTitle = undefined;
|
|
367
409
|
supervisor.promptStarted();
|
|
368
410
|
startHeartbeat();
|
|
411
|
+
promptInFlight = true;
|
|
369
412
|
return client.prompt(sessionId, resolveSpec(spec));
|
|
370
413
|
});
|
|
371
414
|
thoughtProgress.end();
|
|
@@ -392,15 +435,25 @@ export async function runDelegate({
|
|
|
392
435
|
questionsAsked,
|
|
393
436
|
resumed: !!resumeSessionId && sessionId === resumeSessionId,
|
|
394
437
|
};
|
|
438
|
+
if (sessionTitle) out.sessionTitle = sessionTitle;
|
|
439
|
+
if (resumeError) protocolWarnings.push(`resuming ${resumeSessionId} failed, started a fresh session: ${resumeError}`);
|
|
440
|
+
for (const id of unsupportedOptions) protocolWarnings.push(`model ${model} has no ${id} option; the requested value was ignored`);
|
|
395
441
|
if (planEntries.length > 0 || planOverview !== undefined || planDetail !== undefined) {
|
|
396
442
|
out.plan = sanitizePlan(protocolWarnings);
|
|
397
443
|
}
|
|
398
444
|
// Most successful turns emit no todos at all, so an empty list would read as "nothing
|
|
399
445
|
// done" rather than "not tracked". Report only what the agent actually sent.
|
|
400
446
|
if (sawTodoFrame) Object.assign(out, sanitizeTodos(protocolWarnings));
|
|
447
|
+
if (modeChanged) {
|
|
448
|
+
out.modeChanged = modeChanged;
|
|
449
|
+
protocolWarnings.push(`agent switched mode from ${modeChanged.from} to ${modeChanged.to} mid-session`);
|
|
450
|
+
}
|
|
401
451
|
if (protocolWarnings.length) out.protocolWarnings = protocolWarnings;
|
|
402
452
|
return out;
|
|
403
453
|
} catch (err) {
|
|
454
|
+
// A JSON-RPC code means the agent rejected something rather than the bridge breaking.
|
|
455
|
+
// Classifying it here is what lets the caller tell "fix your arguments" from "retry".
|
|
456
|
+
if (!err?.reason && typeof err?.code === "number") err.reason = "agent-error";
|
|
404
457
|
// A bare duration says nothing about whether the agent wedged or a command was just
|
|
405
458
|
// slow. Name what was outstanding so the caller can tell the two apart, and so a
|
|
406
459
|
// retry can resume from the work already done instead of restarting blind.
|
|
@@ -408,6 +461,7 @@ export async function runDelegate({
|
|
|
408
461
|
if (isTimeout || err?.reason === "aborted" || err?.reason === "agent-exit") {
|
|
409
462
|
const age = fmtDuration(supervisor.msSinceActivity());
|
|
410
463
|
err.message += `\n\nLast ACP frame ${age} ago${lastToolLabel ? `; last tool call: ${lastToolLabel}` : ""}.`;
|
|
464
|
+
if (sessionTitle) err.message += ` The agent titled this turn ${JSON.stringify(sessionTitle)}.`;
|
|
411
465
|
if (sawTodoFrame) {
|
|
412
466
|
const { todoProgress } = sanitizeTodos([]);
|
|
413
467
|
const current = todoLabel();
|
|
@@ -416,6 +470,12 @@ export async function runDelegate({
|
|
|
416
470
|
}
|
|
417
471
|
const files = normalizeAgentReportedFiles([...touched], workspace);
|
|
418
472
|
if (files.length) err.message += ` Files reported edited: ${files.join(", ")}.`;
|
|
473
|
+
// Without this the resume hint below reads as "carry on from where you were", when the
|
|
474
|
+
// requested session was never loaded and this turn started from nothing.
|
|
475
|
+
if (resumeError) {
|
|
476
|
+
err.message += ` Note: resuming ${resumeSessionId} had already failed (${resumeError}),`
|
|
477
|
+
+ ` so this ran as a fresh session and none of that earlier work was in context.`;
|
|
478
|
+
}
|
|
419
479
|
if (sessionId) err.message += ` Resume with resumeSessionId ${sessionId}.`;
|
|
420
480
|
if (isTimeout) {
|
|
421
481
|
err.message += " cursor-agent does not stream shell output over ACP, so a long-running command emits"
|
package/src/jsonrpc.js
CHANGED
|
@@ -44,7 +44,13 @@ export class JsonRpcPeer {
|
|
|
44
44
|
const p = this.pending.get(msg.id);
|
|
45
45
|
if (p) {
|
|
46
46
|
this.pending.delete(msg.id);
|
|
47
|
-
|
|
47
|
+
// cursor-agent puts the actual reason in error.data.message; error.message alone
|
|
48
|
+
// is a bare "Invalid params".
|
|
49
|
+
if (msg.error) {
|
|
50
|
+
const err = new Error([msg.error.message || "rpc error", msg.error.data?.message].filter(Boolean).join(": "));
|
|
51
|
+
err.code = msg.error.code;
|
|
52
|
+
p.reject(err);
|
|
53
|
+
}
|
|
48
54
|
else p.resolve(msg.result);
|
|
49
55
|
}
|
|
50
56
|
} else if (msg.method && hasId) {
|
package/src/server.js
CHANGED
|
@@ -36,6 +36,9 @@ const delegateOutputSchema = z.object({
|
|
|
36
36
|
),
|
|
37
37
|
questionsAsked: z.array(z.string()).optional(),
|
|
38
38
|
resumed: z.boolean().optional(),
|
|
39
|
+
sessionTitle: z.string().optional().describe(
|
|
40
|
+
"Short title the agent gave this turn. Present on most turns; purely a label."
|
|
41
|
+
),
|
|
39
42
|
autoAnswered: z.array(z.object({ prompt: z.string(), chosen: z.string() })).optional(),
|
|
40
43
|
fallbackAnswers: z.array(z.object({ prompt: z.string(), given: z.string(), chosen: z.string() })).optional(),
|
|
41
44
|
cancelRequested: z.boolean().optional(),
|
|
@@ -52,6 +55,9 @@ const delegateOutputSchema = z.object({
|
|
|
52
55
|
})).optional().describe(
|
|
53
56
|
"Todo list the agent tracked for this turn. Absent when the agent tracked none, which is common on short tasks and does not imply incompleteness."
|
|
54
57
|
),
|
|
58
|
+
modeChanged: z.object({ from: z.string(), to: z.string() }).optional().describe(
|
|
59
|
+
"Set when the agent switched itself out of the requested mode mid-session."
|
|
60
|
+
),
|
|
55
61
|
todoProgress: z.object({
|
|
56
62
|
total: z.number(),
|
|
57
63
|
completed: z.number(),
|
|
@@ -86,10 +92,29 @@ export const delegateInputSchema = z.object({
|
|
|
86
92
|
workspace: z.string().optional().describe("Working directory for the agent (defaults to cwd)"),
|
|
87
93
|
model: z.string().trim().min(1, "model must be a non-empty string").default(DEFAULT_MODEL),
|
|
88
94
|
fast: z.boolean().default(false).describe("Fast speed tier — higher cost; enable only when the user asks"),
|
|
95
|
+
// Which options a model offers, and their valid values, are only knowable by asking the
|
|
96
|
+
// agent, so these stay open strings and the agent rejects what it does not accept.
|
|
97
|
+
reasoning: z.string().trim().min(1).optional().describe("Reasoning effort. Not offered by every model; gpt-5.x accepts none, low, medium, high, extra-high."),
|
|
98
|
+
context: z.string().trim().min(1).optional().describe("Context window size. Not offered by every model; gpt-5.x accepts 272k and 1m."),
|
|
89
99
|
});
|
|
90
100
|
|
|
101
|
+
// A cursor/ask_question question is multi-select when it sets allowMultiple, and the answer
|
|
102
|
+
// field (selectedOptionIds) is a list either way. Only split on commas for those, so a
|
|
103
|
+
// single-select label that itself contains a comma still matches whole.
|
|
104
|
+
function matchOptions(choice, opts = [], allowMultiple = false) {
|
|
105
|
+
const norm = (s) => String(s ?? "").trim().toLowerCase();
|
|
106
|
+
const ids = [];
|
|
107
|
+
for (const part of allowMultiple ? String(choice).split(",") : [String(choice)]) {
|
|
108
|
+
const want = norm(part);
|
|
109
|
+
if (!want) continue;
|
|
110
|
+
const hit = opts.find((o) => norm(o.id) === want || norm(o.label) === want);
|
|
111
|
+
if (hit && !ids.includes(hit.id)) ids.push(hit.id);
|
|
112
|
+
}
|
|
113
|
+
return ids;
|
|
114
|
+
}
|
|
115
|
+
|
|
91
116
|
export async function runDelegateTool({ args, extra, server, runDelegate, inFlight }) {
|
|
92
|
-
const { spec, mode, resumeSessionId, workspace, model, fast } = args;
|
|
117
|
+
const { spec, mode, resumeSessionId, workspace, model, fast, reasoning, context } = args;
|
|
93
118
|
|
|
94
119
|
const progressToken = extra?._meta?.progressToken;
|
|
95
120
|
let onProgress = () => {};
|
|
@@ -114,12 +139,15 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
|
|
|
114
139
|
for (const q of questions || []) {
|
|
115
140
|
const opts = q.options || [];
|
|
116
141
|
if (!supportsElicitation) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
142
|
+
// There is no user to ask, so allowMultiple changes nothing here: selecting every
|
|
143
|
+
// option would consent to more than the caller asked for. Take the first and
|
|
144
|
+
// disclose it. An option-less question answers empty rather than [null].
|
|
145
|
+
const first = opts[0];
|
|
146
|
+
autoAnswered.push({ prompt: String(q.prompt ?? ""), chosen: String(first?.label || first?.id || "") });
|
|
147
|
+
answers.push({ questionId: q.id, selectedOptionIds: first?.id ? [first.id] : [] });
|
|
121
148
|
continue;
|
|
122
149
|
}
|
|
150
|
+
const listing = opts.map((o) => o.label || o.id).join(", ");
|
|
123
151
|
const result = await server.server.elicitInput({
|
|
124
152
|
message: title ? `${title}: ${q.prompt}` : `cursor-agent asks: ${q.prompt}`,
|
|
125
153
|
requestedSchema: {
|
|
@@ -127,7 +155,11 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
|
|
|
127
155
|
properties: {
|
|
128
156
|
choice: {
|
|
129
157
|
type: "string",
|
|
130
|
-
description: opts.length
|
|
158
|
+
description: !opts.length
|
|
159
|
+
? "your answer"
|
|
160
|
+
: q.allowMultiple
|
|
161
|
+
? `Options (pick one or more, comma-separated): ${listing}`
|
|
162
|
+
: `Options: ${listing}`,
|
|
131
163
|
},
|
|
132
164
|
},
|
|
133
165
|
required: ["choice"],
|
|
@@ -135,23 +167,16 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
|
|
|
135
167
|
});
|
|
136
168
|
if (result.action !== "accept") return null;
|
|
137
169
|
const choice = result.content?.choice || "";
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
matched = true;
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (!matched && opts.length) {
|
|
151
|
-
const chosen = opts.find((o) => o.id === chosenOptionId)?.label || chosenOptionId || "";
|
|
152
|
-
fallbackAnswers.push({ prompt: String(q.prompt ?? ""), given: String(choice), chosen: String(chosen) });
|
|
170
|
+
const selectedOptionIds = matchOptions(choice, opts, q.allowMultiple);
|
|
171
|
+
if (!selectedOptionIds.length && opts.length) {
|
|
172
|
+
selectedOptionIds.push(opts[0].id);
|
|
173
|
+
fallbackAnswers.push({
|
|
174
|
+
prompt: String(q.prompt ?? ""),
|
|
175
|
+
given: String(choice),
|
|
176
|
+
chosen: String(opts[0].label || opts[0].id || ""),
|
|
177
|
+
});
|
|
153
178
|
}
|
|
154
|
-
answers.push({ questionId: q.id, selectedOptionIds
|
|
179
|
+
answers.push({ questionId: q.id, selectedOptionIds });
|
|
155
180
|
}
|
|
156
181
|
return { answers };
|
|
157
182
|
};
|
|
@@ -166,6 +191,8 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
|
|
|
166
191
|
workspace: workspace || process.cwd(),
|
|
167
192
|
model,
|
|
168
193
|
fast,
|
|
194
|
+
reasoning,
|
|
195
|
+
context,
|
|
169
196
|
onElicit,
|
|
170
197
|
onProgress,
|
|
171
198
|
signal: extra?.signal,
|
|
@@ -184,7 +211,9 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
|
|
|
184
211
|
};
|
|
185
212
|
} catch (err) {
|
|
186
213
|
return {
|
|
187
|
-
|
|
214
|
+
// The reason is already decided upstream; naming it here saves the caller parsing
|
|
215
|
+
// prose to tell a timeout from a rejected argument.
|
|
216
|
+
content: [{ type: "text", text: `delegate failed${err?.reason ? ` [${err.reason}]` : ""}: ` + (err?.message || String(err)) }],
|
|
188
217
|
isError: true,
|
|
189
218
|
};
|
|
190
219
|
} finally {
|