Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 5 additions & 39 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package http

import (
"context"
"encoding/json"
"errors"
"net/http"
"os"
Expand All @@ -19,11 +18,10 @@ import (
)

const (
oauthProtectedResourceEndpoint = "/.well-known/oauth-protected-resource"
healthEndpoint = "/healthz"
mcpEndpoint = "/mcp"
sseEndpoint = "/sse"
sseMessageEndpoint = "/message"
healthEndpoint = "/healthz"
mcpEndpoint = "/mcp"
sseEndpoint = "/sse"
sseMessageEndpoint = "/message"
)

func Serve(ctx context.Context, mcpServer *mcp.Server, staticConfig *config.StaticConfig, oidcProvider *oidc.Provider) error {
Expand All @@ -46,39 +44,7 @@ func Serve(ctx context.Context, mcpServer *mcp.Server, staticConfig *config.Stat
mux.HandleFunc(healthEndpoint, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc(oauthProtectedResourceEndpoint, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

var authServers []string
if staticConfig.AuthorizationURL != "" {
authServers = []string{staticConfig.AuthorizationURL}
} else {
// Fallback to Kubernetes API server host if authorization_server is not configured
if apiServerHost := mcpServer.GetKubernetesAPIServerHost(); apiServerHost != "" {
authServers = []string{apiServerHost}
}
}

response := map[string]interface{}{
"authorization_servers": authServers,
"authorization_server": authServers[0],
"scopes_supported": mcpServer.GetEnabledTools(),
"bearer_methods_supported": []string{"header"},
}

if staticConfig.ServerURL != "" {
response["resource"] = staticConfig.ServerURL
}

if staticConfig.JwksURL != "" {
response["jwks_uri"] = staticConfig.JwksURL
}

w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.HandleFunc(oauthProtectedResourceEndpoint, OAuthProtectedResourceHandler(mcpServer, staticConfig))

ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down
49 changes: 49 additions & 0 deletions pkg/http/wellknown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package http

import (
"encoding/json"
"net/http"

"github.com/containers/kubernetes-mcp-server/pkg/config"
"github.com/containers/kubernetes-mcp-server/pkg/mcp"
)

const (
oauthProtectedResourceEndpoint = "/.well-known/oauth-protected-resource"
)

func OAuthProtectedResourceHandler(mcpServer *mcp.Server, staticConfig *config.StaticConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

var authServers []string
if staticConfig.AuthorizationURL != "" {
authServers = []string{staticConfig.AuthorizationURL}
} else {
// Fallback to Kubernetes API server host if authorization_server is not configured
if apiServerHost := mcpServer.GetKubernetesAPIServerHost(); apiServerHost != "" {
authServers = []string{apiServerHost}
}
}

response := map[string]interface{}{
"authorization_servers": authServers,
"authorization_server": authServers[0],
"scopes_supported": mcpServer.GetEnabledTools(),
"bearer_methods_supported": []string{"header"},
}

if staticConfig.ServerURL != "" {
response["resource"] = staticConfig.ServerURL
}

if staticConfig.JwksURL != "" {
response["jwks_uri"] = staticConfig.JwksURL
}

w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
Loading