@2kw/ai-mcp-server 6.3.0-dev.8 → 6.3.0-dev.89

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.
@@ -0,0 +1,260 @@
1
+ /**
2
+ * The run envelope of `bb agents run --json` (spec 2026-09-14-cli-agents-design.md §4.3), copied
3
+ * from `cli/src/lib/agent-run.ts` because cli/ and mcp/ share no package (#667). Keep the two in step;
4
+ * only the text of `next` differs, because an MCP caller decides through a tool, not a shell command.
5
+ */
6
+ /** Fallback chat.2kw.ai origin when the API host is not one we recognise. */
7
+ export const DEFAULT_CHAT_URL = "https://chat.2kw.ai";
8
+ /** Known API-host → chat web host mappings, as the CLI's `CHAT_URL_BY_API_HOST` (#807 R11). */
9
+ const CHAT_URL_BY_API_HOST = {
10
+ "api.2kw.ai": "https://chat.2kw.ai",
11
+ "api-dev.2kw.ai": "https://chat-dev.2kw.ai",
12
+ "backbone.manfred-kunze.dev": "https://chat.2kw.ai",
13
+ "localhost:8080": "http://localhost:3000",
14
+ "127.0.0.1:8080": "http://localhost:3000",
15
+ };
16
+ /**
17
+ * The chat web host that belongs to the server's API base URL (`AI_2KW_BASE_URL`), where a member
18
+ * connects a connector. `AI_2KW_CHAT_URL` overrides the mapping; a trailing slash is dropped.
19
+ */
20
+ export function chatUrlFor(baseUrl, env = process.env) {
21
+ const override = env.AI_2KW_CHAT_URL?.trim();
22
+ if (override)
23
+ return override.replace(/\/+$/, "");
24
+ try {
25
+ const host = new URL(baseUrl ?? "").host;
26
+ // hasOwn: a host named after an Object.prototype key must not resolve.
27
+ return Object.hasOwn(CHAT_URL_BY_API_HOST, host) ? CHAT_URL_BY_API_HOST[host] : DEFAULT_CHAT_URL;
28
+ }
29
+ catch {
30
+ return DEFAULT_CHAT_URL;
31
+ }
32
+ }
33
+ export const CONVERSATION_MODES = ["plan", "ask", "auto"];
34
+ /**
35
+ * Who may change the mode (#656 D8), stated in both tool descriptions: the mode outlives the request
36
+ * that sets it, so a model that switched on its own would lift the user's choice for every later turn.
37
+ */
38
+ export const MODE_RULE = "Set `mode` only when the user explicitly asks for that mode. Never set it to retry a refused approval " +
39
+ "or to leave plan mode on your own: ask the user first. plan = read-only; ask = no automatic approver, " +
40
+ "calls that need approval wait for a person; auto = the operator's policy as written. Leave it out to " +
41
+ "keep the conversation's mode.";
42
+ function isConversationMode(value) {
43
+ return typeof value === "string" && CONVERSATION_MODES.includes(value);
44
+ }
45
+ /** The `backbone:mode` input item (S1 D7); always appended last. */
46
+ export function modeItem(mode) {
47
+ return { type: "backbone:mode", mode };
48
+ }
49
+ /**
50
+ * The mode the response ran under (`conversation_mode`, #656). An absent key (a server before #656)
51
+ * and any value other than the three read as null, which also means "none set".
52
+ */
53
+ export function responseMode(result) {
54
+ const raw = result?.conversation_mode;
55
+ return isConversationMode(raw) ? raw : null;
56
+ }
57
+ /** The assistant's text: every `output_text` part of every `message` item, in order. */
58
+ export function extractResponseText(result) {
59
+ const parts = [];
60
+ for (const item of result?.output ?? []) {
61
+ if (item.type !== "message")
62
+ continue;
63
+ for (const part of item.content ?? []) {
64
+ if (part.type === "output_text" && part.text)
65
+ parts.push(part.text);
66
+ }
67
+ }
68
+ return parts.join("\n");
69
+ }
70
+ /**
71
+ * The response echoes `model` as `agent/{name}@{versionNumber}` (all digits), optionally
72
+ * followed by a `#<model>` override. Only a trailing all-digit `@N` is read as a version;
73
+ * any other `@` suffix stays part of the name.
74
+ */
75
+ export function parseAgentModel(model) {
76
+ // Strip the `#<model>` override (from the last `#`, as the server splits it) before matching.
77
+ const hash = (model ?? "").lastIndexOf("#");
78
+ const ref = hash >= 0 ? (model ?? "").slice(0, hash) : model ?? "";
79
+ const withVersion = /^agent\/(.+)@(\d+)$/.exec(ref);
80
+ if (withVersion)
81
+ return { agent: withVersion[1], version: Number(withVersion[2]) };
82
+ const bare = /^agent\/(.+)$/.exec(ref);
83
+ if (bare)
84
+ return { agent: bare[1], version: null };
85
+ return { agent: null, version: null };
86
+ }
87
+ function parseArguments(raw) {
88
+ if (typeof raw !== "string")
89
+ return raw ?? {};
90
+ try {
91
+ return JSON.parse(raw);
92
+ }
93
+ catch {
94
+ return raw;
95
+ }
96
+ }
97
+ /** An open connector consent request; a continuation's projection carries another status. */
98
+ function isOpenConnectRequest(item) {
99
+ return item.type === "backbone:connector_auth_request" && (item.status === undefined || item.status === "in_progress");
100
+ }
101
+ function pendingConnectionsOf(output) {
102
+ return output.filter(isOpenConnectRequest).map((i) => ({
103
+ serverLabel: String(i.server_label),
104
+ host: String(i.host),
105
+ reason: String(i.reason),
106
+ ...(Array.isArray(i.destinations) && i.destinations.length > 0
107
+ ? { destinations: i.destinations.map((d) => String(d)) }
108
+ : {}),
109
+ }));
110
+ }
111
+ /**
112
+ * The call ids a connect pause withholds from the caller: the call each request names, and every
113
+ * `mcp__<label>__connect` call for a pending connector (one request stands for all of them).
114
+ */
115
+ function connectCallIds(output, connections) {
116
+ const connectNames = new Set(connections.map((c) => `mcp__${c.serverLabel}__connect`));
117
+ const ids = new Set();
118
+ for (const item of output) {
119
+ if (isOpenConnectRequest(item) || (item.type === "function_call" && connectNames.has(String(item.name)))) {
120
+ ids.add(String(item.call_id));
121
+ }
122
+ }
123
+ return ids;
124
+ }
125
+ function connectionPhrase(c) {
126
+ const target = `${c.serverLabel} (${c.host})`;
127
+ if (c.reason === "allow")
128
+ return `allow the agent to use ${target}`;
129
+ if (c.reason === "reconnect")
130
+ return `reconnect ${target}`;
131
+ return `connect ${target}`;
132
+ }
133
+ function connectionsPhrase(connections) {
134
+ const phrases = connections.map(connectionPhrase);
135
+ return phrases.length > 1 ? `${phrases.slice(0, -1).join(", ")} and ${phrases.at(-1)}` : phrases[0] ?? "";
136
+ }
137
+ /**
138
+ * Output item types the envelope decodes, or that never hold a run: text, reasoning and finished
139
+ * connector calls. A pause on anything else (a connector approval's `mcp_approval_request`) is named.
140
+ */
141
+ const DECODED_ITEM_TYPES = new Set([
142
+ "message",
143
+ "reasoning",
144
+ "function_call",
145
+ "function_call_output",
146
+ "backbone:approval_request",
147
+ "backbone:connector_auth_request",
148
+ "mcp_call",
149
+ "mcp_list_tools",
150
+ ]);
151
+ function undecodedPause(output) {
152
+ const types = [...new Set(output.map((i) => String(i.type)).filter((t) => !DECODED_ITEM_TYPES.has(t)))];
153
+ return types.length > 0
154
+ ? `This server cannot show or answer ${types.join(", ")}.`
155
+ : "This server cannot tell what the run waits for.";
156
+ }
157
+ /**
158
+ * @param agentRef the reference the run was started with (`name[@label][#model]`); `next` names it so
159
+ * the decision continues on the same label and model (D11). Falls back to the echoed agent name.
160
+ * @param chatUrl the chat web host of the API ({@link chatUrlFor}); a connect pause's `next` names its
161
+ * Connectors page.
162
+ */
163
+ export function buildRunEnvelope(result, agentRef, chatUrl = DEFAULT_CHAT_URL) {
164
+ const output = result?.output ?? [];
165
+ const { agent, version } = parseAgentModel(result?.model);
166
+ // call_id → status of its tool output; a failed server-side tool run is marked `incomplete`.
167
+ const outputStatuses = new Map(output
168
+ .filter((i) => i.type === "function_call_output")
169
+ .map((i) => [
170
+ String(i.call_id),
171
+ i.status === "incomplete" ? "incomplete" : "completed",
172
+ ]));
173
+ const pendingApprovals = output
174
+ // Only an open request is pending; judge- or grant-decided requests (#634, #629) carry another status.
175
+ .filter((i) => i.type === "backbone:approval_request" && (i.status === undefined || i.status === "in_progress"))
176
+ .map((i) => ({
177
+ approvalId: String(i.id),
178
+ callId: String(i.call_id),
179
+ tool: String(i.tool),
180
+ arguments: parseArguments(i.arguments),
181
+ policyClass: String(i.policy_class),
182
+ reason: typeof i.reason === "string" && i.reason ? i.reason : null,
183
+ }));
184
+ const pendingConnections = pendingConnectionsOf(output);
185
+ const withheldCallIds = new Set([...pendingApprovals.map((a) => a.callId), ...connectCallIds(output, pendingConnections)]);
186
+ const toolCalls = [];
187
+ const pendingToolCalls = [];
188
+ for (const item of output) {
189
+ if (item.type !== "function_call")
190
+ continue;
191
+ const callId = String(item.call_id);
192
+ const outputStatus = outputStatuses.get(callId);
193
+ if (outputStatus) {
194
+ toolCalls.push({ tool: String(item.name), callId, status: outputStatus });
195
+ }
196
+ else if (!withheldCallIds.has(callId)) {
197
+ pendingToolCalls.push({ tool: String(item.name), callId, arguments: parseArguments(item.arguments) });
198
+ }
199
+ }
200
+ let status;
201
+ switch (result?.status) {
202
+ case "completed":
203
+ status = "completed";
204
+ break;
205
+ case "incomplete":
206
+ status = "incomplete";
207
+ break;
208
+ case "requires_action":
209
+ // A connect pause is requires_tool_output too (#807 R12): no tool here can answer it.
210
+ status =
211
+ pendingToolCalls.length > 0 || pendingConnections.length > 0 || pendingApprovals.length === 0
212
+ ? "requires_tool_output"
213
+ : "requires_approval";
214
+ break;
215
+ default:
216
+ throw new Error(`Unexpected response status: ${result?.status}`);
217
+ }
218
+ const responseId = result?.id ?? null;
219
+ const ref = agentRef ?? agent;
220
+ let next = null;
221
+ if (status === "requires_approval" && ref && responseId) {
222
+ // Never an "approve all" hint: the assistant decides only what the user decided (D12).
223
+ next = `Show the pending approvals to the user; after they decide each one, call 2kw_decide_agent_approvals with agent ${JSON.stringify(ref)} and responseId ${JSON.stringify(responseId)}.`;
224
+ }
225
+ else if (status === "requires_tool_output" && pendingConnections.length > 0 && pendingToolCalls.length === 0 && responseId) {
226
+ // No tool here takes previous_response_id, and a connect pause cannot be continued by
227
+ // conversation (the gateway answers 400), so the honest ways on are a fresh request or a
228
+ // client that continues by response id.
229
+ next =
230
+ `Tell the user to ${connectionsPhrase(pendingConnections)} in ${chatUrl}/connectors. ` +
231
+ `This server cannot continue response ${JSON.stringify(responseId)}: once they have connected, send the request again ` +
232
+ "with 2kw_create_response without `conversation` (a connect pause cannot be continued by conversation), " +
233
+ "or continue the response by its id from n8n (Previous Response ID) or any client that sends previous_response_id.";
234
+ }
235
+ else if (status === "requires_tool_output" && pendingConnections.length === 0 && pendingToolCalls.length === 0 && responseId) {
236
+ // Paused on nothing the envelope decodes: say so, rather than hand back a pause with no way on.
237
+ next =
238
+ `${undecodedPause(output)} Response ${JSON.stringify(responseId)} stays paused: tell the user to answer it ` +
239
+ `in ${chatUrl}/, or continue it from a client that sends previous_response_id.`;
240
+ }
241
+ return {
242
+ status,
243
+ mode: responseMode(result),
244
+ agent,
245
+ version,
246
+ responseId,
247
+ conversationId: result?.conversation?.id ?? null,
248
+ text: extractResponseText(result),
249
+ toolCalls,
250
+ pendingApprovals,
251
+ pendingToolCalls,
252
+ pendingConnections,
253
+ incompleteReason: result?.incomplete_details?.reason ?? null,
254
+ usage: result?.usage
255
+ ? { inputTokens: result.usage.input_tokens ?? 0, outputTokens: result.usage.output_tokens ?? 0 }
256
+ : null,
257
+ next,
258
+ };
259
+ }
260
+ //# sourceMappingURL=agent-run.js.map
@@ -1,5 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { formatErrorForMcp } from "../errors.js";
3
+ import { continueWithDecisions, fetchPendingApprovals, planDecisions, resolveAgentId, splitAgentRef } from "../lib/agent-decide.js";
4
+ import { buildRunEnvelope, chatUrlFor, MODE_RULE } from "../lib/agent-run.js";
3
5
  const modelSchema = z
