Skip to main content
Glama
pmhw

MCP Lottery Demo

by pmhw

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
NameRequiredDescriptionDefault
allow_duplicateNo是否允许重复抽取,默认为false
countNo抽取数量,默认为1
optionsYes抽签选项列表

Implementation Reference

  • 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')}`,
          },
        ],
      };
    }
  • 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,  // 返回工具列表
      };
    });
  • 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')}`,
          },
        ],
      };
    }
  • 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'],
      },
    },
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pmhw/McpDemo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server