|
| 1 | +import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import { copyFile, mkdir } from "node:fs/promises"; |
| 3 | +import { basename, join, resolve } from "node:path"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { GENERATIONS_DIR, validateProjectDir } from "@/config"; |
| 6 | +import { isPathAllowed } from "./utils/sandbox"; |
| 7 | +import type { ToolResponse } from "@/tools/types"; |
| 8 | +import { findMpkFile } from "@/tools/utils/mpk"; |
| 9 | +import { createStructuredError, createStructuredErrorResponse, createToolResponse } from "@/tools/utils/response"; |
| 10 | +import type { SessionState } from "./session-state"; |
| 11 | + |
| 12 | +function formatProjectInfo(validation: Awaited<ReturnType<typeof validateProjectDir>>): string { |
| 13 | + const lines: string[] = [ |
| 14 | + `Project Directory: ${validation.projectDir}`, |
| 15 | + ...(validation.projectName ? [`Project Name: ${validation.projectName}`] : []), |
| 16 | + `Widgets Directory: ${validation.widgetsDir}` |
| 17 | + ]; |
| 18 | + |
| 19 | + if (validation.existingWidgets.length > 0) { |
| 20 | + lines.push(`Existing Widgets (${validation.existingWidgets.length}):`); |
| 21 | + for (const widget of validation.existingWidgets) { |
| 22 | + lines.push(` - ${widget}`); |
| 23 | + } |
| 24 | + } else { |
| 25 | + lines.push(`Existing Widgets: (none)`); |
| 26 | + } |
| 27 | + |
| 28 | + return lines.join("\n"); |
| 29 | +} |
| 30 | + |
| 31 | +export function registerProjectTools(server: McpServer, state: SessionState): void { |
| 32 | + server.registerTool( |
| 33 | + "get-project-info", |
| 34 | + { |
| 35 | + title: "Get Project Info", |
| 36 | + description: |
| 37 | + "Returns information about the configured Mendix project directory. " + |
| 38 | + "Call this first to discover the project context before creating or deploying widgets. " + |
| 39 | + "Returns the project directory, project name, widgets directory, and existing .mpk files.", |
| 40 | + inputSchema: z.object({}) |
| 41 | + }, |
| 42 | + async (): Promise<ToolResponse> => { |
| 43 | + if (!state.projectDir) { |
| 44 | + return createStructuredErrorResponse( |
| 45 | + createStructuredError("ERR_PROJECT_NOT_CONFIGURED", "No Mendix project directory is configured.", { |
| 46 | + suggestion: |
| 47 | + "Set the MENDIX_PROJECT_DIR environment variable when starting the server, e.g.:\n" + |
| 48 | + " MENDIX_PROJECT_DIR=/Users/you/Mendix/MyProject node dist/index.js http\n" + |
| 49 | + "Or call set-project-directory to configure it at runtime." |
| 50 | + }) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + const validation = await validateProjectDir(state.projectDir); |
| 55 | + if (!validation.valid) { |
| 56 | + return createStructuredErrorResponse( |
| 57 | + createStructuredError( |
| 58 | + "ERR_PROJECT_NOT_CONFIGURED", |
| 59 | + `Configured project directory is invalid: ${validation.error}`, |
| 60 | + { suggestion: "Use set-project-directory to set a valid Mendix project directory." } |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + return createToolResponse(`✅ Project configured\n\n${formatProjectInfo(validation)}`); |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + server.registerTool( |
| 70 | + "set-project-directory", |
| 71 | + { |
| 72 | + title: "Set Project Directory", |
| 73 | + description: |
| 74 | + "Configures the Mendix project directory for this session. " + |
| 75 | + "The directory must exist and contain a .mpr file. " + |
| 76 | + "Once set, deploy-widget can copy built .mpk files to the project's widgets/ folder.", |
| 77 | + inputSchema: z.object({ |
| 78 | + projectDir: z |
| 79 | + .string() |
| 80 | + .describe("Absolute path to the Mendix project directory (must contain a .mpr file)") |
| 81 | + }) |
| 82 | + }, |
| 83 | + async (args: { projectDir: string }): Promise<ToolResponse> => { |
| 84 | + const resolvedDir = resolve(args.projectDir); |
| 85 | + const validation = await validateProjectDir(resolvedDir); |
| 86 | + if (!validation.valid) { |
| 87 | + return createStructuredErrorResponse( |
| 88 | + createStructuredError( |
| 89 | + "ERR_PROJECT_NOT_CONFIGURED", |
| 90 | + `Invalid project directory: ${validation.error}`, |
| 91 | + { |
| 92 | + suggestion: |
| 93 | + "Provide the absolute path to a directory that exists and contains a .mpr file, e.g.:\n" + |
| 94 | + " /Users/you/Mendix/MyProject" |
| 95 | + } |
| 96 | + ) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + state.projectDir = validation.projectDir; |
| 101 | + return createToolResponse(`✅ Project directory configured\n\n${formatProjectInfo(validation)}`); |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + server.registerTool( |
| 106 | + "deploy-widget", |
| 107 | + { |
| 108 | + title: "Deploy Widget", |
| 109 | + description: |
| 110 | + "Copies a built widget .mpk file to the configured Mendix project's widgets/ directory. " + |
| 111 | + "Requires a project directory to be configured (via MENDIX_PROJECT_DIR env var or set-project-directory). " + |
| 112 | + "Looks for the .mpk file in the widget's dist/ directory. " + |
| 113 | + "After deploying, synchronize the app directory in Studio Pro to pick up the new widget.", |
| 114 | + inputSchema: z.object({ |
| 115 | + widgetPath: z |
| 116 | + .string() |
| 117 | + .describe("Absolute path to the widget directory (the one containing package.json and dist/)") |
| 118 | + }) |
| 119 | + }, |
| 120 | + async (args: { widgetPath: string }): Promise<ToolResponse> => { |
| 121 | + if (!state.projectDir) { |
| 122 | + return createStructuredErrorResponse( |
| 123 | + createStructuredError("ERR_PROJECT_NOT_CONFIGURED", "No Mendix project directory is configured.", { |
| 124 | + suggestion: |
| 125 | + "Call get-project-info to check the current configuration, " + |
| 126 | + "or set-project-directory to configure a project directory." |
| 127 | + }) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + if (!isPathAllowed(args.widgetPath, state, "MCP_ALLOWED_BUILD_PATHS")) { |
| 132 | + return createStructuredErrorResponse( |
| 133 | + createStructuredError( |
| 134 | + "ERR_NOT_FOUND", |
| 135 | + `Widget path is not within an allowed directory: ${args.widgetPath}`, |
| 136 | + { |
| 137 | + suggestion: `Widget must be within ${GENERATIONS_DIR} or an allowed build path.` |
| 138 | + } |
| 139 | + ) |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + const mpkPath = findMpkFile(args.widgetPath); |
| 144 | + if (!mpkPath) { |
| 145 | + return createStructuredErrorResponse( |
| 146 | + createStructuredError("ERR_MPK_NOT_FOUND", `No .mpk file found in ${args.widgetPath}/dist/`, { |
| 147 | + suggestion: "Run build-widget first to compile the widget and produce the .mpk file." |
| 148 | + }) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + const widgetsDir = join(state.projectDir, "widgets"); |
| 153 | + try { |
| 154 | + await mkdir(widgetsDir, { recursive: true }); |
| 155 | + const mpkFileName = basename(mpkPath); |
| 156 | + const destPath = join(widgetsDir, mpkFileName); |
| 157 | + await copyFile(mpkPath, destPath); |
| 158 | + |
| 159 | + return createToolResponse( |
| 160 | + [ |
| 161 | + `✅ Widget deployed successfully!`, |
| 162 | + ``, |
| 163 | + `Source: ${mpkPath}`, |
| 164 | + `Destination: ${destPath}`, |
| 165 | + ``, |
| 166 | + `Synchronize the app directory in Studio Pro to pick up the new widget.` |
| 167 | + ].join("\n") |
| 168 | + ); |
| 169 | + } catch (error) { |
| 170 | + const message = error instanceof Error ? error.message : String(error); |
| 171 | + return createStructuredErrorResponse( |
| 172 | + createStructuredError("ERR_DEPLOY_FAILED", `Failed to deploy widget: ${message}`, { |
| 173 | + suggestion: "Check write permissions on the widgets directory.", |
| 174 | + rawOutput: message |
| 175 | + }) |
| 176 | + ); |
| 177 | + } |
| 178 | + } |
| 179 | + ); |
| 180 | +} |
0 commit comments