@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 +1 @@
1
- {"version":3,"file":"blocks.cjs","names":["hints","z","projectIdInput","textResult","blockIdInput","errorResult"],"sources":["../../src/tools/blocks.ts"],"sourcesContent":["import type { McpServer } from \"@modelcontextprotocol/server\";\nimport type { BlockPackSpecAny } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\n\nexport function registerBlockTools(server: McpServer, ctx: ToolContext): void {\n server.registerTool(\n \"add_block\",\n {\n title: \"Add Block\",\n annotations: hints.additive,\n description:\n \"Add a block to an opened project. Spec can be from-registry-v2 (for published blocks) or dev-v2 (for local dev blocks).\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n label: z.string().describe(\"Block label\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Block pack specification\"),\n }),\n },\n async ({ projectId, label, spec }) => {\n const project = await ctx.getOpenedProject(projectId);\n const blockId = await project.addBlock(\n label,\n spec as BlockPackSpecAny,\n undefined,\n ctx.getAuthorMarker(),\n );\n return textResult({ blockId });\n },\n );\n\n server.registerTool(\n \"update_block\",\n {\n title: \"Update Block\",\n annotations: hints.destructive,\n description:\n \"Update an existing block's pack (reload from registry or dev folder). Use after rebuilding a dev block to pick up changes without removing/re-adding it.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n blockId: blockIdInput.describe(\"Block ID to update\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Block pack specification\"),\n resetArgs: z\n .boolean()\n .optional()\n .describe(\"Reset block arguments to initial values (default: false)\"),\n }),\n },\n async ({ projectId, blockId, spec, resetArgs }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.updateBlockPack(\n blockId,\n spec as BlockPackSpecAny,\n resetArgs ?? false,\n ctx.getAuthorMarker(),\n );\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"remove_block\",\n {\n title: \"Remove Block\",\n annotations: hints.destructive,\n description:\n \"Remove a block from an opened project, with its configuration and results. Blocks below that refer to it are left with a missing reference.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to remove\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.deleteBlock(blockId, ctx.getAuthorMarker());\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"run_block\",\n {\n title: \"Run Block\",\n // Starts work like an additive tool, but replaces results and stops blocks computing below.\n annotations: { ...hints.additive, destructiveHint: true },\n description:\n \"Run a block. Stale upstream blocks are started automatically. A run replaces the previous results of each block it starts; downstream blocks that are still computing are stopped and their unfinished work is lost.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to run\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.runBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"stop_block\",\n {\n title: \"Stop Block\",\n annotations: hints.destructive,\n description: \"Stop the computation of a running block. Use run_block to start it again.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to stop\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.stopBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"reorder_blocks\",\n {\n title: \"Reorder Blocks\",\n annotations: hints.destructive,\n description: \"Reorder blocks in a project. Must provide ALL block IDs in the desired order.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockIds: z.array(blockIdInput).describe(\"All block IDs in the desired order\"),\n }),\n },\n async ({ projectId, blockIds }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.reorderBlocks(blockIds);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"list_available_blocks\",\n {\n title: \"List Available Blocks\",\n annotations: hints.read,\n description:\n \"List available blocks from configured registries. Optional query to filter by name.\",\n inputSchema: z.object({\n query: z\n .string()\n .optional()\n .describe(\"Filter blocks by name (case-insensitive substring match)\"),\n }),\n },\n async ({ query }) => {\n if (!ctx.callbacks.listAvailableBlocks) {\n return errorResult(\n \"Block registry is not available.\",\n \"This usually means the desktop app encounters a problem connecting block registry. Check Settings > Override Main Registry find which registry really in use and check connection.\",\n );\n }\n const blocks = await ctx.callbacks.listAvailableBlocks(query);\n return textResult(blocks);\n },\n );\n\n server.registerTool(\n \"get_block_info\",\n {\n title: \"Get Block Info\",\n annotations: hints.read,\n description:\n \"Get detailed info about a specific block package from the registry. Use list_available_blocks first to find the block.\",\n inputSchema: z.object({\n registryUrl: z.string().describe(\"Registry URL (from list_available_blocks)\"),\n organization: z.string().describe(\"Organization name\"),\n name: z.string().describe(\"Block package name\"),\n version: z.string().describe(\"Block version\"),\n }),\n },\n async ({ registryUrl, organization, name, version }) => {\n if (!ctx.callbacks.getBlockInfo) {\n return errorResult(\n \"Block info is not available in this environment.\",\n 'Maybe the name of the block was written incerrectly. Use list_available_blocks to browse blocks instead. Or ask user to check \"Additional Registries\" in Settings panel',\n );\n }\n const info = await ctx.callbacks.getBlockInfo(registryUrl, organization, name, version);\n return textResult(info);\n },\n );\n\n server.registerTool(\n \"select_block\",\n {\n title: \"Show Block in the Application\",\n annotations: hints.idempotent,\n description: \"Navigate the desktop UI to show a specific block's interface\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to display\"),\n }),\n },\n async ({ projectId, blockId }) => {\n if (!ctx.callbacks.selectBlock) {\n return errorResult(\n \"Failed to select the block.\",\n \"This feature requires server connected and open project. Use get_connection_status and list_projects to check. If there are no connection, use list_connections and ask user which should be used.\",\n );\n }\n await ctx.callbacks.selectBlock(projectId, blockId);\n return textResult({ ok: true });\n },\n );\n}\n"],"mappings":";;;;AAOA,SAAgB,mBAAmB,QAAmB,KAAwB;CAC5E,OAAO,aACL,aACA;EACE,OAAO;EACP,aAAaA,cAAAA,MAAM;EACnB,aACE;EACF,aAAaC,IAAAA,EAAE,OAAO;GACpB,WAAWC,YAAAA,eAAe,SAAS,6BAA6B;GAChE,OAAOD,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,aAAa;GACxC,MAAMA,IAAAA,EACH,MAAM,CACLA,IAAAA,EAAE,OAAO;IACP,MAAMA,IAAAA,EAAE,QAAQ,kBAAkB;IAClC,aAAaA,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,cAAc;IAC/C,IAAIA,IAAAA,EAAE,OAAO;KACX,cAAcA,IAAAA,EAAE,OAAO;KACvB,MAAMA,IAAAA,EAAE,OAAO;KACf,SAASA,IAAAA,EAAE,OAAO;IACpB,CAAC;GACH,CAAC,GACDA,IAAAA,EAAE,OAAO;IACP,MAAMA,IAAAA,EAAE,QAAQ,QAAQ;IACxB,QAAQA,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,sBAAsB;GACpD,CAAC,CACH,CAAC,CAAC,CACD,SAAS,0BAA0B;EACxC,CAAC;CACH,GACA,OAAO,EAAE,WAAW,OAAO,WAAW;E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,SAAS,YAAY;GAC/C,SAASE,YAAAA,aAAa,SAAS,qBAAqB;EACtD,CAAC;CACH,GACA,OAAO,EAAE,WAAW,cAAc;EAChC,IAAI,CAAC,IAAI,UAAU,aACjB,OAAOC,cAAAA,YACL,+BACA,oMACF;EAEF,MAAM,IAAI,UAAU,YAAY,WAAW,OAAO;EAClD,OAAOF,cAAAA,WAAW,EAAE,IAAI,KAAK,CAAC;CAChC,CACF;AACF"}
1
+ {"version":3,"file":"blocks.cjs","names":["CentralBlockRegistry","z","projectIdInput","blockIdInput","BlockKindRef","errorResult","SUPPORTED_BLOCK_API","textResult","unsupportedBlockApiResult","BlockInitParamsRejectedError","hints"],"sources":["../../src/tools/blocks.ts"],"sourcesContent":["import type { McpServer } from \"@modelcontextprotocol/server\";\nimport type { BlockPackSpecAny, Project } from \"@milaboratories/pl-middle-layer\";\nimport { BlockKindRef, CentralBlockRegistry } from \"@milaboratories/pl-middle-layer\";\nimport { BlockInitParamsRejectedError } from \"@milaboratories/pl-errors\";\nimport type { BlockKindReference } from \"@platforma-sdk/model\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { AuthorMarker, ToolContext } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\nimport { SUPPORTED_BLOCK_API, unsupportedBlockApiResult } from \"./block-api\";\n\n/** Where a kind is resolved when the caller names no registry. */\nfunction centralBlockRegistryUrl(): string {\n if (CentralBlockRegistry.type !== \"remote-v2\")\n throw new Error(\"The central block registry is expected to be a v2 remote registry.\");\n return CentralBlockRegistry.url;\n}\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\nconst addBlockInput = z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n label: z.string().describe(\"Block label\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"kind\"),\n kind: z\n .string()\n .describe(\n 'Block kind reference \"<package name>@<version or range>\", ' +\n 'e.g. \"@platforma-open/milaboratories.samples-and-data.kind@1.2.0\"',\n ),\n registryUrl: z\n .string()\n .optional()\n .describe(\"Registry to resolve the kind in (default: the central block registry)\"),\n allowUnstable: z\n .boolean()\n .optional()\n .describe(\"Also consider blocks not published to the stable channel\"),\n }),\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Which block to add\"),\n params: z\n .record(z.string(), z.unknown())\n .optional()\n .describe(\"Initialization params of the block's kind. Omit for the default state.\"),\n blockIds: z\n .record(z.string(), blockIdInput)\n .optional()\n .describe(\n \"Ids used inside `params` in place of block ids, each mapped to a block id of this \" +\n \"project. Needed only when `params` do not already hold ids of this project.\",\n ),\n before: blockIdInput\n .optional()\n .describe(\"ID of the block to insert the new block before (default: add at the end)\"),\n});\n\n/** Input of the `add_block` tool. */\nexport type AddBlockInput = z.infer<typeof addBlockInput>;\n\n/** What `add_block` needs from the application. */\nexport type AddBlockDeps = {\n project: Pick<Project, \"addBlock\">;\n /** Resolves a kind reference to the block implementing it; throws when there is none. */\n resolveKind: (\n kind: BlockKindReference,\n registryUrl: string | undefined,\n allowUnstable: boolean,\n ) => Promise<BlockPackSpecAny>;\n author: AuthorMarker;\n};\n\n/**\n * Adds the block the input describes, started from its params when it carries them. Every way the\n * block can be refused comes back as an error result saying nothing was created.\n */\nexport async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {\n const { label, spec, params, blockIds, before } = input;\n\n let blockPackSpec: BlockPackSpecAny;\n if (spec.type === \"kind\") {\n try {\n blockPackSpec = await deps.resolveKind(\n BlockKindRef.parse(spec.kind),\n spec.registryUrl,\n spec.allowUnstable ?? false,\n );\n } catch (e: unknown) {\n return errorResult(\n `Kind \"${spec.kind}\" was not resolved to a block: ${errorMessage(e)}. Nothing was created.`,\n \"Check the kind name and version. Pass allowUnstable: true if the block is not \" +\n \"published to the stable channel, or registryUrl if it lives in another registry.\",\n );\n }\n } else {\n blockPackSpec = spec;\n }\n\n try {\n const blockId = await deps.project.addBlock(\n label,\n blockPackSpec,\n before,\n deps.author,\n undefined,\n {\n init: params === undefined ? undefined : { params, blockIds },\n requireModelAPIVersion: SUPPORTED_BLOCK_API,\n },\n );\n return textResult({ blockId });\n } catch (e: unknown) {\n const unsupported = unsupportedBlockApiResult(e);\n if (unsupported) return unsupported;\n if (e instanceof BlockInitParamsRejectedError)\n return errorResult(\n `The block did not accept the params: ${e.reason}. Nothing was created.`,\n \"Fix the params and call add_block again. get_block_params on a configured block \" +\n \"of the same kind shows the expected shape.\",\n );\n throw e;\n }\n}\n\nexport function registerBlockTools(server: McpServer, ctx: ToolContext): void {\n server.registerTool(\n \"add_block\",\n {\n title: \"Add Block\",\n annotations: hints.additive,\n description:\n \"Add a block to an opened project, optionally configured from initialization params.\\n\\n\" +\n \"`spec` names the block: `kind` (a block kind reference, resolved to the newest block \" +\n \"implementing it), `from-registry-v2` (an exact published block) or `dev-v2` (a local dev \" +\n \"block folder).\\n\\n\" +\n \"`params` are the initialization params of the block's kind: the block's own model checks \" +\n \"them and builds its initial state from them. Without `params` the block starts in its \" +\n \"default state. get_block_params shows the params of an existing block.\\n\\n\" +\n \"References to other blocks inside `params` must hold real block ids of this project, \" +\n \"copied from tool results. Params copied from another project or a template hold other \" +\n \"ids: map each of them to a block of this project with `blockIds`.\\n\\n\" +\n \"Only blocks on the current block API can be added. On any error nothing is created.\",\n inputSchema: addBlockInput,\n },\n async (input) => {\n const ml = ctx.requireMl();\n return await addBlock(\n {\n project: await ctx.getOpenedProject(input.projectId),\n resolveKind: (kind, registryUrl, allowUnstable) =>\n ml.blockRegistryProvider\n .getRegistry(registryUrl ?? centralBlockRegistryUrl())\n .resolveKind(kind, { allowUnstable }),\n author: ctx.getAuthorMarker(),\n },\n input,\n );\n },\n );\n\n server.registerTool(\n \"update_block\",\n {\n title: \"Update Block\",\n annotations: hints.destructive,\n description:\n \"Update an existing block's pack (reload from registry or dev folder). Use after rebuilding a dev block to pick up changes without removing/re-adding it.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n blockId: blockIdInput.describe(\"Block ID to update\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Block pack specification\"),\n resetArgs: z\n .boolean()\n .optional()\n .describe(\"Reset block arguments to initial values (default: false)\"),\n }),\n },\n async ({ projectId, blockId, spec, resetArgs }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.updateBlockPack(\n blockId,\n spec as BlockPackSpecAny,\n resetArgs ?? false,\n ctx.getAuthorMarker(),\n );\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"remove_block\",\n {\n title: \"Remove Block\",\n annotations: hints.destructive,\n description:\n \"Remove a block from an opened project, with its configuration and results. Blocks below that refer to it are left with a missing reference.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to remove\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.deleteBlock(blockId, ctx.getAuthorMarker());\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"run_block\",\n {\n title: \"Run Block\",\n // Starts work like an additive tool, but replaces results and stops blocks computing below.\n annotations: { ...hints.additive, destructiveHint: true },\n description:\n \"Run a block. Stale upstream blocks are started automatically. A run replaces the previous results of each block it starts; downstream blocks that are still computing are stopped and their unfinished work is lost.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to run\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.runBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"stop_block\",\n {\n title: \"Stop Block\",\n annotations: hints.destructive,\n description: \"Stop the computation of a running block. Use run_block to start it again.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to stop\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.stopBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"reorder_blocks\",\n {\n title: \"Reorder Blocks\",\n annotations: hints.destructive,\n description: \"Reorder blocks in a project. Must provide ALL block IDs in the desired order.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockIds: z.array(blockIdInput).describe(\"All block IDs in the desired order\"),\n }),\n },\n async ({ projectId, blockIds }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.reorderBlocks(blockIds);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"list_available_blocks\",\n {\n title: \"List Available Blocks\",\n annotations: hints.read,\n description:\n \"List available blocks from configured registries. Optional query to filter by name.\",\n inputSchema: z.object({\n query: z\n .string()\n .optional()\n .describe(\"Filter blocks by name (case-insensitive substring match)\"),\n }),\n },\n async ({ query }) => {\n if (!ctx.callbacks.listAvailableBlocks) {\n return errorResult(\n \"Block registry is not available.\",\n \"This usually means the desktop app encounters a problem connecting block registry. Check Settings > Override Main Registry find which registry really in use and check connection.\",\n );\n }\n const blocks = await ctx.callbacks.listAvailableBlocks(query);\n return textResult(blocks);\n },\n );\n\n server.registerTool(\n \"get_block_info\",\n {\n title: \"Get Block Info\",\n annotations: hints.read,\n description:\n \"Get detailed info about a specific block package from the registry. Use list_available_blocks first to find the block.\",\n inputSchema: z.object({\n registryUrl: z.string().describe(\"Registry URL (from list_available_blocks)\"),\n organization: z.string().describe(\"Organization name\"),\n name: z.string().describe(\"Block package name\"),\n version: z.string().describe(\"Block version\"),\n }),\n },\n async ({ registryUrl, organization, name, version }) => {\n if (!ctx.callbacks.getBlockInfo) {\n return errorResult(\n \"Block info is not available in this environment.\",\n 'Maybe the name of the block was written incerrectly. Use list_available_blocks to browse blocks instead. Or ask user to check \"Additional Registries\" in Settings panel',\n );\n }\n const info = await ctx.callbacks.getBlockInfo(registryUrl, organization, name, version);\n return textResult(info);\n },\n );\n\n server.registerTool(\n \"select_block\",\n {\n title: \"Show Block in the Application\",\n annotations: hints.idempotent,\n description: \"Navigate the desktop UI to show a specific block's interface\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to display\"),\n }),\n },\n async ({ projectId, blockId }) => {\n if (!ctx.callbacks.selectBlock) {\n return errorResult(\n \"Failed to select the block.\",\n \"This feature requires server connected and open project. Use get_connection_status and list_projects to check. If there are no connection, use list_connections and ask user which should be used.\",\n );\n }\n await ctx.callbacks.selectBlock(projectId, blockId);\n return textResult({ ok: true });\n },\n );\n}\n"],"mappings":";;;;;;;;AAYA,SAAS,0BAAkC;CACzC,IAAIA,gCAAAA,qBAAqB,SAAS,aAChC,MAAM,IAAI,MAAM,oEAAoE;CACtF,OAAOA,gCAAAA,qBAAqB;AAC9B;AAEA,SAAS,aAAa,GAAoB;CACxC,OAAO,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC;AAClD;AAEA,MAAM,gBAAgBC,IAAAA,EAAE,OAAO;CAC7B,WAAWC,YAAAA,eAAe,SAAS,6BAA6B;CAChE,OAAOD,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,aAAa;CACxC,MAAMA,IAAAA,EACH,MAAM;EACLA,IAAAA,EAAE,OAAO;GACP,MAAMA,IAAAA,EAAE,QAAQ,MAAM;GACtB,MAAMA,IAAAA,EACH,OAAO,CAAC,CACR,SACC,iIAEF;GACF,aAAaA,IAAAA,EACV,OAAO,CAAC,CACR,SAAS,CAAC,CACV,SAAS,uEAAuE;GACnF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E;EACF,aAAaT,IAAAA,EAAE,OAAO;GACpB,aAAaA,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,2CAA2C;GAC5E,cAAcA,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,mBAAmB;GACrD,MAAMA,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,oBAAoB;GAC9C,SAASA,IAAAA,EAAE,OAAO,CAAC,CAAC,SAAS,eAAe;EAC9C,CAAC;CACH,GACA,OAAO,EAAE,aAAa,cAAc,MAAM,cAAc;EACtD,IAAI,CAAC,IAAI,UAAU,cACjB,OAAOI,cAAAA,YACL,oDACA,2KACF;EAEF,MAAM,OAAO,MAAM,IAAI,UAAU,aAAa,aAAa,cAAc,MAAM,OAAO;EACtF,OAAOE,cAAAA,WAAW,IAAI;CACxB,CACF;CAEA,OAAO,aACL,gBACA;EACE,OAAO;EACP,aAAaG,cAAAA,MAAM;EACnB,aAAa;EACb,aAAaT,IAAAA,EAAE,OAAO;GACpB,WAAWC,YAAAA,eAAe,SAAS,YAAY;GAC/C,SAASC,YAAAA,aAAa,SAAS,qBAAqB;EACtD,CAAC;CACH,GACA,OAAO,EAAE,WAAW,cAAc;EAChC,IAAI,CAAC,IAAI,UAAU,aACjB,OAAOE,cAAAA,YACL,+BACA,oMACF;EAEF,MAAM,IAAI,UAAU,YAAY,WAAW,OAAO;EAClD,OAAOE,cAAAA,WAAW,EAAE,IAAI,KAAK,CAAC;CAChC,CACF;AACF"}
@@ -1,31 +1,88 @@
1
1
  import { errorResult, hints, textResult } from "./types.js";
