GenerateMCPScriptJavaScriptToolScript generates the JavaScript tool file for a mcp-script tool The user's script code is automatically wrapped in a function with module.exports, so users can write simple code without worrying about exports. Input parameters are destructured and available as local va
(toolConfig *MCPScriptToolConfig)
| 186 | // so users can write simple code without worrying about exports. |
| 187 | // Input parameters are destructured and available as local variables. |
| 188 | func GenerateMCPScriptJavaScriptToolScript(toolConfig *MCPScriptToolConfig) string { |
| 189 | mcpScriptsLog.Printf("Generating JavaScript tool script: tool=%s, input_count=%d", toolConfig.Name, len(toolConfig.Inputs)) |
| 190 | var sb strings.Builder |
| 191 | |
| 192 | sb.WriteString("// @ts-check\n") |
| 193 | sb.WriteString("// Auto-generated mcp-script tool: " + toolConfig.Name + "\n\n") |
| 194 | sb.WriteString("/**\n") |
| 195 | sb.WriteString(" * " + toolConfig.Description + "\n") |
| 196 | sb.WriteString(" * @param {Object} inputs - Input parameters\n") |
| 197 | // Sort input names for stable code generation in JSDoc |
| 198 | inputNamesForDoc := sliceutil.SortedKeys(toolConfig.Inputs) |
| 199 | for _, paramName := range inputNamesForDoc { |
| 200 | param := toolConfig.Inputs[paramName] |
| 201 | fmt.Fprintf(&sb, " * @param {%s} inputs.%s - %s\n", param.Type, paramName, param.Description) |
| 202 | } |
| 203 | sb.WriteString(" * @returns {Promise<any>} Tool result\n") |
| 204 | sb.WriteString(" */\n") |
| 205 | sb.WriteString("async function execute(inputs) {\n") |
| 206 | |
| 207 | // Destructure inputs to make parameters available as local variables |
| 208 | if len(toolConfig.Inputs) > 0 { |
| 209 | var paramNames []string |
| 210 | for paramName := range toolConfig.Inputs { |
| 211 | safeName := stringutil.SanitizeParameterName(paramName) |
| 212 | if safeName != paramName { |
| 213 | // If sanitized, use alias |
| 214 | paramNames = append(paramNames, fmt.Sprintf("%s: %s", paramName, safeName)) |
| 215 | } else { |
| 216 | paramNames = append(paramNames, paramName) |
| 217 | } |
| 218 | } |
| 219 | sort.Strings(paramNames) |
| 220 | fmt.Fprintf(&sb, " const { %s } = inputs || {};\n\n", strings.Join(paramNames, ", ")) |
| 221 | } |
| 222 | |
| 223 | // Indent the user's script code |
| 224 | sb.WriteString(" " + strings.ReplaceAll(toolConfig.Script, "\n", "\n ") + "\n") |
| 225 | sb.WriteString("}\n\n") |
| 226 | sb.WriteString("module.exports = { execute };\n\n") |
| 227 | |
| 228 | // Delegate subprocess execution to the shared runner module |
| 229 | sb.WriteString("// Run when executed directly (as a subprocess by the MCP handler)\n") |
| 230 | sb.WriteString("if (require.main === module) {\n") |
| 231 | sb.WriteString(" require(\"./mcp-scripts-runner.cjs\")(execute);\n") |
| 232 | sb.WriteString("}\n") |
| 233 | |
| 234 | return sb.String() |
| 235 | } |
| 236 | |
| 237 | // GenerateMCPScriptShellToolScript generates the shell script for a mcp-script tool |
| 238 | func GenerateMCPScriptShellToolScript(toolConfig *MCPScriptToolConfig) string { |