@milaboratories/pl-mcp-server 53.1.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 +30 -37
  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 +16 -12
  8. package/dist/instructions.cjs.map +1 -1
  9. package/dist/instructions.js +16 -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 +122 -0
  14. package/dist/project-holds.cjs.map +1 -0
  15. package/dist/project-holds.js +122 -0
  16. package/dist/project-holds.js.map +1 -0
  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 +48 -18
  22. package/dist/server-log.cjs.map +1 -1
  23. package/dist/server-log.js +48 -19
  24. package/dist/server-log.js.map +1 -1
  25. package/dist/server.cjs +71 -18
  26. package/dist/server.cjs.map +1 -1
  27. package/dist/server.d.ts +37 -24
  28. package/dist/server.d.ts.map +1 -1
  29. package/dist/server.js +71 -19
  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 -72
  36. package/dist/tools/await.cjs.map +1 -1
  37. package/dist/tools/await.js +212 -74
  38. package/dist/tools/await.js.map +1 -1
  39. package/dist/tools/block-action-context.cjs +14 -9
  40. package/dist/tools/block-action-context.cjs.map +1 -1
  41. package/dist/tools/block-action-context.js +14 -9
  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 +275 -6
  48. package/dist/tools/block-params.cjs.map +1 -1
  49. package/dist/tools/block-params.js +273 -8
  50. package/dist/tools/block-params.js.map +1 -1
  51. package/dist/tools/block-state.cjs +343 -57
  52. package/dist/tools/block-state.cjs.map +1 -1
  53. package/dist/tools/block-state.js +337 -61
  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 +199 -76
  64. package/dist/tools/blocks.cjs.map +1 -1
  65. package/dist/tools/blocks.js +193 -76
  66. package/dist/tools/blocks.js.map +1 -1
  67. package/dist/tools/data-query.cjs +470 -101
  68. package/dist/tools/data-query.cjs.map +1 -1
  69. package/dist/tools/data-query.js +465 -106
  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 +72 -0
  80. package/dist/tools/ids.cjs.map +1 -1
  81. package/dist/tools/ids.js +63 -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 +111 -30
  88. package/dist/tools/logs.cjs.map +1 -1
  89. package/dist/tools/logs.js +108 -31
  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 +68 -38
  104. package/dist/tools/projects.cjs.map +1 -1
  105. package/dist/tools/projects.js +69 -40
  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 +16 -12
  143. package/src/project-holds.test.ts +362 -0
  144. package/src/project-holds.ts +207 -0
  145. package/src/project-list.test.ts +38 -0
  146. package/src/project-list.ts +25 -0
  147. package/src/server-log.test.ts +71 -6
  148. package/src/server-log.ts +58 -27
  149. package/src/server.logging.test.ts +46 -51
  150. package/src/server.test.ts +166 -22
  151. package/src/server.ts +97 -37
  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 -122
  156. package/src/tools/block-action-context.ts +17 -13
  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 +626 -10
  160. package/src/tools/block-state.test.ts +480 -0
  161. package/src/tools/block-state.ts +678 -83
  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 +354 -113
  168. package/src/tools/data-query.test.ts +641 -0
  169. package/src/tools/data-query.ts +839 -145
  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 +89 -1
  176. package/src/tools/imports.ts +304 -0
  177. package/src/tools/logs.test.ts +70 -0
  178. package/src/tools/logs.ts +154 -44
  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 +136 -48
  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 +84 -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
@@ -6,6 +6,7 @@ import type { Server as Listener } from "node:net";
6
6
  import { afterAll, beforeAll, describe, expect, test } from "vitest";
7
7
  import { version } from "../package.json";
8
8
  import type { McpSecret, PlMcpServerCallbacks } from "./server";
9
+ import { SERVER_INSTRUCTIONS } from "./instructions";
9
10
  import { PlMcpServer } from "./server";
10
11
 
11
12
  const secret = "0".repeat(32) as McpSecret;
@@ -73,11 +74,29 @@ describe.each(eras)("to a $era client", (era) => {
73
74
  expect(client.getServerVersion()).toMatchObject({ name: "pl", version });
74
75
  });
75
76
 
