diff --git a/internal/server/routed.go b/internal/server/routed.go index 3ad528669..de5f5159b 100644 --- a/internal/server/routed.go +++ b/internal/server/routed.go @@ -34,11 +34,17 @@ func CreateHTTPServerForRoutedMode(addr string, unifiedServer *UnifiedServer, ap }) mux.Handle("/mcp/.well-known/oauth-authorization-server", withResponseLogging(oauthHandler)) - // Create routes for all backends plus sys - allBackends := append([]string{"sys"}, unifiedServer.GetServerIDs()...) + // Create routes for all backends, plus sys only if DIFC is enabled + allBackends := unifiedServer.GetServerIDs() + if unifiedServer.IsDIFCEnabled() { + allBackends = append([]string{"sys"}, allBackends...) + logRouted.Printf("DIFC enabled: including sys in route registration") + } else { + logRouted.Printf("DIFC disabled: excluding sys from route registration") + } logRouted.Printf("Registering routes for %d backends: %v", len(allBackends), allBackends) - // Create a proxy for each backend server (including sys) + // Create a proxy for each backend server (sys included only when DIFC is enabled) for _, serverID := range allBackends { // Capture serverID for the closure backendID := serverID diff --git a/internal/server/routed_test.go b/internal/server/routed_test.go index 0ceae811c..d496d9bdb 100644 --- a/internal/server/routed_test.go +++ b/internal/server/routed_test.go @@ -367,3 +367,64 @@ func TestRoutedMode_SysToolsBackend_DIFCEnabled(t *testing.T) { } } } + +func TestRoutedMode_SysRouteNotExposed_DIFCDisabled(t *testing.T) { + // When DIFC is disabled (default), /mcp/sys route should NOT be registered + cfg := &config.Config{ + Servers: map[string]*config.ServerConfig{ + "github": {Command: "docker", Args: []string{}}, + }, + // EnableDIFC defaults to false, but explicitly set here for test clarity + EnableDIFC: false, + } + + ctx := context.Background() + us, err := NewUnified(ctx, cfg) + require.NoError(t, err, "NewUnified() failed") + defer us.Close() + + // Create routed mode server + httpServer := CreateHTTPServerForRoutedMode("127.0.0.1:0", us, "") + + // Try to access /mcp/sys route - should get 404 + req := httptest.NewRequest(http.MethodGet, "/mcp/sys", nil) + req.Header.Set("Authorization", "test-session") + w := httptest.NewRecorder() + + httpServer.Handler.ServeHTTP(w, req) + + // Should return 404 because the route is not registered + if w.Code != http.StatusNotFound { + t.Errorf("Expected status 404 for /mcp/sys when DIFC is disabled, got %d", w.Code) + } +} + +func TestRoutedMode_SysRouteExposed_DIFCEnabled(t *testing.T) { + // When DIFC is enabled, /mcp/sys route SHOULD be registered + cfg := &config.Config{ + Servers: map[string]*config.ServerConfig{ + "github": {Command: "docker", Args: []string{}}, + }, + EnableDIFC: true, // Enable DIFC + } + + ctx := context.Background() + us, err := NewUnified(ctx, cfg) + require.NoError(t, err, "NewUnified() failed") + defer us.Close() + + // Create routed mode server + httpServer := CreateHTTPServerForRoutedMode("127.0.0.1:0", us, "") + + // Try to access /mcp/sys route - should NOT get 404 + req := httptest.NewRequest(http.MethodGet, "/mcp/sys", nil) + req.Header.Set("Authorization", "test-session") + w := httptest.NewRecorder() + + httpServer.Handler.ServeHTTP(w, req) + + // Should NOT return 404 because the route should be registered + if w.Code == http.StatusNotFound { + t.Errorf("Expected /mcp/sys route to be registered when DIFC is enabled, but got 404") + } +} diff --git a/internal/server/unified.go b/internal/server/unified.go index 614a2e814..1faa9a012 100644 --- a/internal/server/unified.go +++ b/internal/server/unified.go @@ -766,3 +766,8 @@ func (us *UnifiedServer) SetTestMode(enabled bool) { func (us *UnifiedServer) ShouldExit() bool { return !us.testMode } + +// IsDIFCEnabled returns whether DIFC is enabled +func (us *UnifiedServer) IsDIFCEnabled() bool { + return us.enableDIFC +}