@milaboratories/pl-mcp-server 52.1.0 → 53.1.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 +31 -0
- package/dist/instructions.cjs +2 -1
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +2 -1
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/server-log.cjs +365 -0
- package/dist/server-log.cjs.map +1 -0
- package/dist/server-log.js +363 -0
- package/dist/server-log.js.map +1 -0
- package/dist/server.cjs +49 -15
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +12 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +49 -15
- package/dist/server.js.map +1 -1
- package/dist/tools/await.cjs +4 -6
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +5 -7
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs.map +1 -1
- 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 +2 -2
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +3 -3
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/blocks.cjs +78 -20
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +78 -21
- 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 +1 -1
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +2 -2
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/logs.cjs.map +1 -1
- 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.map +1 -1
- 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 +5 -4
- package/src/instructions.ts +2 -1
- package/src/server-log.test.ts +859 -0
- package/src/server-log.ts +495 -0
- package/src/server.logging.test.ts +380 -0
- package/src/server.request-failure.test.ts +49 -0
- package/src/server.test.ts +4 -2
- package/src/server.ts +70 -15
- package/src/test/recording-logger.ts +33 -0
- package/src/tools/await.ts +7 -10
- package/src/tools/block-action-context.ts +2 -3
- package/src/tools/block-api.ts +24 -0
- package/src/tools/block-params.ts +29 -0
- package/src/tools/block-state.ts +5 -6
- package/src/tools/blocks.test.ts +148 -0
- package/src/tools/blocks.ts +162 -34
- package/src/tools/connection.ts +2 -3
- package/src/tools/data-query.ts +5 -6
- package/src/tools/logs.ts +2 -3
- package/src/tools/ping.ts +2 -3
- package/src/tools/projects.ts +2 -3
- 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 +65 -2
- package/src/tools/ui-interaction.ts +2 -3
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
|
{
|
|
@@ -76,8 +75,7 @@ export function registerAwaitTools(server: McpServer, ctx: ToolContext): void {
|
|
|
76
75
|
.awaitStableValue(AbortSignal.timeout(remaining));
|
|
77
76
|
} catch (e: unknown) {
|
|
78
77
|
if (isTimeoutError(e)) {
|
|
79
|
-
return
|
|
80
|
-
timedOut: true,
|
|
78
|
+
return timedOutResult({
|
|
81
79
|
status: "Done",
|
|
82
80
|
note: "Computation done but outputs did not stabilize in time. Retry with a longer timeout.",
|
|
83
81
|
});
|
|
@@ -111,8 +109,8 @@ export function registerAwaitTools(server: McpServer, ctx: ToolContext): void {
|
|
|
111
109
|
);
|
|
112
110
|
return textResult({ status: "Done", block: blockInfo, result });
|
|
113
111
|
} catch (e: unknown) {
|
|
114
|
-
return
|
|
115
|
-
|
|
112
|
+
return transformFailedResult(
|
|
113
|
+
e,
|
|
116
114
|
"Check your JS expression syntax. Available variables: data, outputs, block.",
|
|
117
115
|
);
|
|
118
116
|
}
|
|
@@ -144,8 +142,7 @@ export function registerAwaitTools(server: McpServer, ctx: ToolContext): void {
|
|
|
144
142
|
// Timed out while running
|
|
145
143
|
const overview = await project.overview.getValue();
|
|
146
144
|
const block = overview?.blocks.find((b) => b.id === blockId);
|
|
147
|
-
return
|
|
148
|
-
timedOut: true,
|
|
145
|
+
return timedOutResult({
|
|
149
146
|
status: block?.calculationStatus ?? "Unknown",
|
|
150
147
|
hint: "The block is still running. Call await_block_done again with a longer timeout.",
|
|
151
148
|
});
|
|
@@ -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
|
{
|
|
@@ -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,29 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { projectIdInput, blockIdInput } from "./ids";
|
|
3
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
4
|
+
import { hints, textResult } from "./types";
|
|
5
|
+
|
|
6
|
+
export function registerBlockParamsTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
7
|
+
server.registerTool(
|
|
8
|
+
"get_block_params",
|
|
9
|
+
{
|
|
10
|
+
title: "Get Block Params",
|
|
11
|
+
annotations: hints.read,
|
|
12
|
+
description:
|
|
13
|
+
"Get a block's configuration as the initialization params of its kind: what a project " +
|
|
14
|
+
"template stores for this block. The block's own model derives them from its current data. " +
|
|
15
|
+
"Pass the result as `params` of add_block, with a block of the same kind, to create a " +
|
|
16
|
+
"block configured the same way. Read-only: params usually leave out view state, so to " +
|
|
17
|
+
"change an existing block use get_block_state and set_block_data.\n\n" +
|
|
18
|
+
"Returns `{ kind, params }`. `kind` is absent for a block that declares none.",
|
|
19
|
+
inputSchema: z.object({
|
|
20
|
+
projectId: projectIdInput.describe("Project ID (must be opened)"),
|
|
21
|
+
blockId: blockIdInput.describe("Block ID"),
|
|
22
|
+
}),
|
|
23
|
+
},
|
|
24
|
+
async ({ projectId, blockId }) => {
|
|
25
|
+
const project = await ctx.getOpenedProject(projectId);
|
|
26
|
+
return textResult(await project.getBlockInitializationParams(blockId));
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
}
|
package/src/tools/block-state.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import { deriveDataFromStorage } from "@platforma-sdk/model";
|
|
3
2
|
import { ModelAPIVersionMismatchError } from "@milaboratories/pl-errors";
|
|
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, transformFailedResult } from "./types";
|
|
10
9
|
|
|
11
|
-
export function registerBlockStateTools(server:
|
|
10
|
+
export function registerBlockStateTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
12
11
|
server.registerTool(
|
|
13
12
|
"get_project_overview",
|
|
14
13
|
{
|
|
@@ -85,8 +84,8 @@ export function registerBlockStateTools(server: McpServer, ctx: ToolContext): vo
|
|
|
85
84
|
);
|
|
86
85
|
return textResult(result);
|
|
87
86
|
} catch (e: unknown) {
|
|
88
|
-
return
|
|
89
|
-
|
|
87
|
+
return transformFailedResult(
|
|
88
|
+
e,
|
|
90
89
|
"Check your JS expression syntax. Available variables: data, outputs.",
|
|
91
90
|
);
|
|
92
91
|
}
|
|
@@ -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
|
+
});
|
package/src/tools/blocks.ts
CHANGED
|
@@ -1,49 +1,177 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
1
|
+
import type { BlockPackSpecAny, Project } from "@milaboratories/pl-middle-layer";
|
|
2
|
+
import { BlockKindRef, CentralBlockRegistry } from "@milaboratories/pl-middle-layer";
|
|
3
|
+
import { BlockInitParamsRejectedError } from "@milaboratories/pl-errors";
|
|
4
|
+
import type { BlockKindReference } from "@platforma-sdk/model";
|
|
3
5
|
import { z } from "zod";
|
|
4
6
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
5
|
-
import type { ToolContext } from "./types";
|
|
7
|
+
import type { AuthorMarker, ToolContext, ToolRegistrar } from "./types";
|
|
6
8
|
import { errorResult, hints, textResult } from "./types";
|
|
9
|
+
import { SUPPORTED_BLOCK_API, unsupportedBlockApiResult } from "./block-api";
|
|
7
10
|
|
|
8
|
-
|
|
11
|
+
/** Where a kind is resolved when the caller names no registry. */
|
|
12
|
+
function centralBlockRegistryUrl(): string {
|
|
13
|
+
if (CentralBlockRegistry.type !== "remote-v2")
|
|
14
|
+
throw new Error("The central block registry is expected to be a v2 remote registry.");
|
|
15
|
+
return CentralBlockRegistry.url;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function errorMessage(e: unknown): string {
|
|
19
|
+
return e instanceof Error ? e.message : String(e);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const addBlockInput = z.object({
|
|
23
|
+
projectId: projectIdInput.describe("Project ID (must be opened)"),
|
|
24
|
+
label: z.string().describe("Block label"),
|
|
25
|
+
spec: z
|
|
26
|
+
.union([
|
|
27
|
+
z.object({
|
|
28
|
+
type: z.literal("kind"),
|
|
29
|
+
kind: z
|
|
30
|
+
.string()
|
|
31
|
+
.describe(
|
|
32
|
+
'Block kind reference "<package name>@<version or range>", ' +
|
|
33
|
+
'e.g. "@platforma-open/milaboratories.samples-and-data.kind@1.2.0"',
|
|
34
|
+
),
|
|
35
|
+
registryUrl: z
|
|
36
|
+
.string()
|
|
37
|
+
.optional()
|
|
38
|
+
.describe("Registry to resolve the kind in (default: the central block registry)"),
|
|
39
|
+
allowUnstable: z
|
|
40
|
+
.boolean()
|
|
41
|
+
.optional()
|
|
42
|
+
.describe("Also consider blocks not published to the stable channel"),
|
|
43
|
+
}),
|
|
44
|
+
z.object({
|
|
45
|
+
type: z.literal("from-registry-v2"),
|
|
46
|
+
registryUrl: z.string().describe("Registry URL"),
|
|
47
|
+
id: z.object({
|
|
48
|
+
organization: z.string(),
|
|
49
|
+
name: z.string(),
|
|
50
|
+
version: z.string(),
|
|
51
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
z.object({
|
|
54
|
+
type: z.literal("dev-v2"),
|
|
55
|
+
folder: z.string().describe("Path to block folder"),
|
|
56
|
+
}),
|
|
57
|
+
])
|
|
58
|
+
.describe("Which block to add"),
|
|
59
|
+
params: z
|
|
60
|
+
.record(z.string(), z.unknown())
|
|
61
|
+
.optional()
|
|
62
|
+
.describe("Initialization params of the block's kind. Omit for the default state."),
|
|
63
|
+
blockIds: z
|
|
64
|
+
.record(z.string(), blockIdInput)
|
|
65
|
+
.optional()
|
|
66
|
+
.describe(
|
|
67
|
+
"Ids used inside `params` in place of block ids, each mapped to a block id of this " +
|
|
68
|
+
"project. Needed only when `params` do not already hold ids of this project.",
|
|
69
|
+
),
|
|
70
|
+
before: blockIdInput
|
|
71
|
+
.optional()
|
|
72
|
+
.describe("ID of the block to insert the new block before (default: add at the end)"),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/** Input of the `add_block` tool. */
|
|
76
|
+
export type AddBlockInput = z.infer<typeof addBlockInput>;
|
|
77
|
+
|
|
78
|
+
/** What `add_block` needs from the application. */
|
|
79
|
+
export type AddBlockDeps = {
|
|
80
|
+
project: Pick<Project, "addBlock">;
|
|
81
|
+
/** Resolves a kind reference to the block implementing it; throws when there is none. */
|
|
82
|
+
resolveKind: (
|
|
83
|
+
kind: BlockKindReference,
|
|
84
|
+
registryUrl: string | undefined,
|
|
85
|
+
allowUnstable: boolean,
|
|
86
|
+
) => Promise<BlockPackSpecAny>;
|
|
87
|
+
author: AuthorMarker;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Adds the block the input describes, started from its params when it carries them. Every way the
|
|
92
|
+
* block can be refused comes back as an error result saying nothing was created.
|
|
93
|
+
*/
|
|
94
|
+
export async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {
|
|
95
|
+
const { label, spec, params, blockIds, before } = input;
|
|
96
|
+
|
|
97
|
+
let blockPackSpec: BlockPackSpecAny;
|
|
98
|
+
if (spec.type === "kind") {
|
|
99
|
+
try {
|
|
100
|
+
blockPackSpec = await deps.resolveKind(
|
|
101
|
+
BlockKindRef.parse(spec.kind),
|
|
102
|
+
spec.registryUrl,
|
|
103
|
+
spec.allowUnstable ?? false,
|
|
104
|
+
);
|
|
105
|
+
} catch (e: unknown) {
|
|
106
|
+
return errorResult(
|
|
107
|
+
`Kind "${spec.kind}" was not resolved to a block: ${errorMessage(e)}. Nothing was created.`,
|
|
108
|
+
"Check the kind name and version. Pass allowUnstable: true if the block is not " +
|
|
109
|
+
"published to the stable channel, or registryUrl if it lives in another registry.",
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
blockPackSpec = spec;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const blockId = await deps.project.addBlock(
|
|
118
|
+
label,
|
|
119
|
+
blockPackSpec,
|
|
120
|
+
before,
|
|
121
|
+
deps.author,
|
|
122
|
+
undefined,
|
|
123
|
+
{
|
|
124
|
+
init: params === undefined ? undefined : { params, blockIds },
|
|
125
|
+
requireModelAPIVersion: SUPPORTED_BLOCK_API,
|
|
126
|
+
},
|
|
127
|
+
);
|
|
128
|
+
return textResult({ blockId });
|
|
129
|
+
} catch (e: unknown) {
|
|
130
|
+
const unsupported = unsupportedBlockApiResult(e);
|
|
131
|
+
if (unsupported) return unsupported;
|
|
132
|
+
if (e instanceof BlockInitParamsRejectedError)
|
|
133
|
+
return errorResult(
|
|
134
|
+
`The block did not accept the params: ${e.reason}. Nothing was created.`,
|
|
135
|
+
"Fix the params and call add_block again. get_block_params on a configured block " +
|
|
136
|
+
"of the same kind shows the expected shape.",
|
|
137
|
+
);
|
|
138
|
+
throw e;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function registerBlockTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
9
143
|
server.registerTool(
|
|
10
144
|
"add_block",
|
|
11
145
|
{
|
|
12
146
|
title: "Add Block",
|
|
13
147
|
annotations: hints.additive,
|
|
14
148
|
description:
|
|
15
|
-
"Add a block to an opened project
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
version: z.string(),
|
|
28
|
-
}),
|
|
29
|
-
}),
|
|
30
|
-
z.object({
|
|
31
|
-
type: z.literal("dev-v2"),
|
|
32
|
-
folder: z.string().describe("Path to block folder"),
|
|
33
|
-
}),
|
|
34
|
-
])
|
|
35
|
-
.describe("Block pack specification"),
|
|
36
|
-
}),
|
|
149
|
+
"Add a block to an opened project, optionally configured from initialization params.\n\n" +
|
|
150
|
+
"`spec` names the block: `kind` (a block kind reference, resolved to the newest block " +
|
|
151
|
+
"implementing it), `from-registry-v2` (an exact published block) or `dev-v2` (a local dev " +
|
|
152
|
+
"block folder).\n\n" +
|
|
153
|
+
"`params` are the initialization params of the block's kind: the block's own model checks " +
|
|
154
|
+
"them and builds its initial state from them. Without `params` the block starts in its " +
|
|
155
|
+
"default state. get_block_params shows the params of an existing block.\n\n" +
|
|
156
|
+
"References to other blocks inside `params` must hold real block ids of this project, " +
|
|
157
|
+
"copied from tool results. Params copied from another project or a template hold other " +
|
|
158
|
+
"ids: map each of them to a block of this project with `blockIds`.\n\n" +
|
|
159
|
+
"Only blocks on the current block API can be added. On any error nothing is created.",
|
|
160
|
+
inputSchema: addBlockInput,
|
|
37
161
|
},
|
|
38
|
-
async (
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
162
|
+
async (input) => {
|
|
163
|
+
const ml = ctx.requireMl();
|
|
164
|
+
return await addBlock(
|
|
165
|
+
{
|
|
166
|
+
project: await ctx.getOpenedProject(input.projectId),
|
|
167
|
+
resolveKind: (kind, registryUrl, allowUnstable) =>
|
|
168
|
+
ml.blockRegistryProvider
|
|
169
|
+
.getRegistry(registryUrl ?? centralBlockRegistryUrl())
|
|
170
|
+
.resolveKind(kind, { allowUnstable }),
|
|
171
|
+
author: ctx.getAuthorMarker(),
|
|
172
|
+
},
|
|
173
|
+
input,
|
|
45
174
|
);
|
|
46
|
-
return textResult({ blockId });
|
|
47
175
|
},
|
|
48
176
|
);
|
|
49
177
|
|
package/src/tools/connection.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import { z } from "zod";
|
|
3
|
-
import type { ToolContext } from "./types";
|
|
2
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
4
3
|
import { errorResult, hints, textResult } from "./types";
|
|
5
4
|
|
|
6
|
-
export function registerConnectionTools(server:
|
|
5
|
+
export function registerConnectionTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
7
6
|
server.registerTool(
|
|
8
7
|
"get_connection_status",
|
|
9
8
|
{
|
package/src/tools/data-query.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import type {
|
|
3
2
|
PFrameHandle,
|
|
4
3
|
PTableHandle,
|
|
@@ -13,9 +12,9 @@ import {
|
|
|
13
12
|
} from "@milaboratories/pl-model-common";
|
|
14
13
|
import { z } from "zod";
|
|
15
14
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
16
|
-
import type { ToolContext } from "./types";
|
|
15
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
17
16
|
import { safeEval } from "./sandbox";
|
|
18
|
-
import { errorResult, hints, textResult } from "./types";
|
|
17
|
+
import { errorResult, hints, textResult, transformFailedResult } from "./types";
|
|
19
18
|
|
|
20
19
|
const HEX_HASH_RE = /^[a-f0-9]{64}$/;
|
|
21
20
|
|
|
@@ -119,7 +118,7 @@ function vectorToJson(vector: PTableVector, rows: number): (string | number | nu
|
|
|
119
118
|
return result;
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
export function registerDataQueryTools(server:
|
|
121
|
+
export function registerDataQueryTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
123
122
|
server.registerTool(
|
|
124
123
|
"get_block_outputs",
|
|
125
124
|
{
|
|
@@ -297,8 +296,8 @@ export function registerDataQueryTools(server: McpServer, ctx: ToolContext): voi
|
|
|
297
296
|
);
|
|
298
297
|
return textResult(result);
|
|
299
298
|
} catch (e: unknown) {
|
|
300
|
-
return
|
|
301
|
-
|
|
299
|
+
return transformFailedResult(
|
|
300
|
+
e,
|
|
302
301
|
"Check your JS expression syntax. Available variables: rows, columns, offset, rowCount.",
|
|
303
302
|
);
|
|
304
303
|
}
|
package/src/tools/logs.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import { isAnyLogHandle } from "@milaboratories/pl-model-common";
|
|
3
2
|
import type { AnyLogHandle } from "@milaboratories/pl-model-common";
|
|
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 { errorResult, hints, textResult } from "./types";
|
|
8
7
|
|
|
9
|
-
export function registerLogTools(server:
|
|
8
|
+
export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
10
9
|
server.registerTool(
|
|
11
10
|
"get_block_logs",
|
|
12
11
|
{
|
package/src/tools/ping.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { ToolContext } from "./types";
|
|
1
|
+
import type { ToolContext, ToolRegistrar } from "./types";
|
|
3
2
|
import { hints, textResult } from "./types";
|
|
4
3
|
|
|
5
|
-
export function registerPingTool(server:
|
|
4
|
+
export function registerPingTool(server: ToolRegistrar, ctx: ToolContext): void {
|
|
6
5
|
server.registerTool(
|
|
7
6
|
"ping",
|
|
8
7
|
{
|
package/src/tools/projects.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
1
|
import { z } from "zod";
|
|
3
2
|
import { projectIdInput } 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 registerProjectTools(server:
|
|
6
|
+
export function registerProjectTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
8
7
|
server.registerTool(
|
|
9
8
|
"list_projects",
|
|
10
9
|
{
|