Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/kubernetes-mcp-server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strconv"
"strings"

"github.com/manusa/kubernetes-mcp-server/pkg/middleware"

"github.com/spf13/cobra"

"k8s.io/cli-runtime/pkg/genericiooptions"
Expand Down Expand Up @@ -206,9 +208,11 @@ func (m *MCPServerOptions) Run() error {

if m.StaticConfig.Port != "" {
mux := http.NewServeMux()
wrappedMux := middleware.RequestMiddleware(mux)

httpServer := &http.Server{
Addr: ":" + m.StaticConfig.Port,
Handler: mux,
Handler: wrappedMux,
}

sseServer := mcpServer.ServeSse(m.SSEBaseUrl, httpServer)
Expand Down
1 change: 1 addition & 0 deletions pkg/mcp/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (s *Server) ServeHTTP(httpServer *http.Server) *server.StreamableHTTPServer
options := []server.StreamableHTTPOption{
server.WithHTTPContextFunc(contextFunc),
server.WithStreamableHTTPServer(httpServer),
server.WithStateLess(true),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to the changes in this PR. I updated this, because I think we want stateless streamable http server. If it is suggested that this change should be in a different PR, I can remove it from here.

}
return server.NewStreamableHTTPServer(s.server, options...)
}
Expand Down
61 changes: 61 additions & 0 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package middleware
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this scope is too narrow, what do you think about a broader http package to include everything related to our customized http server?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firstly, I renamed it to http to be honest. But this name collides with the native Go http package. But I can rename it to, if you want to. And we can use package name as internalhttp.

Copy link
Member Author

@ardaguclu ardaguclu Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine whatever you decide.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call, we can always change the name in the future.

For me it's uncomfortable to see the http server logic in root.go and I think that we should probably move that somewhere else (hence the broader scope for this package, to cover everything related to http/internalhttp).

In any case, this can be covered in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to http, because it is indeed better. We can move server logic under http in a followup PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move server logic in http package, when I wire the sig terms to the server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened this #164


import (
"bufio"
"net"
"net/http"
"time"

"k8s.io/klog/v2"
)

func RequestMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

lrw := &loggingResponseWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
}

next.ServeHTTP(lrw, r)

duration := time.Since(start)
klog.V(5).Infof("%s %s %d %v", r.Method, r.URL.Path, lrw.statusCode, duration)
})
}

type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
headerWritten bool
}

func (lrw *loggingResponseWriter) WriteHeader(code int) {
if !lrw.headerWritten {
lrw.statusCode = code
lrw.headerWritten = true
lrw.ResponseWriter.WriteHeader(code)
}
}

func (lrw *loggingResponseWriter) Write(b []byte) (int, error) {
if !lrw.headerWritten {
lrw.statusCode = http.StatusOK
lrw.headerWritten = true
}
return lrw.ResponseWriter.Write(b)
}

func (lrw *loggingResponseWriter) Flush() {
if flusher, ok := lrw.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}

func (lrw *loggingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := lrw.ResponseWriter.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, http.ErrNotSupported
}
Loading