@milaboratories/pl-mcp-server 52.1.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.
@@ -1,9 +1,144 @@
1
1
  import type { McpServer } from "@modelcontextprotocol/server";
2
- import type { BlockPackSpecAny } from "@milaboratories/pl-middle-layer";
2
+ import type { BlockPackSpecAny, Project } from "@milaboratories/pl-middle-layer";
3
+ import { BlockKindRef, CentralBlockRegistry } from "@milaboratories/pl-middle-layer";
4
+ import { BlockInitParamsRejectedError } from "@milaboratories/pl-errors";
5
+ import type { BlockKindReference } from "@platforma-sdk/model";
3
6
  import { z } from "zod";
4
7
  import { projectIdInput, blockIdInput } from "./ids";
5
- import type { ToolContext } from "./types";
8
+ import type { AuthorMarker, ToolContext } from "./types";
6
9
  import { errorResult, hints, textResult } from "./types";
10
+ import { SUPPORTED_BLOCK_API, unsupportedBlockApiResult } from "./block-api";
11
+
12
+ /** Where a kind is resolved when the caller names no registry. */
13
+ function centralBlockRegistryUrl(): string {
14
+ if (CentralBlockRegistry.type !== "remote-v2")
15
+ throw new Error("The central block registry is expected to be a v2 remote registry.");
16
+ return CentralBlockRegistry.url;
17
+ }
18
+
19
+ function errorMessage(e: unknown): string {
20
+ return e instanceof Error ? e.message : String(e);
21
+ }
22
+
23
+ const addBlockInput = z.object({
24
+ projectId: projectIdInput.describe("Project ID (must be opened)"),
25
+ label: z.string().describe("Block label"),
26
+ spec: z
27
+ .union([
28
+ z.object({
29
+ type: z.literal("kind"),
30
+ kind: z
31
+ .string()
32
+ .describe(
33
+ 'Block kind reference "<package name>@<version or range>", ' +
34
+ 'e.g. "@platforma-open/milaboratories.samples-and-data.kind@1.2.0"',
35
+ ),
36
+ registryUrl: z
37
+ .string()
38
+ .optional()
39
+ .describe("Registry to resolve the kind in (default: the central block registry)"),
40
+ allowUnstable: z
41
+ .boolean()
42
+ .optional()
43
+ .describe("Also consider blocks not published to the stable channel"),
44
+ }),
45
+ z.object({
46
+ type: z.literal("from-registry-v2"),
47
+ registryUrl: z.string().describe("Registry URL"),
48
+ id: z.object({
49
+ organization: z.string(),
50
+ name: z.string(),
51
+ version: z.string(),
52
+ }),
53
+ }),
54
+ z.object({
55
+ type: z.literal("dev-v2"),
56
+ folder: z.string().describe("Path to block folder"),
57
+ }),
58
+ ])
59
+ .describe("Which block to add"),
60
+ params: z
61
+ .record(z.string(), z.unknown())
62
+ .optional()
63
+ .describe("Initialization params of the block's kind. Omit for the default state."),
64
+ blockIds: z
65
+ .record(z.string(), blockIdInput)
66
+ .optional()
67
+ .describe(
68
+ "Ids used inside `params` in place of block ids, each mapped to a block id of this " +
69
+ "project. Needed only when `params` do not already hold ids of this project.",
70
+ ),
71
+ before: blockIdInput
72
+ .optional()
73
+ .describe("ID of the block to insert the new block before (default: add at the end)"),
74
+ });
75
+
76
+ /** Input of the `add_block` tool. */
77
+ export type AddBlockInput = z.infer<typeof addBlockInput>;
78
+
79
+ /** What `add_block` needs from the application. */
80
+ export type AddBlockDeps = {
81
+ project: Pick<Project, "addBlock">;
82
+ /** Resolves a kind reference to the block implementing it; throws when there is none. */
83
+ resolveKind: (
84
+ kind: BlockKindReference,
85
+ registryUrl: string | undefined,
86
+ allowUnstable: boolean,
87
+ ) => Promise<BlockPackSpecAny>;
88
+ author: AuthorMarker;
89
+ };
90
+
91
+ /**
92
+ * Adds the block the input describes, started from its params when it carries them. Every way the
93
+ * block can be refused comes back as an error result saying nothing was created.
94
+ */
95
+ export async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {
96
+ const { label, spec, params, blockIds, before } = input;
97
+
98
+ let blockPackSpec: BlockPackSpecAny;
99
+ if (spec.type === "kind") {
100
+ try {
101
+ blockPackSpec = await deps.resolveKind(
102
+ BlockKindRef.parse(spec.kind),
103
+ spec.registryUrl,
104
+ spec.allowUnstable ?? false,
105
+ );
106
+ } catch (e: unknown) {
107
+ return errorResult(
108
+ `Kind "${spec.kind}" was not resolved to a block: ${errorMessage(e)}. Nothing was created.`,
109
+ "Check the kind name and version. Pass allowUnstable: true if the block is not " +
110
+ "published to the stable channel, or registryUrl if it lives in another registry.",
111
+ );
112
+ }
113
+ } else {
114
+ blockPackSpec = spec;
115
+ }
116
+
117
+ try {
118
+ const blockId = await deps.project.addBlock(
119
+ label,
120
+ blockPackSpec,
121
+ before,
122
+ deps.author,
123
+ undefined,
124
+ {
125
+ init: params === undefined ? undefined : { params, blockIds },
126
+ requireModelAPIVersion: SUPPORTED_BLOCK_API,
127
+ },
128
+ );
129
+ return textResult({ blockId });
130
+ } catch (e: unknown) {
131
+ const unsupported = unsupportedBlockApiResult(e);
132
+ if (unsupported) return unsupported;
133
+ if (e instanceof BlockInitParamsRejectedError)
134
+ return errorResult(
135
+ `The block did not accept the params: ${e.reason}. Nothing was created.`,
136
+ "Fix the params and call add_block again. get_block_params on a configured block " +
137
+ "of the same kind shows the expected shape.",
138
+ );
139
+ throw e;
140
+ }
141
+ }
7
142
 
