@milaboratories/pl-mcp-server 54.0.0 → 55.0.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.
Files changed (208) hide show
  1. package/README.md +15 -25
  2. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  3. package/dist/agent-tables.cjs +116 -0
  4. package/dist/agent-tables.cjs.map +1 -0
  5. package/dist/agent-tables.js +115 -0
  6. package/dist/agent-tables.js.map +1 -0
  7. package/dist/instructions.cjs +15 -12
  8. package/dist/instructions.cjs.map +1 -1
  9. package/dist/instructions.js +15 -12
  10. package/dist/instructions.js.map +1 -1
  11. package/dist/package.cjs +1 -1
  12. package/dist/package.js +1 -1
  13. package/dist/project-holds.cjs +58 -8
  14. package/dist/project-holds.cjs.map +1 -1
  15. package/dist/project-holds.js +58 -8
  16. package/dist/project-holds.js.map +1 -1
  17. package/dist/project-list.cjs +16 -0
  18. package/dist/project-list.cjs.map +1 -0
  19. package/dist/project-list.js +16 -0
  20. package/dist/project-list.js.map +1 -0
  21. package/dist/server-log.cjs +40 -18
  22. package/dist/server-log.cjs.map +1 -1
  23. package/dist/server-log.js +40 -19
  24. package/dist/server-log.js.map +1 -1
  25. package/dist/server.cjs +48 -18
  26. package/dist/server.cjs.map +1 -1
  27. package/dist/server.d.ts +21 -20
  28. package/dist/server.d.ts.map +1 -1
  29. package/dist/server.js +48 -18
  30. package/dist/server.js.map +1 -1
  31. package/dist/tools/agent-tables.cjs +424 -0
  32. package/dist/tools/agent-tables.cjs.map +1 -0
  33. package/dist/tools/agent-tables.js +417 -0
  34. package/dist/tools/agent-tables.js.map +1 -0
  35. package/dist/tools/await.cjs +216 -73
  36. package/dist/tools/await.cjs.map +1 -1
  37. package/dist/tools/await.js +212 -75
  38. package/dist/tools/await.js.map +1 -1
  39. package/dist/tools/block-action-context.cjs +3 -0
  40. package/dist/tools/block-action-context.cjs.map +1 -1
  41. package/dist/tools/block-action-context.js +3 -0
  42. package/dist/tools/block-action-context.js.map +1 -1
  43. package/dist/tools/block-api.cjs +17 -2
  44. package/dist/tools/block-api.cjs.map +1 -1
  45. package/dist/tools/block-api.js +17 -3
  46. package/dist/tools/block-api.js.map +1 -1
  47. package/dist/tools/block-params.cjs +272 -4
  48. package/dist/tools/block-params.cjs.map +1 -1
  49. package/dist/tools/block-params.js +270 -6
  50. package/dist/tools/block-params.js.map +1 -1
  51. package/dist/tools/block-state.cjs +323 -42
  52. package/dist/tools/block-state.cjs.map +1 -1
  53. package/dist/tools/block-state.js +317 -46
  54. package/dist/tools/block-state.js.map +1 -1
  55. package/dist/tools/block-storage.cjs +153 -0
  56. package/dist/tools/block-storage.cjs.map +1 -0
  57. package/dist/tools/block-storage.js +147 -0
  58. package/dist/tools/block-storage.js.map +1 -0
  59. package/dist/tools/block-updates.cjs +405 -0
  60. package/dist/tools/block-updates.cjs.map +1 -0
  61. package/dist/tools/block-updates.js +397 -0
  62. package/dist/tools/block-updates.js.map +1 -0
  63. package/dist/tools/blocks.cjs +183 -70
  64. package/dist/tools/blocks.cjs.map +1 -1
  65. package/dist/tools/blocks.js +177 -70
  66. package/dist/tools/blocks.js.map +1 -1
  67. package/dist/tools/data-query.cjs +451 -113
  68. package/dist/tools/data-query.cjs.map +1 -1
  69. package/dist/tools/data-query.js +446 -118
  70. package/dist/tools/data-query.js.map +1 -1
  71. package/dist/tools/errors.cjs +21 -0
  72. package/dist/tools/errors.cjs.map +1 -0
  73. package/dist/tools/errors.js +20 -0
  74. package/dist/tools/errors.js.map +1 -0
  75. package/dist/tools/files.cjs +643 -0
  76. package/dist/tools/files.cjs.map +1 -0
  77. package/dist/tools/files.js +613 -0
  78. package/dist/tools/files.js.map +1 -0
  79. package/dist/tools/ids.cjs +56 -0
  80. package/dist/tools/ids.cjs.map +1 -1
  81. package/dist/tools/ids.js +51 -2
  82. package/dist/tools/ids.js.map +1 -1
  83. package/dist/tools/imports.cjs +195 -0
  84. package/dist/tools/imports.cjs.map +1 -0
  85. package/dist/tools/imports.js +186 -0
  86. package/dist/tools/imports.js.map +1 -0
  87. package/dist/tools/logs.cjs +98 -19
  88. package/dist/tools/logs.cjs.map +1 -1
  89. package/dist/tools/logs.js +95 -20
  90. package/dist/tools/logs.js.map +1 -1
  91. package/dist/tools/paging.cjs +27 -0
  92. package/dist/tools/paging.cjs.map +1 -0
  93. package/dist/tools/paging.js +26 -0
  94. package/dist/tools/paging.js.map +1 -0
  95. package/dist/tools/params-merge.cjs +106 -0
  96. package/dist/tools/params-merge.cjs.map +1 -0
  97. package/dist/tools/params-merge.js +106 -0
  98. package/dist/tools/params-merge.js.map +1 -0
  99. package/dist/tools/project-template.cjs +75 -0
  100. package/dist/tools/project-template.cjs.map +1 -0
  101. package/dist/tools/project-template.js +72 -0
  102. package/dist/tools/project-template.js.map +1 -0
  103. package/dist/tools/projects.cjs +66 -19
  104. package/dist/tools/projects.cjs.map +1 -1
  105. package/dist/tools/projects.js +66 -20
  106. package/dist/tools/projects.js.map +1 -1
  107. package/dist/tools/readiness.cjs +104 -0
  108. package/dist/tools/readiness.cjs.map +1 -0
  109. package/dist/tools/readiness.js +99 -0
  110. package/dist/tools/readiness.js.map +1 -0
  111. package/dist/tools/result-pool.cjs +260 -0
  112. package/dist/tools/result-pool.cjs.map +1 -0
  113. package/dist/tools/result-pool.js +252 -0
  114. package/dist/tools/result-pool.js.map +1 -0
  115. package/dist/tools/sandbox.cjs +112 -14
  116. package/dist/tools/sandbox.cjs.map +1 -1
  117. package/dist/tools/sandbox.js +110 -15
  118. package/dist/tools/sandbox.js.map +1 -1
  119. package/dist/tools/sdk-types.cjs +24 -0
  120. package/dist/tools/sdk-types.cjs.map +1 -0
  121. package/dist/tools/sdk-types.js +24 -0
  122. package/dist/tools/sdk-types.js.map +1 -0
  123. package/dist/tools/settle.cjs +155 -0
  124. package/dist/tools/settle.cjs.map +1 -0
  125. package/dist/tools/settle.js +148 -0
  126. package/dist/tools/settle.js.map +1 -0
  127. package/dist/tools/tokens.cjs +49 -8
  128. package/dist/tools/tokens.cjs.map +1 -1
  129. package/dist/tools/tokens.js +45 -9
  130. package/dist/tools/tokens.js.map +1 -1
  131. package/dist/tools/types.cjs +62 -11
  132. package/dist/tools/types.cjs.map +1 -1
  133. package/dist/tools/types.js +60 -12
  134. package/dist/tools/types.js.map +1 -1
  135. package/dist/tools/value-outline.cjs +119 -0
  136. package/dist/tools/value-outline.cjs.map +1 -0
  137. package/dist/tools/value-outline.js +117 -0
  138. package/dist/tools/value-outline.js.map +1 -0
  139. package/package.json +9 -8
  140. package/src/agent-tables.test.ts +246 -0
  141. package/src/agent-tables.ts +145 -0
  142. package/src/instructions.ts +15 -12
  143. package/src/project-holds.test.ts +179 -0
  144. package/src/project-holds.ts +114 -12
  145. package/src/project-list.test.ts +38 -0
  146. package/src/project-list.ts +25 -0
  147. package/src/server-log.test.ts +54 -1
  148. package/src/server-log.ts +49 -27
  149. package/src/server.logging.test.ts +38 -46
  150. package/src/server.test.ts +151 -17
  151. package/src/server.ts +62 -34
  152. package/src/tools/agent-tables.test.ts +650 -0
  153. package/src/tools/agent-tables.ts +673 -0
  154. package/src/tools/await.test.ts +287 -0
  155. package/src/tools/await.ts +393 -123
  156. package/src/tools/block-action-context.ts +3 -0
  157. package/src/tools/block-api.ts +19 -4
  158. package/src/tools/block-params.test.ts +828 -0
  159. package/src/tools/block-params.ts +623 -8
  160. package/src/tools/block-state.test.ts +480 -0
  161. package/src/tools/block-state.ts +659 -66
  162. package/src/tools/block-storage.test.ts +270 -0
  163. package/src/tools/block-storage.ts +195 -0
  164. package/src/tools/block-updates.test.ts +605 -0
  165. package/src/tools/block-updates.ts +751 -0
  166. package/src/tools/blocks.test.ts +169 -22
  167. package/src/tools/blocks.ts +336 -105
  168. package/src/tools/data-query.test.ts +641 -0
  169. package/src/tools/data-query.ts +807 -157
  170. package/src/tools/errors.test.ts +33 -0
  171. package/src/tools/errors.ts +23 -0
  172. package/src/tools/files.test.ts +842 -0
  173. package/src/tools/files.ts +990 -0
  174. package/src/tools/ids.test.ts +87 -0
  175. package/src/tools/ids.ts +67 -0
  176. package/src/tools/imports.ts +304 -0
  177. package/src/tools/logs.test.ts +70 -0
  178. package/src/tools/logs.ts +130 -21
  179. package/src/tools/paging.test.ts +39 -0
  180. package/src/tools/paging.ts +39 -0
  181. package/src/tools/params-merge.test.ts +274 -0
  182. package/src/tools/params-merge.ts +149 -0
  183. package/src/tools/project-template.test.ts +230 -0
  184. package/src/tools/project-template.ts +135 -0
  185. package/src/tools/projects.test.ts +122 -0
  186. package/src/tools/projects.ts +135 -25
  187. package/src/tools/readiness.test.ts +195 -0
  188. package/src/tools/readiness.ts +189 -0
  189. package/src/tools/result-pool.test.ts +377 -0
  190. package/src/tools/result-pool.ts +483 -0
  191. package/src/tools/sandbox.test.ts +128 -10
  192. package/src/tools/sandbox.ts +132 -18
  193. package/src/tools/sdk-types.ts +54 -0
  194. package/src/tools/settle.test.ts +340 -0
  195. package/src/tools/settle.ts +312 -0
  196. package/src/tools/tokens.ts +74 -9
  197. package/src/tools/types.ts +77 -12
  198. package/src/tools/value-outline.ts +143 -0
  199. package/dist/tools/screenshot.cjs +0 -44
  200. package/dist/tools/screenshot.cjs.map +0 -1
  201. package/dist/tools/screenshot.js +0 -44
  202. package/dist/tools/screenshot.js.map +0 -1
  203. package/dist/tools/ui-interaction.cjs +0 -140
  204. package/dist/tools/ui-interaction.cjs.map +0 -1
  205. package/dist/tools/ui-interaction.js +0 -140
  206. package/dist/tools/ui-interaction.js.map +0 -1
  207. package/src/tools/screenshot.ts +0 -61
  208. package/src/tools/ui-interaction.ts +0 -184
