Skip to main content
Glama
ciel240
by ciel240

calculator

Perform basic arithmetic operations on two numbers including addition, subtraction, multiplication, and division to solve mathematical calculations.

Instructions

두 숫자에 대한 사칙연산을 수행하는 계산기 도구

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
num1Yes첫 번째 숫자
num2Yes두 번째 숫자
operationYes연산자

Implementation Reference

  • Handles the 'calculator' tool execution: parses input arguments using CalculatorToolSchema, calls the calculate helper function, formats the result with operation symbol, and returns it as text content. Includes error handling.
    if (request.params.name === 'calculator') {
        try {
            const { num1, num2, operation } = CalculatorToolSchema.parse(request.params.arguments)
            
            const result = calculate(num1, num2, operation)
            const operationSymbols = {
                add: '+',
                subtract: '-',
                multiply: '*',
                divide: '/'
            }
            const symbol = operationSymbols[operation]
            const message = `${num1} ${symbol} ${num2} = ${result}`
            
            return {
                content: [
                    {
                        type: 'text',
                        text: message
                    }
                ]
            }
        } catch (error) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `계산 오류: ${error instanceof Error ? error.message : '알 수 없는 오류'}`
                    }
                ],
                isError: true
            }
        }
  • Zod schema defining the input structure for the calculator tool: num1 and num2 as numbers, operation as enum of 'add', 'subtract', 'multiply', 'divide'.
    const CalculatorToolSchema = z.object({
        num1: z.number().describe('첫 번째 숫자'),
        num2: z.number().describe('두 번째 숫자'),
        operation: z.enum(['add', 'subtract', 'multiply', 'divide']).describe('연산자 (add: 덧셈, subtract: 뺄셈, multiply: 곱셈, divide: 나눗셈)')
    })
  • src/index.ts:334-356 (registration)
    Registers the 'calculator' tool in the ListToolsResponse, providing name, description, and inputSchema matching the Zod schema.
    {
        name: 'calculator',
        description: '두 숫자에 대한 사칙연산을 수행하는 계산기 도구',
        inputSchema: {
            type: 'object',
            properties: {
                num1: {
                    type: 'number',
                    description: '첫 번째 숫자'
                },
                num2: {
                    type: 'number',
                    description: '두 번째 숫자'
                },
                operation: {
                    type: 'string',
                    description: '연산자',
                    enum: ['add', 'subtract', 'multiply', 'divide']
                }
            },
            required: ['num1', 'num2', 'operation']
        }
    },
  • Helper function that performs the arithmetic calculation based on the operation enum: add (+), subtract (-), multiply (*), divide (/). Handles division by zero and invalid operations with errors.
    const calculate = (num1: number, num2: number, operation: string): number => {
        switch (operation) {
            case 'add':
                return num1 + num2
            case 'subtract':
                return num1 - num2
            case 'multiply':
                return num1 * num2
            case 'divide':
                if (num2 === 0) {
                    throw new Error('0으로 나눌 수 없습니다')
                }
                return num1 / num2
            default:
                throw new Error('지원하지 않는 연산입니다')
        }
    }

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/ciel240/class_study'

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