mirror of
https://github.com/openshift/openshift-mcp-server.git
synced 2025-10-17 14:27:48 +03:00
* feat: add cluster provider for kubeconfig Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: move server to use ClusterProvider interface Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: authentication middleware works with cluster provider Signed-off-by: Calum Murray <cmurray@redhat.com> * fix: unit tests work after cluster provider changes Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: add tool mutator to add cluster parameter Signed-off-by: Calum Murray <cmurray@redhat.com> * test: handle cluster parameter Signed-off-by: Calum Murray <cmurray@redhat.com> * fix: handle lazy init correctly Signed-off-by: Calum Murray <cmurray@redhat.com> * refactor: move to using multi-strategy ManagerProvider Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: add contexts_list tool Signed-off-by: Calum Murray <cmurray@redhat.com> * refactor: make tool mutator generic between cluster/context naming Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: introduce tool filter Signed-off-by: Calum Murray <cmurray@redhat.com> * refactor: use new ManagerProvider/mutator/filter within mcp server Signed-off-by: Calum Murray <cmurray@redhat.com> * fix(test): tests expect context parameter in tool defs Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: auth handles multi-cluster case correctly Signed-off-by: Calum Murray <cmurray@redhat.com> * fix: small changes from local testing Signed-off-by: Calum Murray <cmurray@redhat.com> * chore: fix enum test Signed-off-by: Calum Murray <cmurray@redhat.com> * review: Multi Cluster support (#1) * nit: rename contexts_list to configuration_contexts_list Besides the conventional naming, it helps LLMs understand the context of the tool by providing a certain level of hierarchy. Signed-off-by: Marc Nuri <marc@marcnuri.com> * fix(mcp): ToolMutator doesn't rely on magic strings Signed-off-by: Marc Nuri <marc@marcnuri.com> * refactor(api): don't expose ManagerProvider to toolsets Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(mcp): configuration_contexts_list basic tests Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(toolsets): revert edge-case test This test should not be touched. Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(toolsets): add specific metadata tests for multi-cluster Signed-off-by: Marc Nuri <marc@marcnuri.com> * fix(mcp): ToolFilter doesn't rely on magic strings (partially) Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(api): IsClusterAware and IsTargetListProvider default values Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(mcp): revert unneeded changes in mcp_tools_test.go Signed-off-by: Marc Nuri <marc@marcnuri.com> --------- Signed-off-by: Marc Nuri <marc@marcnuri.com> * fix: always include configuration_contexts_list if contexts > 1 Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: include server urls in configuration_contexts_list Signed-off-by: Calum Murray <cmurray@redhat.com> --------- Signed-off-by: Calum Murray <cmurray@redhat.com> Signed-off-by: Marc Nuri <marc@marcnuri.com> Co-authored-by: Marc Nuri <marc@marcnuri.com>
285 lines
7.9 KiB
Go
285 lines
7.9 KiB
Go
package mcp
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
authenticationapiv1 "k8s.io/api/authentication/v1"
|
|
"k8s.io/klog/v2"
|
|
"k8s.io/utils/ptr"
|
|
|
|
"github.com/containers/kubernetes-mcp-server/pkg/api"
|
|
"github.com/containers/kubernetes-mcp-server/pkg/config"
|
|
internalk8s "github.com/containers/kubernetes-mcp-server/pkg/kubernetes"
|
|
"github.com/containers/kubernetes-mcp-server/pkg/output"
|
|
"github.com/containers/kubernetes-mcp-server/pkg/toolsets"
|
|
"github.com/containers/kubernetes-mcp-server/pkg/version"
|
|
)
|
|
|
|
type ContextKey string
|
|
|
|
const TokenScopesContextKey = ContextKey("TokenScopesContextKey")
|
|
|
|
type Configuration struct {
|
|
*config.StaticConfig
|
|
listOutput output.Output
|
|
toolsets []api.Toolset
|
|
}
|
|
|
|
func (c *Configuration) Toolsets() []api.Toolset {
|
|
if c.toolsets == nil {
|
|
for _, toolset := range c.StaticConfig.Toolsets {
|
|
c.toolsets = append(c.toolsets, toolsets.ToolsetFromString(toolset))
|
|
}
|
|
}
|
|
return c.toolsets
|
|
}
|
|
|
|
func (c *Configuration) ListOutput() output.Output {
|
|
if c.listOutput == nil {
|
|
c.listOutput = output.FromString(c.StaticConfig.ListOutput)
|
|
}
|
|
return c.listOutput
|
|
}
|
|
|
|
func (c *Configuration) isToolApplicable(tool api.ServerTool) bool {
|
|
if c.ReadOnly && !ptr.Deref(tool.Tool.Annotations.ReadOnlyHint, false) {
|
|
return false
|
|
}
|
|
if c.DisableDestructive && ptr.Deref(tool.Tool.Annotations.DestructiveHint, false) {
|
|
return false
|
|
}
|
|
if c.EnabledTools != nil && !slices.Contains(c.EnabledTools, tool.Tool.Name) {
|
|
return false
|
|
}
|
|
if c.DisabledTools != nil && slices.Contains(c.DisabledTools, tool.Tool.Name) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
type Server struct {
|
|
configuration *Configuration
|
|
server *server.MCPServer
|
|
enabledTools []string
|
|
p internalk8s.ManagerProvider
|
|
}
|
|
|
|
func NewServer(configuration Configuration) (*Server, error) {
|
|
var serverOptions []server.ServerOption
|
|
serverOptions = append(serverOptions,
|
|
server.WithResourceCapabilities(true, true),
|
|
server.WithPromptCapabilities(true),
|
|
server.WithToolCapabilities(true),
|
|
server.WithLogging(),
|
|
server.WithToolHandlerMiddleware(toolCallLoggingMiddleware),
|
|
)
|
|
if configuration.RequireOAuth && false { // TODO: Disabled scope auth validation for now
|
|
serverOptions = append(serverOptions, server.WithToolHandlerMiddleware(toolScopedAuthorizationMiddleware))
|
|
}
|
|
|
|
s := &Server{
|
|
configuration: &configuration,
|
|
server: server.NewMCPServer(
|
|
version.BinaryName,
|
|
version.Version,
|
|
serverOptions...,
|
|
),
|
|
}
|
|
if err := s.reloadKubernetesClusterProvider(); err != nil {
|
|
return nil, err
|
|
}
|
|
s.p.WatchTargets(s.reloadKubernetesClusterProvider)
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Server) reloadKubernetesClusterProvider() error {
|
|
ctx := context.Background()
|
|
p, err := internalk8s.NewManagerProvider(s.configuration.StaticConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// close the old provider
|
|
if s.p != nil {
|
|
s.p.Close()
|
|
}
|
|
|
|
s.p = p
|
|
|
|
k, err := s.p.GetManagerFor(ctx, s.p.GetDefaultTarget())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
targets, err := p.GetTargets(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
filter := CompositeFilter(
|
|
s.configuration.isToolApplicable,
|
|
ShouldIncludeTargetListTool(p.GetTargetParameterName(), targets),
|
|
)
|
|
|
|
mutator := WithTargetParameter(
|
|
p.GetDefaultTarget(),
|
|
p.GetTargetParameterName(),
|
|
targets,
|
|
)
|
|
|
|
applicableTools := make([]api.ServerTool, 0)
|
|
for _, toolset := range s.configuration.Toolsets() {
|
|
for _, tool := range toolset.GetTools(k) {
|
|
tool := mutator(tool)
|
|
if !filter(tool) {
|
|
continue
|
|
}
|
|
|
|
applicableTools = append(applicableTools, tool)
|
|
s.enabledTools = append(s.enabledTools, tool.Tool.Name)
|
|
}
|
|
}
|
|
m3labsServerTools, err := ServerToolToM3LabsServerTool(s, applicableTools)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to convert tools: %v", err)
|
|
}
|
|
|
|
s.server.SetTools(m3labsServerTools...)
|
|
|
|
// start new watch
|
|
s.p.WatchTargets(s.reloadKubernetesClusterProvider)
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) ServeStdio() error {
|
|
return server.ServeStdio(s.server)
|
|
}
|
|
|
|
func (s *Server) ServeSse(baseUrl string, httpServer *http.Server) *server.SSEServer {
|
|
options := make([]server.SSEOption, 0)
|
|
options = append(options, server.WithSSEContextFunc(contextFunc), server.WithHTTPServer(httpServer))
|
|
if baseUrl != "" {
|
|
options = append(options, server.WithBaseURL(baseUrl))
|
|
}
|
|
return server.NewSSEServer(s.server, options...)
|
|
}
|
|
|
|
func (s *Server) ServeHTTP(httpServer *http.Server) *server.StreamableHTTPServer {
|
|
options := []server.StreamableHTTPOption{
|
|
server.WithHTTPContextFunc(contextFunc),
|
|
server.WithStreamableHTTPServer(httpServer),
|
|
server.WithStateLess(true),
|
|
}
|
|
return server.NewStreamableHTTPServer(s.server, options...)
|
|
}
|
|
|
|
// KubernetesApiVerifyToken verifies the given token with the audience by
|
|
// sending an TokenReview request to API Server for the specified cluster.
|
|
func (s *Server) KubernetesApiVerifyToken(ctx context.Context, token string, audience string, cluster string) (*authenticationapiv1.UserInfo, []string, error) {
|
|
if s.p == nil {
|
|
return nil, nil, fmt.Errorf("kubernetes cluster provider is not initialized")
|
|
}
|
|
|
|
// Use provided cluster or default
|
|
if cluster == "" {
|
|
cluster = s.p.GetDefaultTarget()
|
|
}
|
|
|
|
// Get the cluster manager for the specified cluster
|
|
m, err := s.p.GetManagerFor(ctx, cluster)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return m.VerifyToken(ctx, token, audience)
|
|
}
|
|
|
|
// GetTargetParameterName returns the parameter name used for target identification in MCP requests
|
|
func (s *Server) GetTargetParameterName() string {
|
|
if s.p == nil {
|
|
return "" // fallback for uninitialized provider
|
|
}
|
|
return s.p.GetTargetParameterName()
|
|
}
|
|
|
|
func (s *Server) GetEnabledTools() []string {
|
|
return s.enabledTools
|
|
}
|
|
|
|
func (s *Server) Close() {
|
|
if s.p != nil {
|
|
s.p.Close()
|
|
}
|
|
}
|
|
|
|
func NewTextResult(content string, err error) *mcp.CallToolResult {
|
|
if err != nil {
|
|
return &mcp.CallToolResult{
|
|
IsError: true,
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: err.Error(),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
return &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: content,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func contextFunc(ctx context.Context, r *http.Request) context.Context {
|
|
// Get the standard Authorization header (OAuth compliant)
|
|
authHeader := r.Header.Get(string(internalk8s.OAuthAuthorizationHeader))
|
|
if authHeader != "" {
|
|
return context.WithValue(ctx, internalk8s.OAuthAuthorizationHeader, authHeader)
|
|
}
|
|
|
|
// Fallback to custom header for backward compatibility
|
|
customAuthHeader := r.Header.Get(string(internalk8s.CustomAuthorizationHeader))
|
|
if customAuthHeader != "" {
|
|
return context.WithValue(ctx, internalk8s.OAuthAuthorizationHeader, customAuthHeader)
|
|
}
|
|
|
|
return ctx
|
|
}
|
|
|
|
func toolCallLoggingMiddleware(next server.ToolHandlerFunc) server.ToolHandlerFunc {
|
|
return func(ctx context.Context, ctr mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
klog.V(5).Infof("mcp tool call: %s(%v)", ctr.Params.Name, ctr.Params.Arguments)
|
|
if ctr.Header != nil {
|
|
buffer := bytes.NewBuffer(make([]byte, 0))
|
|
if err := ctr.Header.WriteSubset(buffer, map[string]bool{"Authorization": true, "authorization": true}); err == nil {
|
|
klog.V(7).Infof("mcp tool call headers: %s", buffer)
|
|
}
|
|
}
|
|
return next(ctx, ctr)
|
|
}
|
|
}
|
|
|
|
func toolScopedAuthorizationMiddleware(next server.ToolHandlerFunc) server.ToolHandlerFunc {
|
|
return func(ctx context.Context, ctr mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
scopes, ok := ctx.Value(TokenScopesContextKey).([]string)
|
|
if !ok {
|
|
return NewTextResult("", fmt.Errorf("authorization failed: Access denied: Tool '%s' requires scope 'mcp:%s' but no scope is available", ctr.Params.Name, ctr.Params.Name)), nil
|
|
}
|
|
if !slices.Contains(scopes, "mcp:"+ctr.Params.Name) && !slices.Contains(scopes, ctr.Params.Name) {
|
|
return NewTextResult("", fmt.Errorf("authorization failed: Access denied: Tool '%s' requires scope 'mcp:%s' but only scopes %s are available", ctr.Params.Name, ctr.Params.Name, scopes)), nil
|
|
}
|
|
return next(ctx, ctr)
|
|
}
|
|
}
|