9
9
"sync"
10
10
11
11
"github.com/mark3labs/mcp-go/server"
12
+
13
+ "github.com/docker/mcp-gateway/cmd/docker-mcp/internal/health"
12
14
)
13
15
14
16
func (g * Gateway ) startStdioServer (ctx context.Context , mcpServer * server.MCPServer , stdin io.Reader , stdout io.Writer ) error {
@@ -17,19 +19,12 @@ func (g *Gateway) startStdioServer(ctx context.Context, mcpServer *server.MCPSer
17
19
18
20
func (g * Gateway ) startSseServer (ctx context.Context , mcpServer * server.MCPServer , ln net.Listener ) error {
19
21
mux := http .NewServeMux ()
22
+ mux .Handle ("/health" , healthHandler (& g .health ))
23
+ mux .Handle ("/" , redirectHandler ("/sse" ))
24
+
20
25
sseServer := server .NewSSEServer (mcpServer )
21
- mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
22
- http .Redirect (w , r , "/sse" , http .StatusTemporaryRedirect )
23
- })
24
26
mux .Handle ("/sse" , sseServer .SSEHandler ())
25
27
mux .Handle ("/message" , sseServer .MessageHandler ())
26
- mux .HandleFunc ("/health" , func (w http.ResponseWriter , _ * http.Request ) {
27
- if g .health .IsHealthy () {
28
- w .WriteHeader (http .StatusOK )
29
- } else {
30
- w .WriteHeader (http .StatusServiceUnavailable )
31
- }
32
- })
33
28
httpServer := & http.Server {
34
29
Handler : mux ,
35
30
}
@@ -42,17 +37,10 @@ func (g *Gateway) startSseServer(ctx context.Context, mcpServer *server.MCPServe
42
37
43
38
func (g * Gateway ) startStreamingServer (ctx context.Context , mcpServer * server.MCPServer , ln net.Listener ) error {
44
39
mux := http .NewServeMux ()
45
- mux .HandleFunc ("/" , func ( w http. ResponseWriter , r * http. Request ) {
46
- http . Redirect ( w , r , "/mcp" , http . StatusTemporaryRedirect )
47
- })
40
+ mux .Handle ("/health " , healthHandler ( & g . health ))
41
+ mux . Handle ( "/" , redirectHandler ( "/mcp" ) )
42
+
48
43
mux .Handle ("/mcp" , server .NewStreamableHTTPServer (mcpServer ))
49
- mux .HandleFunc ("/health" , func (w http.ResponseWriter , _ * http.Request ) {
50
- if g .health .IsHealthy () {
51
- w .WriteHeader (http .StatusOK )
52
- } else {
53
- w .WriteHeader (http .StatusServiceUnavailable )
54
- }
55
- })
56
44
httpServer := & http.Server {
57
45
Handler : mux ,
58
46
}
@@ -65,13 +53,12 @@ func (g *Gateway) startStreamingServer(ctx context.Context, mcpServer *server.MC
65
53
}
66
54
67
55
func (g * Gateway ) startCentralStreamingServer (ctx context.Context , newMCPServer func () * server.MCPServer , ln net.Listener , configuration Configuration ) error {
56
+ mux := http .NewServeMux ()
57
+ mux .Handle ("/health" , healthHandler (& g .health ))
58
+ mux .Handle ("/" , redirectHandler ("/mcp" ))
59
+
68
60
var lock sync.Mutex
69
61
handlersPerSelectionOfServers := map [string ]* server.StreamableHTTPServer {}
70
-
71
- mux := http .NewServeMux ()
72
- mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
73
- http .Redirect (w , r , "/mcp" , http .StatusTemporaryRedirect )
74
- })
75
62
mux .HandleFunc ("/mcp" , func (w http.ResponseWriter , r * http.Request ) {
76
63
serverNames := r .Header .Get ("x-mcp-servers" )
77
64
if len (serverNames ) == 0 {
@@ -98,14 +85,6 @@ func (g *Gateway) startCentralStreamingServer(ctx context.Context, newMCPServer
98
85
99
86
handler .ServeHTTP (w , r )
100
87
})
101
-
102
- mux .HandleFunc ("/health" , func (w http.ResponseWriter , _ * http.Request ) {
103
- if g .health .IsHealthy () {
104
- w .WriteHeader (http .StatusOK )
105
- } else {
106
- w .WriteHeader (http .StatusServiceUnavailable )
107
- }
108
- })
109
88
httpServer := & http.Server {
110
89
Handler : mux ,
111
90
}
@@ -125,3 +104,19 @@ func parseServerNames(serverNames string) []string {
125
104
}
126
105
return names
127
106
}
107
+
108
+ func redirectHandler (target string ) http.HandlerFunc {
109
+ return func (w http.ResponseWriter , r * http.Request ) {
110
+ http .Redirect (w , r , target , http .StatusTemporaryRedirect )
111
+ }
112
+ }
113
+
114
+ func healthHandler (state * health.State ) http.HandlerFunc {
115
+ return func (w http.ResponseWriter , _ * http.Request ) {
116
+ if state .IsHealthy () {
117
+ w .WriteHeader (http .StatusOK )
118
+ } else {
119
+ w .WriteHeader (http .StatusServiceUnavailable )
120
+ }
121
+ }
122
+ }
0 commit comments