77
+ // Claude Code cuts instructions at 4096 characters. The text is kept under 3600 so that new
78
+ // tools fit; the test guards the cut and that headroom.
76
79
  test("sends instructions that fit the length clients keep", () => {
77
80
  const instructions = client.getInstructions() ?? "";
78
- expect(instructions).toContain("open_project");
81
+ expect(instructions).toContain("list_projects");
79
82
  expect(instructions.length).toBeGreaterThan(0);
80
83
  expect(instructions.length).toBeLessThanOrEqual(4096);
84
+ expect(SERVER_INSTRUCTIONS.length).toBeLessThan(3600);
85
+ });
86
+
87
+ test("say a block param takes its declared form and column tools take a columnId", () => {
88
+ expect(SERVER_INSTRUCTIONS).toContain(
89
+ "A block param takes the form get_block_contract declares: a column's `ref` for a PlRef " +
90
+ "object, its `columnId` for an id string, `valueIds` for picked values.",
91
+ );
92
+ expect(SERVER_INSTRUCTIONS).toContain(
93
+ "Tools that take a column (create_table, get_pool_column, get_column_values) take its " +
94
+ "`columnId`.",
95
+ );
96
+ expect(SERVER_INSTRUCTIONS).not.toMatch(/wherever a column is taken/);
97
+ expect(SERVER_INSTRUCTIONS).toMatch(
98
+ /\{__isRef: true, blockId, name\}, may be written by hand/,
99
+ );
81
100
  });
82
101
 
83
102
  test("names in the instructions are tools the server has", () => {
@@ -87,12 +106,41 @@ describe.each(eras)("to a $era client", (era) => {
87
106
  expect(mentioned.filter((name) => !known.has(name))).toEqual([]);
88
107
  });
89
108
 
109
+ // A project is loaded by whatever tool needs it; an agent is never asked to open one.
110
+ test("nothing it says asks for a project to be opened", () => {
111
+ const said = [
112
+ client.getInstructions() ?? "",
113
+ ...tools.map((tool) => JSON.stringify(tool)),
114
+ ].join("\n");
115
+ expect(said).not.toMatch(/open_project|close_project|\bopened\b|must be open/i);
116
+ });
117
+
118
+ // A pool column's leaf id fits a block field of that form; a discovered id is not offered.
119
+ test("the result pool is not ruled out for configuring a block", () => {
120
+ const pool = tools.find((tool) => tool.name === "list_pool_columns")?.description ?? "";
121
+ for (const said of [client.getInstructions() ?? "", pool]) {
122
+ expect(said).not.toMatch(/not to configure a block|To configure a block/);
123
+ expect(said).toContain("own option outputs");
124
+ expect(said).toMatch(
125
+ /`?ref`?, or its `?columnId`? when it has a `?ref`?, fits a block field/,
126
+ );
127
+ }
128
+ });
129
+
90
130
  test("says its set of tools does not change", () => {
91
131
  expect(client.getServerCapabilities()?.tools).toEqual({ listChanged: false });
92
132
  });
93
133
  });
94
134
 
95
135
  describe("every tool", () => {
136
+ // Claude Code cuts tool descriptions it shows upfront at 4096 characters.
137
+ test("has a description that fits the length clients keep", () => {
138
+ const tooLong = tools
139
+ .filter((tool) => (tool.description ?? "").length > 4096)
140
+ .map((tool) => tool.name);
141
+ expect(tooLong).toEqual([]);
142
+ });
143
+
96
144
  test("has a title and a description that says what it is for", () => {
97
145
  const lacking = tools
98
146
  .filter((tool) => !tool.title || (tool.description ?? "").length < 40)
@@ -135,43 +183,46 @@ describe.each(eras)("to a $era client", (era) => {
135
183
  "await_block_done",
136
184
  "get_app_log",
137
185
  "get_block_action_context",
186
+ "get_block_contract",
138
187
  "get_block_info",
139
188
  "get_block_logs",
140
189
  "get_block_outputs",
141
190
  "get_block_params",
142
191
  "get_block_state",
192
+ "get_column_values",
143
193
  "get_connection_status",
194
+ "get_file_handles",
195
+ "get_pool_column",
144
196
  "get_project_overview",
145
197
  "list_available_blocks",
198
+ "list_block_updates",
146
199
  "list_columns",
147
200
  "list_connections",
201
+ "list_files",
202
+ "list_pool_columns",
148
203
  "list_projects",
149
204
  "ping",
150
205
  "query_table",
206
+ "read_file",
151
207
  ]);
152
208
  });
153
209
 
154
210
  test("destructive tools", () => {
155
211
  expect(namesWhere((tool) => tool.annotations?.destructiveHint)).toEqual([
156
- "click",
157
212
  "delete_project",
158
- "execute_js",
159
- "press_key",
160
213
  "remove_block",
161
214
  "reorder_blocks",
162
215
  "run_block",
163
216
  "set_block_data",
217
+ "set_block_params",
164
218
  "stop_block",
165
- "type_text",
166
219
  "update_block",
220
+ "update_blocks",
167
221
  ]);
168
222
  });
169
223
 
170
224
  test("open-world tools", () => {
171
- expect(namesWhere((tool) => tool.annotations?.openWorldHint)).toEqual([
172
- "connect_to_server",
173
- "execute_js",
174
- ]);
225
+ expect(namesWhere((tool) => tool.annotations?.openWorldHint)).toEqual(["connect_to_server"]);
175
226
  });
176
227
  });
