Skip to content

Commit 1542f1e

Browse files
authored
Merge pull request #157 from githubnext/copilot/review-health-endpoints-compliance
2 parents bf6f77f + 86a38c3 commit 1542f1e

3 files changed

Lines changed: 51 additions & 46 deletions

File tree

internal/server/health.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package server
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
// HealthResponse represents the JSON structure for the /health endpoint response
9+
// as defined in MCP Gateway Specification section 8.1.1
10+
type HealthResponse struct {
11+
Status string `json:"status"` // "healthy" or "unhealthy"
12+
SpecVersion string `json:"specVersion"` // MCP Gateway Specification version
13+
GatewayVersion string `json:"gatewayVersion"` // Gateway implementation version
14+
Servers map[string]ServerStatus `json:"servers"` // Map of server names to their health status
15+
}
16+
17+
// BuildHealthResponse constructs a HealthResponse from the unified server's status
18+
func BuildHealthResponse(unifiedServer *UnifiedServer) HealthResponse {
19+
// Get server status
20+
serverStatus := unifiedServer.GetServerStatus()
21+
22+
// Determine overall health based on server status
23+
overallStatus := "healthy"
24+
for _, status := range serverStatus {
25+
if status.Status == "error" {
26+
overallStatus = "unhealthy"
27+
break
28+
}
29+
}
30+
31+
return HealthResponse{
32+
Status: overallStatus,
33+
SpecVersion: MCPGatewaySpecVersion,
34+
GatewayVersion: gatewayVersion,
35+
Servers: serverStatus,
36+
}
37+
}
38+
39+
// HandleHealth returns an http.HandlerFunc that handles the /health endpoint
40+
// This function is used by both routed and unified modes to ensure consistent behavior
41+
func HandleHealth(unifiedServer *UnifiedServer) http.HandlerFunc {
42+
return func(w http.ResponseWriter, r *http.Request) {
43+
w.Header().Set("Content-Type", "application/json")
44+
w.WriteHeader(http.StatusOK)
45+
46+
response := BuildHealthResponse(unifiedServer)
47+
json.NewEncoder(w).Encode(response)
48+
}
49+
}

internal/server/routed.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,7 @@ func CreateHTTPServerForRoutedMode(addr string, unifiedServer *UnifiedServer, ap
110110
}
111111

112112
// Health check (spec 8.1.1)
113-
healthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
114-
w.Header().Set("Content-Type", "application/json")
115-
116-
// Get server status
117-
serverStatus := unifiedServer.GetServerStatus()
118-
119-
// Determine overall health based on server status
120-
overallStatus := "healthy"
121-
for _, status := range serverStatus {
122-
if status.Status == "error" {
123-
overallStatus = "unhealthy"
124-
break
125-
}
126-
}
127-
128-
w.WriteHeader(http.StatusOK)
129-
json.NewEncoder(w).Encode(map[string]interface{}{
130-
"status": overallStatus,
131-
"specVersion": MCPGatewaySpecVersion,
132-
"gatewayVersion": gatewayVersion,
133-
"servers": serverStatus,
134-
})
135-
})
113+
healthHandler := HandleHealth(unifiedServer)
136114
mux.Handle("/health", withResponseLogging(healthHandler))
137115

138116
// Close endpoint for graceful shutdown (spec 5.1.3)

internal/server/transport.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,29 +158,7 @@ func CreateHTTPServerForMCP(addr string, unifiedServer *UnifiedServer, apiKey st
158158
mux.Handle("/mcp", finalHandler)
159159

160160
// Health check (spec 8.1.1)
161-
healthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
162-
w.Header().Set("Content-Type", "application/json")
163-
164-
// Get server status
165-
serverStatus := unifiedServer.GetServerStatus()
166-
167-
// Determine overall health based on server status
168-
overallStatus := "healthy"
169-
for _, status := range serverStatus {
170-
if status.Status == "error" {
171-
overallStatus = "unhealthy"
172-
break
173-
}
174-
}
175-
176-
w.WriteHeader(http.StatusOK)
177-
json.NewEncoder(w).Encode(map[string]interface{}{
178-
"status": overallStatus,
179-
"specVersion": MCPGatewaySpecVersion,
180-
"gatewayVersion": gatewayVersion,
181-
"servers": serverStatus,
182-
})
183-
})
161+
healthHandler := HandleHealth(unifiedServer)
184162
mux.Handle("/health", withResponseLogging(healthHandler))
185163

186164
// Close endpoint for graceful shutdown (spec 5.1.3)

0 commit comments

Comments
 (0)