-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
117 lines (108 loc) · 3.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"log"
"os"
"github.com/strowk/mcp-k8s-go/internal/k8s"
"github.com/strowk/mcp-k8s-go/internal/k8s/apps/v1/deployment"
"github.com/strowk/mcp-k8s-go/internal/prompts"
"github.com/strowk/mcp-k8s-go/internal/resources"
"github.com/strowk/mcp-k8s-go/internal/tools"
"github.com/strowk/mcp-k8s-go/internal/utils"
"github.com/strowk/foxy-contexts/pkg/app"
"github.com/strowk/foxy-contexts/pkg/mcp"
"github.com/strowk/foxy-contexts/pkg/stdio"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func getCapabilities() *mcp.ServerCapabilities {
return &mcp.ServerCapabilities{
Resources: &mcp.ServerCapabilitiesResources{
ListChanged: utils.Ptr(false),
Subscribe: utils.Ptr(false),
},
Prompts: &mcp.ServerCapabilitiesPrompts{
ListChanged: utils.Ptr(false),
},
}
}
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
if len(os.Args) > 1 {
if os.Args[1] == "--version" {
println(version)
}
if os.Args[1] == "version" {
println("Version: ", version)
println("Commit: ", commit)
println("Date: ", date)
}
if os.Args[1] == "help" || os.Args[1] == "--help" {
println("mcp-k8s is an MCP server for Kubernetes")
println("Read more about it in: https://github.com/strowk/mcp-k8s-go\n")
println("Usage: <bin> [flags]")
println(" Run with no flags to start the server\n")
println("Flags:")
println(" help, --help: Print this help message")
println(" --version: Print the version of the server")
println(" version: Print the version, commit and date of the server")
}
return
}
foxyApp := getApp()
err := foxyApp.Run()
if err != nil {
log.Fatalf("Error: %v", err)
}
}
func getApp() *app.Builder {
return app.
NewBuilder().
WithFxOptions(
fx.Provide(func() clientcmd.ClientConfig {
return k8s.GetKubeConfig()
}),
fx.Provide(func() (*kubernetes.Clientset, error) {
return k8s.GetKubeClientset()
}),
fx.Provide(func() k8s.ClientPool {
return k8s.NewClientPool()
}),
).
WithTool(tools.NewPodLogsTool).
WithTool(tools.NewListContextsTool).
WithTool(tools.NewListNamespacesTool).
WithTool(tools.NewListEventsTool).
WithTool(tools.NewListPodsTool).
WithTool(tools.NewListServicesTool).
WithTool(tools.NewListNodesTool).
WithPrompt(prompts.NewListPodsPrompt).
WithPrompt(prompts.NewListNamespacesPrompt).
WithFxOptions(deployment.Registration).
WithResourceProvider(resources.NewContextsResourceProvider).
WithServerCapabilities(getCapabilities()).
// setting up server
WithName("mcp-k8s-go").
WithVersion("0.0.1").
WithTransport(stdio.NewTransport()).
// Configuring fx logging to only show errors
WithFxOptions(
fx.Provide(func() *zap.Logger {
cfg := zap.NewDevelopmentConfig()
cfg.Level.SetLevel(zap.ErrorLevel)
logger, _ := cfg.Build()
return logger
}),
fx.Option(fx.WithLogger(
func(logger *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: logger}
},
)),
)
}