diff --git a/src/fast_agent/commands/handlers/mcp_runtime.py b/src/fast_agent/commands/handlers/mcp_runtime.py index 48f3bd6c..85e3243f 100644 --- a/src/fast_agent/commands/handlers/mcp_runtime.py +++ b/src/fast_agent/commands/handlers/mcp_runtime.py @@ -2,6 +2,7 @@ from __future__ import annotations +import math import re import shlex from dataclasses import dataclass @@ -123,6 +124,10 @@ def parse_connect_input(target_text: str) -> ParsedMcpConnectInput: if idx >= len(tokens): raise ValueError("Missing value for --timeout") timeout_seconds = float(tokens[idx]) + if not math.isfinite(timeout_seconds) or timeout_seconds <= 0: + raise ValueError( + "Invalid value for --timeout: expected a finite number greater than 0" + ) elif token == "--oauth": trigger_oauth = True elif token == "--no-oauth": diff --git a/tests/unit/fast_agent/commands/test_mcp_runtime_handlers.py b/tests/unit/fast_agent/commands/test_mcp_runtime_handlers.py index 615071f8..8a7ed2b1 100644 --- a/tests/unit/fast_agent/commands/test_mcp_runtime_handlers.py +++ b/tests/unit/fast_agent/commands/test_mcp_runtime_handlers.py @@ -152,6 +152,15 @@ async def attach_mcp_server(self, agent_name, server_name, server_config=None, o "for this connection mode." ) + +@pytest.mark.parametrize("raw_timeout", ["nan", "inf", "-inf", "0", "-1"]) +def test_parse_connect_input_rejects_non_finite_or_non_positive_timeout( + raw_timeout: str, +) -> None: + with pytest.raises(ValueError, match="--timeout"): + mcp_runtime.parse_connect_input(f"npx demo-server --timeout {raw_timeout}") + + @pytest.mark.asyncio async def test_handle_mcp_connect_and_disconnect() -> None: manager = _Manager()