calculator
Perform basic arithmetic operations on two numbers including addition, subtraction, multiplication, and division to solve mathematical calculations.
Instructions
두 숫자에 대한 사칙연산을 수행하는 계산기 도구
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| num1 | Yes | 첫 번째 숫자 | |
| num2 | Yes | 두 번째 숫자 | |
| operation | Yes | 연산자 |
Input Schema (JSON Schema)
{
"properties": {
"num1": {
"description": "첫 번째 숫자",
"type": "number"
},
"num2": {
"description": "두 번째 숫자",
"type": "number"
},
"operation": {
"description": "연산자",
"enum": [
"add",
"subtract",
"multiply",
"divide"
],
"type": "string"
}
},
"required": [
"num1",
"num2",
"operation"
],
"type": "object"
}
Implementation Reference
- src/index.ts:489-521 (handler)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 } }
- src/index.ts:20-24 (schema)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'] } },
- src/index.ts:58-74 (helper)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('지원하지 않는 연산입니다') } }