8
143
  export function registerBlockTools(server: McpServer, ctx: ToolContext): void {
9
144
  server.registerTool(
@@ -12,38 +147,32 @@ export function registerBlockTools(server: McpServer, ctx: ToolContext): void {
12
147
  title: "Add Block",
13
148
  annotations: hints.additive,
14
149
  description:
15
- "Add a block to an opened project. Spec can be from-registry-v2 (for published blocks) or dev-v2 (for local dev blocks).",
16
- inputSchema: z.object({
17
- projectId: projectIdInput.describe("Project ID (must be opened)"),
18
- label: z.string().describe("Block label"),
19
- spec: z
20
- .union([
21
- z.object({
22
- type: z.literal("from-registry-v2"),
23
- registryUrl: z.string().describe("Registry URL"),
24
- id: z.object({
25
- organization: z.string(),
26
- name: z.string(),
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
- }),
150
+ "Add a block to an opened project, optionally configured from initialization params.\n\n" +
151
+ "`spec` names the block: `kind` (a block kind reference, resolved to the newest block " +
152
+ "implementing it), `from-registry-v2` (an exact published block) or `dev-v2` (a local dev " +
153
+ "block folder).\n\n" +
154
+ "`params` are the initialization params of the block's kind: the block's own model checks " +
155
+ "them and builds its initial state from them. Without `params` the block starts in its " +
156
+ "default state. get_block_params shows the params of an existing block.\n\n" +
157
+ "References to other blocks inside `params` must hold real block ids of this project, " +
158
+ "copied from tool results. Params copied from another project or a template hold other " +
159
+ "ids: map each of them to a block of this project with `blockIds`.\n\n" +
160
+ "Only blocks on the current block API can be added. On any error nothing is created.",
161
+ inputSchema: addBlockInput,
37
162
  },
38
- async ({ projectId, label, spec }) => {
39
- const project = await ctx.getOpenedProject(projectId);
40
- const blockId = await project.addBlock(
41
- label,
42
- spec as BlockPackSpecAny,
43
- undefined,
44
- ctx.getAuthorMarker(),
163
+ async (input) => {
164
+ const ml = ctx.requireMl();
165
+ return await addBlock(
166
+ {
167
+ project: await ctx.getOpenedProject(input.projectId),
168
+ resolveKind: (kind, registryUrl, allowUnstable) =>
169
+ ml.blockRegistryProvider
170
+ .getRegistry(registryUrl ?? centralBlockRegistryUrl())
171
+ .resolveKind(kind, { allowUnstable }),
172
+ author: ctx.getAuthorMarker(),
173
+ },
174
+ input,
45
175
  );
46
- return textResult({ blockId });
47
176
  },
48
177
  );
49
178