Skip to main content
Glama
pmhw

MCP Lottery Demo

by pmhw

flip_coin

Simulate coin flips to generate random heads or tails outcomes for decision-making or probability testing. Specify the number of flips needed.

Instructions

抛硬币,返回正面或反面

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
countNo抛硬币次数,默认为1

Implementation Reference

  • Handler for flip_coin tool in stdio MCP server switch statement: validates input count, performs multiple coin flips using Math.random() < 0.5 for '正面'(heads) or '反面'(tails), tallies results, formats output text.
    case 'flip_coin': {
      // 抛硬币工具:模拟抛硬币
      const { count = 1 } = args;
      
      // 参数验证
      if (count < 1) {
        throw new Error('抛硬币次数至少为1');
      }
    
      // 执行抛硬币逻辑
      const results = [];
      let heads = 0;  // 正面次数
      let tails = 0;  // 反面次数
      
      for (let i = 0; i < count; i++) {
        // 50% 概率生成正面或反面
        const result = Math.random() < 0.5 ? '正面' : '反面';
        results.push(result);
        if (result === '正面') heads++;
        else tails++;
      }
    
      // 根据抛硬币次数格式化输出
      const resultText = count === 1 
        ? `🪙 抛硬币结果:${results[0]}`
        : `🪙 抛硬币结果:${results.join(', ')}\n📊 统计:正面 ${heads} 次,反面 ${tails} 次`;
    
      return {
        content: [
          {
            type: 'text',
            text: resultText,
          },
        ],
      };
    }
  • Handler for flip_coin tool in HTTP server's handleToolCall function: identical implementation to stdio server version.
    case 'flip_coin': {
      const { count = 1 } = args;
      
      if (count < 1) {
        throw new Error('抛硬币次数至少为1');
      }
    
      const results = [];
      let heads = 0;
      let tails = 0;
      
      for (let i = 0; i < count; i++) {
        const result = Math.random() < 0.5 ? '正面' : '反面';
        results.push(result);
        if (result === '正面') heads++;
        else tails++;
      }
    
      const resultText = count === 1 
        ? `🪙 抛硬币结果:${results[0]}`
        : `🪙 抛硬币结果:${results.join(', ')}\n📊 统计:正面 ${heads} 次,反面 ${tails} 次`;
    
      return {
        content: [
          {
            type: 'text',
            text: resultText,
          },
        ],
      };
    }
  • src/server.js:98-110 (registration)
    Registration of flip_coin tool in tools array for ListToolsRequestHandler, including name, description, and input schema.
      name: 'flip_coin',
      description: '抛硬币,返回正面或反面',
      inputSchema: {
        type: 'object',
        properties: {
          count: {
            type: 'number',
            description: '抛硬币次数,默认为1',
            default: 1,
          },
        },
      },
    },
  • Registration of flip_coin tool in tools array for HTTP /tools endpoint, including name, description, and input schema.
      name: 'flip_coin',
      description: '抛硬币,返回正面或反面',
      inputSchema: {
        type: 'object',
        properties: {
          count: {
            type: 'number',
            description: '抛硬币次数,默认为1',
            default: 1,
          },
        },
      },
    },

Tool Definition Quality

Score is being calculated. Check back soon.

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