@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.
- package/README.md +15 -12
- package/dist/instructions.cjs +3 -2
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +3 -2
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +72 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +72 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/server-log.cjs +8 -0
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +8 -0
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +29 -6
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +19 -7
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +29 -7
- package/dist/server.js.map +1 -1
- package/dist/tools/await.cjs +59 -58
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +59 -58
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +11 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +11 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-params.cjs +4 -3
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +4 -3
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +46 -41
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +46 -41
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/blocks.cjs +30 -20
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +30 -20
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +111 -80
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +113 -82
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/ids.cjs +16 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +13 -1
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/logs.cjs +28 -26
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +28 -26
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/projects.cjs +10 -27
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +11 -28
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js.map +1 -1
- package/package.json +5 -5
- package/src/instructions.ts +3 -2
- package/src/project-holds.test.ts +183 -0
- package/src/project-holds.ts +105 -0
- package/src/server-log.test.ts +17 -5
- package/src/server-log.ts +9 -0
- package/src/server.logging.test.ts +8 -5
- package/src/server.test.ts +15 -5
- package/src/server.ts +42 -10
- package/src/tools/await.ts +94 -93
- package/src/tools/block-action-context.ts +14 -13
- package/src/tools/block-params.ts +4 -3
- package/src/tools/block-state.ts +58 -56
- package/src/tools/blocks.ts +47 -37
- package/src/tools/data-query.ts +148 -104
- package/src/tools/ids.ts +22 -1
- package/src/tools/logs.ts +41 -40
- package/src/tools/projects.ts +20 -42
- package/src/tools/types.ts +8 -1
package/src/server-log.test.ts
CHANGED
|
@@ -102,7 +102,11 @@ describe("a call that succeeds", () => {
|
|
|
102
102
|
|
|
103
103
|
test("takes an id only from a string argument", async () => {
|
|
104
104
|
const { log, at, returning } = logOnManualClock();
|
|
105
|
-
await log.run(
|
|
105
|
+
await log.run(
|
|
106
|
+
"get_project_overview",
|
|
107
|
+
{ projectId: 5, blockId: { id: "b" } },
|
|
108
|
+
returning(textResult(1)),
|
|
109
|
+
);
|
|
106
110
|
|
|
107
111
|
expect(parse(at("info")[0] ?? "")).not.toHaveProperty("project");
|
|
108
112
|
expect(parse(at("info")[0] ?? "")).not.toHaveProperty("block");
|
|
@@ -447,8 +451,12 @@ describe("argument values", () => {
|
|
|
447
451
|
const { log, at, returning } = logOnManualClock();
|
|
448
452
|
const odd = 'p 1"\noutcome=ok';
|
|
449
453
|
|
|
450
|
-
await log.run("
|
|
451
|
-
await log.run(
|
|
454
|
+
await log.run("get_project_overview", { projectId: odd }, returning(textResult(1)));
|
|
455
|
+
await log.run(
|
|
456
|
+
"get_project_overview",
|
|
457
|
+
{ projectId: "x".repeat(5000) },
|
|
458
|
+
returning(textResult(1)),
|
|
459
|
+
);
|
|
452
460
|
|
|
453
461
|
const [quoted, long] = at("info");
|
|
454
462
|
expect(quoted).not.toContain("\n");
|
|
@@ -635,7 +643,11 @@ describe("a secret given to the log", () => {
|
|
|
635
643
|
secrets: [secret],
|
|
636
644
|
});
|
|
637
645
|
|
|
638
|
-
await log.run(
|
|
646
|
+
await log.run(
|
|
647
|
+
"get_project_overview",
|
|
648
|
+
{ projectId: secret },
|
|
649
|
+
returning(errorResult(`at /${secret}/`)),
|
|
650
|
+
);
|
|
639
651
|
await expect(
|
|
640
652
|
log.run("ping", undefined, throwing(new Error(`http://127.0.0.1:1/${secret}/mcp`))),
|
|
641
653
|
).rejects.toThrow(secret);
|
|
@@ -656,7 +668,7 @@ describe("a secret given to the log", () => {
|
|
|
656
668
|
log.requestFailed("POST", 500, new Error(message));
|
|
657
669
|
await expect(log.run("ping", undefined, throwing(new Error(message)))).rejects.toThrow();
|
|
658
670
|
await log.run(
|
|
659
|
-
"
|
|
671
|
+
"get_project_overview",
|
|
660
672
|
{ projectId: `${"y".repeat(190)}${secret}` },
|
|
661
673
|
returning(textResult(1)),
|
|
662
674
|
);
|
package/src/server-log.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { MiLogger } from "@milaboratories/helpers";
|
|
2
|
+
import type { ProjectId } from "@milaboratories/pl-model-common";
|
|
2
3
|
import { inspect } from "node:util";
|
|
3
4
|
import type { McpInstanceId } from "./server";
|
|
4
5
|
import type { ToolRegistrar } from "./tools/types";
|
|
@@ -194,6 +195,14 @@ export class ServerLog {
|
|
|
194
195
|
);
|
|
195
196
|
}
|
|
196
197
|
|
|
198
|
+
/** The server gave back its hold on a project it had kept loaded between calls. */
|
|
199
|
+
projectReleased(projectId: ProjectId, reason: string): void {
|
|
200
|
+
this.write(
|
|
201
|
+
"info",
|
|
202
|
+
() => `mcp-project-release ${fields({ id: this.instanceId, project: projectId, reason })}`,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
197
206
|
/**
|
|
198
207
|
* An HTTP request failed outside the MCP handler. `status` is what it is answered with, absent
|
|
199
208
|
* when the answer had already begun.
|
|
@@ -235,7 +235,10 @@ describe("the tools of the server", () => {
|
|
|
235
235
|
const { connect, lines } = await startLogged();
|
|
236
236
|
const client = await connect();
|
|
237
237
|
|
|
238
|
-
const result = await client.callTool({
|
|
238
|
+
const result = await client.callTool({
|
|
239
|
+
name: "get_project_overview",
|
|
240
|
+
arguments: { projectId: 5 },
|
|
241
|
+
});
|
|
239
242
|
|
|
240
243
|
expect(text(result)).toContain("Input validation error");
|
|
241
244
|
expect(callLines({ lines })).toEqual([]);
|
|
@@ -292,8 +295,8 @@ describe("a logger that throws", () => {
|
|
|
292
295
|
for (const call of [
|
|
293
296
|
{ name: "ping", arguments: {} },
|
|
294
297
|
{ name: "capture_screenshot", arguments: {} },
|
|
295
|
-
{ name: "
|
|
296
|
-
{ name: "
|
|
298
|
+
{ name: "get_project_overview", arguments: { projectId: "p-1" } },
|
|
299
|
+
{ name: "get_project_overview", arguments: { projectId: 5 } },
|
|
297
300
|
]) {
|
|
298
301
|
const answer = await broken.callTool(call);
|
|
299
302
|
expect(answer).toEqual(await silent.callTool(call));
|
|
@@ -313,7 +316,7 @@ describe("a server given no logger", () => {
|
|
|
313
316
|
for (const call of [
|
|
314
317
|
{ name: "ping", arguments: {} },
|
|
315
318
|
{ name: "capture_screenshot", arguments: {} },
|
|
316
|
-
{ name: "
|
|
319
|
+
{ name: "get_project_overview", arguments: { projectId: "p-1" } },
|
|
317
320
|
])
|
|
318
321
|
expect(await without.callTool(call)).toEqual(await withLog.callTool(call));
|
|
319
322
|
expect(callLines(logged)).toHaveLength(5);
|
|
@@ -351,7 +354,7 @@ describe("the server's own lines", () => {
|
|
|
351
354
|
const client = await connect();
|
|
352
355
|
|
|
353
356
|
await client.callTool({ name: "ping", arguments: {} });
|
|
354
|
-
await client.callTool({ name: "
|
|
357
|
+
await client.callTool({ name: "get_project_overview", arguments: { projectId: secret } });
|
|
355
358
|
await client.callTool({ name: "capture_screenshot", arguments: {} });
|
|
356
359
|
await fetch(server.url, { method: "POST", headers: notJson, body: "ping" });
|
|
357
360
|
await fetch(server.url, { method: "GET" });
|
package/src/server.test.ts
CHANGED
|
@@ -75,7 +75,7 @@ describe.each(eras)("to a $era client", (era) => {
|
|
|
75
75
|
|
|
76
76
|
test("sends instructions that fit the length clients keep", () => {
|
|
77
77
|
const instructions = client.getInstructions() ?? "";
|
|
78
|
-
expect(instructions).toContain("
|
|
78
|
+
expect(instructions).toContain("list_projects");
|
|
79
79
|
expect(instructions.length).toBeGreaterThan(0);
|
|
80
80
|
expect(instructions.length).toBeLessThanOrEqual(4096);
|
|
81
81
|
});
|
|
@@ -87,6 +87,15 @@ describe.each(eras)("to a $era client", (era) => {
|
|
|
87
87
|
expect(mentioned.filter((name) => !known.has(name))).toEqual([]);
|
|
88
88
|
});
|
|
89
89
|
|
|
90
|
+
// A project is loaded by whatever tool needs it; an agent is never asked to open one.
|
|
91
|
+
test("nothing it says asks for a project to be opened", () => {
|
|
92
|
+
const said = [
|
|
93
|
+
client.getInstructions() ?? "",
|
|
94
|
+
...tools.map((tool) => JSON.stringify(tool)),
|
|
95
|
+
].join("\n");
|
|
96
|
+
expect(said).not.toMatch(/open_project|close_project|\bopened\b|must be open/i);
|
|
97
|
+
});
|
|
98
|
+
|
|
90
99
|
test("says its set of tools does not change", () => {
|
|
91
100
|
expect(client.getServerCapabilities()?.tools).toEqual({ listChanged: false });
|
|
92
101
|
});
|
|
@@ -183,7 +192,6 @@ describe.each(eras)("to a $era client", (era) => {
|
|
|
183
192
|
"await_block_done",
|
|
184
193
|
"capture_screenshot",
|
|
185
194
|
"click",
|
|
186
|
-
"close_project",
|
|
187
195
|
"connect_to_server",
|
|
188
196
|
"create_project",
|
|
189
197
|
"delete_project",
|
|
@@ -202,7 +210,6 @@ describe.each(eras)("to a $era client", (era) => {
|
|
|
202
210
|
"list_columns",
|
|
203
211
|
"list_connections",
|
|
204
212
|
"list_projects",
|
|
205
|
-
"open_project",
|
|
206
213
|
"ping",
|
|
207
214
|
"press_key",
|
|
208
215
|
"query_table",
|
|
@@ -229,7 +236,7 @@ describe.each(eras)("to a $era client", (era) => {
|
|
|
229
236
|
|
|
230
237
|
test("required arguments are the ones without `optional` or a default", () => {
|
|
231
238
|
expect(inputSchemaOf("add_block").required).toEqual(["projectId", "label", "spec"]);
|
|
232
|
-
expect(inputSchemaOf("
|
|
239
|
+
expect(inputSchemaOf("get_project_overview").required).toEqual(["projectId"]);
|
|
233
240
|
});
|
|
234
241
|
|
|
235
242
|
test("an optional argument keeps its description", () => {
|
|
@@ -251,7 +258,10 @@ describe.each(eras)("to a $era client", (era) => {
|
|
|
251
258
|
});
|
|
252
259
|
|
|
253
260
|
test("arguments of the wrong type are refused before the tool runs", async () => {
|
|
254
|
-
const result = await client.callTool({
|
|
261
|
+
const result = await client.callTool({
|
|
262
|
+
name: "get_project_overview",
|
|
263
|
+
arguments: { projectId: 5 },
|
|
264
|
+
});
|
|
255
265
|
expect(result.isError).toBe(true);
|
|
256
266
|
expect(JSON.stringify(result.content)).toContain("Input validation error");
|
|
257
267
|
});
|
package/src/server.ts
CHANGED
|
@@ -3,10 +3,12 @@ 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
|
|
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";
|
|
9
10
|
import { SERVER_INSTRUCTIONS } from "./instructions";
|
|
11
|
+
import { ProjectHolds } from "./project-holds";
|
|
10
12
|
import type { TxCounters } from "./server-log";
|
|
11
13
|
import { ServerLog } from "./server-log";
|
|
12
14
|
import type { ToolContext, ToolRegistrar } from "./tools/types";
|
|
@@ -26,8 +28,6 @@ import { registerUIInteractionTools } from "./tools/ui-interaction";
|
|
|
26
28
|
|
|
27
29
|
export interface PlMcpServerCallbacks {
|
|
28
30
|
onProjectCreated?: (projectId: ProjectId) => void | Promise<void>;
|
|
29
|
-
onProjectOpened?: (projectId: ProjectId) => void | Promise<void>;
|
|
30
|
-
onProjectClosed?: (projectId: ProjectId) => void | Promise<void>;
|
|
31
31
|
onProjectDeleted?: (projectId: ProjectId) => void | Promise<void>;
|
|
32
32
|
/** Capture the current application window as a PNG screenshot. Returns base64-encoded PNG. */
|
|
33
33
|
captureScreenshot?: () => Promise<string>;
|
|
@@ -45,7 +45,7 @@ export interface PlMcpServerCallbacks {
|
|
|
45
45
|
) => Promise<unknown>;
|
|
46
46
|
/** List available blocks from all configured registries. */
|
|
47
47
|
listAvailableBlocks?: (query?: string) => Promise<unknown[]>;
|
|
48
|
-
/**
|
|
48
|
+
/** Show a block to the user: the application's window navigates to it. */
|
|
49
49
|
selectBlock?: (projectId: ProjectId, blockId: BlockId) => Promise<void>;
|
|
50
50
|
/** Read recent lines from the application log. */
|
|
51
51
|
readAppLog?: (lines: number, search?: string) => Promise<string>;
|
|
@@ -112,8 +112,17 @@ export interface PlMcpServerOptions {
|
|
|
112
112
|
* written as it is. Without a logger the server writes nothing.
|
|
113
113
|
*/
|
|
114
114
|
logger?: MiLogger;
|
|
115
|
+
/**
|
|
116
|
+
* How long, in milliseconds, the server keeps holding a project after the last tool call on it
|
|
117
|
+
* ended. Holding keeps the project loaded, so the next call does not load it again; the price is
|
|
118
|
+
* the project's maintenance loop running meanwhile. Default {@link DEFAULT_PROJECT_IDLE_RELEASE_MS}.
|
|
119
|
+
*/
|
|
120
|
+
projectIdleReleaseMs?: number;
|
|
115
121
|
}
|
|
116
122
|
|
|
123
|
+
/** Default of {@link PlMcpServerOptions.projectIdleReleaseMs}: three minutes. */
|
|
124
|
+
export const DEFAULT_PROJECT_IDLE_RELEASE_MS = 180_000;
|
|
125
|
+
|
|
117
126
|
export class PlMcpServer {
|
|
118
127
|
private ml: MiddleLayer | null;
|
|
119
128
|
private port: number;
|
|
@@ -129,6 +138,8 @@ export class PlMcpServer {
|
|
|
129
138
|
private readonly toolContext: ToolContext;
|
|
130
139
|
/** Writes the server's log; absent when the server was given no logger. */
|
|
131
140
|
private readonly log: ServerLog | undefined;
|
|
141
|
+
/** The server's holds on the projects its tools work on. */
|
|
142
|
+
private readonly holds: ProjectHolds<Project>;
|
|
132
143
|
|
|
133
144
|
constructor(options: PlMcpServerOptions) {
|
|
134
145
|
this.ml = options.middleLayer ?? null;
|
|
@@ -146,19 +157,33 @@ export class PlMcpServer {
|
|
|
146
157
|
secrets: [this.secret],
|
|
147
158
|
})
|
|
148
159
|
: undefined;
|
|
160
|
+
// Every hold the server takes carries this label, so the middle layer can tell them from the
|
|
161
|
+
// holds of the application and of other servers.
|
|
162
|
+
const projectHolder = asProjectHolder(`mcp:${instanceId}`);
|
|
163
|
+
this.holds = new ProjectHolds<Project>({
|
|
164
|
+
holder: projectHolder,
|
|
165
|
+
idleMs: options.projectIdleReleaseMs ?? DEFAULT_PROJECT_IDLE_RELEASE_MS,
|
|
166
|
+
onRelease: (projectId, reason) => this.log?.projectReleased(projectId, reason),
|
|
167
|
+
});
|
|
149
168
|
this.toolContext = {
|
|
150
169
|
getMl: () => this.ml,
|
|
151
170
|
requireMl: () => this.requireMl(),
|
|
152
171
|
resolveProject: (id) => this.resolveProject(id),
|
|
153
|
-
|
|
172
|
+
withProject: (id, fn) => this.withProject(id, fn),
|
|
173
|
+
projectHolder,
|
|
154
174
|
callbacks: this.callbacks,
|
|
155
175
|
getAuthorMarker: authorMarkerSource(instanceId),
|
|
156
176
|
};
|
|
157
177
|
}
|
|
158
178
|
|
|
159
|
-
/**
|
|
179
|
+
/**
|
|
180
|
+
* Set or update the MiddleLayer instance (e.g. after connecting to a server). The projects held
|
|
181
|
+
* on the previous one are given back.
|
|
182
|
+
*/
|
|
160
183
|
setMiddleLayer(ml: MiddleLayer | null | undefined) {
|
|
161
|
-
|
|
184
|
+
const next = ml ?? null;
|
|
185
|
+
if (next !== this.ml) this.holds.releaseAll();
|
|
186
|
+
this.ml = next;
|
|
162
187
|
}
|
|
163
188
|
|
|
164
189
|
get url(): string {
|
|
@@ -260,6 +285,7 @@ export class PlMcpServer {
|
|
|
260
285
|
}
|
|
261
286
|
|
|
262
287
|
async stop(): Promise<void> {
|
|
288
|
+
this.holds.releaseAll();
|
|
263
289
|
const handler = this.handler;
|
|
264
290
|
this.handler = undefined;
|
|
265
291
|
await handler?.close();
|
|
@@ -316,11 +342,17 @@ export class PlMcpServer {
|
|
|
316
342
|
return entry;
|
|
317
343
|
}
|
|
318
344
|
|
|
319
|
-
/**
|
|
320
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Runs `fn` on the project, loading it when nobody holds it. The project is held while `fn`
|
|
347
|
+
* runs, so nothing else can destroy it under the call, and for the idle time after.
|
|
348
|
+
*/
|
|
349
|
+
private async withProject<T>(
|
|
350
|
+
projectId: ProjectId,
|
|
351
|
+
fn: (project: Project) => Promise<T>,
|
|
352
|
+
): Promise<T> {
|
|
321
353
|
const ml = this.requireMl();
|
|
322
354
|
const entry = await this.resolveProject(projectId);
|
|
323
|
-
return
|
|
355
|
+
return await this.holds.use(ml, entry.id, fn);
|
|
324
356
|
}
|
|
325
357
|
}
|
|
326
358
|
|
package/src/tools/await.ts
CHANGED
|
@@ -37,114 +37,115 @@ export function registerAwaitTools(server: ToolRegistrar, ctx: ToolContext): voi
|
|
|
37
37
|
}),
|
|
38
38
|
},
|
|
39
39
|
async ({ projectId, blockId, timeout, transform, transformTimeout }) => {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
41
|
+
const deadline = Date.now() + timeout;
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// Terminal error states — return immediately
|
|
54
|
-
if (block.calculationStatus === "Limbo") {
|
|
55
|
-
return errorResult(
|
|
56
|
-
"Block entered Limbo state (upstream failed or was stopped).",
|
|
57
|
-
"Check upstream blocks with get_project_overview. Fix or re-run the failed upstream, then retry.",
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (block.calculationStatus === "NotCalculated") {
|
|
62
|
-
return errorResult(
|
|
63
|
-
"Block has not been started.",
|
|
64
|
-
"Use run_block to start it first, then call await_block_done.",
|
|
65
|
-
);
|
|
66
|
-
}
|
|
43
|
+
while (Date.now() < deadline) {
|
|
44
|
+
const overview = await project.overview.getValue();
|
|
45
|
+
if (!overview) continue;
|
|
46
|
+
const block = overview.blocks.find((b) => b.id === blockId);
|
|
47
|
+
if (!block)
|
|
48
|
+
return errorResult(
|
|
49
|
+
`Block ${blockId} not found in project ${projectId}.`,
|
|
50
|
+
"Use get_project_overview to list all block IDs in this project.",
|
|
51
|
+
);
|
|
67
52
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const remaining = Math.max(deadline - Date.now(), 1000);
|
|
71
|
-
let state;
|
|
72
|
-
try {
|
|
73
|
-
state = await project
|
|
74
|
-
.getBlockState(blockId)
|
|
75
|
-
.awaitStableValue(AbortSignal.timeout(remaining));
|
|
76
|
-
} catch (e: unknown) {
|
|
77
|
-
if (isTimeoutError(e)) {
|
|
78
|
-
return timedOutResult({
|
|
79
|
-
status: "Done",
|
|
80
|
-
note: "Computation done but outputs did not stabilize in time. Retry with a longer timeout.",
|
|
81
|
-
});
|
|
82
|
-
}
|
|
53
|
+
// Terminal error states — return immediately
|
|
54
|
+
if (block.calculationStatus === "Limbo") {
|
|
83
55
|
return errorResult(
|
|
84
|
-
|
|
56
|
+
"Block entered Limbo state (upstream failed or was stopped).",
|
|
57
|
+
"Check upstream blocks with get_project_overview. Fix or re-run the failed upstream, then retry.",
|
|
85
58
|
);
|
|
86
59
|
}
|
|
87
60
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
canRun: block.canRun,
|
|
95
|
-
stale: block.stale,
|
|
96
|
-
outputErrors: block.outputErrors,
|
|
97
|
-
};
|
|
61
|
+
if (block.calculationStatus === "NotCalculated") {
|
|
62
|
+
return errorResult(
|
|
63
|
+
"Block has not been started.",
|
|
64
|
+
"Use run_block to start it first, then call await_block_done.",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
98
67
|
|
|
99
|
-
if (
|
|
68
|
+
if (block.calculationStatus === "Done") {
|
|
69
|
+
// Await stable block state with remaining time budget
|
|
70
|
+
const remaining = Math.max(deadline - Date.now(), 1000);
|
|
71
|
+
let state;
|
|
100
72
|
try {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
data,
|
|
105
|
-
outputs: state.outputs,
|
|
106
|
-
block: blockInfo,
|
|
107
|
-
},
|
|
108
|
-
transformTimeout,
|
|
109
|
-
);
|
|
110
|
-
return textResult({ status: "Done", block: blockInfo, result });
|
|
73
|
+
state = await project
|
|
74
|
+
.getBlockState(blockId)
|
|
75
|
+
.awaitStableValue(AbortSignal.timeout(remaining));
|
|
111
76
|
} catch (e: unknown) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
77
|
+
if (isTimeoutError(e)) {
|
|
78
|
+
return timedOutResult({
|
|
79
|
+
status: "Done",
|
|
80
|
+
note: "Computation done but outputs did not stabilize in time. Retry with a longer timeout.",
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return errorResult(
|
|
84
|
+
`Failed to get block state: ${e instanceof Error ? e.message : String(e)}`,
|
|
115
85
|
);
|
|
116
86
|
}
|
|
117
|
-
}
|
|
118
87
|
|
|
119
|
-
|
|
120
|
-
status: "Done",
|
|
121
|
-
block: blockInfo,
|
|
122
|
-
data,
|
|
123
|
-
outputs: summarizeOutputs(state.outputs as Record<string, unknown> | undefined),
|
|
124
|
-
});
|
|
125
|
-
}
|
|
88
|
+
const data = deriveDataFromStorage(state.blockStorage);
|
|
126
89
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
90
|
+
const blockInfo = {
|
|
91
|
+
id: block.id,
|
|
92
|
+
title: block.title ?? block.label,
|
|
93
|
+
calculationStatus: block.calculationStatus,
|
|
94
|
+
canRun: block.canRun,
|
|
95
|
+
stale: block.stale,
|
|
96
|
+
outputErrors: block.outputErrors,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (transform) {
|
|
100
|
+
try {
|
|
101
|
+
const result = await safeEval(
|
|
102
|
+
transform,
|
|
103
|
+
{
|
|
104
|
+
data,
|
|
105
|
+
outputs: state.outputs,
|
|
106
|
+
block: blockInfo,
|
|
107
|
+
},
|
|
108
|
+
transformTimeout,
|
|
109
|
+
);
|
|
110
|
+
return textResult({ status: "Done", block: blockInfo, result });
|
|
111
|
+
} catch (e: unknown) {
|
|
112
|
+
return transformFailedResult(
|
|
113
|
+
e,
|
|
114
|
+
"Check your JS expression syntax. Available variables: data, outputs, block.",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return textResult({
|
|
120
|
+
status: "Done",
|
|
121
|
+
block: blockInfo,
|
|
122
|
+
data,
|
|
123
|
+
outputs: summarizeOutputs(state.outputs as Record<string, unknown> | undefined),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Still running — wait up to 5s for overview to change, then re-poll.
|
|
128
|
+
// Minimum 500ms delay to avoid busy-looping if awaitChange resolves immediately.
|
|
129
|
+
const pollStart = Date.now();
|
|
130
|
+
try {
|
|
131
|
+
const result = await project.overview.getFullValue();
|
|
132
|
+
await project.overview.awaitChange(AbortSignal.timeout(5000), result.uTag);
|
|
133
|
+
} catch {
|
|
134
|
+
// timeout or abort — just re-poll
|
|
135
|
+
}
|
|
136
|
+
const elapsed = Date.now() - pollStart;
|
|
137
|
+
if (elapsed < 500) {
|
|
138
|
+
await new Promise((r) => setTimeout(r, 500 - elapsed));
|
|
139
|
+
}
|
|
139
140
|
}
|
|
140
|
-
}
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
142
|
+
// Timed out while running
|
|
143
|
+
const overview = await project.overview.getValue();
|
|
144
|
+
const block = overview?.blocks.find((b) => b.id === blockId);
|
|
145
|
+
return timedOutResult({
|
|
146
|
+
status: block?.calculationStatus ?? "Unknown",
|
|
147
|
+
hint: "The block is still running. Call await_block_done again with a longer timeout.",
|
|
148
|
+
});
|
|
148
149
|
});
|
|
149
150
|
},
|
|
150
151
|
);
|
|
@@ -71,7 +71,7 @@ export function registerBlockActionContextTools(server: ToolRegistrar, ctx: Tool
|
|
|
71
71
|
"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. " +
|
|
72
72
|
"Reads the current state; call again to refresh.",
|
|
73
73
|
inputSchema: z.object({
|
|
74
|
-
projectId: projectIdInput.describe("Project ID
|
|
74
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
75
75
|
blockId: blockIdInput.describe("Block ID"),
|
|
76
76
|
mode: z
|
|
77
77
|
.enum(["prod", "staging"])
|
|
@@ -98,20 +98,21 @@ export function registerBlockActionContextTools(server: ToolRegistrar, ctx: Tool
|
|
|
98
98
|
}),
|
|
99
99
|
},
|
|
100
100
|
async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
102
|
+
const tree = await project.getBlockActionContext(blockId, mode ?? "prod");
|
|
103
|
+
if (tree === undefined)
|
|
104
|
+
return errorResult(
|
|
105
|
+
"Block has no action context tree: it was not rendered yet, or the backend does not support block action context.",
|
|
106
|
+
);
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
108
|
+
const node = contextId === undefined ? tree : findBlockActionContextNode(tree, contextId);
|
|
109
|
+
if (node === undefined)
|
|
110
|
+
return errorResult(
|
|
111
|
+
`Action context ${contextId} is not in the action context tree of the block. Call without contextId to see the tree.`,
|
|
112
|
+
);
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
return textResult(limitBlockActionContextTree(node, { maxDepth, maxTransitions }));
|
|
115
|
+
});
|
|
115
116
|
},
|
|
116
117
|
);
|
|
117
118
|
}
|
|
@@ -17,13 +17,14 @@ export function registerBlockParamsTools(server: ToolRegistrar, ctx: ToolContext
|
|
|
17
17
|
"change an existing block use get_block_state and set_block_data.\n\n" +
|
|
18
18
|
"Returns `{ kind, params }`. `kind` is absent for a block that declares none.",
|
|
19
19
|
inputSchema: z.object({
|
|
20
|
-
projectId: projectIdInput.describe("Project ID
|
|
20
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
21
21
|
blockId: blockIdInput.describe("Block ID"),
|
|
22
22
|
}),
|
|
23
23
|
},
|
|
24
24
|
async ({ projectId, blockId }) => {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
26
|
+
return textResult(await project.getBlockInitializationParams(blockId));
|
|
27
|
+
});
|
|
27
28
|
},
|
|
28
29
|
);
|
|
29
30
|
}
|