|
| 1 | +package mcpapp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 11 | + ctrl "sigs.k8s.io/controller-runtime" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + // DefaultHTTPAddr is the default listen address used by MCP HTTP mode. |
| 16 | + DefaultHTTPAddr = ":8090" |
| 17 | + // streamableHTTPSessionTimeout ensures abandoned MCP streamable HTTP sessions are reclaimed. |
| 18 | + streamableHTTPSessionTimeout = 15 * time.Minute |
| 19 | +) |
| 20 | + |
| 21 | +var setupLog = ctrl.Log.WithName("setup") |
| 22 | + |
| 23 | +// RunHTTP starts the MCP server using streamable HTTP transport. |
| 24 | +func RunHTTP(ctx context.Context) error { |
| 25 | + if ctx == nil { |
| 26 | + return fmt.Errorf("assertion failed: context must not be nil") |
| 27 | + } |
| 28 | + |
| 29 | + k8sClient, clientset, err := newClients() |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + server := NewServer(k8sClient, clientset) |
| 35 | + if server == nil { |
| 36 | + return fmt.Errorf("assertion failed: MCP server is nil after successful construction") |
| 37 | + } |
| 38 | + |
| 39 | + mcpHandler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { |
| 40 | + return server |
| 41 | + }, &mcp.StreamableHTTPOptions{ |
| 42 | + SessionTimeout: streamableHTTPSessionTimeout, |
| 43 | + }) |
| 44 | + if mcpHandler == nil { |
| 45 | + return fmt.Errorf("assertion failed: MCP HTTP handler is nil after successful construction") |
| 46 | + } |
| 47 | + |
| 48 | + mux := http.NewServeMux() |
| 49 | + mux.Handle("/mcp", mcpHandler) |
| 50 | + mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { |
| 51 | + w.WriteHeader(http.StatusOK) |
| 52 | + _, _ = w.Write([]byte("ok")) |
| 53 | + }) |
| 54 | + mux.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) { |
| 55 | + w.WriteHeader(http.StatusOK) |
| 56 | + _, _ = w.Write([]byte("ok")) |
| 57 | + }) |
| 58 | + |
| 59 | + httpServer := &http.Server{ |
| 60 | + Addr: DefaultHTTPAddr, |
| 61 | + Handler: mux, |
| 62 | + ReadHeaderTimeout: 5 * time.Second, |
| 63 | + } |
| 64 | + |
| 65 | + listenErr := make(chan error, 1) |
| 66 | + go func() { |
| 67 | + listenErr <- httpServer.ListenAndServe() |
| 68 | + }() |
| 69 | + |
| 70 | + setupLog.Info("MCP HTTP server listening on " + DefaultHTTPAddr) |
| 71 | + |
| 72 | + select { |
| 73 | + case err := <-listenErr: |
| 74 | + if err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 75 | + return fmt.Errorf("run MCP HTTP server: %w", err) |
| 76 | + } |
| 77 | + return nil |
| 78 | + case <-ctx.Done(): |
| 79 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 80 | + defer cancel() |
| 81 | + if err := httpServer.Shutdown(shutdownCtx); err != nil { |
| 82 | + return fmt.Errorf("shutdown MCP HTTP server: %w", err) |
| 83 | + } |
| 84 | + err := <-listenErr |
| 85 | + if err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 86 | + return fmt.Errorf("run MCP HTTP server: %w", err) |
| 87 | + } |
| 88 | + return nil |
| 89 | + } |
| 90 | +} |
0 commit comments