roll_dice
Simulate dice rolls using standard dice notation to generate random numbers for games, probability testing, or decision-making scenarios.
Instructions
Roll the dice with the given notation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| notation | Yes | ||
| num_rolls | No |
Implementation Reference
- server.py:23-27 (handler)This is the primary handler and registration point for the MCP tool named 'roll_dice'. The @mcp.tool() decorator registers the function as a tool, and the function executes the tool logic by instantiating DiceRoller and returning its formatted string output containing the dice roll results.@mcp.tool() def roll_dice(notation: str, num_rolls: int = 1) -> str: """Roll the dice with the given notation""" roller = DiceRoller(notation, num_rolls) return str(roller)
- dice_roller.py:10-23 (helper)Core helper method in the DiceRoller class that implements the dice rolling logic. It parses dice notation (e.g., '2d20k1'), generates random rolls, sorts them descending, selects the top 'k' rolls, and returns all rolls and kept rolls.def roll_dice(self): match = self.dice_pattern.match(self.notation) if not match: raise ValueError("Invalid dice notation") num_dice = int(match.group(1)) dice_sides = int(match.group(2)) keep = int(match.group(4)) if match.group(4) else num_dice rolls = [random.randint(1, dice_sides) for _ in range(num_dice)] rolls.sort(reverse=True) kept_rolls = rolls[:keep] return rolls, kept_rolls