roll_dice
Generate random dice rolls using standard dice notation for games, simulations, 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:19-23 (handler)The primary handler for the 'roll_dice' MCP tool. Registered via @mcp.tool() decorator. It instantiates DiceRoller with inputs and returns its string representation containing the 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:37-46 (helper)The __str__ method of DiceRoller class, which generates the formatted output string for single or multiple rolls by calling roll_dice or roll_multiple.def __str__(self): if self.num_rolls == 1: rolls, kept_rolls = self.roll_dice() return f"ROLLS: {', '.join(map(str, rolls))} -> RETURNS: {sum(kept_rolls)}" else: results = self.roll_multiple() result_strs = [] for i, result in enumerate(results, 1): result_strs.append(f"Roll {i}: ROLLS: {', '.join(map(str, result['rolls']))} -> RETURNS: {result['total']}") return "\n".join(result_strs)
- dice_roller.py:10-23 (helper)Core helper method in DiceRoller that parses dice notation (e.g., 2d20k1), rolls the dice using random.randint, sorts descending, 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
- dice_roller.py:25-36 (helper)Helper method to perform multiple rolls when num_rolls > 1, aggregating results with totals.def roll_multiple(self): """Roll the dice multiple times according to num_rolls""" results = [] for _ in range(self.num_rolls): rolls, kept_rolls = self.roll_dice() results.append({ "rolls": rolls, "kept": kept_rolls, "total": sum(kept_rolls) }) return results
- server.py:19-19 (registration)The @mcp.tool() decorator registers the roll_dice function as an MCP tool.@mcp.tool()