Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions internal/health/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ func CalculateHealth(input HealthCalculatorInput, cfg *HealthCalculatorConfig) *
Detail: input.LastError,
Action: action,
}
case "pending auth", "pending_auth":
// Parked awaiting user login (#1013): the client stopped redialing on
// purpose, so this never "resolves on its own" — it is always an
// attention item with a Sign-in CTA, regardless of OAuthRequired (a
// header-auth server whose token expired is parked the same way).
level, action, summary := oauthAttentionState(input.LastError)
return &contracts.HealthStatus{
Level: level,
AdminState: StateEnabled,
Summary: summary,
Detail: input.LastError,
Action: action,
}
case "connecting", "idle":
return &contracts.HealthStatus{
Level: LevelHealthy,
Expand Down
32 changes: 32 additions & 0 deletions internal/health/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ func TestCalculateHealth_OAuthLoginRequired(t *testing.T) {
}
}

// TestCalculateHealth_PendingAuthParked verifies that a server parked in
// PendingAuth (the client stopped redialing until a human signs in, #1013) is an
// attention item with a Sign-in CTA — not a silently healthy server. The state
// string arrives as ConnectionState.String() ("Pending Auth"), possibly
// lowercased by the supervisor's stateview.
func TestCalculateHealth_PendingAuthParked(t *testing.T) {
loginErr := "OAuth authentication required for github: login available via Web UI, system tray menu, or 'mcpproxy auth login' CLI command"

for _, state := range []string{"Pending Auth", "pending auth", "pending_auth"} {
// First-time sign-in: amber, actionable.
result := CalculateHealth(HealthCalculatorInput{
Name: "test-server",
Enabled: true,
State: state,
LastError: loginErr,
}, nil)
assert.Equal(t, LevelDegraded, result.Level, "state=%s", state)
assert.Equal(t, "Sign-in required", result.Summary, "state=%s", state)
assert.Equal(t, ActionLogin, result.Action, "state=%s", state)

// A broken stored token stays red.
reauth := CalculateHealth(HealthCalculatorInput{
Name: "test-server",
Enabled: true,
State: state,
LastError: "OAuth authentication required for slack: server error with stored token - re-login available via Web UI",
}, nil)
assert.Equal(t, LevelUnhealthy, reauth.Level, "state=%s", state)
assert.Equal(t, ActionLogin, reauth.Action, "state=%s", state)
}
}

