Skip to main content
Glama
calculator.ts1.45 kB
import { z } from 'zod'; import { CalculatorParams, ToolResponse } from '@/types'; export const calculatorParams = { operation: z.enum(['add', 'subtract', 'multiply', 'divide']).describe('运算类型'), a: z.number().describe('第一个数字'), b: z.number().describe('第二个数字'), }; export async function calculate({ operation, a, b }: CalculatorParams): Promise<ToolResponse> { let result: number; let operationSymbol: string; switch (operation) { case 'add': result = a + b; operationSymbol = '+'; break; case 'subtract': result = a - b; operationSymbol = '-'; break; case 'multiply': result = a * b; operationSymbol = '×'; break; case 'divide': if (b === 0) { return { content: [ { type: 'text', text: '错误:除数不能为零', _meta: {}, }, ], _meta: {}, }; } result = a / b; operationSymbol = '÷'; break; default: return { content: [ { type: 'text', text: '错误:不支持的运算类型', _meta: {}, }, ], _meta: {}, }; } return { content: [ { type: 'text', text: `${a} ${operationSymbol} ${b} = ${result}`, _meta: {}, }, ], _meta: {}, }; }

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/chendaleiQ/mcp-iot'

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