cursor-delegate-mcp 1.6.0 → 1.7.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cursor-delegate-mcp",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "type": "module",
5
5
  "description": "MCP server bridging AI coding hosts to cursor-agent over the Agent Client Protocol (ACP).",
6
6
  "license": "MIT",
package/src/delegate.js CHANGED
@@ -36,7 +36,13 @@ export async function runDelegate({
36
36
  model = "composer-2.5", fast = false, clientFactory, onElicit,
37
37
  idleMs = 90000, hardCapMs, timeoutMs, cancelGraceMs = 10000, killGraceMs = 5000,
38
38
  onSessionReady, onProgress, progressThrottleMs = 2000, gitChangedSet = gitChangedSetReal,
39
+ signal,
39
40
  } = {}) {
41
+ if (signal?.aborted) {
42
+ const err = new Error("delegation aborted by MCP host");
43
+ err.reason = "aborted";
44
+ throw err;
45
+ }
40
46
  const capMs = hardCapMs ?? timeoutMs ?? 3600000;
41
47
  const MAX_OUTPUT = 10 * 1024 * 1024;
42
48
  const TRUNCATION_MARKER = "\n\n[output truncated at 10MB]";
@@ -97,6 +103,8 @@ export async function runDelegate({
97
103
  const make = clientFactory || ((opts) => new AcpClient(opts));
98
104
  const client = make({ onElicit: wrappedElicit, mode, onCreatePlan: recordCreatePlan });
99
105
  const supervisor = new SessionSupervisor(client, { idleMs, hardCapMs: capMs, cancelGraceMs, killGraceMs });
106
+ const onAbort = () => supervisor.abort();
107
+ signal?.addEventListener("abort", onAbort, { once: true });
100
108
 
101
109
  const resultChunks = [];
102
110
  let resultLength = 0;
@@ -282,6 +290,7 @@ export async function runDelegate({
282
290
  } catch {}
283
291
  throw err;
284
292
  } finally {
293
+ signal?.removeEventListener("abort", onAbort);
285
294
  try { supervisor.finish(); } catch {}
286
295
  client.stop();
287
296
  }
package/src/server.js CHANGED
@@ -37,6 +37,7 @@ const delegateOutputSchema = z.object({
37
37
  resumed: z.boolean().optional(),
38
38
  autoAnswered: z.array(z.object({ prompt: z.string(), chosen: z.string() })).optional(),
39
39
  fallbackAnswers: z.array(z.object({ prompt: z.string(), given: z.string(), chosen: z.string() })).optional(),
40
+ cancelRequested: z.boolean().optional(),
40
41
  protocolWarnings: z.array(z.string()).optional(),
41
42
  plan: z.object({
42
43
  entries: z.array(planEntrySchema),
@@ -133,6 +134,7 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
133
134
  };
134
135
 
135
136
  let capturedSessionId;
137
+ let handle;
136
138
  try {
137
139
  const out = await runDelegate({
138
140
  spec,
@@ -143,13 +145,16 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
143
145
  fast,
144
146
  onElicit,
145
147
  onProgress,
148
+ signal: extra?.signal,
146
149
  onSessionReady: (sessionId, client) => {
147
150
  capturedSessionId = sessionId;
148
- inFlight.set(sessionId, client);
151
+ handle = { client, cancelRequested: false };
152
+ inFlight.set(sessionId, handle);
149
153
  },
150
154
  });
151
155
  if (autoAnswered.length) out.autoAnswered = autoAnswered;
152
156
  if (fallbackAnswers.length) out.fallbackAnswers = fallbackAnswers;
157
+ if (handle?.cancelRequested) out.cancelRequested = true;
153
158
  return {
154
159
  content: [{ type: "text", text: JSON.stringify(out, null, 2) }],
155
160
  structuredContent: out,
@@ -164,7 +169,7 @@ export async function runDelegateTool({ args, extra, server, runDelegate, inFlig
164
169
  }
165
170
  }
166
171
 
167
- export function buildServer({ runDelegate: runDelegateInjected, runDoctor: runDoctorInjected } = {}) {
172
+ export function buildServer({ runDelegate: runDelegateInjected, runDoctor: runDoctorInjected, forceGraceMs = 5000 } = {}) {
168
173
  const runDelegate = runDelegateInjected || runDelegateDefault;
169
174
  const runDoctor = runDoctorInjected || runDoctorDefault;
170
175
  const server = new McpServer(
@@ -178,11 +183,11 @@ export function buildServer({ runDelegate: runDelegateInjected, runDoctor: runDo
178
183
  description:
179
184
  "Delegate a coding task to cursor-agent over ACP. Never shell out to cursor-agent — use this tool only. Pass structured task text inline in spec (default); a file path is optional when the user wants a persisted brief. Defaults: mode=agent, model=composer-2.5, fast=false. Plan workflow: mode=plan, then resume with mode=agent and resumeSessionId. Auto-approves writes in workspace; uses MCP elicitation for clarifying questions and selects the first option when the client lacks elicitation. Returns the final result, selection source, stop reason, session ID, changed files, and optional plan. See the delegate skill for orchestration.",
180
185
  inputSchema: {
181
- spec: z.string().describe("Inline task brief (default): goal, scope, acceptance criteria. Optional file path if the user wants a persisted spec."),
186
+ spec: z.string().describe("Inline task brief (default): goal, scope, decisions already made (constraints and fixed choices — quote the user's exact values verbatim), acceptance criteria. Point at files to read or mimic rather than pasting code. Optional file path if the user wants a persisted spec."),
182
187
  mode: z.enum(["agent", "plan", "ask"]).default("agent"),
183
188
  resumeSessionId: z.string().optional().describe("Resume an existing ACP session instead of a new one"),
184
189
  workspace: z.string().optional().describe("Working directory for the agent (defaults to cwd)"),
185
- model: z.string().default("composer-2.5"),
190
+ model: z.string().trim().min(1, "model must be a non-empty string").default("composer-2.5"),
186
191
  fast: z.boolean().default(false).describe("Fast speed tier — higher cost; enable only when the user asks"),
187
192
  },
188
193
  outputSchema: delegateOutputSchema,
@@ -197,13 +202,16 @@ export function buildServer({ runDelegate: runDelegateInjected, runDoctor: runDo
197
202
  async (args, extra) => runDelegateTool({ args, extra, server, runDelegate, inFlight })
198
203
  );
199
204
 
200
- // cancel is best-effort: MCP tool calls are serialized, so delegate must finish first.
201
205
  server.registerTool(
202
206
  "cancel",
203
207
  {
204
- description: "Cancel an in-flight ACP delegation by sessionId.",
205
- inputSchema: { sessionId: z.string() },
206
- outputSchema: z.object({ status: z.enum(["cancelled", "not-found"]), sessionId: z.string() }),
208
+ description:
209
+ "Best-effort cancel of an in-flight ACP delegation by sessionId. Sends session/cancel; the agent may finish the turn anyway. The delegate result carries cancelRequested: true when the turn was cancelled mid-run. MCP hosts that serialize tool calls cannot run this while delegate is in flight. With force: true, the agent process is killed if the turn is still running after a short grace period.",
210
+ inputSchema: {
211
+ sessionId: z.string(),
212
+ force: z.boolean().default(false).describe("After the cancel notify, wait a short grace period and kill the agent process if the delegation is still running"),
213
+ },
214
+ outputSchema: z.object({ status: z.enum(["cancelled", "killed", "not-found"]), sessionId: z.string() }),
207
215
  annotations: {
208
216
  title: "Cancel Cursor delegation",
209
217
  readOnlyHint: false,
@@ -212,19 +220,35 @@ export function buildServer({ runDelegate: runDelegateInjected, runDoctor: runDo
212
220
  openWorldHint: false,
213
221
  },
214
222
  },
215
- async ({ sessionId }) => {
216
- const client = inFlight.get(sessionId);
217
- if (client) {
218
- await client.cancel(sessionId).catch(() => {});
223
+ async ({ sessionId, force }) => {
224
+ const handle = inFlight.get(sessionId);
225
+ if (!handle) {
226
+ return {
227
+ content: [{ type: "text", text: `no in-flight session ${sessionId}` }],
228
+ structuredContent: { status: "not-found", sessionId },
229
+ };
230
+ }
231
+ handle.cancelRequested = true;
232
+ await handle.client.cancel(sessionId).catch(() => {});
233
+ if (!force) {
219
234
  inFlight.delete(sessionId);
220
235
  return {
221
236
  content: [{ type: "text", text: `cancelled ${sessionId}` }],
222
237
  structuredContent: { status: "cancelled", sessionId },
223
238
  };
224
239
  }
240
+ await new Promise((r) => setTimeout(r, forceGraceMs));
241
+ if (!inFlight.has(sessionId)) {
242
+ return {
243
+ content: [{ type: "text", text: `cancelled ${sessionId}` }],
244
+ structuredContent: { status: "cancelled", sessionId },
245
+ };
246
+ }
247
+ handle.client.stop();
248
+ inFlight.delete(sessionId);
225
249
  return {
226
- content: [{ type: "text", text: `no in-flight session ${sessionId}` }],
227
- structuredContent: { status: "not-found", sessionId },
250
+ content: [{ type: "text", text: `killed ${sessionId}` }],
251
+ structuredContent: { status: "killed", sessionId },
228
252
  };
229
253
  }
230
254
  );
@@ -70,12 +70,16 @@ export class SessionSupervisor {
70
70
  }, this.idleMs);
71
71
  }
72
72
 
73
+ abort() {
74
+ this._trip("aborted", "delegation aborted by MCP host");
75
+ }
76
+
73
77
  _trip(reason, message) {
74
78
  if (this._settled) return;
75
79
  this._settled = true;
76
80
  this.disarm();
77
81
  const err = makeError(reason, message);
78
- if (reason === "idle-timeout" || reason === "hard-cap") {
82
+ if (reason === "idle-timeout" || reason === "hard-cap" || reason === "aborted") {
79
83
  if (!this._escalation) this._escalation = this._runEscalation();
80
84
  }
81
85
  this._guardReject?.(err);