177
228
 
@@ -181,40 +232,45 @@ describe.each(eras)("to a $era client", (era) => {
181
232
  expect(tools.map((tool) => tool.name).toSorted()).toEqual([
182
233
  "add_block",
183
234
  "await_block_done",
184
- "capture_screenshot",
185
- "click",
186
- "close_project",
187
235
  "connect_to_server",
188
236
  "create_project",
237
+ "create_table",
189
238
  "delete_project",
190
239
  "disconnect",
191
- "execute_js",
240
+ "drop_table",
241
+ "export_project_template",
192
242
  "get_app_log",
193
243
  "get_block_action_context",
244
+ "get_block_contract",
194
245
  "get_block_info",
195
246
  "get_block_logs",
196
247
  "get_block_outputs",
197
248
  "get_block_params",
198
249
  "get_block_state",
250
+ "get_column_values",
199
251
  "get_connection_status",
252
+ "get_file_handles",
253
+ "get_pool_column",
200
254
  "get_project_overview",
201
255
  "list_available_blocks",
256
+ "list_block_updates",
202
257
  "list_columns",
203
258
  "list_connections",
259
+ "list_files",
260
+ "list_pool_columns",
204
261
  "list_projects",
205
- "open_project",
206
262
  "ping",
207
- "press_key",
208
263
  "query_table",
264
+ "read_file",
209
265
  "remove_block",
210
266
  "reorder_blocks",
211
267
  "run_block",
212
- "scroll",
213
268
  "select_block",
214
269
  "set_block_data",
270
+ "set_block_params",
215
271
  "stop_block",
216
- "type_text",
217
272
  "update_block",
273
+ "update_blocks",
218
274
  ]);
219
275
  });
220
276
 
