Skip to content

Commit 52f01ac

Browse files
authored
Make transparent proxy healthcheck configurable (#1138)
1 parent e665235 commit 52f01ac

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

cmd/thv/app/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func proxyCmdFunc(cmd *cobra.Command, args []string) error {
214214
port, proxyTargetURI)
215215

216216
// Create the transparent proxy with middlewares
217-
proxy := transparent.NewTransparentProxy(proxyHost, port, serverName, proxyTargetURI, nil, middlewares...)
217+
proxy := transparent.NewTransparentProxy(proxyHost, port, serverName, proxyTargetURI, nil, false, middlewares...)
218218
if err := proxy.Start(ctx); err != nil {
219219
return fmt.Errorf("failed to start proxy: %v", err)
220220
}

pkg/transport/http.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ func (t *HTTPTransport) Start(ctx context.Context) error {
229229
logger.Infof("Setting up transparent proxy to forward from host port %d to %s",
230230
t.proxyPort, targetURI)
231231

232-
// Create the transparent proxy with middlewares
233-
t.proxy = transparent.NewTransparentProxy(t.host, t.proxyPort, t.containerName, targetURI, t.prometheusHandler, t.middlewares...)
232+
// Create the transparent proxy with middlewares (enable healthcheck for MCP servers)
233+
t.proxy = transparent.NewTransparentProxy(
234+
t.host, t.proxyPort, t.containerName, targetURI, t.prometheusHandler, true, t.middlewares...)
234235
if err := t.proxy.Start(ctx); err != nil {
235236
return err
236237
}

pkg/transport/proxy/transparent/transparent_proxy.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func NewTransparentProxy(
7070
containerName string,
7171
targetURI string,
7272
prometheusHandler http.Handler,
73+
enableHealthCheck bool,
7374
middlewares ...types.Middleware,
7475
) *TransparentProxy {
7576
proxy := &TransparentProxy{
@@ -83,9 +84,11 @@ func NewTransparentProxy(
8384
sessionManager: session.NewManager(30*time.Minute, session.NewProxySession),
8485
}
8586

86-
// Create MCP pinger and health checker
87-
mcpPinger := NewMCPPinger(targetURI)
88-
proxy.healthChecker = healthcheck.NewHealthChecker("sse", mcpPinger)
87+
// Create MCP pinger and health checker only if enabled
88+
if enableHealthCheck {
89+
mcpPinger := NewMCPPinger(targetURI)
90+
proxy.healthChecker = healthcheck.NewHealthChecker("sse", mcpPinger)
91+
}
8992

9093
return proxy
9194
}
@@ -273,8 +276,10 @@ func (p *TransparentProxy) Start(ctx context.Context) error {
273276
finalHandler.ServeHTTP(w, r)
274277
})
275278

276-
// Add health check endpoint (no middlewares)
277-
mux.Handle("/health", p.healthChecker)
279+
// Add health check endpoint (no middlewares) only if health checker is enabled
280+
if p.healthChecker != nil {
281+
mux.Handle("/health", p.healthChecker)
282+
}
278283

279284
// Add Prometheus metrics endpoint if handler is provided (no middlewares)
280285
if p.prometheusHandler != nil {
@@ -299,8 +304,10 @@ func (p *TransparentProxy) Start(ctx context.Context) error {
299304
}
300305
}()
301306

302-
// Start health-check monitoring
303-
go p.monitorHealth(ctx)
307+
// Start health-check monitoring only if health checker is enabled
308+
if p.healthChecker != nil {
309+
go p.monitorHealth(ctx)
310+
}
304311

305312
return nil
306313
}

pkg/transport/proxy/transparent/transparent_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func init() {
2020

2121
func TestStreamingSessionIDDetection(t *testing.T) {
2222
t.Parallel()
23-
proxy := NewTransparentProxy("127.0.0.1", 0, "test", "http://example.com", nil)
23+
proxy := NewTransparentProxy("127.0.0.1", 0, "test", "http://example.com", nil, true)
2424
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2525
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
2626
w.WriteHeader(200)
@@ -77,7 +77,7 @@ func createBasicProxy(p *TransparentProxy, targetURL *url.URL) *httputil.Reverse
7777
func TestNoSessionIDInNonSSE(t *testing.T) {
7878
t.Parallel()
7979

80-
p := NewTransparentProxy("127.0.0.1", 0, "test", "", nil)
80+
p := NewTransparentProxy("127.0.0.1", 0, "test", "", nil, false)
8181

8282
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
8383
// Set both content-type and also optionally MCP header to test behavior
@@ -102,7 +102,7 @@ func TestNoSessionIDInNonSSE(t *testing.T) {
102102
func TestHeaderBasedSessionInitialization(t *testing.T) {
103103
t.Parallel()
104104

105-
p := NewTransparentProxy("127.0.0.1", 0, "test", "", nil)
105+
p := NewTransparentProxy("127.0.0.1", 0, "test", "", nil, false)
106106

107107
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
108108
// Set both content-type and also optionally MCP header to test behavior

0 commit comments

Comments
 (0)