secure_random_int
Generate a cryptographically secure random integer between 0 and a specified upper bound (exclusive).
Instructions
Generate a secure random integer below upper_bound.
Args: upper_bound: Upper bound (exclusive)
Returns: Random integer in range [0, upper_bound)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| upper_bound | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/random_number_mcp/tools.py:139-158 (handler)Core implementation of secure_random_int. Uses secrets.randbelow() to generate a cryptographically secure random integer in [0, upper_bound). Validates upper_bound is a positive integer.
def secure_random_int(upper_bound: int) -> int: """Generate a secure random integer below upper_bound. Args: upper_bound: Upper bound (exclusive) Returns: Random integer in range [0, upper_bound) Raises: ValueError: If upper_bound <= 0 TypeError: If upper_bound is not an integer """ if not isinstance(upper_bound, int): raise TypeError("upper_bound must be an integer") if upper_bound <= 0: raise ValueError("upper_bound must be positive") return secrets.randbelow(upper_bound) - src/random_number_mcp/server.py:112-122 (handler)MCP tool registration wrapper for secure_random_int. Decorated with @app.tool() to expose it as an MCP tool. Delegates to tools.secure_random_int().
@app.tool() def secure_random_int(upper_bound: int) -> int: """Generate a secure random integer below upper_bound. Args: upper_bound: Upper bound (exclusive) Returns: Random integer in range [0, upper_bound) """ return tools.secure_random_int(upper_bound) - src/random_number_mcp/server.py:112-122 (registration)Registration of secure_random_int as an MCP tool via the @app.tool() decorator on the server handler function.
@app.tool() def secure_random_int(upper_bound: int) -> int: """Generate a secure random integer below upper_bound. Args: upper_bound: Upper bound (exclusive) Returns: Random integer in range [0, upper_bound) """ return tools.secure_random_int(upper_bound) - Input schema defined by the type hint 'upper_bound: int' in the tool registration, which FastMCP uses to generate the JSON schema for the tool.
def secure_random_int(upper_bound: int) -> int: """Generate a secure random integer below upper_bound. Args: upper_bound: Upper bound (exclusive) Returns: - src/random_number_mcp/utils.py:1-35 (helper)Utility functions used by tools.py (validate_range, validate_positive_int, etc.) — though secure_random_int does its own inline validation rather than using these helpers.
"""Utility functions for the random number MCP server.""" from typing import Any 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})") def validate_positive_int(value: int, name: str) -> None: """Validate that a value is a positive integer.""" if not isinstance(value, int): raise TypeError(f"{name} must be an integer, got {type(value).__name__}") if value < 0: raise ValueError(f"{name} must be non-negative, got {value}") def validate_list_not_empty(items: list[Any], name: str) -> None: """Validate that a list is not empty.""" if not items: raise ValueError(f"{name} cannot be empty") def validate_weights_match_population( population: list[Any], weights: list[int | float] ) -> None: """Validate that weights list matches population length.""" if len(weights) != len(population): raise ValueError( f"Weights list length ({len(weights)}) must match " f"population length ({len(population)})" )