2
2
  import { blockIdInput, projectIdInput } from "./ids.js";
3
+ import { SUPPORTED_BLOCK_API, unsupportedBlockApiResult } from "./block-api.js";
3
4
  import { z } from "zod";
5
+ import { BlockKindRef, CentralBlockRegistry } from "@milaboratories/pl-middle-layer";
6
+ import { BlockInitParamsRejectedError } from "@milaboratories/pl-errors";
4
7
  // src/tools/blocks.ts
8
+ /** Where a kind is resolved when the caller names no registry. */
9
+ function centralBlockRegistryUrl() {
10
+ if (CentralBlockRegistry.type !== "remote-v2") throw new Error("The central block registry is expected to be a v2 remote registry.");
11
+ return CentralBlockRegistry.url;
12
+ }
13
+ function errorMessage(e) {
14
+ return e instanceof Error ? e.message : String(e);
15
+ }
16
+ const addBlockInput = z.object({
17
+ projectId: projectIdInput.describe("Project ID (must be opened)"),
18
+ label: z.string().describe("Block label"),
19
+ spec: z.union([
20
+ z.object({
21
+ type: z.literal("kind"),
22
+ kind: z.string().describe("Block kind reference \"<package name>@<version or range>\", e.g. \"@platforma-open/milaboratories.samples-and-data.kind@1.2.0\""),
23
+ registryUrl: z.string().optional().describe("Registry to resolve the kind in (default: the central block registry)"),
24
+ allowUnstable: z.boolean().optional().describe("Also consider blocks not published to the stable channel")
25
+ }),
26
+ z.object({
27
+ type: z.literal("from-registry-v2"),
28
+ registryUrl: z.string().describe("Registry URL"),
29
+ id: z.object({
30
+ organization: z.string(),
31
+ name: z.string(),
32
+ version: z.string()
33
+ })
34
+ }),
35
+ z.object({
36
+ type: z.literal("dev-v2"),
37
+ folder: z.string().describe("Path to block folder")
38
+ })
39
+ ]).describe("Which block to add"),
40
+ params: z.record(z.string(), z.unknown()).optional().describe("Initialization params of the block's kind. Omit for the default state."),
41
+ blockIds: z.record(z.string(), blockIdInput).optional().describe("Ids used inside `params` in place of block ids, each mapped to a block id of this project. Needed only when `params` do not already hold ids of this project."),
42
+ before: blockIdInput.optional().describe("ID of the block to insert the new block before (default: add at the end)")
43
+ });
44
+ /**
45
+ * Adds the block the input describes, started from its params when it carries them. Every way the
46
+ * block can be refused comes back as an error result saying nothing was created.
47
+ */
48
+ async function addBlock(deps, input) {
49
+ const { label, spec, params, blockIds, before } = input;
50
+ let blockPackSpec;
51
+ if (spec.type === "kind") try {
52
+ blockPackSpec = await deps.resolveKind(BlockKindRef.parse(spec.kind), spec.registryUrl, spec.allowUnstable ?? false);
53
+ } catch (e) {
54
+ return errorResult(`Kind "${spec.kind}" was not resolved to a block: ${errorMessage(e)}. Nothing was created.`, "Check the kind name and version. Pass allowUnstable: true if the block is not published to the stable channel, or registryUrl if it lives in another registry.");
55
+ }
56
+ else blockPackSpec = spec;
57
+ try {
58
+ const blockId = await deps.project.addBlock(label, blockPackSpec, before, deps.author, void 0, {
59
+ init: params === void 0 ? void 0 : {
60
+ params,
61
+ blockIds
62
+ },
63
+ requireModelAPIVersion: SUPPORTED_BLOCK_API
64
+ });
65
+ return textResult({ blockId });
66
+ } catch (e) {
67
+ const unsupported = unsupportedBlockApiResult(e);
68
+ if (unsupported) return unsupported;
69
+ if (e instanceof BlockInitParamsRejectedError) return errorResult(`The block did not accept the params: ${e.reason}. Nothing was created.`, "Fix the params and call add_block again. get_block_params on a configured block of the same kind shows the expected shape.");
70
+ throw e;
71
+ }
72
+ }
5
73
  function registerBlockTools(server, ctx) {
6
74
  server.registerTool("add_block", {
7
75
  title: "Add Block",
8
76
  annotations: hints.additive,
9
- description: "Add a block to an opened project. Spec can be from-registry-v2 (for published blocks) or dev-v2 (for local dev blocks).",
10
- inputSchema: z.object({
11
- projectId: projectIdInput.describe("Project ID (must be opened)"),
12
- label: z.string().describe("Block label"),
13
- spec: z.union([z.object({
14
- type: z.literal("from-registry-v2"),
15
- registryUrl: z.string().describe("Registry URL"),
16
- id: z.object({
17
- organization: z.string(),
18
- name: z.string(),
19
- version: z.string()
20
- })
21
- }), z.object({
22
- type: z.literal("dev-v2"),
23
- folder: z.string().describe("Path to block folder")
24
- })]).describe("Block pack specification")
25
- })
26
- }, async ({ projectId, label, spec }) => {
27
- const blockId = await (await ctx.getOpenedProject(projectId)).addBlock(label, spec, void 0, ctx.getAuthorMarker());
28
- return textResult({ blockId });
77
+ description: "Add a block to an opened project, optionally configured from initialization params.\n\n`spec` names the block: `kind` (a block kind reference, resolved to the newest block implementing it), `from-registry-v2` (an exact published block) or `dev-v2` (a local dev block folder).\n\n`params` are the initialization params of the block's kind: the block's own model checks them and builds its initial state from them. Without `params` the block starts in its default state. get_block_params shows the params of an existing block.\n\nReferences to other blocks inside `params` must hold real block ids of this project, copied from tool results. Params copied from another project or a template hold other ids: map each of them to a block of this project with `blockIds`.\n\nOnly blocks on the current block API can be added. On any error nothing is created.",
78
+ inputSchema: addBlockInput
79
+ }, async (input) => {
80
+ const ml = ctx.requireMl();
81
+ return await addBlock({
82
+ project: await ctx.getOpenedProject(input.projectId),
83
+ resolveKind: (kind, registryUrl, allowUnstable) => ml.blockRegistryProvider.getRegistry(registryUrl ?? centralBlockRegistryUrl()).resolveKind(kind, { allowUnstable }),
84
+ author: ctx.getAuthorMarker()
85
+ }, input);
29
86
  });
30
87
  server.registerTool("update_block", {
31
88
  title: "Update Block",
@@ -142,6 +199,6 @@ function registerBlockTools(server, ctx) {
142
199
  return textResult({ ok: true });
143
200
  });
144
201
  }
145
- export { registerBlockTools };
202
+ export { addBlock, registerBlockTools };
146
203
 
147
204
  //# sourceMappingURL=blocks.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"blocks.js","names":[],"sources":["../../src/tools/blocks.ts"],"sourcesContent":["import type { McpServer } from \"@modelcontextprotocol/server\";\nimport type { BlockPackSpecAny } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\n\nexport function registerBlockTools(server: McpServer, ctx: ToolContext): void {\n server.registerTool(\n \"add_block\",\n {\n title: \"Add Block\",\n annotations: hints.additive,\n description:\n \"Add a block to an opened project. Spec can be from-registry-v2 (for published blocks) or dev-v2 (for local dev blocks).\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n label: z.string().describe(\"Block label\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Block pack specification\"),\n }),\n },\n async ({ projectId, label, spec }) => {\n const project = await ctx.getOpenedProject(projectId);\n const blockId = await project.addBlock(\n label,\n spec as BlockPackSpecAny,\n undefined,\n ctx.getAuthorMarker(),\n );\n return textResult({ blockId });\n },\n );\n\n server.registerTool(\n \"update_block\",\n {\n title: \"Update Block\",\n annotations: hints.destructive,\n description:\n \"Update an existing block's pack (reload from registry or dev folder). Use after rebuilding a dev block to pick up changes without removing/re-adding it.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n blockId: blockIdInput.describe(\"Block ID to update\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Block pack specification\"),\n resetArgs: z\n .boolean()\n .optional()\n .describe(\"Reset block arguments to initial values (default: false)\"),\n }),\n },\n async ({ projectId, blockId, spec, resetArgs }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.updateBlockPack(\n blockId,\n spec as BlockPackSpecAny,\n resetArgs ?? false,\n ctx.getAuthorMarker(),\n );\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"remove_block\",\n {\n title: \"Remove Block\",\n annotations: hints.destructive,\n description:\n \"Remove a block from an opened project, with its configuration and results. Blocks below that refer to it are left with a missing reference.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to remove\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.deleteBlock(blockId, ctx.getAuthorMarker());\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"run_block\",\n {\n title: \"Run Block\",\n // Starts work like an additive tool, but replaces results and stops blocks computing below.\n annotations: { ...hints.additive, destructiveHint: true },\n description:\n \"Run a block. Stale upstream blocks are started automatically. A run replaces the previous results of each block it starts; downstream blocks that are still computing are stopped and their unfinished work is lost.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to run\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.runBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"stop_block\",\n {\n title: \"Stop Block\",\n annotations: hints.destructive,\n description: \"Stop the computation of a running block. Use run_block to start it again.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to stop\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.stopBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"reorder_blocks\",\n {\n title: \"Reorder Blocks\",\n annotations: hints.destructive,\n description: \"Reorder blocks in a project. Must provide ALL block IDs in the desired order.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockIds: z.array(blockIdInput).describe(\"All block IDs in the desired order\"),\n }),\n },\n async ({ projectId, blockIds }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.reorderBlocks(blockIds);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"list_available_blocks\",\n {\n title: \"List Available Blocks\",\n annotations: hints.read,\n description:\n \"List available blocks from configured registries. Optional query to filter by name.\",\n inputSchema: z.object({\n query: z\n .string()\n .optional()\n .describe(\"Filter blocks by name (case-insensitive substring match)\"),\n }),\n },\n async ({ query }) => {\n if (!ctx.callbacks.listAvailableBlocks) {\n return errorResult(\n \"Block registry is not available.\",\n \"This usually means the desktop app encounters a problem connecting block registry. Check Settings > Override Main Registry find which registry really in use and check connection.\",\n );\n }\n const blocks = await ctx.callbacks.listAvailableBlocks(query);\n return textResult(blocks);\n },\n );\n\n server.registerTool(\n \"get_block_info\",\n {\n title: \"Get Block Info\",\n annotations: hints.read,\n description:\n \"Get detailed info about a specific block package from the registry. Use list_available_blocks first to find the block.\",\n inputSchema: z.object({\n registryUrl: z.string().describe(\"Registry URL (from list_available_blocks)\"),\n organization: z.string().describe(\"Organization name\"),\n name: z.string().describe(\"Block package name\"),\n version: z.string().describe(\"Block version\"),\n }),\n },\n async ({ registryUrl, organization, name, version }) => {\n if (!ctx.callbacks.getBlockInfo) {\n return errorResult(\n \"Block info is not available in this environment.\",\n 'Maybe the name of the block was written incerrectly. Use list_available_blocks to browse blocks instead. Or ask user to check \"Additional Registries\" in Settings panel',\n );\n }\n const info = await ctx.callbacks.getBlockInfo(registryUrl, organization, name, version);\n return textResult(info);\n },\n );\n\n server.registerTool(\n \"select_block\",\n {\n title: \"Show Block in the Application\",\n annotations: hints.idempotent,\n description: \"Navigate the desktop UI to show a specific block's interface\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to display\"),\n }),\n },\n async ({ projectId, blockId }) => {\n if (!ctx.callbacks.selectBlock) {\n return errorResult(\n \"Failed to select the block.\",\n \"This feature requires server connected and open project. Use get_connection_status and list_projects to check. If there are no connection, use list_connections and ask user which should be used.\",\n );\n }\n await ctx.callbacks.selectBlock(projectId, blockId);\n return textResult({ ok: true });\n },\n );\n}\n"],"mappings":";;;;AAOA,SAAgB,mBAAmB,QAAmB,KAAwB;CAC5E,OAAO,aACL,aACA;EACE,OAAO;EACP,aAAa,MAAM;EACnB,aACE;EACF,aAAa,EAAE,OAAO;GACpB,WAAW,eAAe,SAAS,6BAA6B;GAChE,OAAO,EAAE,OAAO,CAAC,CAAC,SAAS,aAAa;GACxC,MAAM,EACH,MAAM,CACL,EAAE,OAAO;IACP,MAAM,EAAE,QAAQ,kBAAkB;IAClC,aAAa,EAAE,OAAO,CAAC,CAAC,SAAS,cAAc;IAC/C,IAAI,EAAE,OAAO;KACX,cAA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UAAU,YAAY,WAAW,OAAO;EAClD,OAAO,WAAW,EAAE,IAAI,KAAK,CAAC;CAChC,CACF;AACF"}
1
+ {"version":3,"file":"blocks.js","names":[],"sources":["../../src/tools/blocks.ts"],"sourcesContent":["import type { McpServer } from \"@modelcontextprotocol/server\";\nimport type { BlockPackSpecAny, Project } from \"@milaboratories/pl-middle-layer\";\nimport { BlockKindRef, CentralBlockRegistry } from \"@milaboratories/pl-middle-layer\";\nimport { BlockInitParamsRejectedError } from \"@milaboratories/pl-errors\";\nimport type { BlockKindReference } from \"@platforma-sdk/model\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { AuthorMarker, ToolContext } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\nimport { SUPPORTED_BLOCK_API, unsupportedBlockApiResult } from \"./block-api\";\n\n/** Where a kind is resolved when the caller names no registry. */\nfunction centralBlockRegistryUrl(): string {\n if (CentralBlockRegistry.type !== \"remote-v2\")\n throw new Error(\"The central block registry is expected to be a v2 remote registry.\");\n return CentralBlockRegistry.url;\n}\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\nconst addBlockInput = z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n label: z.string().describe(\"Block label\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"kind\"),\n kind: z\n .string()\n .describe(\n 'Block kind reference \"<package name>@<version or range>\", ' +\n 'e.g. \"@platforma-open/milaboratories.samples-and-data.kind@1.2.0\"',\n ),\n registryUrl: z\n .string()\n .optional()\n .describe(\"Registry to resolve the kind in (default: the central block registry)\"),\n allowUnstable: z\n .boolean()\n .optional()\n .describe(\"Also consider blocks not published to the stable channel\"),\n }),\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Which block to add\"),\n params: z\n .record(z.string(), z.unknown())\n .optional()\n .describe(\"Initialization params of the block's kind. Omit for the default state.\"),\n blockIds: z\n .record(z.string(), blockIdInput)\n .optional()\n .describe(\n \"Ids used inside `params` in place of block ids, each mapped to a block id of this \" +\n \"project. Needed only when `params` do not already hold ids of this project.\",\n ),\n before: blockIdInput\n .optional()\n .describe(\"ID of the block to insert the new block before (default: add at the end)\"),\n});\n\n/** Input of the `add_block` tool. */\nexport type AddBlockInput = z.infer<typeof addBlockInput>;\n\n/** What `add_block` needs from the application. */\nexport type AddBlockDeps = {\n project: Pick<Project, \"addBlock\">;\n /** Resolves a kind reference to the block implementing it; throws when there is none. */\n resolveKind: (\n kind: BlockKindReference,\n registryUrl: string | undefined,\n allowUnstable: boolean,\n ) => Promise<BlockPackSpecAny>;\n author: AuthorMarker;\n};\n\n/**\n * Adds the block the input describes, started from its params when it carries them. Every way the\n * block can be refused comes back as an error result saying nothing was created.\n */\nexport async function addBlock(deps: AddBlockDeps, input: AddBlockInput) {\n const { label, spec, params, blockIds, before } = input;\n\n let blockPackSpec: BlockPackSpecAny;\n if (spec.type === \"kind\") {\n try {\n blockPackSpec = await deps.resolveKind(\n BlockKindRef.parse(spec.kind),\n spec.registryUrl,\n spec.allowUnstable ?? false,\n );\n } catch (e: unknown) {\n return errorResult(\n `Kind \"${spec.kind}\" was not resolved to a block: ${errorMessage(e)}. Nothing was created.`,\n \"Check the kind name and version. Pass allowUnstable: true if the block is not \" +\n \"published to the stable channel, or registryUrl if it lives in another registry.\",\n );\n }\n } else {\n blockPackSpec = spec;\n }\n\n try {\n const blockId = await deps.project.addBlock(\n label,\n blockPackSpec,\n before,\n deps.author,\n undefined,\n {\n init: params === undefined ? undefined : { params, blockIds },\n requireModelAPIVersion: SUPPORTED_BLOCK_API,\n },\n );\n return textResult({ blockId });\n } catch (e: unknown) {\n const unsupported = unsupportedBlockApiResult(e);\n if (unsupported) return unsupported;\n if (e instanceof BlockInitParamsRejectedError)\n return errorResult(\n `The block did not accept the params: ${e.reason}. Nothing was created.`,\n \"Fix the params and call add_block again. get_block_params on a configured block \" +\n \"of the same kind shows the expected shape.\",\n );\n throw e;\n }\n}\n\nexport function registerBlockTools(server: McpServer, ctx: ToolContext): void {\n server.registerTool(\n \"add_block\",\n {\n title: \"Add Block\",\n annotations: hints.additive,\n description:\n \"Add a block to an opened project, optionally configured from initialization params.\\n\\n\" +\n \"`spec` names the block: `kind` (a block kind reference, resolved to the newest block \" +\n \"implementing it), `from-registry-v2` (an exact published block) or `dev-v2` (a local dev \" +\n \"block folder).\\n\\n\" +\n \"`params` are the initialization params of the block's kind: the block's own model checks \" +\n \"them and builds its initial state from them. Without `params` the block starts in its \" +\n \"default state. get_block_params shows the params of an existing block.\\n\\n\" +\n \"References to other blocks inside `params` must hold real block ids of this project, \" +\n \"copied from tool results. Params copied from another project or a template hold other \" +\n \"ids: map each of them to a block of this project with `blockIds`.\\n\\n\" +\n \"Only blocks on the current block API can be added. On any error nothing is created.\",\n inputSchema: addBlockInput,\n },\n async (input) => {\n const ml = ctx.requireMl();\n return await addBlock(\n {\n project: await ctx.getOpenedProject(input.projectId),\n resolveKind: (kind, registryUrl, allowUnstable) =>\n ml.blockRegistryProvider\n .getRegistry(registryUrl ?? centralBlockRegistryUrl())\n .resolveKind(kind, { allowUnstable }),\n author: ctx.getAuthorMarker(),\n },\n input,\n );\n },\n );\n\n server.registerTool(\n \"update_block\",\n {\n title: \"Update Block\",\n annotations: hints.destructive,\n description:\n \"Update an existing block's pack (reload from registry or dev folder). Use after rebuilding a dev block to pick up changes without removing/re-adding it.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID (must be opened)\"),\n blockId: blockIdInput.describe(\"Block ID to update\"),\n spec: z\n .union([\n z.object({\n type: z.literal(\"from-registry-v2\"),\n registryUrl: z.string().describe(\"Registry URL\"),\n id: z.object({\n organization: z.string(),\n name: z.string(),\n version: z.string(),\n }),\n }),\n z.object({\n type: z.literal(\"dev-v2\"),\n folder: z.string().describe(\"Path to block folder\"),\n }),\n ])\n .describe(\"Block pack specification\"),\n resetArgs: z\n .boolean()\n .optional()\n .describe(\"Reset block arguments to initial values (default: false)\"),\n }),\n },\n async ({ projectId, blockId, spec, resetArgs }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.updateBlockPack(\n blockId,\n spec as BlockPackSpecAny,\n resetArgs ?? false,\n ctx.getAuthorMarker(),\n );\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"remove_block\",\n {\n title: \"Remove Block\",\n annotations: hints.destructive,\n description:\n \"Remove a block from an opened project, with its configuration and results. Blocks below that refer to it are left with a missing reference.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to remove\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.deleteBlock(blockId, ctx.getAuthorMarker());\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"run_block\",\n {\n title: \"Run Block\",\n // Starts work like an additive tool, but replaces results and stops blocks computing below.\n annotations: { ...hints.additive, destructiveHint: true },\n description:\n \"Run a block. Stale upstream blocks are started automatically. A run replaces the previous results of each block it starts; downstream blocks that are still computing are stopped and their unfinished work is lost.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to run\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.runBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"stop_block\",\n {\n title: \"Stop Block\",\n annotations: hints.destructive,\n description: \"Stop the computation of a running block. Use run_block to start it again.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to stop\"),\n }),\n },\n async ({ projectId, blockId }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.stopBlock(blockId);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"reorder_blocks\",\n {\n title: \"Reorder Blocks\",\n annotations: hints.destructive,\n description: \"Reorder blocks in a project. Must provide ALL block IDs in the desired order.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockIds: z.array(blockIdInput).describe(\"All block IDs in the desired order\"),\n }),\n },\n async ({ projectId, blockIds }) => {\n const project = await ctx.getOpenedProject(projectId);\n await project.reorderBlocks(blockIds);\n return textResult({ ok: true });\n },\n );\n\n server.registerTool(\n \"list_available_blocks\",\n {\n title: \"List Available Blocks\",\n annotations: hints.read,\n description:\n \"List available blocks from configured registries. Optional query to filter by name.\",\n inputSchema: z.object({\n query: z\n .string()\n .optional()\n .describe(\"Filter blocks by name (case-insensitive substring match)\"),\n }),\n },\n async ({ query }) => {\n if (!ctx.callbacks.listAvailableBlocks) {\n return errorResult(\n \"Block registry is not available.\",\n \"This usually means the desktop app encounters a problem connecting block registry. Check Settings > Override Main Registry find which registry really in use and check connection.\",\n );\n }\n const blocks = await ctx.callbacks.listAvailableBlocks(query);\n return textResult(blocks);\n },\n );\n\n server.registerTool(\n \"get_block_info\",\n {\n title: \"Get Block Info\",\n annotations: hints.read,\n description:\n \"Get detailed info about a specific block package from the registry. Use list_available_blocks first to find the block.\",\n inputSchema: z.object({\n registryUrl: z.string().describe(\"Registry URL (from list_available_blocks)\"),\n organization: z.string().describe(\"Organization name\"),\n name: z.string().describe(\"Block package name\"),\n version: z.string().describe(\"Block version\"),\n }),\n },\n async ({ registryUrl, organization, name, version }) => {\n if (!ctx.callbacks.getBlockInfo) {\n return errorResult(\n \"Block info is not available in this environment.\",\n 'Maybe the name of the block was written incerrectly. Use list_available_blocks to browse blocks instead. Or ask user to check \"Additional Registries\" in Settings panel',\n );\n }\n const info = await ctx.callbacks.getBlockInfo(registryUrl, organization, name, version);\n return textResult(info);\n },\n );\n\n server.registerTool(\n \"select_block\",\n {\n title: \"Show Block in the Application\",\n annotations: hints.idempotent,\n description: \"Navigate the desktop UI to show a specific block's interface\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to display\"),\n }),\n },\n async ({ projectId, blockId }) => {\n if (!ctx.callbacks.selectBlock) {\n return errorResult(\n \"Failed to select the block.\",\n \"This feature requires server connected and open project. Use get_connection_status and list_projects to check. If there are no connection, use list_connections and ask user which should be used.\",\n );\n }\n await ctx.callbacks.selectBlock(projectId, blockId);\n return textResult({ ok: true });\n },\n );\n}\n"],"mappings":";;;;;;;;AAYA,SAAS,0BAAkC;CACzC,IAAI,qBAAqB,SAAS,aAChC,MAAM,IAAI,MAAM,oEAAoE;CACtF,OAAO,qBAAqB;AAC9B;AAEA,SAAS,aAAa,GAAoB;CACxC,OAAO,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC;AAClD;AAEA,MAAM,gBAAgB,EAAE,OAAO;CAC7B,WAAW,eAAe,SAAS,6BAA6B;CAChE,OAAO,EAAE,OAAO,CAAC,CAAC,SAAS,aAAa;CACxC,MAAM,EACH,MAAM;EACL,EAAE,OAAO;GACP,MAAM,EAAE,QAAQ,MAAM;GACtB,MAAM,EACH,OAAO,CAAC,CACR,SACC,iIAEF;GACF,aAAa,EACV,OAAO,CAAC,CACR,SAAS,CAAC,CACV,SAAS,uEAAuE;GACnF,eAAe,EACZ,QAAQ,CAAC,CACT,SAAS,CAAC,CACV,SAAS,0DAA0D;EACxE,CAAC;EACD,EAAE,OAAO;GACP,MAAM,EAAE,QAAQ,kBAAkB;GAClC,aAAa,EAAE,OAAO,CAAC,CAAC,SAAS,cAAc;GAC/C,IAAI,EAAE,OAAO;IACX,cAAc,EAAE,OAAO;IACvB,MAAM,EAAE,OAAO;IACf,SAAS,EAAE,OAAO;GACpB,CAAC;EACH,CAAC;EACD,EAAE,OAAO;GACP,MAAM,EAAE,QAAQ,QAAQ;GACxB,QAAQ,EAAE,OAAO,CAAC,CAAC,SAAS,sBAAsB;E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AS,0DAA0D,EACxE,CAAC;CACH,GACA,OAAO,EAAE,YAAY;EACnB,IAAI,CAAC,IAAI,UAAU,qBACjB,OAAO,YACL,oCACA,oLACF;EAEF,MAAM,SAAS,MAAM,IAAI,UAAU,oBAAoB,KAAK;EAC5D,OAAO,WAAW,MAAM;CAC1B,CACF;CAEA,OAAO,aACL,kBACA;EACE,OAAO;EACP,aAAa,MAAM;EACnB,aACE;EACF,aAAa,EAAE,OAAO;GACpB,aAAa,EAAE,OAAO,CAAC,CAAC,SAAS,2CAA2C;GAC5E,cAAc,EAAE,OAAO,CAAC,CAAC,SAAS,mBAAmB;GACrD,MAAM,EAAE,OAAO,CAAC,CAAC,SAAS,oBAAoB;GAC9C,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS,eAAe;EAC9C,CAAC;CACH,GACA,OAAO,EAAE,aAAa,cAAc,MAAM,cAAc;EACtD,IAAI,CAAC,IAAI,UAAU,cACjB,OAAO,YACL,oDACA,2KACF;EAEF,MAAM,OAAO,MAAM,IAAI,UAAU,aAAa,aAAa,cAAc,MAAM,OAAO;EACtF,OAAO,WAAW,IAAI;CACxB,CACF;CAEA,OAAO,aACL,gBACA;EACE,OAAO;EACP,aAAa,MAAM;EACnB,aAAa;EACb,aAAa,EAAE,OAAO;GACpB,WAAW,eAAe,SAAS,YAAY;GAC/C,SAAS,aAAa,SAAS,qBAAqB;EACtD,CAAC;CACH,GACA,OAAO,EAAE,WAAW,cAAc;EAChC,IAAI,CAAC,IAAI,UAAU,aACjB,OAAO,YACL,+BACA,oMACF;EAEF,MAAM,IAAI,UAAU,YAAY,WAAW,OAAO;EAClD,OAAO,WAAW,EAAE,IAAI,KAAK,CAAC;CAChC,CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/pl-mcp-server",
3
- "version": "52.1.0",
3
+ "version": "53.0.0",
4
4
  "description": "MCP server for Platforma Desktop",
5
5
  "keywords": [],
6
6
  "license": "UNLICENSED",
@@ -26,8 +26,8 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@milaboratories/build-configs": "2.0.3",
29
- "@milaboratories/pl-errors": "1.4.73",
30
- "@milaboratories/pl-middle-layer": "2.8.10",
29
+ "@milaboratories/pl-errors": "1.5.0",
30
+ "@milaboratories/pl-middle-layer": "2.9.0",
31
31
  "@milaboratories/pl-model-common": "1.60.0",
32
32
  "@milaboratories/ts-builder": "1.8.0",
33
33
  "@milaboratories/ts-configs": "1.4.1",
@@ -40,7 +40,7 @@
40
40
  "vitest": "^4.1.12"
41
41
  },
42
42
  "peerDependencies": {
43
- "@milaboratories/pl-middle-layer": ">=2.8.10",
43
+ "@milaboratories/pl-middle-layer": ">=2.9.0",
44
44
  "@platforma-sdk/model": ">=1.96.1"
45
45
  },
46
46
  "engines": {
@@ -11,7 +11,7 @@ export const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analys
11
11
  Order of work:
12
12
  1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.
13
13
  2. Project: list_projects or create_project, then open_project. Every block tool needs an opened project.
14
- 3. Add blocks: list_available_blocks finds a block; add_block adds it. Add blocks in the order the data flows.
14
+ 3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.
15
15
  4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.
16
16
  5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.
17
17
  6. Results: get_block_outputs, then list_columns and query_table for tables, get_block_logs for logs.
@@ -20,4 +20,5 @@ Rules:
20
20
  - Project ids, block ids, references to blocks and columns, and table handles come from tool results. Copy them; do not compose them.
21
21
  - set_block_data replaces the block's data: send the whole object, not the changed part.
22
22
  - Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.
23
+ - Only blocks on the current block API can be added.
23
24
  - capture_screenshot, click, type_text, press_key, scroll, execute_js and select_block drive the application window. They are for testing the application; configuring and running blocks does not need them.`;
@@ -138,6 +138,7 @@ describe.each(eras)("to a $era client", (era) => {
138
138
  "get_block_info",
139
139
  "get_block_logs",
140
140
  "get_block_outputs",
141
+ "get_block_params",
141
142
  "get_block_state",
142
143
  "get_connection_status",
143
144
  "get_project_overview",
@@ -193,6 +194,7 @@ describe.each(eras)("to a $era client", (era) => {
193
194
  "get_block_info",
194
195
  "get_block_logs",
195
196
  "get_block_outputs",
197
+ "get_block_params",
196
198
  "get_block_state",
197
199
  "get_connection_status",
198
200
  "get_project_overview",
@@ -231,8 +233,8 @@ describe.each(eras)("to a $era client", (era) => {
231
233
  });
232
234
 
233
235
  test("an optional argument keeps its description", () => {
234
- expect(inputSchemaOf("list_available_blocks").properties?.query).toMatchObject({
235
- description: "Filter blocks by name (case-insensitive substring match)",
236
+ expect(inputSchemaOf("add_block").properties?.params).toMatchObject({
237
+ description: "Initialization params of the block's kind. Omit for the default state.",
236
238
  });
237
239
  });
238
240
 
package/src/server.ts CHANGED
@@ -12,6 +12,7 @@ import { registerPingTool } from "./tools/ping";
12
12
  import { registerConnectionTools } from "./tools/connection";
13
13
  import { registerProjectTools } from "./tools/projects";
14
14
  import { registerBlockTools } from "./tools/blocks";
15
+ import { registerBlockParamsTools } from "./tools/block-params";
15
16
  import { registerBlockStateTools } from "./tools/block-state";
16
17
  import { registerBlockActionContextTools } from "./tools/block-action-context";
17
18
  import { registerAwaitTools } from "./tools/await";
@@ -252,6 +253,7 @@ export class PlMcpServer {
252
253
  registerConnectionTools(server, ctx);
253
254
  registerProjectTools(server, ctx);
254
255
  registerBlockTools(server, ctx);
256
+ registerBlockParamsTools(server, ctx);
255
257
  registerBlockStateTools(server, ctx);
256
258
  registerBlockActionContextTools(server, ctx);
257
259
  registerAwaitTools(server, ctx);
@@ -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
+ }
@@ -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
+ });