@milaboratories/pl-mcp-server 52.0.0 → 53.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/dist/index.d.ts +2 -1
- package/dist/instructions.cjs +28 -0
- package/dist/instructions.cjs.map +1 -0
- package/dist/instructions.js +28 -0
- package/dist/instructions.js.map +1 -0
- package/dist/package.cjs +10 -0
- package/dist/package.cjs.map +1 -0
- package/dist/package.js +5 -0
- package/dist/package.js.map +1 -0
- package/dist/server.cjs +45 -41
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +15 -9
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +44 -40
- package/dist/server.js.map +1 -1
- package/dist/tools/await.cjs +8 -5
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +9 -6
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +7 -4
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +8 -5
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +23 -0
- package/dist/tools/block-api.cjs.map +1 -0
- package/dist/tools/block-api.js +22 -0
- package/dist/tools/block-api.js.map +1 -0
- package/dist/tools/block-params.cjs +21 -0
- package/dist/tools/block-params.cjs.map +1 -0
- package/dist/tools/block-params.js +21 -0
- package/dist/tools/block-params.js.map +1 -0
- package/dist/tools/block-state.cjs +18 -11
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +19 -12
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/blocks.cjs +130 -50
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +131 -52
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/connection.cjs +23 -6
- package/dist/tools/connection.cjs.map +1 -1
- package/dist/tools/connection.js +24 -7
- package/dist/tools/connection.js.map +1 -1
- package/dist/tools/data-query.cjs +14 -7
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +15 -8
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/ids.cjs +14 -0
- package/dist/tools/ids.cjs.map +1 -0
- package/dist/tools/ids.js +13 -0
- package/dist/tools/ids.js.map +1 -0
- package/dist/tools/logs.cjs +11 -6
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +12 -7
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/ping.cjs +5 -1
- package/dist/tools/ping.cjs.map +1 -1
- package/dist/tools/ping.js +6 -2
- package/dist/tools/ping.js.map +1 -1
- package/dist/tools/projects.cjs +20 -7
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +21 -8
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/screenshot.cjs +3 -1
- package/dist/tools/screenshot.cjs.map +1 -1
- package/dist/tools/screenshot.js +4 -2
- package/dist/tools/screenshot.js.map +1 -1
- package/dist/tools/types.cjs +51 -0
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +50 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/ui-interaction.cjs +27 -13
- package/dist/tools/ui-interaction.cjs.map +1 -1
- package/dist/tools/ui-interaction.js +28 -14
- package/dist/tools/ui-interaction.js.map +1 -1
- package/package.json +11 -9
- package/src/index.ts +1 -0
- package/src/instructions.ts +24 -0
- package/src/server.test.ts +633 -0
- package/src/server.ts +65 -58
- package/src/tools/await.ts +9 -6
- package/src/tools/block-action-context.ts +9 -6
- package/src/tools/block-api.ts +24 -0
- package/src/tools/block-params.ts +30 -0
- package/src/tools/block-state.ts +21 -14
- package/src/tools/blocks.test.ts +148 -0
- package/src/tools/blocks.ts +216 -65
- package/src/tools/connection.ts +38 -17
- package/src/tools/data-query.ts +17 -10
- package/src/tools/ids.ts +11 -0
- package/src/tools/logs.ts +13 -8
- package/src/tools/ping.ts +14 -5
- package/src/tools/projects.ts +26 -11
- package/src/tools/screenshot.ts +7 -5
- package/src/tools/types.test.ts +19 -0
- package/src/tools/types.ts +71 -3
- package/src/tools/ui-interaction.ts +29 -18
package/src/server.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { toNodeHandler } from "@modelcontextprotocol/node";
|
|
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
5
|
import type { MiddleLayer } from "@milaboratories/pl-middle-layer";
|
|
6
|
-
import type { Branded } from "@milaboratories/pl-model-common";
|
|
6
|
+
import type { BlockId, Branded, ProjectId } from "@milaboratories/pl-model-common";
|
|
7
|
+
import { version } from "../package.json";
|
|
8
|
+
import { SERVER_INSTRUCTIONS } from "./instructions";
|
|
7
9
|
import type { ToolContext } from "./tools/types";
|
|
10
|
+
import { authorMarkerSource } from "./tools/types";
|
|
8
11
|
import { registerPingTool } from "./tools/ping";
|
|
9
12
|
import { registerConnectionTools } from "./tools/connection";
|
|
10
13
|
import { registerProjectTools } from "./tools/projects";
|
|
11
14
|
import { registerBlockTools } from "./tools/blocks";
|
|
15
|
+
import { registerBlockParamsTools } from "./tools/block-params";
|
|
12
16
|
import { registerBlockStateTools } from "./tools/block-state";
|
|
13
17
|
import { registerBlockActionContextTools } from "./tools/block-action-context";
|
|
14
18
|
import { registerAwaitTools } from "./tools/await";
|
|
@@ -18,10 +22,10 @@ import { registerScreenshotTool } from "./tools/screenshot";
|
|
|
18
22
|
import { registerUIInteractionTools } from "./tools/ui-interaction";
|
|
19
23
|
|
|
20
24
|
export interface PlMcpServerCallbacks {
|
|
21
|
-
onProjectCreated?: (projectId:
|
|
22
|
-
onProjectOpened?: (projectId:
|
|
23
|
-
onProjectClosed?: (projectId:
|
|
24
|
-
onProjectDeleted?: (projectId:
|
|
25
|
+
onProjectCreated?: (projectId: ProjectId) => void | Promise<void>;
|
|
26
|
+
onProjectOpened?: (projectId: ProjectId) => void | Promise<void>;
|
|
27
|
+
onProjectClosed?: (projectId: ProjectId) => void | Promise<void>;
|
|
28
|
+
onProjectDeleted?: (projectId: ProjectId) => void | Promise<void>;
|
|
25
29
|
/** Capture the current application window as a PNG screenshot. Returns base64-encoded PNG. */
|
|
26
30
|
captureScreenshot?: () => Promise<string>;
|
|
27
31
|
/** Send an input event to the application window. */
|
|
@@ -34,12 +38,12 @@ export interface PlMcpServerCallbacks {
|
|
|
34
38
|
*/
|
|
35
39
|
executeJavaScript?: (
|
|
36
40
|
code: string,
|
|
37
|
-
target?: { projectId:
|
|
41
|
+
target?: { projectId: ProjectId; blockId: BlockId },
|
|
38
42
|
) => Promise<unknown>;
|
|
39
43
|
/** List available blocks from all configured registries. */
|
|
40
44
|
listAvailableBlocks?: (query?: string) => Promise<unknown[]>;
|
|
41
45
|
/** Navigate the desktop UI to show a specific block. */
|
|
42
|
-
selectBlock?: (projectId:
|
|
46
|
+
selectBlock?: (projectId: ProjectId, blockId: BlockId) => Promise<void>;
|
|
43
47
|
/** Read recent lines from the application log. */
|
|
44
48
|
readAppLog?: (lines: number, search?: string) => Promise<string>;
|
|
45
49
|
/** List saved server connections. */
|
|
@@ -95,13 +99,27 @@ export class PlMcpServer {
|
|
|
95
99
|
private readonly secret: McpSecret;
|
|
96
100
|
private readonly callbacks: PlMcpServerCallbacks;
|
|
97
101
|
private httpServer: Server | undefined;
|
|
98
|
-
|
|
102
|
+
/** Serves the MCP requests of the running HTTP server. */
|
|
103
|
+
private handler: McpHttpHandler | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* What the tools work with. One per server instance, so every write made through MCP carries the
|
|
106
|
+
* same author id, whichever client and connection it came from.
|
|
107
|
+
*/
|
|
108
|
+
private readonly toolContext: ToolContext;
|
|
99
109
|
|
|
100
110
|
constructor(options: PlMcpServerOptions) {
|
|
101
111
|
this.ml = options.middleLayer ?? null;
|
|
102
112
|
this.port = options.port;
|
|
103
113
|
this.secret = options.secret;
|
|
104
114
|
this.callbacks = options.callbacks ?? {};
|
|
115
|
+
this.toolContext = {
|
|
116
|
+
getMl: () => this.ml,
|
|
117
|
+
requireMl: () => this.requireMl(),
|
|
118
|
+
resolveProject: (id) => this.resolveProject(id),
|
|
119
|
+
getOpenedProject: (id) => this.getOpenedProject(id),
|
|
120
|
+
callbacks: this.callbacks,
|
|
121
|
+
getAuthorMarker: authorMarkerSource(`mcp-${randomUUID().slice(0, 8)}`),
|
|
122
|
+
};
|
|
105
123
|
}
|
|
106
124
|
|
|
107
125
|
/** Set or update the MiddleLayer instance (e.g. after connecting to a server). */
|
|
@@ -120,7 +138,18 @@ export class PlMcpServer {
|
|
|
120
138
|
|
|
121
139
|
const expectedPath = `/${this.secret}/mcp`;
|
|
122
140
|
|
|
123
|
-
|
|
141
|
+
// Nothing is kept between requests: every request is answered by an MCP server made for it.
|
|
142
|
+
// Clients of the 2025 protocol revisions are served the same way, without a session.
|
|
143
|
+
const handler = createMcpHandler(() => this.createMcpServer(), {
|
|
144
|
+
// A tool can hold a request for minutes (a wait for a block). Answering as an event stream
|
|
145
|
+
// with keep-alives is what keeps a client from timing out while it waits for the headers;
|
|
146
|
+
// without this a 2026-07-28 request gets one JSON body, and no bytes until the tool returns.
|
|
147
|
+
responseMode: "sse",
|
|
148
|
+
});
|
|
149
|
+
const serveMcp = toNodeHandler(handler);
|
|
150
|
+
this.handler = handler;
|
|
151
|
+
|
|
152
|
+
const requestHandler = async (req: IncomingMessage, res: ServerResponse): Promise<void> => {
|
|
124
153
|
try {
|
|
125
154
|
// Origin validation — only allow localhost
|
|
126
155
|
if (req.headers.origin !== undefined) {
|
|
@@ -145,41 +174,18 @@ export class PlMcpServer {
|
|
|
145
174
|
return;
|
|
146
175
|
}
|
|
147
176
|
|
|
148
|
-
|
|
149
|
-
const sessionId = req.headers["mcp-session-id"] as string | undefined;
|
|
150
|
-
if (sessionId && this.transports.has(sessionId)) {
|
|
151
|
-
await this.transports.get(sessionId)!.handleRequest(req, res);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// New session — create transport and connect MCP server
|
|
156
|
-
const transport = new StreamableHTTPServerTransport({
|
|
157
|
-
sessionIdGenerator: () => randomUUID(),
|
|
158
|
-
});
|
|
159
|
-
transport.onclose = () => {
|
|
160
|
-
const sid = transport.sessionId;
|
|
161
|
-
if (sid) this.transports.delete(sid);
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
const server = this.createMcpServer();
|
|
165
|
-
await server.connect(transport);
|
|
166
|
-
await transport.handleRequest(req, res);
|
|
167
|
-
|
|
168
|
-
const sid = transport.sessionId;
|
|
169
|
-
if (sid) this.transports.set(sid, transport);
|
|
177
|
+
await serveMcp(req, res);
|
|
170
178
|
} catch {
|
|
171
179
|
if (!res.headersSent) {
|
|
172
180
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
173
181
|
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
174
182
|
}
|
|
175
183
|
}
|
|
176
|
-
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
this.httpServer = createServer(requestHandler);
|
|
177
187
|
|
|
178
188
|
const maxRetries = 10;
|
|
179
|
-
const requestHandler = this.httpServer.listeners("request")[0] as (
|
|
180
|
-
req: IncomingMessage,
|
|
181
|
-
res: ServerResponse,
|
|
182
|
-
) => void;
|
|
183
189
|
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
184
190
|
const server = this.httpServer;
|
|
185
191
|
try {
|
|
@@ -205,48 +211,49 @@ export class PlMcpServer {
|
|
|
205
211
|
this.port++;
|
|
206
212
|
continue;
|
|
207
213
|
}
|
|
214
|
+
// Nothing is listening: leave the instance as it was before the call, so that `stop()`
|
|
215
|
+
// has nothing to close and a later `start()` is not refused as already running.
|
|
216
|
+
this.httpServer = undefined;
|
|
217
|
+
this.handler = undefined;
|
|
208
218
|
throw err;
|
|
209
219
|
}
|
|
210
220
|
}
|
|
211
221
|
}
|
|
212
222
|
|
|
213
223
|
async stop(): Promise<void> {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
this.transports.clear();
|
|
224
|
+
const handler = this.handler;
|
|
225
|
+
this.handler = undefined;
|
|
226
|
+
await handler?.close();
|
|
218
227
|
|
|
219
228
|
const server = this.httpServer;
|
|
220
229
|
if (server) {
|
|
221
230
|
await new Promise<void>((resolve, reject) => {
|
|
222
231
|
server.close((err) => (err ? reject(err) : resolve()));
|
|
232
|
+
// Requests still being answered would keep the server open until they finish, and a
|
|
233
|
+
// request waiting for a block can take long.
|
|
234
|
+
server.closeAllConnections();
|
|
223
235
|
});
|
|
224
236
|
this.httpServer = undefined;
|
|
225
237
|
}
|
|
226
238
|
}
|
|
227
239
|
|
|
228
240
|
private createMcpServer(): McpServer {
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
241
|
+
const server = new McpServer(
|
|
242
|
+
{ name: "pl", title: "Platforma", version },
|
|
243
|
+
// The set of tools never changes while the server runs.
|
|
244
|
+
{ capabilities: { tools: { listChanged: false } }, instructions: SERVER_INSTRUCTIONS },
|
|
245
|
+
);
|
|
246
|
+
this.registerTools(server);
|
|
232
247
|
return server;
|
|
233
248
|
}
|
|
234
249
|
|
|
235
|
-
private registerTools(server: McpServer
|
|
236
|
-
const
|
|
237
|
-
let localVersion = 0;
|
|
238
|
-
const ctx: ToolContext = {
|
|
239
|
-
getMl: () => this.ml,
|
|
240
|
-
requireMl: () => this.requireMl(),
|
|
241
|
-
resolveProject: (id) => this.resolveProject(id),
|
|
242
|
-
getOpenedProject: (id) => this.getOpenedProject(id),
|
|
243
|
-
callbacks: this.callbacks,
|
|
244
|
-
getAuthorMarker: () => ({ authorId, localVersion: ++localVersion }),
|
|
245
|
-
};
|
|
250
|
+
private registerTools(server: McpServer): void {
|
|
251
|
+
const ctx = this.toolContext;
|
|
246
252
|
registerPingTool(server, ctx);
|
|
247
253
|
registerConnectionTools(server, ctx);
|
|
248
254
|
registerProjectTools(server, ctx);
|
|
249
255
|
registerBlockTools(server, ctx);
|
|
256
|
+
registerBlockParamsTools(server, ctx);
|
|
250
257
|
registerBlockStateTools(server, ctx);
|
|
251
258
|
registerBlockActionContextTools(server, ctx);
|
|
252
259
|
registerAwaitTools(server, ctx);
|
|
@@ -263,7 +270,7 @@ export class PlMcpServer {
|
|
|
263
270
|
}
|
|
264
271
|
|
|
265
272
|
/** Resolves a project from the list by its projectId. */
|
|
266
|
-
private async resolveProject(projectId:
|
|
273
|
+
private async resolveProject(projectId: ProjectId) {
|
|
267
274
|
const ml = this.requireMl();
|
|
268
275
|
await ml.projectList.refreshState();
|
|
269
276
|
const projects = await ml.projectList.awaitStableValue();
|
|
@@ -273,7 +280,7 @@ export class PlMcpServer {
|
|
|
273
280
|
}
|
|
274
281
|
|
|
275
282
|
/** Gets an opened project by projectId. */
|
|
276
|
-
private async getOpenedProject(projectId:
|
|
283
|
+
private async getOpenedProject(projectId: ProjectId) {
|
|
277
284
|
const ml = this.requireMl();
|
|
278
285
|
const entry = await this.resolveProject(projectId);
|
|
279
286
|
return ml.getOpenedProject(entry.id);
|
package/src/tools/await.ts
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
2
|
import { isTimeoutError } from "@milaboratories/pl-middle-layer";
|
|
3
3
|
import { deriveDataFromStorage } from "@platforma-sdk/model";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { projectIdInput, blockIdInput } from "./ids";
|
|
5
6
|
import type { ToolContext } from "./types";
|
|
6
7
|
import { summarizeOutputs } from "./tokens";
|
|
7
8
|
import { safeEval } from "./sandbox";
|
|
8
|
-
import { errorResult, textResult } from "./types";
|
|
9
|
+
import { errorResult, hints, textResult } from "./types";
|
|
9
10
|
|
|
10
11
|
export function registerAwaitTools(server: McpServer, ctx: ToolContext): void {
|
|
11
12
|
server.registerTool(
|
|
12
13
|
"await_block_done",
|
|
13
14
|
{
|
|
15
|
+
title: "Wait for Block",
|
|
16
|
+
annotations: hints.read,
|
|
14
17
|
description:
|
|
15
18
|
"Wait for a block to finish computation and outputs to stabilize. " +
|
|
16
19
|
"Returns block status, data, and concise output summary with token estimates. " +
|
|
17
20
|
"Use `transform` to extract specific data server-side on completion.",
|
|
18
|
-
inputSchema: {
|
|
19
|
-
projectId:
|
|
20
|
-
blockId:
|
|
21
|
+
inputSchema: z.object({
|
|
22
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
23
|
+
blockId: blockIdInput.describe("Block ID to wait for"),
|
|
21
24
|
timeout: z.number().optional().default(120000).describe("Timeout in ms (default 120000)"),
|
|
22
25
|
transform: z
|
|
23
26
|
.string()
|
|
@@ -32,7 +35,7 @@ export function registerAwaitTools(server: McpServer, ctx: ToolContext): void {
|
|
|
32
35
|
.optional()
|
|
33
36
|
.default(5000)
|
|
34
37
|
.describe("Timeout in ms for transform evaluation (default 5000)."),
|
|
35
|
-
},
|
|
38
|
+
}),
|
|
36
39
|
},
|
|
37
40
|
async ({ projectId, blockId, timeout, transform, transformTimeout }) => {
|
|
38
41
|
const project = await ctx.getOpenedProject(projectId);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
2
|
import type { BlockActionContextNode } from "@milaboratories/pl-middle-layer";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
+
import { projectIdInput, blockIdInput } from "./ids";
|
|
4
5
|
import type { ToolContext } from "./types";
|
|
5
|
-
import { errorResult, textResult } from "./types";
|
|
6
|
+
import { errorResult, hints, textResult } from "./types";
|
|
6
7
|
|
|
7
8
|
/** An action context tree node as the tool returns it: cut to the requested depth and history. */
|
|
8
9
|
export type LimitedBlockActionContextNode = Omit<BlockActionContextNode, "children"> & {
|
|
@@ -60,6 +61,8 @@ export function registerBlockActionContextTools(server: McpServer, ctx: ToolCont
|
|
|
60
61
|
server.registerTool(
|
|
61
62
|
"get_block_action_context",
|
|
62
63
|
{
|
|
64
|
+
title: "Get Block Action Context",
|
|
65
|
+
annotations: hints.read,
|
|
63
66
|
description:
|
|
64
67
|
"Get the block action context tree of a block run: one node per template render, each with its baggage " +
|
|
65
68
|
"(name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order " +
|
|
@@ -68,9 +71,9 @@ export function registerBlockActionContextTools(server: McpServer, ctx: ToolCont
|
|
|
68
71
|
"The result is cut to maxDepth levels and the latest maxTransitions per node; a node with " +
|
|
69
72
|
"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. " +
|
|
70
73
|
"Reads the current state; call again to refresh.",
|
|
71
|
-
inputSchema: {
|
|
72
|
-
projectId:
|
|
73
|
-
blockId:
|
|
74
|
+
inputSchema: z.object({
|
|
75
|
+
projectId: projectIdInput.describe("Project ID (must be opened)"),
|
|
76
|
+
blockId: blockIdInput.describe("Block ID"),
|
|
74
77
|
mode: z
|
|
75
78
|
.enum(["prod", "staging"])
|
|
76
79
|
.optional()
|
|
@@ -93,7 +96,7 @@ export function registerBlockActionContextTools(server: McpServer, ctx: ToolCont
|
|
|
93
96
|
.optional()
|
|
94
97
|
.default(20)
|
|
95
98
|
.describe("Latest transitions to include per node (default 20)"),
|
|
96
|
-
},
|
|
99
|
+
}),
|
|
97
100
|
},
|
|
98
101
|
async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {
|
|
99
102
|
const project = await ctx.getOpenedProject(projectId);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ModelAPIVersionMismatchError } from "@milaboratories/pl-errors";
|
|
2
|
+
import { BLOCK_STORAGE_FACADE_VERSION } from "@platforma-sdk/model";
|
|
3
|
+
import { errorResult } from "./types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The block API MCP creates and writes blocks on (BlockModelV3): the model API version a create
|
|
7
|
+
* or an update requires of the block pack.
|
|
8
|
+
*/
|
|
9
|
+
export const SUPPORTED_BLOCK_API = BLOCK_STORAGE_FACADE_VERSION;
|
|
10
|
+
|
|
11
|
+
/** What a tool answers when the block it would create or write is on another block API. */
|
|
12
|
+
export const UNSUPPORTED_BLOCK_API_TEXT =
|
|
13
|
+
"Blocks on block API 1 are not supported; nothing was changed.";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The guard of every create and write: the error result for `e` when it is the middle layer's
|
|
17
|
+
* refusal of a block on another block API (`ModelAPIVersionMismatchError`), `undefined` for any
|
|
18
|
+
* other error.
|
|
19
|
+
*/
|
|
20
|
+
export function unsupportedBlockApiResult(e: unknown) {
|
|
21
|
+
return e instanceof ModelAPIVersionMismatchError
|
|
22
|
+
? errorResult(UNSUPPORTED_BLOCK_API_TEXT)
|
|
23
|
+
: undefined;
|
|
24
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { projectIdInput, blockIdInput } from "./ids";
|
|
4
|
+
import type { ToolContext } from "./types";
|
|
5
|
+
import { hints, textResult } from "./types";
|
|
6
|
+
|
|
7
|
+
export function registerBlockParamsTools(server: McpServer, ctx: ToolContext): void {
|
|
8
|
+
server.registerTool(
|
|
9
|
+
"get_block_params",
|
|
10
|
+
{
|
|
11
|
+
title: "Get Block Params",
|
|
12
|
+
annotations: hints.read,
|
|
13
|
+
description:
|
|
14
|
+
"Get a block's configuration as the initialization params of its kind: what a project " +
|
|
15
|
+
"template stores for this block. The block's own model derives them from its current data. " +
|
|
16
|
+
"Pass the result as `params` of add_block, with a block of the same kind, to create a " +
|
|
17
|
+
"block configured the same way. Read-only: params usually leave out view state, so to " +
|
|
18
|
+
"change an existing block use get_block_state and set_block_data.\n\n" +
|
|
19
|
+
"Returns `{ kind, params }`. `kind` is absent for a block that declares none.",
|
|
20
|
+
inputSchema: z.object({
|
|
21
|
+
projectId: projectIdInput.describe("Project ID (must be opened)"),
|
|
22
|
+
blockId: blockIdInput.describe("Block ID"),
|
|
23
|
+
}),
|
|
24
|
+
},
|
|
25
|
+
async ({ projectId, blockId }) => {
|
|
26
|
+
const project = await ctx.getOpenedProject(projectId);
|
|
27
|
+
return textResult(await project.getBlockInitializationParams(blockId));
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
}
|
package/src/tools/block-state.ts
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
2
|
import { deriveDataFromStorage } from "@platforma-sdk/model";
|
|
3
3
|
import { ModelAPIVersionMismatchError } from "@milaboratories/pl-errors";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { projectIdInput, blockIdInput } from "./ids";
|
|
5
6
|
import type { ToolContext } from "./types";
|
|
6
7
|
import { summarizeOutputs } from "./tokens";
|
|
7
8
|
import { safeEval } from "./sandbox";
|
|
8
|
-
import { errorResult, textResult } from "./types";
|
|
9
|
+
import { errorResult, hints, textResult } from "./types";
|
|
9
10
|
|
|
10
11
|
export function registerBlockStateTools(server: McpServer, ctx: ToolContext): void {
|
|
11
12
|
server.registerTool(
|
|
12
13
|
"get_project_overview",
|
|
13
14
|
{
|
|
15
|
+
title: "Get Project Overview",
|
|
16
|
+
annotations: hints.read,
|
|
14
17
|
description:
|
|
15
18
|
"Get project overview with all blocks and their statuses (calculationStatus, canRun, stale, errors, upstreams/downstreams)",
|
|
16
|
-
inputSchema: {
|
|
17
|
-
projectId:
|
|
18
|
-
},
|
|
19
|
+
inputSchema: z.object({
|
|
20
|
+
projectId: projectIdInput.describe("Project ID (must be opened)"),
|
|
21
|
+
}),
|
|
19
22
|
},
|
|
20
23
|
async ({ projectId }) => {
|
|
21
24
|
const project = await ctx.getOpenedProject(projectId);
|
|
@@ -41,6 +44,8 @@ export function registerBlockStateTools(server: McpServer, ctx: ToolContext): vo
|
|
|
41
44
|
server.registerTool(
|
|
42
45
|
"get_block_state",
|
|
43
46
|
{
|
|
47
|
+
title: "Get Block State",
|
|
48
|
+
annotations: hints.read,
|
|
44
49
|
description:
|
|
45
50
|
"Get block state. Returns block args (data) and a concise output summary with token estimates by default. " +
|
|
46
51
|
"Use `transform` to extract specific data server-side without loading full outputs into context.\n\n" +
|
|
@@ -49,9 +54,9 @@ export function registerBlockStateTools(server: McpServer, ctx: ToolContext): vo
|
|
|
49
54
|
"- `outputs.logs?.value` — get one specific output value\n" +
|
|
50
55
|
"- `data` — get only block args\n" +
|
|
51
56
|
"- `({ preset: outputs.preset?.value, qc: outputs.qc?.value })` — get specific outputs",
|
|
52
|
-
inputSchema: {
|
|
53
|
-
projectId:
|
|
54
|
-
blockId:
|
|
57
|
+
inputSchema: z.object({
|
|
58
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
59
|
+
blockId: blockIdInput.describe("Block ID"),
|
|
55
60
|
transform: z
|
|
56
61
|
.string()
|
|
57
62
|
.optional()
|
|
@@ -65,7 +70,7 @@ export function registerBlockStateTools(server: McpServer, ctx: ToolContext): vo
|
|
|
65
70
|
.optional()
|
|
66
71
|
.default(5000)
|
|
67
72
|
.describe("Timeout in ms for transform evaluation (default 5000)."),
|
|
68
|
-
},
|
|
73
|
+
}),
|
|
69
74
|
},
|
|
70
75
|
async ({ projectId, blockId, transform, transformTimeout }) => {
|
|
71
76
|
const project = await ctx.getOpenedProject(projectId);
|
|
@@ -96,12 +101,14 @@ export function registerBlockStateTools(server: McpServer, ctx: ToolContext): vo
|
|
|
96
101
|
server.registerTool(
|
|
97
102
|
"set_block_data",
|
|
98
103
|
{
|
|
104
|
+
title: "Set Block Data",
|
|
105
|
+
annotations: hints.destructive,
|
|
99
106
|
description: "Set the user-facing data of a block (triggers args derivation and staging)",
|
|
100
|
-
inputSchema: {
|
|
101
|
-
projectId:
|
|
102
|
-
blockId:
|
|
103
|
-
data: z.record(z.unknown()).describe("Block data object"),
|
|
104
|
-
},
|
|
107
|
+
inputSchema: z.object({
|
|
108
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
109
|
+
blockId: blockIdInput.describe("Block ID"),
|
|
110
|
+
data: z.record(z.string(), z.unknown()).describe("Block data object"),
|
|
111
|
+
}),
|
|
105
112
|
},
|
|
106
113
|
async ({ projectId, blockId, data }) => {
|
|
107
114
|
const project = await ctx.getOpenedProject(projectId);
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import type { AddBlockOps, BlockPackSpecAny } from "@milaboratories/pl-middle-layer";
|
|
3
|
+
import { asBlockId, asProjectId } from "@milaboratories/pl-model-common";
|
|
4
|
+
import {
|
|
5
|
+
BlockInitParamsRejectedError,
|
|
6
|
+
ModelAPIVersionMismatchError,
|
|
7
|
+
} from "@milaboratories/pl-errors";
|
|
8
|
+
import type { AddBlockDeps, AddBlockInput } from "./blocks";
|
|
9
|
+
import { addBlock } from "./blocks";
|
|
10
|
+
import { UNSUPPORTED_BLOCK_API_TEXT } from "./block-api";
|
|
11
|
+
|
|
12
|
+
const devSpec: AddBlockInput["spec"] = { type: "dev-v2", folder: "/blocks/enter-numbers" };
|
|
13
|
+
const resolvedSpec: BlockPackSpecAny = {
|
|
14
|
+
type: "from-registry-v2",
|
|
15
|
+
registryUrl: "https://registry.test/",
|
|
16
|
+
id: { organization: "org", name: "block", version: "1.2.3" },
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type AddBlockCall = {
|
|
20
|
+
label: string;
|
|
21
|
+
spec: BlockPackSpecAny;
|
|
22
|
+
before: string | undefined;
|
|
23
|
+
ops: AddBlockOps | undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Dependencies whose project records each addBlock call and answers with `outcome`. */
|
|
27
|
+
function depsRecording(outcome: () => string, resolved: BlockPackSpecAny = resolvedSpec) {
|
|
28
|
+
const calls: AddBlockCall[] = [];
|
|
29
|
+
const kinds: { kind: string; registryUrl: string | undefined; allowUnstable: boolean }[] = [];
|
|
30
|
+
const deps: AddBlockDeps = {
|
|
31
|
+
project: {
|
|
32
|
+
addBlock: async (label, spec, before, _author, _blockId, ops) => {
|
|
33
|
+
calls.push({ label, spec, before, ops });
|
|
34
|
+
return outcome();
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
resolveKind: async (kind, registryUrl, allowUnstable) => {
|
|
38
|
+
kinds.push({ kind, registryUrl, allowUnstable });
|
|
39
|
+
return resolved;
|
|
40
|
+
},
|
|
41
|
+
author: { authorId: "test", localVersion: 1 },
|
|
42
|
+
};
|
|
43
|
+
return { deps, calls, kinds };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function input(overrides: Partial<AddBlockInput> = {}): AddBlockInput {
|
|
47
|
+
return { projectId: asProjectId("project"), label: "Block", spec: devSpec, ...overrides };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function textOf(result: Awaited<ReturnType<typeof addBlock>>): string {
|
|
51
|
+
return result.content[0].text;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe("add_block", () => {
|
|
55
|
+
test("adds a block in its default state when no params are given", async () => {
|
|
56
|
+
const { deps, calls } = depsRecording(() => "new-block");
|
|
57
|
+
|
|
58
|
+
const result = await addBlock(deps, input());
|
|
59
|
+
|
|
60
|
+
expect(JSON.parse(textOf(result))).toEqual({ blockId: "new-block" });
|
|
61
|
+
expect(calls).toEqual([
|
|
62
|
+
{
|
|
63
|
+
label: "Block",
|
|
64
|
+
spec: devSpec,
|
|
65
|
+
before: undefined,
|
|
66
|
+
ops: { init: undefined, requireModelAPIVersion: 2 },
|
|
67
|
+
},
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("passes params, block ids and position to the project", async () => {
|
|
72
|
+
const { deps, calls } = depsRecording(() => "new-block");
|
|
73
|
+
|
|
74
|
+
await addBlock(
|
|
75
|
+
deps,
|
|
76
|
+
input({
|
|
77
|
+
params: { numbers: [1] },
|
|
78
|
+
blockIds: { alias: asBlockId("real") },
|
|
79
|
+
before: asBlockId("other"),
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(calls[0].before).toBe("other");
|
|
84
|
+
expect(calls[0].ops).toEqual({
|
|
85
|
+
init: { params: { numbers: [1] }, blockIds: { alias: "real" } },
|
|
86
|
+
requireModelAPIVersion: 2,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("adds the block a kind resolves to", async () => {
|
|
91
|
+
const { deps, calls, kinds } = depsRecording(() => "new-block");
|
|
92
|
+
|
|
93
|
+
await addBlock(deps, input({ spec: { type: "kind", kind: "@org/block.kind@1.0.0" } }));
|
|
94
|
+
|
|
95
|
+
expect(kinds).toEqual([
|
|
96
|
+
{ kind: "@org/block.kind@1.0.0", registryUrl: undefined, allowUnstable: false },
|
|
97
|
+
]);
|
|
98
|
+
expect(calls[0].spec).toEqual(resolvedSpec);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("reports a kind that resolves to no block and adds nothing", async () => {
|
|
102
|
+
const { deps, calls } = depsRecording(() => "new-block");
|
|
103
|
+
deps.resolveKind = async () => {
|
|
104
|
+
throw new Error("no-matching-kind-version");
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const result = await addBlock(
|
|
108
|
+
deps,
|
|
109
|
+
input({ spec: { type: "kind", kind: "@org/block.kind@9.0.0" } }),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(result).toMatchObject({ isError: true });
|
|
113
|
+
expect(textOf(result)).toContain("no-matching-kind-version");
|
|
114
|
+
expect(textOf(result)).toContain("Nothing was created");
|
|
115
|
+
expect(calls).toEqual([]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("refuses a block on block API 1", async () => {
|
|
119
|
+
const { deps } = depsRecording(() => {
|
|
120
|
+
throw new ModelAPIVersionMismatchError("new-block", 2, 1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const result = await addBlock(deps, input());
|
|
124
|
+
|
|
125
|
+
expect(result).toMatchObject({ isError: true });
|
|
126
|
+
expect(textOf(result)).toBe(UNSUPPORTED_BLOCK_API_TEXT);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("reports params the block does not accept, in the block's words", async () => {
|
|
130
|
+
const { deps } = depsRecording(() => {
|
|
131
|
+
throw new BlockInitParamsRejectedError("'numbers' must be an array of numbers.");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const result = await addBlock(deps, input({ params: { numbers: "x" } }));
|
|
135
|
+
|
|
136
|
+
expect(result).toMatchObject({ isError: true });
|
|
137
|
+
expect(textOf(result)).toContain("'numbers' must be an array of numbers.");
|
|
138
|
+
expect(textOf(result)).toContain("Nothing was created");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("lets an unexpected failure through", async () => {
|
|
142
|
+
const { deps } = depsRecording(() => {
|
|
143
|
+
throw new Error("backend is down");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
await expect(addBlock(deps, input())).rejects.toThrow("backend is down");
|
|
147
|
+
});
|
|
148
|
+
});
|