4
6
  .string()
5
7
  .min(1)
@@ -301,6 +303,30 @@ export function register(server, client) {
301
303
  };
302
304
  }
303
305
  });
306
+ // ── list_agent_skills ───────────────────────────────────────────────────
307
+ server.tool("2kw_list_agent_skills", "List the skills an agent's latest published version binds, in authored order. This is the "
308
+ + "read a chat-only USER key may perform; 2kw_get_latest_agent_version carries the full version "
309
+ + "and needs VIEWER or above. An agent with no published version lists none.", { agentId: z.string().describe("The agent ID") }, async ({ agentId }) => {
310
+ try {
311
+ const { data } = await client.GET("/v1/agents/{agentId}/skills", {
312
+ params: { path: { agentId } },
313
+ });
314
+ const lines = (data ?? []).map((s) => {
315
+ const plugin = s.pluginName ? `, plugin: ${s.pluginName}` : "";
316
+ const description = s.description ? ` — ${s.description}` : "";
317
+ return `- ${s.name} (v${s.versionNumber}, ref: ${s.ref}${plugin})${description}`;
318
+ });
319
+ return {
320
+ content: [{ type: "text", text: `Agent skills:\n${lines.join("\n") || "(none)"}` }],
321
+ };
322
+ }
323
+ catch (error) {
324
+ return {
325
+ content: [{ type: "text", text: formatErrorForMcp(error) }],
326
+ isError: true,
327
+ };
328
+ }
329
+ });
304
330
  // ── get_agent_version ───────────────────────────────────────────────────
