Skip to content

Commit 94b8599

Browse files
authored
fix(npm): child process exits gracefully on SIGxxx (#243)
Signed-off-by: Marc Nuri <[email protected]>
1 parent 4dcede1 commit 94b8599

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

pkg/http/http.go

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package http
22

33
import (
44
"context"
5-
"encoding/json"
65
"errors"
76
"net/http"
87
"os"
@@ -19,11 +18,10 @@ import (
1918
)
2019

2120
const (
22-
oauthProtectedResourceEndpoint = "/.well-known/oauth-protected-resource"
23-
healthEndpoint = "/healthz"
24-
mcpEndpoint = "/mcp"
25-
sseEndpoint = "/sse"
26-
sseMessageEndpoint = "/message"
21+
healthEndpoint = "/healthz"
22+
mcpEndpoint = "/mcp"
23+
sseEndpoint = "/sse"
24+
sseMessageEndpoint = "/message"
2725
)
2826

2927
func Serve(ctx context.Context, mcpServer *mcp.Server, staticConfig *config.StaticConfig, oidcProvider *oidc.Provider) error {
@@ -46,39 +44,7 @@ func Serve(ctx context.Context, mcpServer *mcp.Server, staticConfig *config.Stat
4644
mux.HandleFunc(healthEndpoint, func(w http.ResponseWriter, r *http.Request) {
4745
w.WriteHeader(http.StatusOK)
4846
})
49-
mux.HandleFunc(oauthProtectedResourceEndpoint, func(w http.ResponseWriter, r *http.Request) {
50-
w.Header().Set("Content-Type", "application/json")
51-
52-
var authServers []string
53-
if staticConfig.AuthorizationURL != "" {
54-
authServers = []string{staticConfig.AuthorizationURL}
55-
} else {
56-
// Fallback to Kubernetes API server host if authorization_server is not configured
57-
if apiServerHost := mcpServer.GetKubernetesAPIServerHost(); apiServerHost != "" {
58-
authServers = []string{apiServerHost}
59-
}
60-
}
61-
62-
response := map[string]interface{}{
63-
"authorization_servers": authServers,
64-
"authorization_server": authServers[0],
65-
"scopes_supported": mcpServer.GetEnabledTools(),
66-
"bearer_methods_supported": []string{"header"},
67-
}
68-
69-
if staticConfig.ServerURL != "" {
70-
response["resource"] = staticConfig.ServerURL
71-
}
72-
73-
if staticConfig.JwksURL != "" {
74-
response["jwks_uri"] = staticConfig.JwksURL
75-
}
76-
77-
w.WriteHeader(http.StatusOK)
78-
if err := json.NewEncoder(w).Encode(response); err != nil {
79-
http.Error(w, err.Error(), http.StatusInternalServerError)
80-
}
81-
})
47+
mux.HandleFunc(oauthProtectedResourceEndpoint, OAuthProtectedResourceHandler(mcpServer, staticConfig))
8248

8349
ctx, cancel := context.WithCancel(ctx)
8450
defer cancel()

pkg/http/wellknown.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package http
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/containers/kubernetes-mcp-server/pkg/config"
8+
"github.com/containers/kubernetes-mcp-server/pkg/mcp"
9+
)
10+
11+
const (
12+
oauthProtectedResourceEndpoint = "/.well-known/oauth-protected-resource"
13+
)
14+
15+
func OAuthProtectedResourceHandler(mcpServer *mcp.Server, staticConfig *config.StaticConfig) http.HandlerFunc {
16+
return func(w http.ResponseWriter, r *http.Request) {
17+
w.Header().Set("Content-Type", "application/json")
18+
19+
var authServers []string
20+
if staticConfig.AuthorizationURL != "" {
21+
authServers = []string{staticConfig.AuthorizationURL}
22+
} else {
23+
// Fallback to Kubernetes API server host if authorization_server is not configured
24+
if apiServerHost := mcpServer.GetKubernetesAPIServerHost(); apiServerHost != "" {
25+
authServers = []string{apiServerHost}
26+
}
27+
}
28+
29+
response := map[string]interface{}{
30+
"authorization_servers": authServers,
31+
"authorization_server": authServers[0],
32+
"scopes_supported": mcpServer.GetEnabledTools(),
33+
"bearer_methods_supported": []string{"header"},
34+
}
35+
36+
if staticConfig.ServerURL != "" {
37+
response["resource"] = staticConfig.ServerURL
38+
}
39+
40+
if staticConfig.JwksURL != "" {
41+
response["jwks_uri"] = staticConfig.JwksURL
42+
}
43+
44+
w.WriteHeader(http.StatusOK)
45+
if err := json.NewEncoder(w).Encode(response); err != nil {
46+
http.Error(w, err.Error(), http.StatusInternalServerError)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)