@milaboratories/pl-mcp-server 53.1.0 → 54.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 (78) hide show
  1. package/README.md +15 -12
  2. package/dist/instructions.cjs +3 -2
  3. package/dist/instructions.cjs.map +1 -1
  4. package/dist/instructions.js +3 -2
  5. package/dist/instructions.js.map +1 -1
  6. package/dist/package.cjs +1 -1
  7. package/dist/package.js +1 -1
  8. package/dist/project-holds.cjs +72 -0
  9. package/dist/project-holds.cjs.map +1 -0
  10. package/dist/project-holds.js +72 -0
  11. package/dist/project-holds.js.map +1 -0
  12. package/dist/server-log.cjs +8 -0
  13. package/dist/server-log.cjs.map +1 -1
  14. package/dist/server-log.js +8 -0
  15. package/dist/server-log.js.map +1 -1
  16. package/dist/server.cjs +29 -6
  17. package/dist/server.cjs.map +1 -1
  18. package/dist/server.d.ts +19 -7
  19. package/dist/server.d.ts.map +1 -1
  20. package/dist/server.js +29 -7
  21. package/dist/server.js.map +1 -1
  22. package/dist/tools/await.cjs +59 -58
  23. package/dist/tools/await.cjs.map +1 -1
  24. package/dist/tools/await.js +59 -58
  25. package/dist/tools/await.js.map +1 -1
  26. package/dist/tools/block-action-context.cjs +11 -9
  27. package/dist/tools/block-action-context.cjs.map +1 -1
  28. package/dist/tools/block-action-context.js +11 -9
  29. package/dist/tools/block-action-context.js.map +1 -1
  30. package/dist/tools/block-params.cjs +4 -3
  31. package/dist/tools/block-params.cjs.map +1 -1
  32. package/dist/tools/block-params.js +4 -3
  33. package/dist/tools/block-params.js.map +1 -1
  34. package/dist/tools/block-state.cjs +46 -41
  35. package/dist/tools/block-state.cjs.map +1 -1
  36. package/dist/tools/block-state.js +46 -41
  37. package/dist/tools/block-state.js.map +1 -1
  38. package/dist/tools/blocks.cjs +30 -20
  39. package/dist/tools/blocks.cjs.map +1 -1
  40. package/dist/tools/blocks.js +30 -20
  41. package/dist/tools/blocks.js.map +1 -1
  42. package/dist/tools/data-query.cjs +111 -80
  43. package/dist/tools/data-query.cjs.map +1 -1
  44. package/dist/tools/data-query.js +113 -82
  45. package/dist/tools/data-query.js.map +1 -1
  46. package/dist/tools/ids.cjs +16 -0
  47. package/dist/tools/ids.cjs.map +1 -1
  48. package/dist/tools/ids.js +13 -1
  49. package/dist/tools/ids.js.map +1 -1
  50. package/dist/tools/logs.cjs +28 -26
  51. package/dist/tools/logs.cjs.map +1 -1
  52. package/dist/tools/logs.js +28 -26
  53. package/dist/tools/logs.js.map +1 -1
  54. package/dist/tools/projects.cjs +10 -27
  55. package/dist/tools/projects.cjs.map +1 -1
  56. package/dist/tools/projects.js +11 -28
  57. package/dist/tools/projects.js.map +1 -1
  58. package/dist/tools/types.cjs.map +1 -1
  59. package/dist/tools/types.js.map +1 -1
  60. package/package.json +5 -5
  61. package/src/instructions.ts +3 -2
  62. package/src/project-holds.test.ts +183 -0
  63. package/src/project-holds.ts +105 -0
  64. package/src/server-log.test.ts +17 -5
  65. package/src/server-log.ts +9 -0
  66. package/src/server.logging.test.ts +8 -5
  67. package/src/server.test.ts +15 -5
  68. package/src/server.ts +42 -10
  69. package/src/tools/await.ts +94 -93
  70. package/src/tools/block-action-context.ts +14 -13
  71. package/src/tools/block-params.ts +4 -3
  72. package/src/tools/block-state.ts +58 -56
  73. package/src/tools/blocks.ts +47 -37
  74. package/src/tools/data-query.ts +148 -104
  75. package/src/tools/ids.ts +22 -1
  76. package/src/tools/logs.ts +41 -40
  77. package/src/tools/projects.ts +20 -42
  78. package/src/tools/types.ts +8 -1
package/src/tools/logs.ts CHANGED
@@ -24,55 +24,56 @@ export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void
24
24
  }),
25
25
  },
