|
| 1 | +import { z } from "zod"; |
| 2 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 3 | +import { getConnection } from "../shared/connection.js"; |
| 4 | +import { permissions } from "../config/permissions.js"; |
| 5 | +import { executeSfCommand, executeSfCommandRaw } from "../utils/sfCommand.js"; |
| 6 | + |
| 7 | +const generateComponent = async ( |
| 8 | + name: string, |
| 9 | + template: string, |
| 10 | + outputDirectory: string, |
| 11 | + type: string, |
| 12 | +) => { |
| 13 | + let sfCommand = `sf lightning generate component --name ${name} --json `; |
| 14 | + |
| 15 | + if (template && template.length > 0) { |
| 16 | + sfCommand += `--template ${template} `; |
| 17 | + } |
| 18 | + |
| 19 | + if (outputDirectory && outputDirectory.length > 0) { |
| 20 | + sfCommand += `--output-dir ${outputDirectory} `; |
| 21 | + } |
| 22 | + |
| 23 | + if (type && type.length > 0) { |
| 24 | + sfCommand += `--type ${type}`; |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + const result = await executeSfCommand(sfCommand); |
| 29 | + return result; |
| 30 | + } catch (error) { |
| 31 | + throw error; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +export const registerLightningTools = (server: McpServer) => { |
| 36 | + server.tool( |
| 37 | + "generate_component", |
| 38 | + "", |
| 39 | + { |
| 40 | + input: z.object({ |
| 41 | + name: z |
| 42 | + .string() |
| 43 | + .describe( |
| 44 | + "Name of the generated Lightning Component. The name can be up to 40 characters and must start with a letter.", |
| 45 | + ), |
| 46 | + template: z |
| 47 | + .string() |
| 48 | + .optional() |
| 49 | + .describe( |
| 50 | + "Template to use for file creation. Supplied parameter values or default values are filled into a copy of the template. Permissible values are: default, analyticsDashboard, analyticsDashboardWithStep. Default value: default", |
| 51 | + ), |
| 52 | + outputDirectory: z |
| 53 | + .string() |
| 54 | + .optional() |
| 55 | + .describe( |
| 56 | + "Directory for saving the created files. The location can be an absolute path or relative to the current working directory. The default is the current directory. Default value: .", |
| 57 | + ), |
| 58 | + type: z |
| 59 | + .string() |
| 60 | + .optional() |
| 61 | + .describe( |
| 62 | + "Type of the component bundle. Permissible values are: aura, lwc (lightning web component). Default value: aura", |
| 63 | + ), |
| 64 | + }), |
| 65 | + }, |
| 66 | + async ({ input }) => { |
| 67 | + const { name, template, outputDirectory, type } = input; |
| 68 | + const DEFAULT_OUTPUT = "."; |
| 69 | + if (permissions.isReadOnly()) { |
| 70 | + return { |
| 71 | + content: [ |
| 72 | + { |
| 73 | + type: "text", |
| 74 | + text: JSON.stringify({ |
| 75 | + success: false, |
| 76 | + compiled: false, |
| 77 | + compileProblem: |
| 78 | + "Operation not allowed: Cannot generate components in READ_ONLY mode", |
| 79 | + }), |
| 80 | + }, |
| 81 | + ], |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + const result = await generateComponent( |
| 86 | + name, |
| 87 | + template || "", |
| 88 | + outputDirectory || DEFAULT_OUTPUT, |
| 89 | + type || "", |
| 90 | + ); |
| 91 | + return { |
| 92 | + content: [ |
| 93 | + { |
| 94 | + type: "text", |
| 95 | + text: JSON.stringify(result), |
| 96 | + }, |
| 97 | + ], |
| 98 | + }; |
| 99 | + }, |
| 100 | + ); |
| 101 | +}; |
0 commit comments