@@ -229,7 +285,7 @@ describe.each(eras)("to a $era client", (era) => {
229
285
 
230
286
  test("required arguments are the ones without `optional` or a default", () => {
231
287
  expect(inputSchemaOf("add_block").required).toEqual(["projectId", "label", "spec"]);
232
- expect(inputSchemaOf("open_project").required).toEqual(["projectId"]);
288
+ expect(inputSchemaOf("get_project_overview").required).toEqual(["projectId"]);
233
289
  });
234
290
 
235
291
  test("an optional argument keeps its description", () => {
@@ -241,17 +297,51 @@ describe.each(eras)("to a $era client", (era) => {
241
297
  test("a default is advertised", () => {
242
298
  expect(inputSchemaOf("await_block_done").properties?.timeout).toMatchObject({
243
299
  type: "number",
244
- default: 120000,
300
+ default: 50000,
245
301
  });
246
302
  });
247
303
 
248
304
  test("a tool without arguments takes an empty object", () => {
249
305
  expect(inputSchemaOf("ping")).toEqual({ type: "object", properties: {} });
250
306
  });
307
+
308
+ // Every schema is sent to the agent at the start of each session. A sub-schema used at several
309
+ // places is inlined at each of them unless it has an id, which once made list_pool_columns
310
+ // 40,817 characters and create_table 12,757. The largest schemas are create_table at about
311
+ // 6,300 characters and list_pool_columns at about 4,500; 8,000 leaves them room to grow by a few
312
+ // arguments, while an inlined selector or filter tree (several times larger) does not fit.
313
+ test("no argument schema is larger than 8,000 characters", () => {
314
+ const large = tools
315
+ .map((tool) => ({ name: tool.name, size: JSON.stringify(tool.inputSchema).length }))
316
+ .filter(({ size }) => size > 8000);
317
+ expect(large).toEqual([]);
318
+ });
319
+
320
+ test("a shared sub-schema is sent once, under `$defs`, and every reference resolves", () => {
321
+ const defs = inputSchemaOf("list_pool_columns").$defs;
322
+ expect(Object.keys(defs ?? {})).toEqual(
323
+ expect.arrayContaining(["ColumnSelector", "RelaxedColumnSelector"]),
324
+ );
325
+ expect(Object.keys(inputSchemaOf("create_table").$defs ?? {})).toEqual(
326
+ expect.arrayContaining(["TableFilter", "TableColumnRef", "ColumnId"]),
327
+ );
328
+ for (const tool of tools) {
329
+ const json = JSON.stringify(tool.inputSchema);
330
+ const refs = [...json.matchAll(/"\$ref":"#\/\$defs\/([^"]+)"/g)].map((m) => m[1]);
331
+ const known = Object.keys(tool.inputSchema.$defs ?? {});
332
+ expect({ tool: tool.name, missing: refs.filter((r) => !known.includes(r)) }).toEqual({
333
+ tool: tool.name,
334
+ missing: [],
335
+ });
336
+ }
337
+ });
251
338
  });
252
339
 
253
340
  test("arguments of the wrong type are refused before the tool runs", async () => {
254
- const result = await client.callTool({ name: "open_project", arguments: { projectId: 5 } });
341
+ const result = await client.callTool({
342
+ name: "get_project_overview",
343
+ arguments: { projectId: 5 },
344
+ });
255
345
  expect(result.isError).toBe(true);
256
346
  expect(JSON.stringify(result.content)).toContain("Input validation error");
257
347
  });
@@ -605,7 +695,7 @@ describe("stopping the server", () => {
605
695
  });
606
696
  const callbacks: PlMcpServerCallbacks = {
607
697
  // A tool that never answers, as a wait for a block that never finishes would.
608
- captureScreenshot: () => {
698
+ readAppLog: () => {
609
699
  reached();
610
700
  return new Promise<string>(() => {});
611
701
  },
@@ -616,7 +706,7 @@ describe("stopping the server", () => {
616
706
  await busy.start();
617
707
  client = await connect(busy, era);
618
708
 
619
- const call = client.callTool({ name: "capture_screenshot", arguments: {} });
709
+ const call = client.callTool({ name: "get_app_log", arguments: {} });
620
710
  const callFailed = call.then(
621
711
  () => false,
622
712
  () => true,
@@ -631,3 +721,57 @@ describe("stopping the server", () => {
631
721
  }
632
722
  });
633
723
  });
724
+
725
+ describe("get_block_info", () => {
726
+ async function callWith(args: Record<string, unknown>): Promise<{
727
+ registries: string[];
728
+ isError: boolean | undefined;
729
+ }> {
730
+ const registries: string[] = [];
731
+ const callbacks: PlMcpServerCallbacks = {
732
+ getBlockInfo: async (registry) => {
733
+ registries.push(registry);
734
+ return { ok: true };
735
+ },
736
+ };
737
+ const withInfo = new PlMcpServer({ port: 0, secret, callbacks });
738
+ let client: Client | undefined;
739
+ try {
740
+ await withInfo.start();
741
+ client = await connect(withInfo, eras[0]);
742
+ const result = await client.callTool({
743
+ name: "get_block_info",
744
+ arguments: { organization: "org", name: "block", version: "1.0.0", ...args },
745
+ });
746
+ return { registries, isError: result.isError as boolean | undefined };
747
+ } finally {
748
+ await client?.close();
749
+ await withInfo.stop();
750
+ }
751
+ }
752
+
753
+ test("passes `registryId` to the application as it is", async () => {
754
+ expect(await callWith({ registryId: "central" })).toEqual({
755
+ registries: ["central"],
756
+ isError: undefined,
757
+ });
758
+ });
759
+
760
+ test("still takes the deprecated `registryUrl`", async () => {
761
+ expect(await callWith({ registryUrl: "https://registry.test/" })).toEqual({
762
+ registries: ["https://registry.test/"],
763
+ isError: undefined,
764
+ });
765
+ });
766
+
767
+ test("`registryId` wins when both are given", async () => {
768
+ expect(await callWith({ registryId: "central", registryUrl: "other" })).toEqual({
769
+ registries: ["central"],
770
+ isError: undefined,
771
+ });
772
+ });
773
+
774
+ test("without either it is an error and the application is not asked", async () => {
775
+ expect(await callWith({})).toEqual({ registries: [], isError: true });
776
+ });
777
+ });
package/src/server.ts CHANGED
@@ -3,10 +3,14 @@ import { createMcpHandler, McpServer, type McpHttpHandler } from "@modelcontextp
3
3
  import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
4
4
  import { randomUUID } from "node:crypto";
5
5
  import type { MiLogger } from "@milaboratories/helpers";
6
- import type { MiddleLayer } from "@milaboratories/pl-middle-layer";
6
+ import { asProjectHolder } from "@milaboratories/pl-middle-layer";
7
+ import type { MiddleLayer, Project } from "@milaboratories/pl-middle-layer";
7
8
  import type { BlockId, Branded, ProjectId } from "@milaboratories/pl-model-common";
8
9
  import { version } from "../package.json";
10
+ import { AgentTables, projectHoldsWithTables } from "./agent-tables";
9
11
  import { SERVER_INSTRUCTIONS } from "./instructions";
12
+ import type { ProjectHolds } from "./project-holds";
13
+ import { findListedProject } from "./project-list";
10
14
  import type { TxCounters } from "./server-log";
11
15
  import { ServerLog } from "./server-log";
12
16
  import type { ToolContext, ToolRegistrar } from "./tools/types";
@@ -15,37 +19,28 @@ import { registerPingTool } from "./tools/ping";
15
19
  import { registerConnectionTools } from "./tools/connection";
16
20
  import { registerProjectTools } from "./tools/projects";
17
21
  import { registerBlockTools } from "./tools/blocks";
22
+ import { registerBlockUpdateTools } from "./tools/block-updates";
18
23
  import { registerBlockParamsTools } from "./tools/block-params";
19
24
  import { registerBlockStateTools } from "./tools/block-state";
20
25
  import { registerBlockActionContextTools } from "./tools/block-action-context";
21
26
  import { registerAwaitTools } from "./tools/await";
22
27
  import { registerLogTools } from "./tools/logs";
23
28
  import { registerDataQueryTools } from "./tools/data-query";
24
- import { registerScreenshotTool } from "./tools/screenshot";
25
- import { registerUIInteractionTools } from "./tools/ui-interaction";
29
+ import { registerFileTools } from "./tools/files";
30
+ import { registerResultPoolTools } from "./tools/result-pool";
31
+ import {
32
+ AGENT_TABLE_IDLE_MS,
33
+ MAX_AGENT_TABLES,
34
+ registerAgentTableTools,
35
+ } from "./tools/agent-tables";
36
+ import { pendingUploadsOfProject } from "./tools/imports";
26
37
 
27
38
  export interface PlMcpServerCallbacks {
28
39
  onProjectCreated?: (projectId: ProjectId) => void | Promise<void>;
29
- onProjectOpened?: (projectId: ProjectId) => void | Promise<void>;
30
- onProjectClosed?: (projectId: ProjectId) => void | Promise<void>;
31
40
  onProjectDeleted?: (projectId: ProjectId) => void | Promise<void>;
32
- /** Capture the current application window as a PNG screenshot. Returns base64-encoded PNG. */
33
- captureScreenshot?: () => Promise<string>;
34
- /** Send an input event to the application window. */
35
- sendInputEvent?: (event: unknown) => Promise<void>;
36
- /**
37
- * Execute JavaScript in a renderer and return the result.
38
- * With no `target`, runs in the topmost webContents (usually the main app).
39
- * With `target`, runs in the specified block's webview (where `window.platforma` is
40
- * exposed) — the block must already be loaded (e.g. via `select_block`).
41
- */
42
- executeJavaScript?: (
43
- code: string,
44
- target?: { projectId: ProjectId; blockId: BlockId },
45
- ) => Promise<unknown>;
46
41
  /** List available blocks from all configured registries. */
47
42
  listAvailableBlocks?: (query?: string) => Promise<unknown[]>;
48
- /** Navigate the desktop UI to show a specific block. */
43
+ /** Show a block to the user: the application's window navigates to it. */
49
44
  selectBlock?: (projectId: ProjectId, blockId: BlockId) => Promise<void>;
50
45
  /** Read recent lines from the application log. */
51
46
  readAppLog?: (lines: number, search?: string) => Promise<string>;
@@ -66,9 +61,12 @@ export interface PlMcpServerCallbacks {
66
61
  }>;
67
62
  /** Disconnect from current server. */
68
63
  disconnect?: () => Promise<void>;
69
- /** Get detailed info about a specific block package. */
64
+ /**
65
+ * Get detailed info about a specific block package. `registry` is what the agent gave: the
66
+ * `registryId` list_available_blocks returns, or the address of a remote registry.
67
+ */
70
68
  getBlockInfo?: (
71
- registryUrl: string,
69
+ registry: string,
72
70
  organization: string,
73
71
  name: string,
74
72
  version: string,
@@ -112,8 +110,20 @@ export interface PlMcpServerOptions {
112
110
  * written as it is. Without a logger the server writes nothing.
113
111
  */
114
112
  logger?: MiLogger;
113
+ /**
114
+ * How long, in milliseconds, the server keeps holding a project after the last tool call on it
115
+ * ended. Holding keeps the project loaded, so the next call does not load it again; the price is
116
+ * the project's maintenance loop running meanwhile. A project whose blocks still have files to
117
+ * upload is held on while the uploads move; after 5 idle times in a row with no byte uploaded
118
+ * (15 minutes at the default) it is let go, and the log says so. Default
119
+ * {@link DEFAULT_PROJECT_IDLE_RELEASE_MS}.
120
+ */
121
+ projectIdleReleaseMs?: number;
115
122
  }
116
123
 
124
+ /** Default of {@link PlMcpServerOptions.projectIdleReleaseMs}: three minutes. */
125
+ export const DEFAULT_PROJECT_IDLE_RELEASE_MS = 180_000;
126
+
117
127
  export class PlMcpServer {
118
128
  private ml: MiddleLayer | null;
119
129
  private port: number;
@@ -129,6 +139,16 @@ export class PlMcpServer {
129
139
  private readonly toolContext: ToolContext;
130
140
  /** Writes the server's log; absent when the server was given no logger. */
131
141
  private readonly log: ServerLog | undefined;
142
+ /** The server's holds on the projects its tools work on. */
143
+ private readonly holds: ProjectHolds<Project>;
144
+ /**
145
+ * The tables agents built with create_table. A table goes with the server's hold on its
146
+ * project, at the latest.
147
+ */
148
+ private readonly agentTables = new AgentTables({
149
+ idleMs: AGENT_TABLE_IDLE_MS,
150
+ maxTables: MAX_AGENT_TABLES,
151
+ });
132
152
 
133
153
  constructor(options: PlMcpServerOptions) {
134
154
  this.ml = options.middleLayer ?? null;
@@ -146,19 +166,47 @@ export class PlMcpServer {
146
166
  secrets: [this.secret],
147
167
  })
148
168
  : undefined;
169
+ // Every hold the server takes carries this label, so the middle layer can tell them from the
170
+ // holds of the application and of other servers.
171
+ const projectHolder = asProjectHolder(`mcp:${instanceId}`);
172
+ // The agent tables of a project go with the server's hold on it, but not under a call still
173
+ // running on the project.
174
+ this.holds = projectHoldsWithTables<Project>(this.agentTables, {
175
+ holder: projectHolder,
176
+ idleMs: options.projectIdleReleaseMs ?? DEFAULT_PROJECT_IDLE_RELEASE_MS,
177
+ onRelease: (projectId, reason) => this.log?.projectReleased(projectId, reason),
178
+ // Q-002: an upload runs only while its project is loaded, so a project with files still
179
+ // uploading stays held after the agent's last call, while the uploads move.
180
+ pendingWork: async (_projectId, project) => {
181
+ const uploads = await pendingUploadsOfProject(project);
182
+ return { count: uploads.count, progress: uploads.bytesProcessed };
183
+ },
184
+ onKeep: (projectId, reason, pending) => this.log?.projectKept(projectId, reason, pending),
185
+ });
149
186
  this.toolContext = {
150
187
  getMl: () => this.ml,
151
188
  requireMl: () => this.requireMl(),
152
189
  resolveProject: (id) => this.resolveProject(id),
153
- getOpenedProject: (id) => this.getOpenedProject(id),
190
+ withProject: (id, fn) => this.withProject(id, fn),
191
+ agentTables: this.agentTables,
192
+ projectHolder,
193
+ callsInFlight: (projectId) => this.holds.callsInFlight(projectId),
154
194
  callbacks: this.callbacks,
155
195
  getAuthorMarker: authorMarkerSource(instanceId),
156
196
  };
157
197
  }
158
198
 
159
- /** Set or update the MiddleLayer instance (e.g. after connecting to a server). */
199
+ /**
200
+ * Set or update the MiddleLayer instance (e.g. after connecting to a server). The projects held
201
+ * on the previous one are given back.
202
+ */
160
203
  setMiddleLayer(ml: MiddleLayer | null | undefined) {
161
- this.ml = ml ?? null;
204
+ const next = ml ?? null;
205
+ if (next !== this.ml) {
206
+ this.holds.releaseAll();
207
+ this.agentTables.releaseAll();
208
+ }
209
+ this.ml = next;
162
210
  }
163
211
 
164
212
  get url(): string {
@@ -260,6 +308,8 @@ export class PlMcpServer {
260
308
  }
261
309
 
262
310
  async stop(): Promise<void> {
311
+ this.holds.releaseAll();
312
+ this.agentTables.releaseAll();
263
313
  const handler = this.handler;
264
314
  this.handler = undefined;
265
315
  await handler?.close();
@@ -306,21 +356,29 @@ export class PlMcpServer {
306
356
  return this.ml;
307
357
  }
308
358
 
309
- /** Resolves a project from the list by its projectId. */
310
- private async resolveProject(projectId: ProjectId) {
311
- const ml = this.requireMl();
312
- await ml.projectList.refreshState();
313
- const projects = await ml.projectList.awaitStableValue();
314
- const entry = projects.find((p) => p.id === projectId);
359
+ /**
360
+ * Resolves a project from the list by its projectId: the list as the middle layer last read it,
361
+ * read again from the backend only when the project is not in it (see {@link findListedProject}).
362
+ */
363
+ private async resolveProject(projectId: ProjectId, ml: MiddleLayer = this.requireMl()) {
364
+ const entry = await findListedProject(ml.projectList, projectId);
315
365
  if (!entry) throw new Error(`Project ${projectId} not found`);
316
366
  return entry;
317
367
  }
318
368
 
319
- /** Gets an opened project by projectId. */
320
- private async getOpenedProject(projectId: ProjectId) {
369
+ /**
370
+ * Runs `fn` on the project, loading it when nobody holds it. The project is held while `fn`
371
+ * runs, so nothing else can destroy it under the call, and for the idle time after. `fn` is
372
+ * also given the middle layer the project is of.
373
+ */
374
+ private async withProject<T>(
375
+ projectId: ProjectId,
376
+ fn: (project: Project, ml: MiddleLayer) => Promise<T>,
377
+ ): Promise<T> {
378
+ // The project and what `fn` is given of the middle layer come from the same one.
321
379
  const ml = this.requireMl();
322
- const entry = await this.resolveProject(projectId);
323
- return ml.getOpenedProject(entry.id);
380
+ const entry = await this.resolveProject(projectId, ml);
381
+ return await this.holds.use(ml, entry.id, (project) => fn(project, ml));
324
382
  }
325
383
  }
326
384
 
@@ -330,12 +388,14 @@ function registerAllTools(tools: ToolRegistrar, ctx: ToolContext): void {
330
388
  registerConnectionTools(tools, ctx);
331
389
  registerProjectTools(tools, ctx);
332
390
  registerBlockTools(tools, ctx);
391
+ registerBlockUpdateTools(tools, ctx);
333
392
  registerBlockParamsTools(tools, ctx);
334
393
  registerBlockStateTools(tools, ctx);
335
394
  registerBlockActionContextTools(tools, ctx);
336
395
  registerAwaitTools(tools, ctx);
337
396
  registerLogTools(tools, ctx);
338
397
  registerDataQueryTools(tools, ctx);
339
- registerScreenshotTool(tools, ctx);
340
- registerUIInteractionTools(tools, ctx);
398
+ registerFileTools(tools, ctx);
399
+ registerResultPoolTools(tools, ctx);
400
+ registerAgentTableTools(tools, ctx);
341
401
  }