// TestCalculateHealth_OAuthReauthRequired verifies that a server whose stored
// token broke (re-auth needed) stays unhealthy/red with a login action — it was
// working before, so this is a regression the user should treat as red (MCP-1820).
Expand Down
9 changes: 5 additions & 4 deletions internal/runtime/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,11 @@ func (s *Supervisor) computeReconcilePlan(configSnapshot *configsvc.Snapshot, ac
plan.Actions[name] = ActionNone
} else if actual, ok := actualStates[name]; ok && !actual.ConnectionInfo.ShouldAutoReconnect(time.Now()) {
// Respect the client's retry policy: exponential backoff after
// consecutive failures, gave-up after MaxConnectionRetries, and
// PendingAuth servers waiting on user OAuth login. Without this
// gate the periodic 30s reconciliation re-dials a dead upstream
// forever, hammering the remote server (~3 requests per tick).
// consecutive failures, the coarse OAuth ladder, half-hourly
// probes once the client gave up, and PendingAuth servers
// parked waiting on user OAuth login. Without this gate the
// periodic 30s reconciliation re-dials a dead upstream forever,
// hammering the remote server (~3 requests per tick).
s.logger.Debug("Skipping auto-reconnect (backoff/pending-auth)",
zap.String("server", name),
zap.String("state", actual.ConnectionInfo.State.String()),
Expand Down
52 changes: 41 additions & 11 deletions internal/runtime/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ func TestSupervisor_Reconcile_RespectsRetryBackoff(t *testing.T) {

// First reconciliation - server is added and connected
require.NoError(t, supervisor.reconcile(configSvc.Current()))
time.Sleep(50 * time.Millisecond)
supervisor.actionWg.Wait()

setConnectionState := func(connected bool, info *types.ConnectionInfo) {
mockUpstream.mu.Lock()
Expand All @@ -1148,6 +1148,15 @@ func TestSupervisor_Reconcile_RespectsRetryBackoff(t *testing.T) {
defer mockUpstream.mu.Unlock()
return mockUpstream.connected["flaky-server"]
}
// reconcile dispatches its actions into goroutines tracked by actionWg, so
// draining that group is an exact barrier: after it returns, either the
// connect ran or none was planned. A fixed sleep would let the negative
// assertions below pass before an erroneous redial had a chance to execute.
reconcileAndDrain := func() {
t.Helper()
require.NoError(t, supervisor.reconcile(configSvc.Current()))
supervisor.actionWg.Wait()
}

// Simulate a connection failure with the backoff window still open:
// reconciliation must NOT re-dial.
Expand All @@ -1156,29 +1165,39 @@ func TestSupervisor_Reconcile_RespectsRetryBackoff(t *testing.T) {
RetryCount: 5,
LastRetryTime: time.Now(),
})
require.NoError(t, supervisor.reconcile(configSvc.Current()))
time.Sleep(50 * time.Millisecond)
reconcileAndDrain()
require.False(t, isConnected(), "supervisor re-dialed a failed server inside its backoff window")

// A server that gave up after max retries must not be re-dialed either.
// A server that gave up after max retries must not be re-dialed either,
// until the give-up probe interval has elapsed (see below).
setConnectionState(false, &types.ConnectionInfo{
State: types.StateError,
RetryCount: types.MaxConnectionRetries,
GaveUp: true,
LastRetryTime: time.Now().Add(-time.Hour),
LastRetryTime: time.Now().Add(-time.Minute),
})
require.NoError(t, supervisor.reconcile(configSvc.Current()))
time.Sleep(50 * time.Millisecond)
reconcileAndDrain()
require.False(t, isConnected(), "supervisor re-dialed a server that gave up after max retries")

// An OAuth-classified failure is paced by the OAuth ladder, which bumps
// OAuthRetryCount and never RetryCount — without that gate it reads as
// "no failures yet" and is re-dialed on every tick forever (#1013).
setConnectionState(false, &types.ConnectionInfo{
State: types.StateError,
IsOAuthError: true,
OAuthRetryCount: 2,
LastOAuthAttempt: time.Now().Add(-time.Minute),
})
reconcileAndDrain()
require.False(t, isConnected(), "supervisor re-dialed a server inside its OAuth backoff window")

// A server parked in PendingAuth (waiting for user OAuth login) must not be
// re-dialed - each attempt fires real requests at the upstream and cannot
// succeed until the user completes the login.
setConnectionState(false, &types.ConnectionInfo{
State: types.StatePendingAuth,
})
require.NoError(t, supervisor.reconcile(configSvc.Current()))
time.Sleep(50 * time.Millisecond)
reconcileAndDrain()
require.False(t, isConnected(), "supervisor re-dialed a server pending OAuth login")

// Once the backoff window has elapsed, reconciliation reconnects as before.
Expand All @@ -1187,7 +1206,18 @@ func TestSupervisor_Reconcile_RespectsRetryBackoff(t *testing.T) {
RetryCount: 3,
LastRetryTime: time.Now().Add(-10 * time.Second), // backoff for 3 failures is 4s
})
require.NoError(t, supervisor.reconcile(configSvc.Current()))
time.Sleep(50 * time.Millisecond)
reconcileAndDrain()
require.True(t, isConnected(), "supervisor did not reconnect after the backoff window elapsed")

// A given-up server is still probed once per GaveUpProbeInterval, so an
// outage longer than the retry ladder (sleep, VPN, maintenance) self-heals
// instead of leaving the upstream silently dead until a human notices.
setConnectionState(false, &types.ConnectionInfo{
State: types.StateError,
RetryCount: types.MaxConnectionRetries,
GaveUp: true,
LastRetryTime: time.Now().Add(-types.GaveUpProbeInterval - time.Minute),
})
reconcileAndDrain()
require.True(t, isConnected(), "supervisor never probes a given-up server again")
}
5 changes: 5 additions & 0 deletions internal/server/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5723,6 +5723,11 @@ func (p *MCPProxyServer) monitorConnectionStatus(ctx context.Context, serverName
return "ready", "Server connected and ready"
case types.StateError:
return "error", fmt.Sprintf("Server connection failed: %v", connectionInfo.LastError)
case types.StatePendingAuth:
// Parked awaiting user login (#1013): waiting out the monitor
// timeout would just stall the caller — nothing will change
// until a human signs in.
return statusError, fmt.Sprintf("Server connection failed: %v", connectionInfo.LastError)
case types.StateDisconnected:
// If server is explicitly disconnected and enabled is false, return disabled
for _, serverConfig := range p.config.Servers {
Expand Down
13 changes: 10 additions & 3 deletions internal/upstream/core/connection_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ func (e *ErrOAuthPending) Code() diagnostics.Code {
return diagnostics.OAuthLoginRequired
}

// IsOAuthPending checks if an error is an ErrOAuthPending
// IsOAuthPending checks if an error is (or wraps) an ErrOAuthPending.
//
// It MUST unwrap: the pending error is raised inside an auth strategy and then
// wrapped twice on its way out — connectHTTP/connectSSE add "all authentication
// strategies failed, last error: %w" and Connect adds "failed to connect: %w".
// With a bare type assertion the check therefore never fired in production, and
// every login-blocked server fell through to the generic error path and was
// re-dialed instead of parked (#1013).
func IsOAuthPending(err error) bool {
_, ok := err.(*ErrOAuthPending)
return ok
var pending *ErrOAuthPending
return errors.As(err, &pending)
}

// OAuthStartResult contains the result of initiating an OAuth flow.
Expand Down
14 changes: 13 additions & 1 deletion internal/upstream/core/oauth_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"errors"
"fmt"
"testing"

"github.com/smart-mcp-proxy/mcpproxy-go/internal/diagnostics"
Expand Down Expand Up @@ -217,12 +218,23 @@ func TestIsOAuthPending(t *testing.T) {
expected: false,
},
{
name: "wrapped ErrOAuthPending returns false",
name: "flattened-to-string ErrOAuthPending returns false",
err: errors.New("wrapped: " + (&ErrOAuthPending{
ServerName: "slack",
}).Error()),
expected: false,
},
{
// The production chain: connectHTTP/connectSSE wrap the strategy's
// error and Connect wraps that again. A bare type assertion made the
// pending check unreachable, so login-blocked servers were re-dialed
// forever instead of parked (#1013).
name: "%w-wrapped ErrOAuthPending returns true",
err: fmt.Errorf("failed to connect: %w",
fmt.Errorf("all authentication strategies failed, last error: %w",
&ErrOAuthPending{ServerName: "slack", Message: "login available via Web UI"})),
expected: true,
},
}

for _, tt := range tests {
Expand Down
10 changes: 6 additions & 4 deletions internal/upstream/managed/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (mc *Client) Connect(ctx context.Context) error {
// that died silently (e.g., HTTP server timeout). Without this, core client
// rejects the connect attempt with "client already connected" error.
currentState := mc.StateManager.GetState()
if currentState == types.StateError || currentState == types.StateDisconnected {
if currentState == types.StateError || currentState == types.StateDisconnected || currentState == types.StatePendingAuth {
mc.logger.Debug("Disconnecting core client before reconnect to clear stale state",
zap.String("server", mc.GetConfig().Name),
zap.String("from_state", currentState.String()))
Expand Down Expand Up @@ -318,9 +318,11 @@ func (mc *Client) Connect(ctx context.Context) error {
if core.IsOAuthPending(connectErr) {
mc.logger.Info("⏳ OAuth authentication pending user action",
zap.String("server", mc.GetConfig().Name))
// Transition to PendingAuth state instead of Error
mc.StateManager.TransitionTo(types.StatePendingAuth)
mc.StateManager.SetError(connectErr)
// Park in PendingAuth. SetPendingAuth (not TransitionTo + SetError:
// SetError forces StateError and would immediately undo the park, so
// the supervisor kept redialing a login-blocked server every 30s —
// #1013).
mc.StateManager.SetPendingAuth(connectErr)
return fmt.Errorf("OAuth authentication pending: %w", connectErr)
}
// Check if this is an OAuth authorization requirement (not an error)
Expand Down
Loading
Loading