waitForServerReady waits for the HTTP server to be ready by polling the endpoint.
(ctx context.Context, port int, timeout time.Duration, verbose bool)
| 47 | |
| 48 | // waitForServerReady waits for the HTTP server to be ready by polling the endpoint. |
| 49 | func waitForServerReady(ctx context.Context, port int, timeout time.Duration, verbose bool) error { |
| 50 | deadline := time.Now().Add(timeout) |
| 51 | client := &http.Client{ |
| 52 | Timeout: 1 * time.Second, |
| 53 | } |
| 54 | url := fmt.Sprintf("http://localhost:%d/", port) |
| 55 | |
| 56 | for time.Now().Before(deadline) { |
| 57 | select { |
| 58 | case <-ctx.Done(): |
| 59 | return ctx.Err() |
| 60 | default: |
| 61 | } |
| 62 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 63 | if err != nil { |
| 64 | mcpInspectLog.Printf("Failed to create request: %v", err) |
| 65 | return err |
| 66 | } |
| 67 | resp, err := client.Do(req) |
| 68 | if err == nil { |
| 69 | defer func() { |
| 70 | if closeErr := resp.Body.Close(); closeErr != nil { |
| 71 | mcpInspectLog.Printf("Warning: failed to close response body: %v", closeErr) |
| 72 | } |
| 73 | }() |
| 74 | if verbose { |
| 75 | mcpInspectLog.Printf("Server is ready on port %d", port) |
| 76 | } |
| 77 | return nil |
| 78 | } |
| 79 | timer := time.NewTimer(mcpScriptsServerStartupDelay) |
| 80 | select { |
| 81 | case <-ctx.Done(): |
| 82 | timer.Stop() |
| 83 | return ctx.Err() |
| 84 | case <-timer.C: |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | mcpInspectLog.Printf("Server did not become ready within timeout") |
| 89 | return errMCPScriptsServerStartupTimeout |
| 90 | } |
| 91 | |
| 92 | // startMCPScriptsHTTPServer starts the mcp-scripts HTTP MCP server |
| 93 | func startMCPScriptsHTTPServer(dir string, port int, verbose bool) (*exec.Cmd, error) { |