Skip to content

Commit 8478204

Browse files
authored
feat(http): streamable HTTP transport
1 parent 2a9dddf commit 8478204

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ uvx kubernetes-mcp-server@latest --help
157157

158158
| Option | Description |
159159
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
160-
| `--sse-port` | Starts the MCP server in Server-Sent Event (SSE) mode and listens on the specified port. |
160+
| `--http-port` | Starts the MCP server in Streamable HTTP mode and listens on the specified port (path /mcp). |
161+
| `--sse-port` | Starts the MCP server in Server-Sent Event (SSE) mode and listens on the specified port (path /sse). |
161162
| `--log-level` | Sets the logging level (values [from 0-9](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md)). Similar to [kubectl logging levels](https://kubernetes.io/docs/reference/kubectl/quick-reference/#kubectl-output-verbosity-and-debugging). |
162163
| `--kubeconfig` | Path to the Kubernetes configuration file. If not provided, it will try to resolve the configuration (in-cluster, default location, etc.). |
163164
| `--list-output` | Output format for resource list operations (one of: yaml, table) (default "table") |

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/manusa/kubernetes-mcp-server/pkg/mcp"
88
"github.com/manusa/kubernetes-mcp-server/pkg/output"
99
"github.com/manusa/kubernetes-mcp-server/pkg/version"
10-
"github.com/mark3labs/mcp-go/server"
1110
"github.com/spf13/cobra"
1211
"github.com/spf13/viper"
1312
"golang.org/x/net/context"
@@ -74,16 +73,27 @@ Kubernetes Model Context Protocol (MCP) server
7473
}
7574
defer mcpServer.Close()
7675

77-
var sseServer *server.SSEServer
78-
if ssePort := viper.GetInt("sse-port"); ssePort > 0 {
79-
sseServer = mcpServer.ServeSse(viper.GetString("sse-base-url"))
76+
ssePort := viper.GetInt("sse-port")
77+
if ssePort > 0 {
78+
sseServer := mcpServer.ServeSse(viper.GetString("sse-base-url"))
8079
defer func() { _ = sseServer.Shutdown(cmd.Context()) }()
81-
klog.V(0).Infof("SSE server starting on port %d", ssePort)
80+
klog.V(0).Infof("SSE server starting on port %d and path /sse", ssePort)
8281
if err := sseServer.Start(fmt.Sprintf(":%d", ssePort)); err != nil {
8382
klog.Errorf("Failed to start SSE server: %s", err)
8483
return
8584
}
8685
}
86+
87+
httpPort := viper.GetInt("http-port")
88+
if httpPort > 0 {
89+
httpServer := mcpServer.ServeHTTP()
90+
klog.V(0).Infof("Streaming HTTP server starting on port %d and path /mcp", httpPort)
91+
if err := httpServer.Start(fmt.Sprintf(":%d", httpPort)); err != nil {
92+
klog.Errorf("Failed to start streaming HTTP server: %s", err)
93+
return
94+
}
95+
}
96+
8797
if err := mcpServer.ServeStdio(); err != nil && !errors.Is(err, context.Canceled) {
8898
panic(err)
8999
}
@@ -115,6 +125,7 @@ func flagInit() {
115125
rootCmd.Flags().BoolP("version", "v", false, "Print version information and quit")
116126
rootCmd.Flags().IntP("log-level", "", 0, "Set the log level (from 0 to 9)")
117127
rootCmd.Flags().IntP("sse-port", "", 0, "Start a SSE server on the specified port")
128+
rootCmd.Flags().IntP("http-port", "", 0, "Start a streamable HTTP server on the specified port")
118129
rootCmd.Flags().StringP("sse-base-url", "", "", "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
119130
rootCmd.Flags().StringP("kubeconfig", "", "", "Path to the kubeconfig file to use for authentication")
120131
rootCmd.Flags().String("profile", "full", "MCP profile to use (one of: "+strings.Join(mcp.ProfileNames, ", ")+")")

pkg/mcp/mcp.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ func (s *Server) ServeSse(baseUrl string) *server.SSEServer {
8282
return server.NewSSEServer(s.server, options...)
8383
}
8484

85+
func (s *Server) ServeHTTP() *server.StreamableHTTPServer {
86+
options := []server.StreamableHTTPOption{
87+
server.WithHTTPContextFunc(contextFunc),
88+
}
89+
return server.NewStreamableHTTPServer(s.server, options...)
90+
}
91+
8592
func (s *Server) Close() {
8693
if s.k != nil {
8794
s.k.Close()

0 commit comments

Comments
 (0)