随机数MCP
Python标准库中的基本随机数生成实用程序,包括用于整数、浮点数、加权选择、列表洗牌和安全令牌生成的伪随机和加密安全操作。
演示视频
https://github.com/user-attachments/assets/303a441a-2b10-47e3-b2a5-c8b51840e362
工具
| 工具 | 用途 | Python函数 |
|---|---|---|
random_int | 生成随机整数 | random.randint() |
random_float | 生成随机浮点数 | random.uniform() |
random_choices | 从列表中选择项目(可选权重) | random.choices() |
random_shuffle | 返回一个新列表,其中包含已洗牌的项目 | random.sample() |
random_sample | 从总体中选择k个独特项目 | random.sample() |
secure_token_hex | 生成加密安全的十六进制令牌 | secrets.token_hex() |
secure_random_int | 生成加密安全的整数 | secrets.randbelow() |
设置
克劳德桌面版
将此添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json\ 视窗: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"random-number": {
"command": "uvx",
"args": ["random-number-mcp"]
}
}
}工具参考
random_int
生成一个介于低和高(含)之间的随机整数。
参数:
low(int):下限(含)high(int):上限(含)
例子:
{
"name": "random_int",
"arguments": {
"low": 1,
"high": 100
}
}random_float
在低和高之间生成随机浮动。
参数:
low(浮点数,可选):下限(默认值:0.0)high(浮点数,可选):上限(默认值:1.0)
例子:
{
"name": "random_float",
"arguments": {
"low": 0.5,
"high": 2.5
}
}random_choices
从具有替换的总体中选择k个项目,可选加权。
参数:
population(列表):可供选择的项目列表k(int,可选):要选择的项目数(默认值:1)weights(列表,可选):每个项目的权重(默认值:相等权重)
例子:
{
"name": "random_choices",
"arguments": {
"population": ["red", "blue", "green", "yellow"],
"k": 2,
"weights": [0.4, 0.3, 0.2, 0.1]
}
}random_shuffle
返回一个新列表,其中包含按随机顺序排列的项目。
参数:
items(list):要洗牌的项目列表
例子:
{
"name": "random_shuffle",
"arguments": {
"items": [1, 2, 3, 4, 5]
}
}random_sample
从总体中选择k个唯一项目,不进行替换。
参数:
population(列表):可供选择的项目列表k(int):可选择的项目数
例子:
{
"name": "random_sample",
"arguments": {
"population": ["a", "b", "c", "d", "e"],
"k": 2
}
}secure_token_hex
生成加密安全的随机十六进制令牌。
参数:
nbytes(int,可选):随机字节数(默认值:32)
例子:
{
"name": "secure_token_hex",
"arguments": {
"nbytes": 16
}
}secure_random_int
在upper_bound以下生成一个加密安全的随机整数。
参数:
upper_bound(int):上限(不含)
例子:
{
"name": "secure_random_int",
"arguments": {
"upper_bound": 1000
}
}安全注意事项
该软件包提供标准伪随机函数(适用于模拟、游戏等)和加密安全函数(适用于令牌、密钥等):
- 标准功能 (
random_int,random_float,random_choices,random_shuffle):使用Pythonrandom模块-快速但不加密 - 安全功能 (
secure_token_hex,secure_random_int):使用Pythonsecrets模块-速度较慢但加密安全
发展
先决条件
- Python 3.10+
- 紫外线 包管理器
设置
# Clone the repository
git clone https://github.com/example/random-number-mcp
cd random-number-mcp
# Install dependencies
uv sync --dev
# Run tests
uv run pytest
# Run linting
uv run ruff check --fix
uv run ruff format
# Type checking
uv run mypy src/MCP客户端配置
{
"mcpServers": {
"random-number-dev": {
"command": "uv",
"args": [
"--directory",
"
/random-number-mcp",
"run",
"random-number-mcp"
]
}
}
}注: 替换 /random-number-mcp 带有克隆存储库的绝对路径。
建筑
# Build package
uv build
# Test installation
uv run --with dist/*.whl random-number-mcp发布检查表
- 更新版本:
- 增加 version 数字在 pyproject.toml, src/random_number_mcp/__init__.py,以及 server.json.
- 更新变更日志:
- 在中添加新条目 CHANGELOG.md 为了释放。
- 使用编码剂起草注释 git diff 背景。
Update the @CHANGELOG.md for the latest release.
List all significant changes, bug fixes, and new features.
Here's the git diff:
[GIT_DIFF]- 与任何其他待处理的更改一起提交。
- 创建GitHub版本:
- 在GitHub UI上起草一个新版本。 - 使用UI发布标签。 - GitHub工作流将自动构建包并将其发布到PyPI。
MCP检验员测试
要探索和/或开发此服务器,请使用MCP Inspector npm实用程序:
# Install MCP Inspector
npm install -g @modelcontextprotocol/inspector
# Run local development server with the inspector
npx @modelcontextprotocol/inspector uv run random-number-mcp
# Run PyPI production server with the inspector
npx @modelcontextprotocol/inspector uvx random-number-mcpMCP注册表
mcp名称:io.github.zazencodes/random-number-mcp
许可证
MIT许可证-请参阅 许可证 文件以获取详细信息。
