Skip to content

Commit eaad775

Browse files
committed
fix: improve SSE fallback logic in auto transport mode
- Fix issue where SSE fallback was not attempted when HTTP transport failed - Specific error types (404, 405, CORS) now correctly trigger SSE fallback - Allow SSE fallback even when component state is 'failed' from previous attempts - Maintain proper error handling for non-fallback HTTP errors - All 6 transport scenarios now pass integration tests The core issue was that failConnection() was being called for HTTP errors before returning 'fallback', which set the component state to 'failed' and prevented the orchestration logic from attempting SSE fallback. Now only specific HTTP errors that should trigger fallback avoid calling failConnection immediately, allowing the SSE transport to be attempted as expected.
1 parent fed35bc commit eaad775

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/react/useMcp.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,10 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
369369
}
370370

371371
// Handle other connection errors
372-
// Use stable failConnection
372+
// For HTTP transport, consider fallback only for specific error types
373+
// "Not connected" errors should still be treated as failures, not fallback triggers
373374
failConnection(`Failed to connect via ${transportType.toUpperCase()}: ${errorMessage}`, errorInstance)
374-
return 'fallback' // If our logic above is wrong, we'd prevent HTTPs servers from ever working. So always fall back as a last resort.
375+
return 'failed'
375376
}
376377
} // End of tryConnectWithTransport helper
377378

@@ -392,10 +393,18 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
392393
// Auto mode - try HTTP first, fallback to SSE
393394
addLog('debug', 'Using auto transport mode (HTTP with SSE fallback)')
394395
const httpResult = await tryConnectWithTransport('http')
395-
// 2. Try SSE only if HTTP requested fallback and we haven't failed/redirected
396-
if (httpResult === 'fallback' && isMountedRef.current && stateRef.current !== 'failed' && stateRef.current !== 'authenticating') {
396+
397+
// Try SSE only if HTTP requested fallback and we haven't redirected for auth
398+
// Allow fallback even if state is 'failed' from a previous HTTP attempt in auto mode
399+
if (httpResult === 'fallback' && isMountedRef.current && stateRef.current !== 'authenticating') {
400+
addLog('info', 'HTTP failed, attempting SSE fallback...')
397401
const sseResult = await tryConnectWithTransport('sse')
398402
finalStatus = sseResult // Use SSE result as final status
403+
404+
// If SSE also failed, we need to properly fail the connection since HTTP didn't call failConnection
405+
if (sseResult === 'failed' && isMountedRef.current) {
406+
// SSE failure already called failConnection, so we don't need to do anything else
407+
}
399408
} else {
400409
finalStatus = httpResult // Use HTTP result if no fallback was needed/possible
401410
}

0 commit comments

Comments
 (0)