返回市场
随机数mcp

随机数mcp

作者:zazencodes43 星标更新:2025-09-08

项目介绍

随机数MCP

来自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桌面

在您的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模块 - 较慢但加密安全

🛠️ 开发

前提条件

  • Python 3.10+
  • uv 包管理器

设置

# 克隆仓库
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/

MCP客户端配置

{
  "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

发布检查清单

  1. 更新版本:

    • pyproject.tomlsrc/__init__.py中增加version号。
  2. 更新变更日志:

    • CHANGELOG.md中为此次发布添加新的条目。

      • 使用git diff上下文,由编码代理起草注释。
      更新@CHANGELOG.md以记录最新发布的更改。
      列出所有重要的更改、错误修复和新功能。
      这是git diff:
      [GIT_DIFF]
      
    • 与任何其他待处理的更改一起提交。

  3. 创建GitHub发布:

    • 在GitHub UI上草拟一个新的发布。
      • 使用UI标记发布。
    • GitHub工作流将自动构建并发布包到PyPI。

使用MCP Inspector进行测试

为了探索或开发此服务器,请使用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文件。

🤝 贡献

欢迎贡献!请随时提交拉取请求。

📚 链接