Skip to content

Commit a4cb11b

Browse files
Reset reconnection attempt counter when server makes progress
When the server sends new events before closing the stream (indicated by a changed last_event_id), the reconnection is intentional and the attempt counter should reset to 0. Only increment the counter when no new events were received, which indicates an actual failure to make progress. This prevents legitimate server-initiated reconnection patterns from being penalized by the retry limit while still protecting against infinite loops when the server returns empty responses. Github-Issue: #2397
1 parent 2efbb42 commit a4cb11b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/mcp/client/streamable_http.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@ async def _handle_reconnection(
423423

424424
# Stream ended again without response - reconnect again
425425
logger.info("SSE stream disconnected, reconnecting...")
426-
await self._handle_reconnection(ctx, reconnect_last_event_id, reconnect_retry_ms, attempt + 1)
426+
# Reset attempt counter if server made progress (sent new events),
427+
# otherwise count as a failed attempt
428+
next_attempt = 0 if reconnect_last_event_id != last_event_id else attempt + 1
429+
await self._handle_reconnection(ctx, reconnect_last_event_id, reconnect_retry_ms, next_attempt)
427430
except Exception as e: # pragma: no cover
428431
logger.debug(f"Reconnection failed: {e}")
429432
# Try to reconnect again if we still have an event ID

0 commit comments

Comments
 (0)