Skip to content

Commit eacdb95

Browse files
committed
feat: SSE support
1 parent 39d21d7 commit eacdb95

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/kubernetes-mcp-server/cmd/root.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/manusa/kubernetes-mcp-server/pkg/mcp"
77
"github.com/manusa/kubernetes-mcp-server/pkg/version"
8+
"github.com/mark3labs/mcp-go/server"
89
"github.com/spf13/cobra"
910
"github.com/spf13/viper"
1011
"golang.org/x/net/context"
@@ -22,6 +23,15 @@ Kubernetes Model Context Protocol (MCP) server
2223
# shows version information
2324
kubernetes-mcp-server --version
2425
26+
# start STDIO server
27+
kubernetes-mcp-server
28+
29+
# start a SSE server on port 8080
30+
kubernetes-mcp-server --sse-port 8080
31+
32+
# start a SSE server on port 8080 with a public host of example.com
33+
kubernetes-mcp-server --sse-port 8080 --sse-public-host example.com
34+
2535
# TODO: add more examples`,
2636
Run: func(cmd *cobra.Command, args []string) {
2737
if viper.GetBool("version") {
@@ -32,14 +42,27 @@ Kubernetes Model Context Protocol (MCP) server
3242
if err != nil {
3343
panic(err)
3444
}
45+
46+
var sseServer *server.SSEServer
47+
if ssePort := viper.GetInt("sse-port"); ssePort > 0 {
48+
sseServer = mcpServer.ServeSse(viper.GetString("sse-public-host"), ssePort)
49+
if err := sseServer.Start(fmt.Sprintf(":%d", ssePort)); err != nil {
50+
panic(err)
51+
}
52+
}
3553
if err := mcpServer.ServeStdio(); err != nil && !errors.Is(err, context.Canceled) {
3654
panic(err)
3755
}
56+
if sseServer != nil {
57+
_ = sseServer.Shutdown(cmd.Context())
58+
}
3859
},
3960
}
4061

4162
func init() {
4263
rootCmd.Flags().BoolP("version", "v", false, "Print version information and quit")
64+
rootCmd.Flags().IntP("sse-port", "", 0, "Start a SSE server on the specified port")
65+
rootCmd.Flags().StringP("sse-public-host", "", "localhost", "SSE Public host to use in the server")
4366
_ = viper.BindPFlags(rootCmd.Flags())
4467
}
4568

pkg/mcp/mcp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mcp
22

33
import (
4+
"fmt"
45
"github.com/manusa/kubernetes-mcp-server/pkg/kubernetes"
56
"github.com/manusa/kubernetes-mcp-server/pkg/version"
67
"github.com/mark3labs/mcp-go/mcp"
@@ -48,6 +49,10 @@ func (s *Server) ServeStdio() error {
4849
return server.ServeStdio(s.server)
4950
}
5051

52+
func (s *Server) ServeSse(publicHost string, port int) *server.SSEServer {
53+
return server.NewSSEServer(s.server, fmt.Sprintf("http://%s:%d", publicHost, port))
54+
}
55+
5156
func NewTextResult(content string, err error) *mcp.CallToolResult {
5257
if err != nil {
5358
return &mcp.CallToolResult{

0 commit comments

Comments
 (0)