draw_lottery
Randomly selects one or multiple items from a provided list of options, with control over selection count and duplicate handling.
Instructions
从给定的选项列表中随机抽取一个或多个结果
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| allow_duplicate | No | 是否允许重复抽取,默认为false | |
| count | No | 抽取数量,默认为1 | |
| options | Yes | 抽签选项列表 |
Implementation Reference
- src/server.js:139-177 (handler)Main handler for draw_lottery tool execution within the CallToolRequestSchema handler. Performs input validation, random selection (with optional duplicates), and returns formatted text response.case 'draw_lottery': { // 抽签工具:从选项列表中随机抽取结果 const { options, count = 1, allow_duplicate = false } = args; // 参数验证 if (!Array.isArray(options) || options.length === 0) { throw new Error('选项列表不能为空'); } if (count > options.length && !allow_duplicate) { throw new Error('抽取数量不能超过选项数量(不允许重复时)'); } // 执行抽签逻辑 const results = []; const availableOptions = [...options]; // 创建选项副本 for (let i = 0; i < count; i++) { // 随机选择索引 const randomIndex = Math.floor(Math.random() * availableOptions.length); const selected = availableOptions[randomIndex]; results.push(selected); // 如果不允许重复,从可用选项中移除已选择的 if (!allow_duplicate) { availableOptions.splice(randomIndex, 1); } } // 返回格式化的结果 return { content: [ { type: 'text', text: `🎲 抽签结果:\n${results.map((result, index) => `${index + 1}. ${result}`).join('\n')}`, }, ], }; }
- src/server.js:53-77 (schema)JSON Schema definition for the draw_lottery tool input parameters: options (required array of strings), count (number, default 1), allow_duplicate (boolean, default false).{ name: 'draw_lottery', description: '从给定的选项列表中随机抽取一个或多个结果', inputSchema: { type: 'object', properties: { options: { type: 'array', items: { type: 'string' }, description: '抽签选项列表', }, count: { type: 'number', description: '抽取数量,默认为1', default: 1, }, allow_duplicate: { type: 'boolean', description: '是否允许重复抽取,默认为false', default: false, }, }, required: ['options'], // 必需参数 }, },
- src/server.js:118-122 (registration)Registration of the tools list (including draw_lottery) via setRequestHandler for ListToolsRequestSchema.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools, // 返回工具列表 }; });
- src/server-http.js:75-107 (handler)HTTP server handler for draw_lottery within handleToolCall function. Identical logic to stdio version.case 'draw_lottery': { const { options, count = 1, allow_duplicate = false } = args; if (!Array.isArray(options) || options.length === 0) { throw new Error('选项列表不能为空'); } if (count > options.length && !allow_duplicate) { throw new Error('抽取数量不能超过选项数量(不允许重复时)'); } const results = []; const availableOptions = [...options]; for (let i = 0; i < count; i++) { const randomIndex = Math.floor(Math.random() * availableOptions.length); const selected = availableOptions[randomIndex]; results.push(selected); if (!allow_duplicate) { availableOptions.splice(randomIndex, 1); } } return { content: [ { type: 'text', text: `🎲 抽签结果:\n${results.map((result, index) => `${index + 1}. ${result}`).join('\n')}`, }, ], }; }
- src/server-http.js:10-34 (schema)JSON Schema definition for draw_lottery in HTTP server version.{ name: 'draw_lottery', description: '从给定的选项列表中随机抽取一个或多个结果', inputSchema: { type: 'object', properties: { options: { type: 'array', items: { type: 'string' }, description: '抽签选项列表', }, count: { type: 'number', description: '抽取数量,默认为1', default: 1, }, allow_duplicate: { type: 'boolean', description: '是否允许重复抽取,默认为false', default: false, }, }, required: ['options'], }, },