Skip to main content
Glama
MCP Lottery Demo

MCP Lottery Demo

by pmhw
README.md
# MCP 抽签工具演示

这是一个完整的 MCP (Model Context Protocol) 插件示例,实现了抽签、投骰子、抛硬币等随机功能。

## 功能特性

- 🎲 **抽签工具**:从选项列表中随机抽取结果
- 🎯 **投骰子**:支持自定义面数和数量
- 🪙 **抛硬币**:简单的正面/反面随机选择

## 安装和运行

1. 安装依赖:
```bash
npm install
```

2. 启动 MCP 服务器:

**方式一:标准输入输出模式(推荐用于 AI 客户端集成)**
```bash
npm start
```

**方式二:HTTP 服务器模式(支持 Web 界面和网络访问)**
```bash
npm run start:http
```

启动后访问:
- 🌐 Web 界面:http://localhost:3000
- 📡 MCP 端点:http://localhost:3000/mcp
- ❤️ 健康检查:http://localhost:3000/health

## 工具说明

### 1. draw_lottery - 抽签工具

从给定的选项列表中随机抽取一个或多个结果。

**参数:**
- `options` (必需): 抽签选项列表
- `count` (可选): 抽取数量,默认为1
- `allow_duplicate` (可选): 是否允许重复抽取,默认为false

**示例:**
```json
{
  "name": "draw_lottery",
  "arguments": {
    "options": ["苹果", "香蕉", "橙子", "葡萄"],
    "count": 2,
    "allow_duplicate": false
  }
}
```

### 2. roll_dice - 投骰子

投掷骰子,支持自定义面数和数量。

**参数:**
- `sides` (可选): 骰子面数,默认为6
- `count` (可选): 骰子数量,默认为1

**示例:**
```json
{
  "name": "roll_dice",
  "arguments": {
    "sides": 20,
    "count": 3
  }
}
```

### 3. flip_coin - 抛硬币

抛硬币,返回正面或反面。

**参数:**
- `count` (可选): 抛硬币次数,默认为1

**示例:**
```json
{
  "name": "flip_coin",
  "arguments": {
    "count": 5
  }
}
```

## 演示 Prompt

以下是一些可以触发这些工具的示例 prompt:

### 基础抽签
```
帮我从这些水果中随机选一个:苹果、香蕉、橙子、葡萄、草莓
```

### 多人抽签
```
我们5个人要分这些奖品:一等奖、二等奖、三等奖、四等奖、五等奖。请帮我们随机分配,每个人一个奖品。
```

### 投骰子游戏
```
我想玩一个20面骰子的游戏,投3次,看看结果如何
```

### 抛硬币决定
```
帮我抛5次硬币,看看正反面各出现多少次
```

### 复杂抽签场景
```
我们班有30个同学,要选出5个代表参加活动。请帮我随机选择,不能重复。
```

## 项目结构

```
mcp-lottery-demo/
├── package.json          # 项目配置和依赖
├── README.md            # 项目说明文档
├── demo-prompts.md      # 演示 prompt 集合
├── test-client.js       # 标准输入输出测试客户端
├── test-http-client.js  # HTTP 测试客户端
├── demo.js             # 演示脚本
└── src/
    ├── server.js        # 标准输入输出 MCP 服务器
    └── server-http.js   # HTTP MCP 服务器
```

## 技术实现

- 使用 `@modelcontextprotocol/sdk` 构建 MCP 服务器
- **两种传输模式**:
  - 标准输入输出传输协议(用于 AI 客户端集成)
  - HTTP/SSE 传输协议(支持 Web 界面和网络访问)
- 实现了完整的工具注册和调用机制
- 包含错误处理和参数验证
- 提供 Web 界面和 RESTful API

## 扩展建议

你可以基于这个示例扩展更多功能:

1. **添加更多随机工具**:如随机数生成器、随机颜色选择等
2. **持久化存储**:保存抽签历史记录
3. **用户偏好**:记住用户的常用选项
4. **统计分析**:提供抽签结果的统计分析
5. **可视化**:生成图表展示结果分布

## 许可证

MIT License

TDQS

A3.6/5.0

Scored across 3 tools

Disambiguation5/5

Each tool has a clearly distinct purpose: draw_lottery selects from a list of options, flip_coin returns heads or tails, and roll_dice handles dice with customizable faces and counts. There is no overlap or ambiguity between these random selection mechanisms.

Naming Consistency5/5

All tools follow a consistent verb_noun pattern (draw_lottery, flip_coin, roll_dice) with clear, descriptive names that accurately reflect their functions. The naming is uniform and predictable throughout the set.

Tool Count5/5

With 3 tools, the server is well-scoped for a lottery/gambling demo, covering common random selection scenarios (lottery draws, coin flips, dice rolls). Each tool earns its place without feeling excessive or insufficient for the domain.

Completeness5/5

The tool set provides complete coverage for basic random selection operations in a gambling context: drawing from options, binary coin flips, and customizable dice rolls. There are no obvious gaps for the stated demo purpose, and agents can handle typical use cases without dead ends.