@@ -1,15 +1,33 @@
1
+ import { asBlockId, type BlockId, type ProjectId } from "@milaboratories/pl-model-common";
1
2
  import type { BlockPackSpecAny, Project } from "@milaboratories/pl-middle-layer";
2
3
  import { BlockKindRef, CentralBlockRegistry } from "@milaboratories/pl-middle-layer";
3
- import { BlockInitParamsRejectedError } from "@milaboratories/pl-errors";
4
4
  import type { BlockKindReference } from "@platforma-sdk/model";
5
5
  import { z } from "zod";
6
6
  import { projectIdInput, blockIdInput } from "./ids";
7
7
  import type { AuthorMarker, ToolContext, ToolRegistrar } from "./types";
8
8
  import { errorResult, hints, textResult } from "./types";
9
+ import {
10
+ oneAtATime,
11
+ readinessOf,
12
+ type ReadinessProject,
13
+ settleAfterChange,
14
+ WRITE_WAIT_DEFAULT_MS,
15
+ WRITE_WAIT_MAX_MS,
16
+ WRITE_WAIT_MIN_MS,
17
+ } from "./block-state";
18
+ import { blockStatus, type BlockLookupProject, findBlock, unknownBlockResult } from "./readiness";
19
+ import { clampWait } from "./settle";
20
+ import { page, pagingArgs } from "./paging";
21
+ import { isBlockInitParamsRejectedError } from "./errors";
9
22
  import { SUPPORTED_BLOCK_API, unsupportedBlockApiResult } from "./block-api";
