来自Python标准库的基本随机数生成工具,包括整数、浮点数、加权选择、列表洗牌以及安全令牌生成的伪随机和加密安全操作。
https://github.com/user-attachments/assets/303a441a-2b10-47e3-b2a5-c8b51840e362
<a href="https://glama.ai/mcp/servers/@zazencodes/random-number-mcp"> <img width="380" height="200" src="https://gips3.baidu.com/it/u=1774613851,510906430&fm=3081&app=3081&f=PNG?w=760&h=400" alt="随机数MCP服务器" /> </a>| 工具 | 目的 | 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桌面配置文件中添加以下内容:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %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 (float, 可选): 下界(默认: 0.0)high (float, 可选): 上界(默认: 1.0)示例:
{
"name": "random_float",
"arguments": {
"low": 0.5,
"high": 2.5
}
}
random_choices带替换地从总体中选择k个项目,可选加权。
参数:
population (list): 要从中选择项目的列表k (int, 可选): 要选择的项目数量(默认: 1)weights (list, 可选): 每个项目的权重(默认: 权重相等)示例:
{
"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 (list): 要从中选择项目的列表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 (int): 上界(不包括)示例:
{
"name": "secure_random_int",
"arguments": {
"upper_bound": 1000
}
}
此包提供了标准伪随机函数(适用于模拟、游戏等)和加密安全函数(适用于令牌、密钥等):
random_int, random_float, random_choices, random_shuffle):使用Python的random模块 - 快速但非加密安全secure_token_hex, secure_random_int):使用Python的secrets模块 - 较慢但加密安全# 克隆仓库
git clone https://github.com/example/random-number-mcp
cd random-number-mcp
# 安装依赖
uv sync --dev
# 运行测试
uv run pytest
# 运行代码检查
uv run ruff check --fix
uv run ruff format
# 类型检查
uv run mypy src/
{
"mcpServers": {
"random-number-dev": {
"command": "uv",
"args": [
"--directory",
"<path_to_your_repo>/random-number-mcp",
"run",
"random-number-mcp"
]
}
}
}
注意: 将<path_to_your_repo>/random-number-mcp替换为您克隆的仓库的绝对路径。
# 构建包
uv build
# 测试安装
uv run --with dist/*.whl random-number-mcp
更新版本:
pyproject.toml和src/__init__.py中增加version号。更新变更日志:
在CHANGELOG.md中为此次发布添加新的条目。
git diff上下文,由编码代理起草注释。更新@CHANGELOG.md以记录最新发布的更改。
列出所有重要的更改、错误修复和新功能。
这是git diff:
[GIT_DIFF]
与任何其他待处理的更改一起提交。
创建GitHub发布:
为了探索或开发此服务器,请使用MCP Inspector npm工具:
# 安装MCP Inspector
npm install -g @modelcontextprotocol/inspector
# 使用检查器运行本地开发服务器
npx @modelcontextprotocol/inspector uv run random-number-mcp
# 使用检查器运行PyPI生产服务器
npx @modelcontextprotocol/inspector uvx random-number-mcp
MIT许可证 - 详情见LICENSE文件。
欢迎贡献!请随时提交拉取请求。