26
26
  async ({ projectId, blockId, lines, sampleId }) => {
27
- const project = await ctx.getOpenedProject(projectId);
28
- const state = await project.getBlockState(blockId).getValue();
29
- if (!state.outputs)
30
- return errorResult(
31
- "Block has no outputs yet.",
32
- "The block may not have been run. Use get_project_overview to check its calculationStatus, then run_block if needed.",
33
- );
27
+ return await ctx.withProject(projectId, async (project) => {
28
+ const state = await project.getBlockState(blockId).getValue();
29
+ if (!state.outputs)
30
+ return errorResult(
31
+ "Block has no outputs yet.",
32
+ "The block may not have been run. Use get_project_overview to check its calculationStatus, then run_block if needed.",
33
+ );
34
34
 
35
- // Scan all outputs for log handles (log+ready:// or log+live://)
36
- const outputs = state.outputs as Record<
37
- string,
38
- { ok?: boolean; value?: { data?: { key: string[]; value: unknown }[] } }
39
- >;
40
- const logEntries: { outputKey: string; key: string[]; handle: AnyLogHandle }[] = [];
41
- for (const [outputKey, output] of Object.entries(outputs)) {
42
- if (!output?.ok || !output.value?.data) continue;
43
- for (const entry of output.value.data) {
44
- if (isAnyLogHandle(entry.value)) {
45
- logEntries.push({ outputKey, key: entry.key, handle: entry.value });
35
+ // Scan all outputs for log handles (log+ready:// or log+live://)
36
+ const outputs = state.outputs as Record<
37
+ string,
38
+ { ok?: boolean; value?: { data?: { key: string[]; value: unknown }[] } }
39
+ >;
40
+ const logEntries: { outputKey: string; key: string[]; handle: AnyLogHandle }[] = [];
41
+ for (const [outputKey, output] of Object.entries(outputs)) {
42
+ if (!output?.ok || !output.value?.data) continue;
43
+ for (const entry of output.value.data) {
44
+ if (isAnyLogHandle(entry.value)) {
45
+ logEntries.push({ outputKey, key: entry.key, handle: entry.value });
46
+ }
46
47
  }
47
48
  }
48
- }
49
49
 
50
- if (logEntries.length === 0) {
51
- return errorResult(
52
- "No log handles found in block outputs.",
53
- "This block may not produce logs, or it hasn't run yet. Use get_block_outputs to inspect available output types.",
54
- );
55
- }
50
+ if (logEntries.length === 0) {
51
+ return errorResult(
52
+ "No log handles found in block outputs.",
53
+ "This block may not produce logs, or it hasn't run yet. Use get_block_outputs to inspect available output types.",
54
+ );
55
+ }
56
56
 
57
- const logDriver = ctx.requireMl().driverKit.logDriver;
58
- const results: Record<string, string> = {};
57
+ const logDriver = ctx.requireMl().driverKit.logDriver;
58
+ const results: Record<string, string> = {};
59
59
 
60
- for (const entry of logEntries) {
61
- const key = entry.key.join("/");
62
- if (sampleId && !entry.key.includes(sampleId)) continue;
63
- try {
64
- const response = await logDriver.lastLines(entry.handle, lines);
65
- if (response.shouldUpdateHandle) {
66
- results[key] = "[log handle stale — block may still be running, retry later]";
67
- } else {
68
- results[key] = new TextDecoder().decode(response.data);
60
+ for (const entry of logEntries) {
61
+ const key = entry.key.join("/");
62
+ if (sampleId && !entry.key.includes(sampleId)) continue;
63
+ try {
64
+ const response = await logDriver.lastLines(entry.handle, lines);
65
+ if (response.shouldUpdateHandle) {
66
+ results[key] = "[log handle stale — block may still be running, retry later]";
67
+ } else {
68
+ results[key] = new TextDecoder().decode(response.data);
69
+ }
70
+ } catch (err) {
71
+ results[key] = `Error reading log: ${err}`;
69
72
  }
70
- } catch (err) {
71
- results[key] = `Error reading log: ${err}`;
72
73
  }
73
- }
74
74
 
75
- return textResult(results);
75
+ return textResult(results);
76
+ });
76
77
  },
77
78
  );
78
79
 
@@ -1,7 +1,8 @@
1
+ import { isProjectInUseError } from "@milaboratories/pl-middle-layer";
1
2
  import { z } from "zod";
2
3
  import { projectIdInput } from "./ids";
3
4
  import type { ToolContext, ToolRegistrar } from "./types";
4
- import { hints, textResult } from "./types";
5
+ import { errorResult, hints, textResult } from "./types";
5
6
 
6
7
  export function registerProjectTools(server: ToolRegistrar, ctx: ToolContext): void {
7
8
  server.registerTool(
@@ -9,7 +10,7 @@ export function registerProjectTools(server: ToolRegistrar, ctx: ToolContext): v
9
10
  {
10
11
  title: "List Projects",
11
12
  annotations: hints.read,
12
- description: "List all projects with their IDs, labels, and status",
13
+ description: "List all projects with their IDs, labels, and creation and modification times",
13
14
  },
14
15
  async () => {
15
16
  const ml = ctx.requireMl();
@@ -19,7 +20,6 @@ export function registerProjectTools(server: ToolRegistrar, ctx: ToolContext): v
19
20
  projects.map((p) => ({
20
21
  projectId: p.id,
21
22
  label: p.meta.label,
22
- opened: p.opened,
23
23
  created: p.created.toISOString(),
24
24
  lastModified: p.lastModified.toISOString(),
25
25
  })),
@@ -32,8 +32,7 @@ export function registerProjectTools(server: ToolRegistrar, ctx: ToolContext): v
32
32
  {
33
33
  title: "Create Project",
34
34
  annotations: hints.additive,
35
- description:
36
- "Create a new, empty project and return its ID. Call open_project before adding blocks to it.",
35
+ description: "Create a new, empty project and return its ID.",
37
36
  inputSchema: z.object({ label: z.string().describe("Project name") }),
38
37
  },
39
38
  async ({ label }) => {
@@ -43,52 +42,31 @@ export function registerProjectTools(server: ToolRegistrar, ctx: ToolContext): v
43
42
  },
44
43
  );
45
44
 
46
- server.registerTool(
47
- "open_project",
48
- {
49
- title: "Open Project",
50
- annotations: hints.idempotent,
51
- description: "Open a project for editing. Required before working with blocks.",
52
- inputSchema: z.object({
53
- projectId: projectIdInput.describe("Project ID from list_projects or create_project"),
54
- }),
55
- },
56
- async ({ projectId }) => {
57
- const entry = await ctx.resolveProject(projectId);
58
- await ctx.requireMl().openProject(entry.id);
59
- await ctx.callbacks.onProjectOpened?.(projectId);
60
- return textResult({ ok: true });
61
- },
62
- );
63
-
64
- server.registerTool(
65
- "close_project",
66
- {
67
- title: "Close Project",
68
- annotations: hints.idempotent,
69
- description:
70
- "Close an opened project, releasing its resources in the application. Nothing is deleted.",
71
- inputSchema: z.object({ projectId: projectIdInput.describe("Project ID") }),
72
- },
73
- async ({ projectId }) => {
74
- const entry = await ctx.resolveProject(projectId);
75
- await ctx.requireMl().closeProject(entry.id);
76
- await ctx.callbacks.onProjectClosed?.(projectId);
77
- return textResult({ ok: true });
78
- },
79
- );
80
-
81
45
  server.registerTool(
82
46
  "delete_project",
83
47
  {
84
48
  title: "Delete Project",
85
49
  annotations: hints.destructive,
86
- description: "Delete a project permanently. The project must be closed first.",
50
+ description:
51
+ "Delete a project permanently, with all its blocks and results. Refused while the project " +
52
+ "is in use by another agent or by the user in the application.",
87
53
  inputSchema: z.object({ projectId: projectIdInput.describe("Project ID") }),
88
54
  },
89
55
  async ({ projectId }) => {
90
56
  const entry = await ctx.resolveProject(projectId);
91
- await ctx.requireMl().deleteProject(entry.id);
57
+ try {
58
+ // This server's own holds, kept or of calls still running, end with the delete: a deleted
59
+ // project is not snapshotted. Anyone else's hold refuses it.
60
+ await ctx.requireMl().deleteProject(entry.id, {
61
+ refuseIfHeldBy: (holder) => holder !== ctx.projectHolder,
62
+ });
63
+ } catch (e: unknown) {
64
+ if (!isProjectInUseError(e)) throw e;
65
+ return errorResult(
66
+ `Project ${projectId} was not deleted: it is still in use by another agent or by the ` +
67
+ "user in the application.",
68
+ );
69
+ }
92
70
  await ctx.callbacks.onProjectDeleted?.(projectId);
93
71
  return textResult({ ok: true });
94
72
  },
@@ -2,6 +2,7 @@ import type {
2
2
  AuthorMarker,
3
3
  MiddleLayer,
4
4
  Project,
5
+ ProjectHolder,
5
6
  ProjectId,
6
7
  ProjectListEntry,
7
8
  } from "@milaboratories/pl-middle-layer";
@@ -21,7 +22,13 @@ export interface ToolContext {
21
22
  getMl: () => MiddleLayer | null;
22
23
  requireMl: () => MiddleLayer;
23
24
  resolveProject: (projectId: ProjectId) => Promise<ProjectListEntry>;
24
- getOpenedProject: (projectId: ProjectId) => Promise<Project>;
25
+ /**
26
+ * Runs `fn` on the project, loading it when it is not loaded. The server holds the project
27
+ * while `fn` runs, so neither the application nor anyone else can unload it under the call.
28
+ */
29
+ withProject: <T>(projectId: ProjectId, fn: (project: Project) => Promise<T>) => Promise<T>;
30
+ /** The holder of this server's holds, as the middle layer's `projectHolders` lists them. */
31
+ projectHolder: ProjectHolder;
25
32
  callbacks: PlMcpServerCallbacks;
26
33
  /**
27
34
  * Returns the marker for the next write made through MCP. All writes of one server instance carry