MCPcopy Create free account
hub / github.com/github/gh-aw / waitForServerReady

Function waitForServerReady

pkg/cli/mcp_inspect_mcp_scripts_server.go:49–90  ·  view source on GitHub ↗

waitForServerReady waits for the HTTP server to be ready by polling the endpoint.

(ctx context.Context, port int, timeout time.Duration, verbose bool)

Source from the content-addressed store, hash-verified

47
48// waitForServerReady waits for the HTTP server to be ready by polling the endpoint.
49func 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
93func startMCPScriptsHTTPServer(dir string, port int, verbose bool) (*exec.Cmd, error) {

Calls 5

DoneMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.45
PrintfMethod · 0.45
StopMethod · 0.45