10
23
 
24
+ /** How many blocks list_available_blocks returns when the caller does not say. */
25
+ export const AVAILABLE_BLOCKS_DEFAULT_LIMIT = 100;
26
+ /** The most blocks one list_available_blocks call returns. */
27
+ export const AVAILABLE_BLOCKS_MAX_LIMIT = 500;
28
+
11
29
  /** Where a kind is resolved when the caller names no registry. */
12
- function centralBlockRegistryUrl(): string {
30
+ export function centralBlockRegistryUrl(): string {
13
31
  if (CentralBlockRegistry.type !== "remote-v2")
14
32
  throw new Error("The central block registry is expected to be a v2 remote registry.");
15
33
  return CentralBlockRegistry.url;
@@ -19,43 +37,44 @@ function errorMessage(e: unknown): string {
19
37
  return e instanceof Error ? e.message : String(e);
20
38
  }
21
39
 
40
+ /** The ways a tool input names a block: by kind, as an exact registry block, or a dev folder. */
41
+ export const blockSpecInput = z.union([
42
+ z.object({
43
+ type: z.literal("kind"),
44
+ kind: z
45
+ .string()
46
+ .describe(
47
+ 'Block kind reference "<package name>@<version or range>", ' +
48
+ 'e.g. "@platforma-open/milaboratories.samples-and-data.kind@1.2.0"',
49
+ ),
50
+ registryUrl: z
51
+ .string()
52
+ .optional()
53
+ .describe("Registry to resolve the kind in (default: the central block registry)"),
54
+ allowUnstable: z
55
+ .boolean()
56
+ .optional()
57
+ .describe("Also consider blocks not published to the stable channel"),
58
+ }),
59
+ z.object({
60
+ type: z.literal("from-registry-v2"),
61
+ registryUrl: z.string().describe("Registry URL"),
62
+ id: z.object({
63
+ organization: z.string(),
64
+ name: z.string(),
65
+ version: z.string(),
66
+ }),
67
+ }),
68
+ z.object({
69
+ type: z.literal("dev-v2"),
70
+ folder: z.string().describe("Path to block folder"),
71
+ }),
72
+ ]);
73
+
22
74
  const addBlockInput = z.object({
23
75
  projectId: projectIdInput.describe("Project ID"),
24
76
  label: z.string().describe("Block label"),
25
- spec: z
26
- .union([
27
- z.object({
28
- type: z.literal("kind"),
29
- kind: z
30
- .string()
31
- .describe(
32
- 'Block kind reference "<package name>@<version or range>", ' +
33
- 'e.g. "@platforma-open/milaboratories.samples-and-data.kind@1.2.0"',
34
- ),
35
- registryUrl: z
36
- .string()
37
- .optional()
38
- .describe("Registry to resolve the kind in (default: the central block registry)"),
39
- allowUnstable: z
40
- .boolean()
41
- .optional()
42
- .describe("Also consider blocks not published to the stable channel"),
43
- }),
44
- z.object({
45
- type: z.literal("from-registry-v2"),
46
- registryUrl: z.string().describe("Registry URL"),
47
- id: z.object({
48
- organization: z.string(),
49
- name: z.string(),
50
- version: z.string(),
51
- }),
52
- }),
53
- z.object({
54
- type: z.literal("dev-v2"),
55
- folder: z.string().describe("Path to block folder"),
56
- }),
57
- ])
58
- .describe("Which block to add"),
77
+ spec: blockSpecInput.describe("Which block to add"),
59
78
  params: z
60
79
  .record(z.string(), z.unknown())
61
80
  .optional()
@@ -70,6 +89,17 @@ const addBlockInput = z.object({
70
89
  before: blockIdInput
71
90
  .optional()
72
91
  .describe("ID of the block to insert the new block before (default: add at the end)"),
92
+ wait: z
93
+ .boolean()
94
+ .optional()
95
+ .describe("Wait for the new block's outputs to settle before answering (default false)."),
96
+ timeout: z
97
+ .number()
98
+ .optional()
99
+ .describe(
100
+ `The longest wait in ms with \`wait\` (default ${WRITE_WAIT_DEFAULT_MS}, at most ` +
101
+ `${WRITE_WAIT_MAX_MS}).`,
102
+ ),
73
103
  });
74
104
 
75
105
  /** Input of the `add_block` tool. */
@@ -91,8 +121,12 @@ export type AddBlockDeps = {
91
121
  * Adds the block the input describes, started from its params when it carries them. Every way the
92
122
  * block can be refused comes back as an error result saying nothing was created.
93
123
  */
94
- export async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {
124
+ export async function addNewBlock(
125
+ deps: AddBlockDeps,
126
+ input: Omit<AddBlockInput, "wait" | "timeout">,
127
+ ): Promise<{ blockId: BlockId } | { result: ReturnType<typeof errorResult> }> {
95
128
  const { label, spec, params, blockIds, before } = input;
129
+ const refuse = (message: string, hint: string) => ({ result: errorResult(message, hint) });
96
130
 
97
131
  let blockPackSpec: BlockPackSpecAny;
98
132
  if (spec.type === "kind") {
@@ -103,7 +137,7 @@ export async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {
103
137
  spec.allowUnstable ?? false,
104
138
  );
105
139
  } catch (e: unknown) {
106
- return errorResult(
140
+ return refuse(
107
141
  `Kind "${spec.kind}" was not resolved to a block: ${errorMessage(e)}. Nothing was created.`,
108
142
  "Check the kind name and version. Pass allowUnstable: true if the block is not " +
109
143
  "published to the stable channel, or registryUrl if it lives in another registry.",
@@ -125,20 +159,144 @@ export async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {
125
159
  requireModelAPIVersion: SUPPORTED_BLOCK_API,
126
160
  },
127
161
  );
128
- return textResult({ blockId });
162
+ return { blockId: asBlockId(blockId) };
129
163
  } catch (e: unknown) {
130
164
  const unsupported = unsupportedBlockApiResult(e);
131
- if (unsupported) return unsupported;
132
- if (e instanceof BlockInitParamsRejectedError)
133
- return errorResult(
165
+ if (unsupported) return { result: unsupported };
166
+ if (isBlockInitParamsRejectedError(e))
167
+ return refuse(
134
168
  `The block did not accept the params: ${e.reason}. Nothing was created.`,
135
- "Fix the params and call add_block again. get_block_params on a configured block " +
136
- "of the same kind shows the expected shape.",
169
+ "Fix the params and call add_block again. get_block_contract on the kind shows the " +
170
+ "params it accepts.",
137
171
  );
138
172
  throw e;
139
173
  }
140
174
  }
141
175
 
176
+ /** What `run_block` needs of a project; a middle-layer `Project` has it. */
177
+ export type RunProject = ReadinessProject & {
178
+ /**
179
+ * Starts the block and the stale blocks above it; the middle layer answers with the ids of the
180
+ * blocks it started (older middle layers answer nothing).
181
+ */
182
+ runBlock(blockId: BlockId): Promise<readonly BlockId[] | void>;
183
+ };
184
+
185
+ /**
186
+ * Runs a block, unless it cannot run: the readiness is computed first, and a block with blockers
187
+ * is refused with them, nothing started. The result names the blocks started, or says there was
188
+ * nothing to run (the block and the blocks above it are up to date).
189
+ */
190
+ export async function runBlockResult(project: RunProject, projectId: ProjectId, blockId: BlockId) {
191
+ const { block, error } = await findBlock(project, projectId, blockId, { nothingChanged: true });
192
+ if (error) return error;
193
+ const readiness = await readinessOf(project, blockId);
194
+ if (readiness !== undefined && readiness.blockers.length > 0)
195
+ return errorResult(
196
+ `Block ${blockId} cannot run; nothing was started. ` +
197
+ readiness.blockers
198
+ .map((b) => `[${b.kind}${b.blockId !== undefined ? ` ${b.blockId}` : ""}] ${b.message}`)
199
+ .join(" "),
200
+ "Fix what the blockers name: set_block_params changes the block's params, " +
201
+ "set_block_data its data; get_block_state shows its readiness. A blocker of a block " +
202
+ "above is fixed on that block.",
203
+ );
204
+ const started = await project.runBlock(blockId);
205
+ const nothingToRun = Array.isArray(started)
206
+ ? started.length === 0
207
+ : blockStatus(block) === "up-to-date";
208
+ if (nothingToRun)
209
+ return textResult({
210
+ ok: true,
211
+ nothingToRun: true,
212
+ note:
213
+ "The block and the blocks above it are up to date: nothing was started. Its results " +
214
+ "follow its current data; get_block_outputs reads them.",
215
+ });
216
+ return textResult(Array.isArray(started) ? { ok: true, started } : { ok: true, started: true });
217
+ }
218
+
219
+ /** The block packs `update_block` and `update_blocks` move a block to: a registry block or a dev folder. */
220
+ export const updateBlockSpecInput = z.union([
221
+ z.object({
222
+ type: z.literal("from-registry-v2"),
223
+ registryUrl: z.string().describe("Registry URL"),
224
+ id: z.object({
225
+ organization: z.string(),
226
+ name: z.string(),
227
+ version: z.string(),
228
+ }),
229
+ channel: z
230
+ .string()
231
+ .optional()
232
+ .describe(
233
+ "The registry channel the block follows for its updates; keep the one of the spec " +
234
+ "list_block_updates gives.",
235
+ ),
236
+ }),
237
+ z.object({
238
+ type: z.literal("dev-v2"),
239
+ folder: z.string().describe("Path to block folder"),
240
+ }),
241
+ ]);
242
+
243
+ /** What {@link applyBlockPack} needs of a project; a middle-layer `Project` has it. */
244
+ export type BlockPackProject = Pick<Project, "updateBlockPack">;
245
+
246
+ /**
247
+ * The one way this server moves a block to another block pack (`update_block`, `update_blocks`):
248
+ * in the block's turn among this server's changes of it ({@link oneAtATime}), asking the middle layer for a pack on the
249
+ * current block API. Answers the error result of the shared block API guard when the pack is
250
+ * refused for its block API (nothing is changed), `undefined` when the block was moved; any other
251
+ * failure is thrown.
252
+ *
253
+ * `beforeWrite` runs when the block's turn starts, right before the write: what it throws is
254
+ * thrown here and nothing is changed. A caller that waited for its turn checks there whether it
255
+ * still wants the write.
256
+ */
257
+ export async function applyBlockPack(
258
+ project: BlockPackProject,
259
+ blockId: BlockId,
260
+ spec: BlockPackSpecAny,
261
+ options: { resetArgs: boolean; author: AuthorMarker; beforeWrite?: () => void },
262
+ ): Promise<ReturnType<typeof errorResult> | undefined> {
263
+ try {
264
+ await oneAtATime(project, blockId, async () => {
265
+ options.beforeWrite?.();
266
+ await project.updateBlockPack(blockId, spec, options.resetArgs, options.author, {
267
+ requireModelAPIVersion: SUPPORTED_BLOCK_API,
268
+ });
269
+ });
270
+ } catch (e: unknown) {
271
+ const unsupported = unsupportedBlockApiResult(e);
272
+ if (unsupported) return unsupported;
273
+ throw e;
274
+ }
275
+ return undefined;
276
+ }
277
+
278
+ /** What `update_block` needs of a project; a middle-layer `Project` has it. */
279
+ export type UpdateProject = BlockLookupProject<{ id: string }> & BlockPackProject;
280
+
281
+ /**
282
+ * Moves a block to another block pack. Nothing is changed when the block is not in the project or
283
+ * the pack is refused (the shared block API guard included).
284
+ */
285
+ export async function updateBlockResult(
286
+ project: UpdateProject,
287
+ input: { projectId: ProjectId; blockId: BlockId; spec: BlockPackSpecAny; resetArgs?: boolean },
288
+ author: AuthorMarker,
289
+ ) {
290
+ const { projectId, blockId } = input;
291
+ const { error } = await findBlock(project, projectId, blockId, { nothingChanged: true });
292
+ if (error) return error;
293
+ const refused = await applyBlockPack(project, blockId, input.spec, {
294
+ resetArgs: input.resetArgs ?? false,
295
+ author,
296
+ });
297
+ return refused ?? textResult({ ok: true });
298
+ }
299
+
142
300
  export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): void {
143
301
  server.registerTool(
144
302
  "add_block",
@@ -152,30 +310,63 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
152
310
  "block folder).\n\n" +
153
311
  "`params` are the initialization params of the block's kind: the block's own model checks " +
154
312
  "them and builds its initial state from them. Without `params` the block starts in its " +
155
- "default state. get_block_params shows the params of an existing block.\n\n" +
313
+ "default state. get_block_contract shows the params a kind accepts, get_block_params the " +
314
+ "params of an existing block. Without params, configure the block afterwards: read its " +
315
+ "options outputs (get_block_outputs; references to results of blocks above come from " +
316
+ "there), then write them into its params with set_block_params, or into its data with " +
317
+ "set_block_data for what params do not cover. Params and data can name the same thing " +
318
+ "differently: read the data with get_block_state before writing it. A block offers results " +
319
+ "of blocks above only once they exist, and some blocks publish results only from their " +
320
+ "run: if an option is missing and the block above has not run, run it first (run_block, " +
321
+ "await_block_done).\n\n" +
156
322
  "References to other blocks inside `params` must hold real block ids of this project, " +
157
323
  "copied from tool results. Params copied from another project or a template hold other " +
158
324
  "ids: map each of them to a block of this project with `blockIds`.\n\n" +
159
- "Only blocks on the current block API can be added. On any error nothing is created.",
325
+ "Only blocks on the current block API can be added. On any error nothing is created.\n\n" +
326
+ "Result: `{ blockId, readiness, settled, waitedMs, outputs, ... }`; `readiness` as " +
327
+ "get_block_state gives it: whether the block can run and, if not, the block's own " +
328
+ "messages on why. With `wait: true` the call waits for the outputs to settle; the first " +
329
+ "settle of a new block can take one refresh cycle (about 2 s). A wait that runs out is " +
330
+ "not an error: the block was added, and `settled: false` says what is still pending.",
160
331
  inputSchema: addBlockInput,
161
332
  },
162
- async (input) => {
333
+ async ({ wait, timeout, ...input }, call) => {
163
334
  const ml = ctx.requireMl();
164
- return await ctx.withProject(
165
- input.projectId,
166
- async (project) =>
167
- await addBlock(
168
- {
169
- project,
170
- resolveKind: (kind, registryUrl, allowUnstable) =>
171
- ml.blockRegistryProvider
172
- .getRegistry(registryUrl ?? centralBlockRegistryUrl())
173
- .resolveKind(kind, { allowUnstable }),
174
- author: ctx.getAuthorMarker(),
175
- },
176
- input,
177
- ),
178
- );
335
+ return await ctx.withProject(input.projectId, async (project) => {
336
+ if (input.before !== undefined) {
337
+ const { error } = await findBlock(project, input.projectId, input.before, {
338
+ nothingChanged: true,
339
+ });
340
+ if (error) return error;
341
+ }
342
+ const added = await addNewBlock(
343
+ {
344
+ project,
345
+ resolveKind: (kind, registryUrl, allowUnstable) =>
346
+ ml.blockRegistryProvider
347
+ .getRegistry(registryUrl ?? centralBlockRegistryUrl())
348
+ .resolveKind(kind, { allowUnstable }),
349
+ author: ctx.getAuthorMarker(),
350
+ },
351
+ input,
352
+ );
353
+ if ("result" in added) return added.result;
354
+ return textResult({
355
+ blockId: added.blockId,
356
+ // No marker to wait for: the block's storage was created, not written by a call.
357
+ ...(await settleAfterChange(project, added.blockId, {
358
+ wait: wait ?? false,
359
+ timeoutMs: clampWait(
360
+ timeout,
361
+ WRITE_WAIT_DEFAULT_MS,
362
+ WRITE_WAIT_MIN_MS,
363
+ WRITE_WAIT_MAX_MS,
364
+ ),
365
+ call,
366
+ notePrerun: false,
367
+ })),
368
+ });
369
+ });
179
370
  },
180
371
  );
181
372
 
@@ -185,43 +376,31 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
185
376
  title: "Update Block",
186
377
  annotations: hints.destructive,
187
378
  description:
188
- "Update an existing block's pack (reload from registry or dev folder). Use after rebuilding a dev block to pick up changes without removing/re-adding it.",
379
+ "Update an existing block's pack (reload from registry or dev folder). Use after " +
380
+ "rebuilding a dev block to pick up changes without removing/re-adding it. Only block " +
381
+ "packs on the current block API are taken; on any error nothing is changed. To upgrade " +
382
+ "the blocks of a project to newer versions, use list_block_updates and update_blocks.",
189
383
  inputSchema: z.object({
190
384
  projectId: projectIdInput.describe("Project ID"),
191
385
  blockId: blockIdInput.describe("Block ID to update"),
192
- spec: z
193
- .union([
194
- z.object({
195
- type: z.literal("from-registry-v2"),
196
- registryUrl: z.string().describe("Registry URL"),
197
- id: z.object({
198
- organization: z.string(),
199
- name: z.string(),
200
- version: z.string(),
201
- }),
202
- }),
203
- z.object({
204
- type: z.literal("dev-v2"),
205
- folder: z.string().describe("Path to block folder"),
206
- }),
207
- ])
208
- .describe("Block pack specification"),
386
+ spec: updateBlockSpecInput.describe("Block pack specification"),
209
387
  resetArgs: z
210
388
  .boolean()
211
389
  .optional()
212
- .describe("Reset block arguments to initial values (default: false)"),
390
+ .describe(
391
+ "Start the block over from its initial state: its data and the data of its plugins " +
392
+ "are discarded (default: false, the data is kept and migrated).",
393
+ ),
213
394
  }),
214
395
  },
215
396
  async ({ projectId, blockId, spec, resetArgs }) => {
216
- return await ctx.withProject(projectId, async (project) => {
217
- await project.updateBlockPack(
218
- blockId,
219
- spec as BlockPackSpecAny,
220
- resetArgs ?? false,
397
+ return await ctx.withProject(projectId, (project) =>
398
+ updateBlockResult(
399
+ project,
400
+ { projectId, blockId, spec: spec as BlockPackSpecAny, resetArgs },
221
401
  ctx.getAuthorMarker(),
222
- );
223
- return textResult({ ok: true });
224
- });
402
+ ),
403
+ );
225
404
  },
226
405
  );
227
406
 
@@ -239,7 +418,11 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
239
418
  },
240
419
  async ({ projectId, blockId }) => {
241
420
  return await ctx.withProject(projectId, async (project) => {
242
- await project.deleteBlock(blockId, ctx.getAuthorMarker());
421
+ const { error } = await findBlock(project, projectId, blockId, { nothingChanged: true });
422
+ if (error) return error;
423
+ await oneAtATime(project, blockId, () =>
424
+ project.deleteBlock(blockId, ctx.getAuthorMarker()),
425
+ );
243
426
  return textResult({ ok: true });
244
427
  });
245
428
  },
@@ -252,17 +435,22 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
252
435
  // Starts work like an additive tool, but replaces results and stops blocks computing below.
253
436
  annotations: { ...hints.additive, destructiveHint: true },
254
437
  description:
255
- "Run a block. Stale upstream blocks are started automatically. A run replaces the previous results of each block it starts; downstream blocks that are still computing are stopped and their unfinished work is lost.",
438
+ "Run a block. Stale upstream blocks are started automatically. A run replaces the " +
439
+ "previous results of each block it starts; downstream blocks that are still computing " +
440
+ "are stopped and their unfinished work is lost.\n\n" +
441
+ "A block that cannot run (its readiness has blockers) is refused with the blockers, and " +
442
+ "nothing is started. Result: `{ ok, started }`, the ids of the blocks started (or " +
443
+ "`true` when the application does not say which), or `{ ok, nothingToRun: true }` when " +
444
+ "the block and the blocks above it are up to date. await_block_done waits for the run.",
256
445
  inputSchema: z.object({
257
446
  projectId: projectIdInput.describe("Project ID"),
258
447
  blockId: blockIdInput.describe("Block ID to run"),
259
448
  }),
260
449
  },
261
450
  async ({ projectId, blockId }) => {
262
- return await ctx.withProject(projectId, async (project) => {
263
- await project.runBlock(blockId);
264
- return textResult({ ok: true });
265
- });
451
+ return await ctx.withProject(projectId, (project) =>
452
+ runBlockResult(project, projectId, blockId),
453
+ );
266
454
  },
267
455
  );
268
456
 
@@ -279,6 +467,8 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
279
467
  },
280
468
  async ({ projectId, blockId }) => {
281
469
  return await ctx.withProject(projectId, async (project) => {
470
+ const { error } = await findBlock(project, projectId, blockId, { nothingChanged: true });
471
+ if (error) return error;
282
472
  await project.stopBlock(blockId);
283
473
  return textResult({ ok: true });
284
474
  });
@@ -298,7 +488,11 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
298
488
  },
299
489
  async ({ projectId, blockIds }) => {
300
490
  return await ctx.withProject(projectId, async (project) => {
301
- await project.reorderBlocks(blockIds);
491
+ const overview = await project.overview.getValue();
492
+ const known = new Set(overview?.blocks.map((b) => b.id));
493
+ const unknown = blockIds.find((id) => !known.has(id));
494
+ if (unknown !== undefined) return unknownBlockResult(projectId, unknown, true);
495
+ await project.reorderBlocks(blockIds, ctx.getAuthorMarker());
302
496
  return textResult({ ok: true });
303
497
  });
304
498
  },
@@ -310,23 +504,36 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
310
504
  title: "List Available Blocks",
311
505
  annotations: hints.read,
312
506
  description:
313
- "List available blocks from configured registries. Optional query to filter by name.",
507
+ "List available blocks from configured registries, " +
508
+ `${AVAILABLE_BLOCKS_DEFAULT_LIMIT} at a time. Optional query to filter by name. ` +
509
+ "Result: `{ blocks, total, offset, hasMore }`; page with `offset` and `limit`.",
314
510
  inputSchema: z.object({
315
511
  query: z
316
512
  .string()
317
513
  .optional()
318
514
  .describe("Filter blocks by name (case-insensitive substring match)"),
515
+ ...pagingArgs("blocks", AVAILABLE_BLOCKS_DEFAULT_LIMIT, AVAILABLE_BLOCKS_MAX_LIMIT),
319
516
  }),
320
517
  },
321
- async ({ query }) => {
518
+ async ({ query, limit, offset }) => {
322
519
  if (!ctx.callbacks.listAvailableBlocks) {
323
520
  return errorResult(
324
- "Block registry is not available.",
325
- "This usually means the desktop app encounters a problem connecting block registry. Check Settings > Override Main Registry find which registry really in use and check connection.",
521
+ "Block listing is not available in this environment.",
522
+ "The block list comes from the registries of the desktop application, and the " +
523
+ "application running this server did not provide block listing. add_block with a " +
524
+ "`kind` spec, and get_block_contract, resolve blocks without it.",
326
525
  );
327
526
  }
328
527
  const blocks = await ctx.callbacks.listAvailableBlocks(query);
329
- return textResult(blocks);
528
+ const { items, ...paging } = page(blocks, offset, limit ?? AVAILABLE_BLOCKS_DEFAULT_LIMIT);
529
+ return textResult(
530
+ { blocks: items, ...paging },
531
+ {
532
+ hint:
533
+ "Ask for fewer blocks: a smaller `limit` with `offset` to page, or `query` to narrow " +
534
+ "by name.",
535
+ },
536
+ );
330
537
  },
331
538
  );
332
539
 
@@ -338,20 +545,39 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
338
545
  description:
339
546
  "Get detailed info about a specific block package from the registry. Use list_available_blocks first to find the block.",
340
547
  inputSchema: z.object({
341
- registryUrl: z.string().describe("Registry URL (from list_available_blocks)"),
548
+ registryId: z
549
+ .string()
550
+ .optional()
551
+ .describe("The registry id of the block, `registryId` from list_available_blocks"),
552
+ registryUrl: z
553
+ .string()
554
+ .optional()
555
+ .describe(
556
+ "Deprecated, accepted for one release: the old name of `registryId`. " +
557
+ "When both are given, `registryId` is used.",
558
+ ),
342
559
  organization: z.string().describe("Organization name"),
343
560
  name: z.string().describe("Block package name"),
344
561
  version: z.string().describe("Block version"),
345
562
  }),
346
563
  },
