@milaboratories/pl-mcp-server 53.0.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 +36 -2
- 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 +373 -0
- package/dist/server-log.cjs.map +1 -0
- package/dist/server-log.js +371 -0
- package/dist/server-log.js.map +1 -0
- package/dist/server.cjs +77 -22
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +31 -7
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +77 -23
- package/dist/server.js.map +1 -1
- package/dist/tools/await.cjs +59 -60
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +60 -61
- 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 +47 -42
- 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/connection.cjs.map +1 -1
- package/dist/tools/connection.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 +114 -83
- 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/ping.cjs.map +1 -1
- package/dist/tools/ping.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/sandbox.cjs +110 -8
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +107 -9
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/screenshot.cjs +9 -1
- package/dist/tools/screenshot.cjs.map +1 -1
- package/dist/tools/screenshot.js +9 -1
- package/dist/tools/screenshot.js.map +1 -1
- package/dist/tools/types.cjs +29 -0
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +27 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/ui-interaction.cjs.map +1 -1
- package/dist/tools/ui-interaction.js.map +1 -1
- package/package.json +6 -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 +871 -0
- package/src/server-log.ts +504 -0
- package/src/server.logging.test.ts +383 -0
- package/src/server.request-failure.test.ts +49 -0
- package/src/server.test.ts +15 -5
- package/src/server.ts +111 -26
- package/src/test/recording-logger.ts +33 -0
- package/src/tools/await.ts +96 -98
- package/src/tools/block-action-context.ts +16 -16
- package/src/tools/block-params.ts +6 -6
- package/src/tools/block-state.ts +61 -60
- package/src/tools/blocks.ts +49 -40
- package/src/tools/connection.ts +2 -3
- package/src/tools/data-query.ts +151 -108
- package/src/tools/ids.ts +22 -1
- package/src/tools/logs.ts +43 -43
- package/src/tools/ping.ts +2 -3
- package/src/tools/projects.ts +22 -45
- package/src/tools/sandbox.test.ts +183 -0
- package/src/tools/sandbox.ts +126 -10
- package/src/tools/screenshot.ts +15 -4
- package/src/tools/types.test.ts +4 -3
- package/src/tools/types.ts +73 -3
- package/src/tools/ui-interaction.ts +2 -3
package/src/server.ts
CHANGED
|
@@ -2,11 +2,16 @@ import { toNodeHandler } from "@modelcontextprotocol/node";
|
|
|
2
2
|
import { createMcpHandler, McpServer, type McpHttpHandler } from "@modelcontextprotocol/server";
|
|
3
3
|
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
|
-
import type {
|
|
5
|
+
import type { MiLogger } from "@milaboratories/helpers";
|
|
6
|
+
import { asProjectHolder } from "@milaboratories/pl-middle-layer";
|
|
7
|
+
import type { MiddleLayer, Project } from "@milaboratories/pl-middle-layer";
|
|
6
8
|
import type { BlockId, Branded, ProjectId } from "@milaboratories/pl-model-common";
|
|
7
9
|
import { version } from "../package.json";
|
|
8
10
|
import { SERVER_INSTRUCTIONS } from "./instructions";
|
|
9
|
-
import
|
|
11
|
+
import { ProjectHolds } from "./project-holds";
|
|
12
|
+
import type { TxCounters } from "./server-log";
|
|
13
|
+
import { ServerLog } from "./server-log";
|
|
14
|
+
import type { ToolContext, ToolRegistrar } from "./tools/types";
|
|
10
15
|
import { authorMarkerSource } from "./tools/types";
|
|
11
16
|
import { registerPingTool } from "./tools/ping";
|
|
12
17
|
import { registerConnectionTools } from "./tools/connection";
|
|
@@ -23,8 +28,6 @@ import { registerUIInteractionTools } from "./tools/ui-interaction";
|
|
|
23
28
|
|
|
24
29
|
export interface PlMcpServerCallbacks {
|
|
25
30
|
onProjectCreated?: (projectId: ProjectId) => void | Promise<void>;
|
|
26
|
-
onProjectOpened?: (projectId: ProjectId) => void | Promise<void>;
|
|
27
|
-
onProjectClosed?: (projectId: ProjectId) => void | Promise<void>;
|
|
28
31
|
onProjectDeleted?: (projectId: ProjectId) => void | Promise<void>;
|
|
29
32
|
/** Capture the current application window as a PNG screenshot. Returns base64-encoded PNG. */
|
|
30
33
|
captureScreenshot?: () => Promise<string>;
|
|
@@ -42,7 +45,7 @@ export interface PlMcpServerCallbacks {
|
|
|
42
45
|
) => Promise<unknown>;
|
|
43
46
|
/** List available blocks from all configured registries. */
|
|
44
47
|
listAvailableBlocks?: (query?: string) => Promise<unknown[]>;
|
|
45
|
-
/**
|
|
48
|
+
/** Show a block to the user: the application's window navigates to it. */
|
|
46
49
|
selectBlock?: (projectId: ProjectId, blockId: BlockId) => Promise<void>;
|
|
47
50
|
/** Read recent lines from the application log. */
|
|
48
51
|
readAppLog?: (lines: number, search?: string) => Promise<string>;
|
|
@@ -82,6 +85,17 @@ export interface ServerConnection {
|
|
|
82
85
|
/** Branded type for the MCP server URL secret path segment. */
|
|
83
86
|
export type McpSecret = Branded<string, "McpSecret">;
|
|
84
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Id of one server instance, `mcp-<8 hex>`: the author id of every write the instance makes and the
|
|
90
|
+
* first part of every call id in its log.
|
|
91
|
+
*/
|
|
92
|
+
export type McpInstanceId = Branded<string, "McpInstanceId">;
|
|
93
|
+
|
|
94
|
+
/** Makes the id of a new server instance. */
|
|
95
|
+
function newMcpInstanceId(): McpInstanceId {
|
|
96
|
+
return `mcp-${randomUUID().slice(0, 8)}` as McpInstanceId;
|
|
97
|
+
}
|
|
98
|
+
|
|
85
99
|
export interface PlMcpServerOptions {
|
|
86
100
|
/** MiddleLayer instance providing access to projects, blocks, etc. Optional — server can start without it. */
|
|
87
101
|
middleLayer?: MiddleLayer;
|
|
@@ -91,8 +105,24 @@ export interface PlMcpServerOptions {
|
|
|
91
105
|
secret: McpSecret;
|
|
92
106
|
/** Optional callbacks for project lifecycle events (e.g. to sync UI state). */
|
|
93
107
|
callbacks?: PlMcpServerCallbacks;
|
|
108
|
+
/**
|
|
109
|
+
* Where the server writes what it does: a line per tool call, the errors of failed calls, slow
|
|
110
|
+
* calls, its start and stop, and the failures outside tool handlers. The secret and the values of
|
|
111
|
+
* secret arguments (`password` and the like) are never written; the error of a failed call is
|
|
112
|
+
* written as it is. Without a logger the server writes nothing.
|
|
113
|
+
*/
|
|
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;
|
|
94
121
|
}
|
|
95
122
|
|
|
123
|
+
/** Default of {@link PlMcpServerOptions.projectIdleReleaseMs}: three minutes. */
|
|
124
|
+
export const DEFAULT_PROJECT_IDLE_RELEASE_MS = 180_000;
|
|
125
|
+
|
|
96
126
|
export class PlMcpServer {
|
|
97
127
|
private ml: MiddleLayer | null;
|
|
98
128
|
private port: number;
|
|
@@ -106,25 +136,54 @@ export class PlMcpServer {
|
|
|
106
136
|
* same author id, whichever client and connection it came from.
|
|
107
137
|
*/
|
|
108
138
|
private readonly toolContext: ToolContext;
|
|
139
|
+
/** Writes the server's log; absent when the server was given no logger. */
|
|
140
|
+
private readonly log: ServerLog | undefined;
|
|
141
|
+
/** The server's holds on the projects its tools work on. */
|
|
142
|
+
private readonly holds: ProjectHolds<Project>;
|
|
109
143
|
|
|
110
144
|
constructor(options: PlMcpServerOptions) {
|
|
111
145
|
this.ml = options.middleLayer ?? null;
|
|
112
146
|
this.port = options.port;
|
|
113
147
|
this.secret = options.secret;
|
|
114
148
|
this.callbacks = options.callbacks ?? {};
|
|
149
|
+
// Also the first part of every call id in the log, which ties a logged call to the writes it
|
|
150
|
+
// made.
|
|
151
|
+
const instanceId = newMcpInstanceId();
|
|
152
|
+
this.log = options.logger
|
|
153
|
+
? new ServerLog({
|
|
154
|
+
logger: options.logger,
|
|
155
|
+
instanceId,
|
|
156
|
+
txCounters: () => this.txCounters(),
|
|
157
|
+
secrets: [this.secret],
|
|
158
|
+
})
|
|
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
|
+
});
|
|
115
168
|
this.toolContext = {
|
|
116
169
|
getMl: () => this.ml,
|
|
117
170
|
requireMl: () => this.requireMl(),
|
|
118
171
|
resolveProject: (id) => this.resolveProject(id),
|
|
119
|
-
|
|
172
|
+
withProject: (id, fn) => this.withProject(id, fn),
|
|
173
|
+
projectHolder,
|
|
120
174
|
callbacks: this.callbacks,
|
|
121
|
-
getAuthorMarker: authorMarkerSource(
|
|
175
|
+
getAuthorMarker: authorMarkerSource(instanceId),
|
|
122
176
|
};
|
|
123
177
|
}
|
|
124
178
|
|
|
125
|
-
/**
|
|
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
|
+
*/
|
|
126
183
|
setMiddleLayer(ml: MiddleLayer | null | undefined) {
|
|
127
|
-
|
|
184
|
+
const next = ml ?? null;
|
|
185
|
+
if (next !== this.ml) this.holds.releaseAll();
|
|
186
|
+
this.ml = next;
|
|
128
187
|
}
|
|
129
188
|
|
|
130
189
|
get url(): string {
|
|
@@ -145,6 +204,8 @@ export class PlMcpServer {
|
|
|
145
204
|
// with keep-alives is what keeps a client from timing out while it waits for the headers;
|
|
146
205
|
// without this a 2026-07-28 request gets one JSON body, and no bytes until the tool returns.
|
|
147
206
|
responseMode: "sse",
|
|
207
|
+
// Errors the handler has nobody to answer with, and requests it refused.
|
|
208
|
+
onerror: (error) => this.log?.sdkError(error),
|
|
148
209
|
});
|
|
149
210
|
const serveMcp = toNodeHandler(handler);
|
|
150
211
|
this.handler = handler;
|
|
@@ -175,7 +236,9 @@ export class PlMcpServer {
|
|
|
175
236
|
}
|
|
176
237
|
|
|
177
238
|
await serveMcp(req, res);
|
|
178
|
-
} catch {
|
|
239
|
+
} catch (error: unknown) {
|
|
240
|
+
// With the headers already sent there is no status left to answer with.
|
|
241
|
+
this.log?.requestFailed(req.method, res.headersSent ? undefined : 500, error);
|
|
179
242
|
if (!res.headersSent) {
|
|
180
243
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
181
244
|
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
@@ -198,6 +261,7 @@ export class PlMcpServer {
|
|
|
198
261
|
if (addr && typeof addr === "object") {
|
|
199
262
|
this.port = addr.port;
|
|
200
263
|
}
|
|
264
|
+
this.log?.serverStarted(this.port, version);
|
|
201
265
|
return;
|
|
202
266
|
} catch (err: unknown) {
|
|
203
267
|
if (
|
|
@@ -221,6 +285,7 @@ export class PlMcpServer {
|
|
|
221
285
|
}
|
|
222
286
|
|
|
223
287
|
async stop(): Promise<void> {
|
|
288
|
+
this.holds.releaseAll();
|
|
224
289
|
const handler = this.handler;
|
|
225
290
|
this.handler = undefined;
|
|
226
291
|
await handler?.close();
|
|
@@ -234,6 +299,7 @@ export class PlMcpServer {
|
|
|
234
299
|
server.closeAllConnections();
|
|
235
300
|
});
|
|
236
301
|
this.httpServer = undefined;
|
|
302
|
+
this.log?.serverStopped(this.port);
|
|
237
303
|
}
|
|
238
304
|
}
|
|
239
305
|
|
|
@@ -248,19 +314,16 @@ export class PlMcpServer {
|
|
|
248
314
|
}
|
|
249
315
|
|
|
250
316
|
private registerTools(server: McpServer): void {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
registerDataQueryTools(server, ctx);
|
|
262
|
-
registerScreenshotTool(server, ctx);
|
|
263
|
-
registerUIInteractionTools(server, ctx);
|
|
317
|
+
// The tools are registered through the log, which puts itself around every call. Nothing below
|
|
318
|
+
// sees the server itself, so a tool added here cannot be left out of the log.
|
|
319
|
+
registerAllTools(this.log ? this.log.registrar(server) : server, this.toolContext);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The counters of the backend client in use, for the log; `undefined` when not connected. */
|
|
323
|
+
private txCounters(): TxCounters | undefined {
|
|
324
|
+
const pl = this.ml?.pl;
|
|
325
|
+
if (pl === undefined) return undefined;
|
|
326
|
+
return { client: pl, conflicts: pl.txConflictStat.txCount };
|
|
264
327
|
}
|
|
265
328
|
|
|
266
329
|
/** Throws if MiddleLayer is not available (not connected to a server). */
|
|
@@ -279,10 +342,32 @@ export class PlMcpServer {
|
|
|
279
342
|
return entry;
|
|
280
343
|
}
|
|
281
344
|
|
|
282
|
-
/**
|
|
283
|
-
|
|
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> {
|
|
284
353
|
const ml = this.requireMl();
|
|
285
354
|
const entry = await this.resolveProject(projectId);
|
|
286
|
-
return
|
|
355
|
+
return await this.holds.use(ml, entry.id, fn);
|
|
287
356
|
}
|
|
288
357
|
}
|
|
358
|
+
|
|
359
|
+
/** Registers every tool of the server with `tools`. */
|
|
360
|
+
function registerAllTools(tools: ToolRegistrar, ctx: ToolContext): void {
|
|
361
|
+
registerPingTool(tools, ctx);
|
|
362
|
+
registerConnectionTools(tools, ctx);
|
|
363
|
+
registerProjectTools(tools, ctx);
|
|
364
|
+
registerBlockTools(tools, ctx);
|
|
365
|
+
registerBlockParamsTools(tools, ctx);
|
|
366
|
+
registerBlockStateTools(tools, ctx);
|
|
367
|
+
registerBlockActionContextTools(tools, ctx);
|
|
368
|
+
registerAwaitTools(tools, ctx);
|
|
369
|
+
registerLogTools(tools, ctx);
|
|
370
|
+
registerDataQueryTools(tools, ctx);
|
|
371
|
+
registerScreenshotTool(tools, ctx);
|
|
372
|
+
registerUIInteractionTools(tools, ctx);
|
|
373
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { MiLogger } from "@milaboratories/helpers";
|
|
2
|
+
|
|
3
|
+
/** One message a logger was given. */
|
|
4
|
+
export type LoggedLine = {
|
|
5
|
+
level: "info" | "warn" | "error";
|
|
6
|
+
/** The message; the server always passes one string. */
|
|
7
|
+
text: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** A logger for tests that keeps what it was given, in order. */
|
|
11
|
+
export type RecordingLogger = {
|
|
12
|
+
logger: MiLogger;
|
|
13
|
+
lines: LoggedLine[];
|
|
14
|
+
/** The messages of one level. */
|
|
15
|
+
at: (level: LoggedLine["level"]) => string[];
|
|
16
|
+
/** Every message joined, to search for what must not be there. */
|
|
17
|
+
everything: () => string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function recordingLogger(): RecordingLogger {
|
|
21
|
+
const lines: LoggedLine[] = [];
|
|
22
|
+
const record = (level: LoggedLine["level"]) => (msg: unknown) => {
|
|
23
|
+
if (typeof msg !== "string")
|
|
24
|
+
throw new Error(`The logger was given a ${typeof msg}, not one string`);
|
|
25
|
+
lines.push({ level, text: msg });
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
logger: { info: record("info"), warn: record("warn"), error: record("error") },
|
|
29
|
+
lines,
|
|
30
|
+
at: (level) => lines.filter((line) => line.level === level).map((line) => line.text),
|
|
31
|
+
everything: () => lines.map((line) => line.text).join("\n"),
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/tools/await.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import { isTimeoutError } from "@milaboratories/pl-middle-layer";
|
|
3
2
|
import { deriveDataFromStorage } from "@platforma-sdk/model";
|
|
4
3
|
import { z } from "zod";
|
|
5
4
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
6
|
-
import type { ToolContext } from "./types";
|
|
5
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
7
6
|
import { summarizeOutputs } from "./tokens";
|
|
8
7
|
import { safeEval } from "./sandbox";
|
|
9
|
-
import { errorResult, hints, textResult } from "./types";
|
|
8
|
+
import { errorResult, hints, textResult, timedOutResult, transformFailedResult } from "./types";
|
|
10
9
|
|
|
11
|
-
export function registerAwaitTools(server:
|
|
10
|
+
export function registerAwaitTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
12
11
|
server.registerTool(
|
|
13
12
|
"await_block_done",
|
|
14
13
|
{
|
|
@@ -38,116 +37,115 @@ export function registerAwaitTools(server: McpServer, ctx: ToolContext): void {
|
|
|
38
37
|
}),
|
|
39
38
|
},
|
|
40
39
|
async ({ projectId, blockId, timeout, transform, transformTimeout }) => {
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
41
|
+
const deadline = Date.now() + timeout;
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// Terminal error states — return immediately
|
|
55
|
-
if (block.calculationStatus === "Limbo") {
|
|
56
|
-
return errorResult(
|
|
57
|
-
"Block entered Limbo state (upstream failed or was stopped).",
|
|
58
|
-
"Check upstream blocks with get_project_overview. Fix or re-run the failed upstream, then retry.",
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (block.calculationStatus === "NotCalculated") {
|
|
63
|
-
return errorResult(
|
|
64
|
-
"Block has not been started.",
|
|
65
|
-
"Use run_block to start it first, then call await_block_done.",
|
|
66
|
-
);
|
|
67
|
-
}
|
|
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
|
+
);
|
|
68
52
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const remaining = Math.max(deadline - Date.now(), 1000);
|
|
72
|
-
let state;
|
|
73
|
-
try {
|
|
74
|
-
state = await project
|
|
75
|
-
.getBlockState(blockId)
|
|
76
|
-
.awaitStableValue(AbortSignal.timeout(remaining));
|
|
77
|
-
} catch (e: unknown) {
|
|
78
|
-
if (isTimeoutError(e)) {
|
|
79
|
-
return textResult({
|
|
80
|
-
timedOut: true,
|
|
81
|
-
status: "Done",
|
|
82
|
-
note: "Computation done but outputs did not stabilize in time. Retry with a longer timeout.",
|
|
83
|
-
});
|
|
84
|
-
}
|
|
53
|
+
// Terminal error states — return immediately
|
|
54
|
+
if (block.calculationStatus === "Limbo") {
|
|
85
55
|
return errorResult(
|
|
86
|
-
|
|
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.",
|
|
87
58
|
);
|
|
88
59
|
}
|
|
89
60
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
canRun: block.canRun,
|
|
97
|
-
stale: block.stale,
|
|
98
|
-
outputErrors: block.outputErrors,
|
|
99
|
-
};
|
|
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
|
+
}
|
|
100
67
|
|
|
101
|
-
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;
|
|
102
72
|
try {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
data,
|
|
107
|
-
outputs: state.outputs,
|
|
108
|
-
block: blockInfo,
|
|
109
|
-
},
|
|
110
|
-
transformTimeout,
|
|
111
|
-
);
|
|
112
|
-
return textResult({ status: "Done", block: blockInfo, result });
|
|
73
|
+
state = await project
|
|
74
|
+
.getBlockState(blockId)
|
|
75
|
+
.awaitStableValue(AbortSignal.timeout(remaining));
|
|
113
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
|
+
}
|
|
114
83
|
return errorResult(
|
|
115
|
-
`
|
|
116
|
-
"Check your JS expression syntax. Available variables: data, outputs, block.",
|
|
84
|
+
`Failed to get block state: ${e instanceof Error ? e.message : String(e)}`,
|
|
117
85
|
);
|
|
118
86
|
}
|
|
119
|
-
}
|
|
120
87
|
|
|
121
|
-
|
|
122
|
-
status: "Done",
|
|
123
|
-
block: blockInfo,
|
|
124
|
-
data,
|
|
125
|
-
outputs: summarizeOutputs(state.outputs as Record<string, unknown> | undefined),
|
|
126
|
-
});
|
|
127
|
-
}
|
|
88
|
+
const data = deriveDataFromStorage(state.blockStorage);
|
|
128
89
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
+
}
|
|
141
140
|
}
|
|
142
|
-
}
|
|
143
141
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
+
});
|
|
151
149
|
});
|
|
152
150
|
},
|
|
153
151
|
);
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import type { BlockActionContextNode } from "@milaboratories/pl-middle-layer";
|
|
3
2
|
import { z } from "zod";
|
|
4
3
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
5
|
-
import type { ToolContext } from "./types";
|
|
4
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
6
5
|
import { errorResult, hints, textResult } from "./types";
|
|
7
6
|
|
|
8
7
|
/** An action context tree node as the tool returns it: cut to the requested depth and history. */
|
|
@@ -57,7 +56,7 @@ export function limitBlockActionContextTree(
|
|
|
57
56
|
return limited;
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
export function registerBlockActionContextTools(server:
|
|
59
|
+
export function registerBlockActionContextTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
61
60
|
server.registerTool(
|
|
62
61
|
"get_block_action_context",
|
|
63
62
|
{
|
|
@@ -72,7 +71,7 @@ export function registerBlockActionContextTools(server: McpServer, ctx: ToolCont
|
|
|
72
71
|
"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. " +
|
|
73
72
|
"Reads the current state; call again to refresh.",
|
|
74
73
|
inputSchema: z.object({
|
|
75
|
-
projectId: projectIdInput.describe("Project ID
|
|
74
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
76
75
|
blockId: blockIdInput.describe("Block ID"),
|
|
77
76
|
mode: z
|
|
78
77
|
.enum(["prod", "staging"])
|
|
@@ -99,20 +98,21 @@ export function registerBlockActionContextTools(server: McpServer, ctx: ToolCont
|
|
|
99
98
|
}),
|
|
100
99
|
},
|
|
101
100
|
async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
+
);
|
|
108
107
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
+
);
|
|
114
113
|
|
|
115
|
-
|
|
114
|
+
return textResult(limitBlockActionContextTree(node, { maxDepth, maxTransitions }));
|
|
115
|
+
});
|
|
116
116
|
},
|
|
117
117
|
);
|
|
118
118
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import { z } from "zod";
|
|
3
2
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
4
|
-
import type { ToolContext } from "./types";
|
|
3
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
5
4
|
import { hints, textResult } from "./types";
|
|
6
5
|
|
|
7
|
-
export function registerBlockParamsTools(server:
|
|
6
|
+
export function registerBlockParamsTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
8
7
|
server.registerTool(
|
|
9
8
|
"get_block_params",
|
|
10
9
|
{
|
|
@@ -18,13 +17,14 @@ export function registerBlockParamsTools(server: McpServer, ctx: ToolContext): v
|
|
|
18
17
|
"change an existing block use get_block_state and set_block_data.\n\n" +
|
|
19
18
|
"Returns `{ kind, params }`. `kind` is absent for a block that declares none.",
|
|
20
19
|
inputSchema: z.object({
|
|
21
|
-
projectId: projectIdInput.describe("Project ID
|
|
20
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
22
21
|
blockId: blockIdInput.describe("Block ID"),
|
|
23
22
|
}),
|
|
24
23
|
},
|
|
25
24
|
async ({ projectId, blockId }) => {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
26
|
+
return textResult(await project.getBlockInitializationParams(blockId));
|
|
27
|
+
});
|
|
28
28
|
},
|
|
29
29
|
);
|
|
30
30
|
}
|