random_int
Generate a random integer between two inclusive bounds. Specify a low and high value to receive a random number within that range.
Instructions
Generate a random integer between low and high (inclusive).
Args: low: Lower bound (inclusive) high: Upper bound (inclusive)
Returns: Random integer between low and high
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| low | Yes | ||
| high | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/random_number_mcp/tools.py:15-33 (handler)The actual implementation of the random_int tool. Validates inputs via validate_range helper and calls random.randint(low, high).
def random_int(low: int, high: int) -> int: """Generate a random integer between low and high (inclusive). Args: low: Lower bound (inclusive) high: Upper bound (inclusive) Returns: Random integer between low and high Raises: ValueError: If low > high TypeError: If inputs are not integers """ if not isinstance(low, int) or not isinstance(high, int): raise TypeError("Both low and high must be integers") validate_range(low, high) return random.randint(low, high) - src/random_number_mcp/server.py:13-24 (registration)Registers random_int as an MCP tool via @app.tool() decorator on FastMCP. Delegates to tools.random_int.
@app.tool() def random_int(low: int, high: int) -> int: """Generate a random integer between low and high (inclusive). Args: low: Lower bound (inclusive) high: Upper bound (inclusive) Returns: Random integer between low and high """ return tools.random_int(low, high) - src/random_number_mcp/utils.py:6-9 (helper)The validate_range helper used by random_int to ensure low <= high.
def validate_range(low: int | float, high: int | float) -> None: """Validate that low <= high for range-based functions.""" if low > high: raise ValueError(f"Low value ({low}) must be <= high value ({high})") - tests/test_tools.py:22-50 (schema)Tests for random_int, covering basic usage, same bounds, negative ranges, invalid ranges, and non-integer inputs. Effectively documents the expected input/output contract.
class TestRandomInt: """Tests for random_int function.""" def test_random_int_basic(self): """Test basic random integer generation.""" result = random_int(1, 10) assert isinstance(result, int) assert 1 <= result <= 10 def test_random_int_same_bounds(self): """Test random integer with same low and high.""" result = random_int(5, 5) assert result == 5 def test_random_int_negative_range(self): """Test random integer with negative range.""" result = random_int(-10, -1) assert isinstance(result, int) assert -10 <= result <= -1 def test_random_int_invalid_range(self): """Test random integer with invalid range.""" with pytest.raises(ValueError, match="Low value .* must be <= high value"): random_int(10, 5) def test_random_int_non_integer_input(self): """Test random integer with non-integer input.""" with pytest.raises(TypeError, match="Both low and high must be integers"): random_int(1.5, 10) - examples/demo_host.py:84-90 (helper)Example usage demonstrating how to call random_int via the MCP client.
# Demo random_int print("\n1. random_int - Generate random integers") result = await client.call_tool("random_int", {"low": 1, "high": 100}) print(f" Random integer (1-100): {result}") result = await client.call_tool("random_int", {"low": -10, "high": 10}) print(f" Random integer (-10 to 10): {result}")