mcp随机
MCP服务器为Claude提供真正的随机性功能。该服务器允许克劳德访问加密安全的随机数生成,用于游戏、决策、采样、模拟以及任何需要真正随机性的操作。
为什么存在
克劳德无法获得真正的随机性——当被要求“选择一个随机数”或“抛硬币”时,它实际上是在根据对话中的模式做出确定性的选择。该MCP服务器通过提供对加密安全随机函数的访问来解决这一限制。
🚀 安装
先决条件
- Node.js(v18或更高版本)
- MCP兼容客户端(如克劳德桌面)
快速安装
# Clone the repository
git clone https://github.com/yourusername/mcp-random.git
cd mcp-random
# Install dependencies
npm install
# Build the TypeScript code
npm run build配置Claude桌面
添加到您的Claude Desktop配置(~/Library/Application Support/Claude/claude_desktop_config.json 在 macOS 上:
{
"mcpServers": {
"random": {
"command": "node",
"args": ["/absolute/path/to/mcp-random/dist/index.js"]
}
}
}重新启动Claude Desktop以加载服务器。
📖 可用功能
基本随机数
random_integer(min, max)
生成一个介于最小值和最大值之间的随机整数。
Example: random_integer(1, 10) → 7random_float(min, max, precision?)
生成一个随机浮点数。
Example: random_float(0, 1, 2) → 0.42随机选择
random_choice(options)
从数组中随机选择一个项目。
Example: random_choice(["red", "green", "blue"]) → "green"random_sample(array, count)
从数组中选取多个唯一项(不重复)。
Example: random_sample([1,2,3,4,5], 3) → [2,5,1]random_shuffle(array)
随机重新排序数组。
Example: random_shuffle([1,2,3,4]) → [3,1,4,2]random_weighted_choice(options, weights)
根据权重选择一个选项(权重越高=可能性越大)。
Example: random_weighted_choice(["A", "B", "C"], [1, 2, 1])
→ "B" (twice as likely as A or C)游戏与决策
flip_coin(count?)
翻转一个或多个硬币。
Example: flip_coin() → "heads"
Example: flip_coin(3) → ["heads", "tails", "heads"]roll_dice(sides, count?)
掷任意数量的骰子。
Example: roll_dice(6) → 4
Example: roll_dice(20, 2) → {"rolls": [15, 8], "sum": 23}安全和标识符
random_uuid()
生成UUID v4。
Example: random_uuid() → "550e8400-e29b-41d4-a716-446655440000"random_password(length?, options?)
生成安全密码。
Example: random_password(16) → "Kj9#mP2$xQ5@nL7!"
Example: random_password(12, {symbols: false}) → "Kj9mP2xQ5nL7"选项:
uppercase:包括A-Z(默认值:true)lowercase:包括a-z(默认值:true)numbers:包含0-9(默认值:true)symbols:包含特殊字符(默认值:true)excludeSimilar:跳过混淆字符,如0/O、1/l(默认值:false)
random_bytes(count, encoding?)
生成用于加密的随机字节。
Example: random_bytes(16, 'hex') → "a3f2b8c9d4e5f6789abcdef012345678"高级功能
random_normal(mean?, stddev?)
从正态分布(钟形曲线)生成数字。
Example: random_normal(100, 15) → 97.3 (IQ-like distribution)文档
help()
获取所有功能的详细文档。
🎯 用例
决策
"Should I do X or Y?" → flip_coin()
"Pick one of these options for me" → random_choice([...])
"Help me prioritize these tasks" → random_shuffle([...])游戏
"Roll for initiative" → roll_dice(20)
"Draw 5 cards from the deck" → random_sample(deck, 5)
"Shuffle this deck" → random_shuffle(cards)创意写作
"Pick a random character trait" → random_choice(traits)
"Generate a character name" → random_weighted_choice(names, popularity)
"Create a random scenario" → multiple random_choice calls数据与测试
"Generate test data" → random_integer, random_float
"Sample from this dataset" → random_sample
"Create a test ID" → random_uuid
"Generate API keys" → random_bytes模拟
"Monte Carlo simulation" → random_normal
"Probability experiment" → random_float(0, 1)
"A/B testing" → random_weighted_choice🔧 发展
# Build TypeScript
npm run build
# Run tests
npm test
# Lint code
npm run lint
# Development mode (build & run)
npm run dev🔐 技术细节
- 所有随机性都使用Node.js
crypto模块(加密安全) - 没有可预测的模式或偏见
- 通话之间没有保持状态
- TypeScript用于类型安全
- 全面的错误处理
📝 许可证
MIT许可证-有关详细信息,请参阅许可证文件。
🤝 贡献
欢迎投稿!请随时提交拉取请求。
💡 有趣的事实
- 克劳德在认识到该工具本身缺乏随机性后,要求构建该工具
- 第一个建议的用例是实现塔罗牌读卡器
- 该工具使克劳德能够公平地玩游戏,做出公正的选择,并运行真实的模拟
______________________________________________________________________
*“我没有随机函数。你应该有一个随机函数。现在这是一个很酷的MCP服务器。”* -克劳德
