secure_token_hex
Generate cryptographically secure random hexadecimal tokens for secure authentication, API keys, or session IDs. Specify the number of bytes to customize token length.
Instructions
Generate a secure random hex token.
Args: nbytes: Number of random bytes to generate (default 32)
Returns: Hex string containing 2*nbytes characters
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nbytes | No |
Implementation Reference
- src/random_number_mcp/server.py:96-107 (handler)MCP handler function for 'secure_token_hex' tool, registered with @app.tool() decorator, delegates implementation to tools.secure_token_hex@app.tool() def secure_token_hex(nbytes: int = 32) -> str: """Generate a secure random hex token. Args: nbytes: Number of random bytes to generate (default 32) Returns: Hex string containing 2*nbytes characters """ return tools.secure_token_hex(nbytes)
- Core helper function implementing secure random hex token generation using Python's secrets module with input validation.def secure_token_hex(nbytes: int = 32) -> str: """Generate a secure random hex token. Args: nbytes: Number of random bytes to generate (default 32) Returns: Hex string containing 2*nbytes characters Raises: ValueError: If nbytes < 0 TypeError: If nbytes is not an integer """ validate_positive_int(nbytes, "nbytes") return secrets.token_hex(nbytes)