347
- async ({ registryUrl, organization, name, version }) => {
564
+ async ({ registryId, registryUrl, organization, name, version }) => {
348
565
  if (!ctx.callbacks.getBlockInfo) {
349
566
  return errorResult(
350
567
  "Block info is not available in this environment.",
351
- 'Maybe the name of the block was written incerrectly. Use list_available_blocks to browse blocks instead. Or ask user to check "Additional Registries" in Settings panel',
568
+ "Block info comes from the registries of the desktop application, and this server " +
569
+ "is not running inside it. add_block with a `kind` spec, and get_block_contract, " +
570
+ "resolve blocks without it.",
352
571
  );
353
572
  }
354
- const info = await ctx.callbacks.getBlockInfo(registryUrl, organization, name, version);
573
+ const registry = registryId ?? registryUrl;
574
+ if (registry === undefined) {
575
+ return errorResult(
576
+ "Give `registryId`: the registry of the block.",
577
+ "list_available_blocks gives each block's `registryId`.",
578
+ );
579
+ }
580
+ const info = await ctx.callbacks.getBlockInfo(registry, organization, name, version);
355
581
  return textResult(info);
356
582
  },
357
583
  );
@@ -375,6 +601,11 @@ export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): voi
375
601
  "Failed to show the block: this server is not running inside the desktop application.",
376
602
  );
377
603
  }
604
+ const found = await ctx.withProject(
605
+ projectId,
606
+ async (project) => await findBlock(project, projectId, blockId),
607
+ );
608
+ if (found.error) return found.error;
378
609
  await ctx.callbacks.selectBlock(projectId, blockId);
379
610
  return textResult({ ok: true });
380
611
  },