305
331
  server.tool("2kw_get_agent_version", "Retrieve a specific version of an agent.", {
306
332
  agentId: z.string().describe("The agent ID"),
@@ -333,12 +359,16 @@ export function register(server, client) {
333
359
  .string()
334
360
  .optional()
335
361
  .describe("Resolve this installation's module tool catalog, the way a run of that installation would. Omit to answer for the agent's own tools only."),
336
- }, async ({ agentId, versionId, tool, installationId }) => {
362
+ mode: z
363
+ .enum(["plan", "ask", "auto"])
364
+ .optional()
365
+ .describe("Answer as a conversation in this mode would be gated: plan refuses every call that is not read-only, ask puts a person where the policy would ask the judge, auto is the policy as written. A mode that contributed is listed in matchedRules as conversation_mode.<mode>. Omit for no mode."),
366
+ }, async ({ agentId, versionId, tool, installationId, mode }) => {
337
367
  try {
338
368
  const { data } = await client.GET("/v1/agents/{agentId}/versions/{versionId}/policy", {
339
369
  params: {
340
370
  path: { agentId, versionId },
341
- query: { tool, installation: installationId },
371
+ query: { tool, installation: installationId, mode },
342
372
  },
343
373
  });
344
374
  return {
@@ -503,6 +533,51 @@ export function register(server, client) {
503
533
  };
504
534
  }
505
535
  });
536
+ // ── decide_agent_approvals ──────────────────────────────────────────────
537
+ server.tool("2kw_decide_agent_approvals", "Answer the tool approvals a paused agent run is waiting for (a 2kw_create_response envelope with status requires_approval), then continue the run. "
538
+ + "Decide only what the user decided: show them each pending approval (tool, arguments, policy class) first and never approve on your own. "
539
+ + "Every pending approval of the response must be decided in this one call. Pass `agent` exactly as the run was started (keep '@label' and '#model'). "
540
+ + "Returns the continuation's run envelope, which can pause again: for approval, or on a connector the user must connect "
541
+ + "in chat.2kw.ai → Connectors (`pendingConnections`; follow its `next`)."
542
+ + " " + MODE_RULE, {
543
+ agent: z.string().min(1).describe("Agent id or name as the run used it: 'ref[@label][#model]'"),
544
+ responseId: z.string().min(1).describe("The paused response's id (envelope `responseId`)"),
545
+ decisions: z
546
+ .array(z.object({
547
+ approvalId: z.string().min(1).describe("Envelope `pendingApprovals[].approvalId`"),
548
+ decision: z.enum(["approve", "reject"]),
549
+ reason: z.string().optional().describe("Stored with the decision; the model sees it on a reject"),
550
+ remember: z.boolean().optional().describe("Approve this tool for the rest of the conversation; not on destructive tools"),
551
+ }))
552
+ .optional()
553
+ .describe("One entry per pending approval. Mutually exclusive with `decideAll`."),
554
+ decideAll: z.enum(["approve", "reject"]).optional().describe("Decide every pending approval the same way"),
555
+ reason: z.string().optional().describe("Only with `decideAll`: reason stored on every decision"),
556
+ remember: z.boolean().optional().describe("Only with `decideAll`: remember every approval for the conversation"),
557
+ mode: z
558
+ .enum(["plan", "ask", "auto"])
559
+ .optional()
560
+ .describe("The conversation mode this continuation and later turns run under. Only on the user's explicit request."),
561
+ }, async ({ agent, responseId, decisions, decideAll, reason, remember, mode }) => {
562
+ try {
563
+ if (decisions !== undefined && (reason !== undefined || remember !== undefined)) {
564
+ throw new Error("With `decisions`, give `reason` and `remember` per entry.");
565
+ }
566
+ const { name, label, model } = splitAgentRef(agent);
567
+ const agentId = await resolveAgentId(client, name);
568
+ const pending = await fetchPendingApprovals(client, agentId, responseId);
569
+ const items = planDecisions(pending, { decisions, decideAll, reason, remember }, responseId);
570
+ const result = await continueWithDecisions(client, agentId, responseId, items, label, model, mode);
571
+ const envelope = buildRunEnvelope(result, agent, chatUrlFor(client._config?.baseUrl));
572
+ return { content: [{ type: "text", text: JSON.stringify(envelope, null, 2) }] };
573
+ }
574
+ catch (error) {
575
+ return {
576
+ content: [{ type: "text", text: formatErrorForMcp(error) }],
577
+ isError: true,
578
+ };
579
+ }
580
+ });
506
581
  // ── list_agent_tool_catalogs ─────────────────────────────────────────────
507
582
  server.tool("2kw_list_agent_tool_catalogs", "List an agent's tool-catalog sync history, newest first. Optionally narrow to one installation. The signed bytes and their signature are never returned.", {
508
583
  agentId: z.string().describe("The agent ID"),
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { formatErrorForMcp } from "../errors.js";
3
+ import { buildRunEnvelope, chatUrlFor, MODE_RULE, modeItem } from "../lib/agent-run.js";
3
4
  /**
4
5
  * The `model` value for an agent run: `agent/<ref>[@<label>]`, plus `#<model>` when the
5
6
  * request switches to another entry of the version's `models` list (#626, spec #591 §4.1).
@@ -92,7 +93,7 @@ export function register(server, client) {
92
93
  }
93
94
  });
94
95
  // ── create_response ─────────────────────────────────────────────────────
95
- server.tool("2kw_create_response", "Send a request through Backbone's OpenAI-compatible OpenResponses endpoint (POST /v1/responses). Model format: 'provider/model' for a direct gateway call via `model`, or use `agent` to invoke a stored agent by id or name (optionally 'ref@label', e.g. 'support-bot@latest'). With `agent`, `agentModel` runs this one request on another model from the agent version's `models` list. The reply ends with the model that answered. Always non-streaming.", {
96
+ server.tool("2kw_create_response", "Send a request through Backbone's OpenAI-compatible OpenResponses endpoint (POST /v1/responses). Model format: 'provider/model' for a direct gateway call via `model`, or use `agent` to invoke a stored agent by id or name (optionally 'ref@label', e.g. 'support-bot@latest'). With `agent`, `agentModel` runs this one request on another model from the agent version's `models` list. With `agent`, the reply is the run envelope as JSON (status, text, tool calls, pending approvals, response id, next step) followed by the model that answered; answer an approval pause with 2kw_decide_agent_approvals. A run that needs the user's own sign-in to a connector pauses with status requires_tool_output and `pendingConnections` (label, host, reason): no tool here can answer that; tell the user to connect it in chat.2kw.ai → Connectors, as the envelope's `next` says. Always non-streaming. " + MODE_RULE, {
96
97
  input: z.string().min(1).describe("The input text (sent as a single user message)"),
97
98
  agent: z
98
99
  .string()
@@ -108,7 +109,11 @@ export function register(server, client) {
108
109
  .optional()
109
110
  .describe("Model identifier in 'provider/model' format. Mutually exclusive with `agent`."),
110
111
  conversation: z.string().optional().describe("Conversation ID this response belongs to"),
111
- }, async ({ input, agent, agentModel, model, conversation }) => {
112
+ mode: z
113
+ .enum(["plan", "ask", "auto"])
114
+ .optional()
115
+ .describe("Only with `agent`: the conversation mode for this and later turns (plan, ask or auto). Only on the user's explicit request."),
116
+ }, async ({ input, agent, agentModel, model, conversation, mode }) => {
112
117
  try {
113
118
  if (agent && model) {
114
119
  throw new Error("Use either `agent` or `model`, not both.");
@@ -122,13 +127,16 @@ export function register(server, client) {
122
127
  if (agentModel !== undefined && agent?.includes("#")) {
123
128
  throw new Error("Give the model switch once: either 'ref#model' in `agent` or `agentModel`.");
124
129
  }
130
+ if (mode !== undefined && !agent) {
131
+ throw new Error("`mode` applies to agent runs; it needs `agent`.");
132
+ }
125
133
  const resolvedModel = agentModelReference(agent, agentModel) ?? model;
126
134
  const body = {
127
135
  model: resolvedModel,
128
- // The wire `input` field also accepts a bare string (shorthand for
129
- // a single user message; see ResponseItemInputDeserializer), which
130
- // the generated type does not model.
131
- input,
136
+ // Without `mode` the wire `input` is the bare string (shorthand for one user message; see
137
+ // ResponseItemInputDeserializer), which the generated type does not model. With it, the
138
+ // backbone:mode item follows the message (#656).
139
+ input: mode ? [{ type: "message", role: "user", content: input }, modeItem(mode)] : input,
132
140
  stream: false,
133
141
  ...(conversation !== undefined && { conversation }),
134
142
  };
@@ -137,6 +145,17 @@ export function register(server, client) {
137
145
  body: body,
138
146
  });
139
147
  const result = data;
148
+ if (agent) {
149
+ // The reference the run used, so the envelope's `next` keeps its label and model (D11).
150
+ const agentRef = agentModel !== undefined ? `${agent}#${agentModel}` : agent;
151
+ const envelope = buildRunEnvelope(result, agentRef, chatUrlFor(client._config?.baseUrl));
152
+ const content = [{ type: "text", text: JSON.stringify(envelope, null, 2) }];
153
+ // The echoed model is 'agent/<name>@<version>', plus '#<model>' when the request switched it (#591).
154
+ if (typeof result.model === "string") {
155
+ content.push({ type: "text", text: `[model: ${result.model}]` });
156
+ }
157
+ return { content };
158
+ }
140
159
  const parts = [];
141
160
  const output = result.output;
142
161
  for (const item of output ?? []) {
@@ -162,11 +181,6 @@ export function register(server, client) {
162
181
  if (parts.length === 0) {
163
182
  parts.push({ type: "text", text: JSON.stringify(result, null, 2) });
164
183
  }
165
- // The response echoes the model that answered: for an agent run
166
- // 'agent/<name>@<version>', plus '#<model>' when the request switched it (#591).
167
- if (agent && typeof result.model === "string") {
168
- parts.push({ type: "text", text: `[model: ${result.model}]` });
169
- }
170
184
  return { content: parts };
171
185
  }
172
186
  catch (error) {
@@ -134,6 +134,32 @@ export function register(server, client) {
134
134
  };
135
135
  }
136
136
  });
137
+ // ── cancel_conversation_turn ─────────────────────────────────────────────
138
+ server.tool("2kw_cancel_conversation_turn", "Ask a running turn of a conversation to stop. turnId is the value the client sent as the Backbone-Turn-Id header on the responses call. The stop is accepted, not confirmed: the turn's own response reports status cancelled, or completed when the stop arrived too late.", {
139
+ conversationId: z.string().describe("The conversation ID"),
140
+ turnId: z
141
+ .string()
142
+ .regex(/^[A-Za-z0-9_-]{1,64}$/)
143
+ .describe("The turn id from the Backbone-Turn-Id header: 1-64 characters of A-Z, a-z, 0-9, '_' or '-'"),
144
+ }, async ({ conversationId, turnId }) => {
145
+ try {
146
+ await client.POST("/v1/conversations/{conversationId}/cancel", {
147
+ params: { path: { conversationId } },
148
+ body: { turn_id: turnId },
149
+ });
150
+ return {
151
+ content: [
152
+ { type: "text", text: `Stop requested for turn ${turnId} of conversation ${conversationId}.` },
153
+ ],
154
+ };
155
+ }
156
+ catch (error) {
157
+ return {
158
+ content: [{ type: "text", text: formatErrorForMcp(error) }],
159
+ isError: true,
160
+ };
161
+ }
162
+ });
137
163
  // ── list_conversation_items ──────────────────────────────────────────────
138
164
  server.tool("2kw_list_conversation_items", "Fetch a conversation's items in replay order, oldest first.", { conversationId: z.string().describe("The conversation ID") }, async ({ conversationId }) => {
139
165
  try {
@@ -157,7 +157,7 @@ export function register(server, client) {
157
157
  server.tool("2kw_add_variant", "Add a variant to an experiment with a task type and configuration.", {
158
158
  experimentId: z.string().describe("The experiment ID"),
159
159
  name: z.string().min(1).describe("Variant name"),
160
- taskType: z.string().min(1).describe("Task type (e.g., EXTRACTION, CLASSIFICATION)"),
160
+ taskType: z.string().min(1).describe("Task type: extraction or agent"),
161
161
  configuration: z.unknown().describe("Variant configuration (JSON object)"),
162
162
  description: z.string().optional().describe("Variant description"),
163
163
  sortOrder: z.number().optional().describe("Sort order for display"),
@@ -214,7 +214,7 @@ export function register(server, client) {
214
214
  experimentId: z.string().describe("The experiment ID"),
215
215
  variantId: z.string().describe("The variant ID"),
216
216
  name: z.string().optional().describe("New variant name"),
217
- taskType: z.string().optional().describe("New task type"),
217
+ taskType: z.string().optional().describe("Task type; must equal the variant's current one (it cannot change)"),
218
218
  configuration: z.unknown().optional().describe("New variant configuration (JSON object)"),
219
219
  description: z.string().optional().describe("New variant description"),
220
220
  sortOrder: z.number().optional().describe("New sort order for display"),
@@ -0,0 +1,21 @@
1
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { ApiClient } from "../client.js";
3
+ export interface ToolDeps {
4
+ client: ApiClient;
5
+ baseUrl: string;
6
+ apiKey: string;
7
+ }
8
+ /**
9
+ * One tool module. `id` is its file name under src/tools; `title` and `summary` head its table on
10
+ * the public docs page, which `npm run docs:tools` generates from this list (#1148).
11
+ */
12
+ export interface ToolGroup {
13
+ id: string;
14
+ title: string;
15
+ summary: string;
16
+ register(server: McpServer, deps: ToolDeps): void;
17
+ }
18
+ /** Every tool module, in the order the docs page lists them. A new module must be added here. */
19
+ export declare const TOOL_GROUPS: readonly ToolGroup[];
20
+ export declare function registerAllTools(server: McpServer, deps: ToolDeps): void;
21
+ //# sourceMappingURL=index.d.ts.map