mirror of
https://github.com/openshift/openshift-mcp-server.git
synced 2025-10-17 14:27:48 +03:00
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package mcp
|
|
|
|
import (
|
|
"github.com/manusa/kubernetes-mcp-server/pkg/kubernetes"
|
|
"github.com/manusa/kubernetes-mcp-server/pkg/version"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
"slices"
|
|
)
|
|
|
|
type Configuration struct {
|
|
Kubeconfig string
|
|
}
|
|
|
|
type Server struct {
|
|
configuration *Configuration
|
|
server *server.MCPServer
|
|
k *kubernetes.Kubernetes
|
|
}
|
|
|
|
func NewSever(configuration Configuration) (*Server, error) {
|
|
s := &Server{
|
|
configuration: &configuration,
|
|
server: server.NewMCPServer(
|
|
version.BinaryName,
|
|
version.Version,
|
|
server.WithResourceCapabilities(true, true),
|
|
server.WithPromptCapabilities(true),
|
|
server.WithToolCapabilities(true),
|
|
server.WithLogging(),
|
|
),
|
|
}
|
|
if err := s.reloadKubernetesClient(); err != nil {
|
|
return nil, err
|
|
}
|
|
s.k.WatchKubeConfig(s.reloadKubernetesClient)
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Server) reloadKubernetesClient() error {
|
|
k, err := kubernetes.NewKubernetes(s.configuration.Kubeconfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.k = k
|
|
s.server.SetTools(slices.Concat(
|
|
s.initConfiguration(),
|
|
s.initEvents(),
|
|
s.initNamespaces(),
|
|
s.initPods(),
|
|
s.initResources(),
|
|
)...)
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) ServeStdio() error {
|
|
return server.ServeStdio(s.server)
|
|
}
|
|
|
|
func (s *Server) ServeSse(baseUrl string) *server.SSEServer {
|
|
options := make([]server.SSEOption, 0)
|
|
if baseUrl != "" {
|
|
options = append(options, server.WithBaseURL(baseUrl))
|
|
}
|
|
return server.NewSSEServer(s.server, options...)
|
|
}
|
|
|
|
func (s *Server) Close() {
|
|
if s.k != nil {
|
|
s.k.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,
|
|
},
|
|
},
|
|
}
|
|
}
|