writeMCPScriptsFiles writes all mcp-scripts MCP server files to the specified directory
(dir string, mcpScriptsConfig *workflow.MCPScriptsConfig, verbose bool)
| 13 | |
| 14 | // writeMCPScriptsFiles writes all mcp-scripts MCP server files to the specified directory |
| 15 | func writeMCPScriptsFiles(dir string, mcpScriptsConfig *workflow.MCPScriptsConfig, verbose bool) error { |
| 16 | mcpInspectLog.Printf("Writing mcp-scripts files to: %s", dir) |
| 17 | |
| 18 | // Create logs directory |
| 19 | logsDir := filepath.Join(dir, "logs") |
| 20 | if err := os.MkdirAll(logsDir, constants.DirPermPublic); err != nil { |
| 21 | errMsg := fmt.Sprintf("failed to create logs directory: %v", err) |
| 22 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 23 | return fmt.Errorf("failed to create logs directory: %w", err) |
| 24 | } |
| 25 | |
| 26 | // Write JavaScript dependencies that are needed |
| 27 | jsFiles := []struct { |
| 28 | name string |
| 29 | content string |
| 30 | }{ |
| 31 | {"read_buffer.cjs", workflow.GetReadBufferScript()}, |
| 32 | {"mcp_http_transport.cjs", workflow.GetMCPHTTPTransportScript()}, |
| 33 | {"mcp_scripts_config_loader.cjs", workflow.GetMCPScriptsConfigLoaderScript()}, |
| 34 | {"mcp_server_core.cjs", workflow.GetMCPServerCoreScript()}, |
| 35 | {"mcp_scripts_validation.cjs", workflow.GetMCPScriptsValidationScript()}, |
| 36 | {"mcp_logger.cjs", workflow.GetMCPLoggerScript()}, |
| 37 | {"mcp_handler_shell.cjs", workflow.GetMCPHandlerShellScript()}, |
| 38 | {"mcp_handler_python.cjs", workflow.GetMCPHandlerPythonScript()}, |
| 39 | {"mcp_scripts_mcp_server_http.cjs", workflow.GetMCPScriptsMCPServerHTTPScript()}, |
| 40 | } |
| 41 | |
| 42 | for _, jsFile := range jsFiles { |
| 43 | filePath := filepath.Join(dir, jsFile.name) |
| 44 | if err := os.WriteFile(filePath, []byte(jsFile.content), constants.FilePermPublic); err != nil { |
| 45 | errMsg := fmt.Sprintf("failed to write %s: %v", jsFile.name, err) |
| 46 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 47 | return fmt.Errorf("failed to write %s: %w", jsFile.name, err) |
| 48 | } |
| 49 | if verbose { |
| 50 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Wrote "+jsFile.name)) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Generate and write tools.json |
| 55 | toolsJSON := workflow.GenerateMCPScriptsToolsConfig(mcpScriptsConfig) |
| 56 | toolsPath := filepath.Join(dir, "tools.json") |
| 57 | if err := os.WriteFile(toolsPath, []byte(toolsJSON), constants.FilePermPublic); err != nil { |
| 58 | errMsg := fmt.Sprintf("failed to write tools.json: %v", err) |
| 59 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 60 | return fmt.Errorf("failed to write tools.json: %w", err) |
| 61 | } |
| 62 | if verbose { |
| 63 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Wrote tools.json")) |
| 64 | } |
| 65 | |
| 66 | // Generate and write mcp-server.cjs entry point |
| 67 | mcpServerScript := workflow.GenerateMCPScriptsMCPServerScript(mcpScriptsConfig) |
| 68 | mcpServerPath := filepath.Join(dir, "mcp-server.cjs") |
| 69 | if err := os.WriteFile(mcpServerPath, []byte(mcpServerScript), constants.FilePermExecutable); err != nil { |
| 70 | errMsg := fmt.Sprintf("failed to write mcp-server.cjs: %v", err) |
| 71 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 72 | return fmt.Errorf("failed to write mcp-server.cjs: %w", err) |
no test coverage detected