π The fast, Pythonic way to build MCP servers and clients.
Guard default progress handler against zero totals (#3432)
Jeremiah Lowin44b8238e40c4
diff --git a/src/fastmcp/client/progress.py b/src/fastmcp/client/progress.pyindex 2470f18ce..826d2cb99 100644--- a/src/fastmcp/client/progress.py+++ b/src/fastmcp/client/progress.py@@ -21,10 +21,13 @@ async def default_progress_handler(total: Optional total expected valuemessage: Optional status message"""- if total is not None:+ if total not in (None, 0):# We have both progress and totalpercent = (progress / total) * 100progress_str = f"{progress}/{total} ({percent:.1f}%)"+ elif total == 0:+ # Avoid division by zero when a server reports an invalid total.+ progress_str = f"{progress}/{total}"else:# We only have progressprogress_str = f"{progress}"diff --git a/tests/client/test_progress.py b/tests/client/test_progress.pyindex 63244df7c..946c5b65c 100644--- a/tests/client/test_progress.py+++ b/tests/client/test_progress.py@@ -68,3 +68,9 @@ async def test_progress_handler_supplied_on_tool_call_overrides_default(await client.call_tool("progress_tool", {}, progress_handler=progress_handler)assert PROGRESS_MESSAGES == EXPECTED_PROGRESS_MESSAGES+++async def test_default_progress_handler_handles_zero_total() -> None:+ from fastmcp.client.progress import default_progress_handler++ await default_progress_handler(progress=1, total=0, message="starting")