diff --git a/integrations/rustchain-mcp/client.py b/integrations/rustchain-mcp/client.py index b9847046b..ab4bc150c 100644 --- a/integrations/rustchain-mcp/client.py +++ b/integrations/rustchain-mcp/client.py @@ -39,8 +39,18 @@ # Default configuration RUSTCHAIN_API_BASE = os.getenv("RUSTCHAIN_API_BASE", "https://50.28.86.131") RUSTCHAIN_NODE_URL = os.getenv("RUSTCHAIN_NODE_URL", "https://50.28.86.131:5000") -REQUEST_TIMEOUT = int(os.getenv("RUSTCHAIN_TIMEOUT", "30")) -RETRY_COUNT = int(os.getenv("RUSTCHAIN_RETRY", "2")) + + +def _safe_int_env(name: str, default: int) -> int: + try: + return int(os.getenv(name, str(default))) + except (TypeError, ValueError): + logger.warning("Invalid %s env value; using default %s", name, default) + return default + + +REQUEST_TIMEOUT = _safe_int_env("RUSTCHAIN_TIMEOUT", 30) +RETRY_COUNT = _safe_int_env("RUSTCHAIN_RETRY", 2) class RustChainClient: diff --git a/integrations/rustchain-mcp/tests/test_client.py b/integrations/rustchain-mcp/tests/test_client.py index b8dd768e9..8a452ba8b 100644 --- a/integrations/rustchain-mcp/tests/test_client.py +++ b/integrations/rustchain-mcp/tests/test_client.py @@ -5,6 +5,7 @@ Unit tests for RustChainClient with mocked HTTP responses. """ +import importlib import os import sys from unittest.mock import AsyncMock, MagicMock, patch @@ -14,6 +15,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) try: + import rustchain_mcp.client as client_module from rustchain_mcp.client import RustChainClient from rustchain_mcp.schemas import ( APIError, @@ -24,6 +26,7 @@ WalletBalance, ) except ImportError: + import client as client_module from client import RustChainClient from schemas import ( APIError, @@ -35,6 +38,26 @@ ) +def test_malformed_numeric_env_uses_safe_defaults(monkeypatch): + monkeypatch.setenv("RUSTCHAIN_TIMEOUT", "not-a-timeout") + monkeypatch.setenv("RUSTCHAIN_RETRY", "bad-retry") + + module = importlib.reload(client_module) + + assert module.REQUEST_TIMEOUT == 30 + assert module.RETRY_COUNT == 2 + + +def test_numeric_env_overrides_are_preserved(monkeypatch): + monkeypatch.setenv("RUSTCHAIN_TIMEOUT", "12") + monkeypatch.setenv("RUSTCHAIN_RETRY", "4") + + module = importlib.reload(client_module) + + assert module.REQUEST_TIMEOUT == 12 + assert module.RETRY_COUNT == 4 + + class AsyncContextManager: """Simple async context manager for testing."""