@@ -40,12 +40,18 @@ Kubernetes Model Context Protocol (MCP) server
40
40
# TODO: add more examples` ,
41
41
Run : func (cmd * cobra.Command , args []string ) {
42
42
initLogging ()
43
- klog .V (5 ).Infof ("Starting kubernetes-mcp-server" )
43
+ profile := mcp .ProfileFromString (viper .GetString ("profile" ))
44
+ if profile == nil {
45
+ fmt .Printf ("Invalid profile name: %s, valid names are: %s\n " , viper .GetString ("profile" ), mcp .ProfileNames )
46
+ os .Exit (1 )
47
+ }
48
+ klog .V (1 ).Infof ("Starting kubernetes-mcp-server with profile: %s" , profile .GetName ())
44
49
if viper .GetBool ("version" ) {
45
50
fmt .Println (version .Version )
46
51
return
47
52
}
48
53
mcpServer , err := mcp .NewSever (mcp.Configuration {
54
+ Profile : profile ,
49
55
Kubeconfig : viper .GetString ("kubeconfig" ),
50
56
})
51
57
if err != nil {
@@ -70,27 +76,51 @@ Kubernetes Model Context Protocol (MCP) server
70
76
},
71
77
}
72
78
73
- func init () {
74
- rootCmd .Flags ().BoolP ("version" , "v" , false , "Print version information and quit" )
75
- rootCmd .Flags ().IntP ("log-level" , "" , 0 , "Set the log level (from 0 to 9)" )
76
- rootCmd .Flags ().IntP ("sse-port" , "" , 0 , "Start a SSE server on the specified port" )
77
- rootCmd .Flags ().StringP ("sse-base-url" , "" , "" , "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)" )
78
- rootCmd .Flags ().StringP ("kubeconfig" , "" , "" , "Path to the kubeconfig file to use for authentication" )
79
- _ = viper .BindPFlags (rootCmd .Flags ())
80
- }
81
-
82
79
func Execute () {
83
80
if err := rootCmd .Execute (); err != nil {
84
- panic (err )
81
+ klog .Errorf ("Failed to execute command: %s" , err )
82
+ os .Exit (1 )
85
83
}
86
84
}
87
85
88
86
func initLogging () {
89
- logger := textlogger .NewLogger (textlogger .NewConfig (textlogger .Output (os .Stdout )))
90
- klog .SetLoggerWithOptions (logger )
91
87
flagSet := flag .NewFlagSet ("kubernetes-mcp-server" , flag .ContinueOnError )
92
88
klog .InitFlags (flagSet )
89
+ loggerOptions := []textlogger.ConfigOption {textlogger .Output (os .Stdout )}
93
90
if logLevel := viper .GetInt ("log-level" ); logLevel >= 0 {
91
+ loggerOptions = append (loggerOptions , textlogger .Verbosity (logLevel ))
94
92
_ = flagSet .Parse ([]string {"--v" , strconv .Itoa (logLevel )})
95
93
}
94
+ logger := textlogger .NewLogger (textlogger .NewConfig (loggerOptions ... ))
95
+ klog .SetLoggerWithOptions (logger )
96
+ }
97
+
98
+ type profileFlag struct {
99
+ mcp.Profile
100
+ }
101
+
102
+ func (p * profileFlag ) String () string {
103
+ return p .GetName ()
104
+ }
105
+
106
+ func (p * profileFlag ) Set (v string ) error {
107
+ p .Profile = mcp .ProfileFromString (v )
108
+ if p .Profile != nil {
109
+ return nil
110
+ }
111
+ return fmt .Errorf ("invalid profile name: %s, valid names are: %s" , v , mcp .ProfileNames )
112
+ }
113
+
114
+ func (p * profileFlag ) Type () string {
115
+ return "profile"
116
+ }
117
+
118
+ func init () {
119
+ rootCmd .Flags ().BoolP ("version" , "v" , false , "Print version information and quit" )
120
+ rootCmd .Flags ().IntP ("log-level" , "" , 0 , "Set the log level (from 0 to 9)" )
121
+ rootCmd .Flags ().IntP ("sse-port" , "" , 0 , "Start a SSE server on the specified port" )
122
+ rootCmd .Flags ().StringP ("sse-base-url" , "" , "" , "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)" )
123
+ rootCmd .Flags ().StringP ("kubeconfig" , "" , "" , "Path to the kubeconfig file to use for authentication" )
124
+ rootCmd .Flags ().Var (& profileFlag {& mcp.FullProfile {}}, "profile" , "MCP profile to use" )
125
+ _ = viper .BindPFlags (rootCmd .Flags ())
96
126
}
0 commit comments