startMCPScriptsServer starts the mcp-scripts HTTP server and returns the MCP config
(ctx context.Context, mcpScriptsConfig *workflow.MCPScriptsConfig, verbose bool)
| 122 | |
| 123 | // startMCPScriptsServer starts the mcp-scripts HTTP server and returns the MCP config |
| 124 | func startMCPScriptsServer(ctx context.Context, mcpScriptsConfig *workflow.MCPScriptsConfig, verbose bool) (*parser.RegistryMCPServerConfig, *exec.Cmd, string, error) { |
| 125 | mcpInspectLog.Printf("Starting mcp-scripts server with %d tools", len(mcpScriptsConfig.Tools)) |
| 126 | |
| 127 | // Check if node is available |
| 128 | if _, err := exec.LookPath("node"); err != nil { |
| 129 | errMsg := "node not found. Please install Node.js to run the mcp-scripts MCP server" |
| 130 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 131 | return nil, nil, "", fmt.Errorf("node not found. Please install Node.js to run the mcp-scripts MCP server: %w", err) |
| 132 | } |
| 133 | |
| 134 | if verbose { |
| 135 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Found %d mcp-script tool(s) to configure", len(mcpScriptsConfig.Tools)))) |
| 136 | } |
| 137 | |
| 138 | // Create temporary directory for mcp-scripts files |
| 139 | tmpDir, err := os.MkdirTemp("", "gh-aw-mcp-scripts-*") |
| 140 | if err != nil { |
| 141 | errMsg := fmt.Sprintf("failed to create temporary directory: %v", err) |
| 142 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 143 | return nil, nil, "", fmt.Errorf("failed to create temporary directory: %w", err) |
| 144 | } |
| 145 | |
| 146 | if verbose { |
| 147 | if _, err := fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Created temporary directory: "+tmpDir)); err != nil { |
| 148 | mcpInspectLog.Printf("Warning: failed to write to stderr: %v", err) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Write mcp-scripts files to temporary directory |
| 153 | if err := writeMCPScriptsFiles(tmpDir, mcpScriptsConfig, verbose); err != nil { |
| 154 | // Clean up temporary directory on error |
| 155 | if err := os.RemoveAll(tmpDir); err != nil && verbose { |
| 156 | mcpInspectLog.Printf("Warning: failed to clean up temporary directory %s: %v", tmpDir, err) |
| 157 | } |
| 158 | errMsg := fmt.Sprintf("failed to write mcp-scripts files: %v", err) |
| 159 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 160 | return nil, nil, "", fmt.Errorf("failed to write mcp-scripts files: %w", err) |
| 161 | } |
| 162 | |
| 163 | // Find an available port for the HTTP server |
| 164 | port := findAvailablePort(mcpScriptsStartPort, verbose) |
| 165 | if port == 0 { |
| 166 | if err := os.RemoveAll(tmpDir); err != nil && verbose { |
| 167 | mcpInspectLog.Printf("Warning: failed to clean up temporary directory %s: %v", tmpDir, err) |
| 168 | } |
| 169 | errMsg := "failed to find an available port for the HTTP server" |
| 170 | fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errMsg)) |
| 171 | return nil, nil, "", errors.New("failed to find an available port for the HTTP server") |
| 172 | } |
| 173 | |
| 174 | if verbose { |
| 175 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Using port %d for mcp-scripts HTTP server", port))) |
| 176 | } |
| 177 | |
| 178 | // Start the HTTP server |
| 179 | serverCmd, err := startMCPScriptsHTTPServer(tmpDir, port, verbose) |
| 180 | if err != nil { |
| 181 | // Clean up temporary directory on error |
no test coverage detected