calculator
Perform basic arithmetic operations including addition, subtraction, multiplication, and division with two numbers.
Instructions
Perform basic arithmetic operations
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | The operation to perform | |
| a | Yes | First number | |
| b | Yes | Second number |
Input Schema (JSON Schema)
{
"properties": {
"a": {
"description": "First number",
"type": "number"
},
"b": {
"description": "Second number",
"type": "number"
},
"operation": {
"description": "The operation to perform",
"enum": [
"add",
"subtract",
"multiply",
"divide"
],
"type": "string"
}
},
"required": [
"operation",
"a",
"b"
],
"type": "object"
}
Implementation Reference
- src/index.ts:59-95 (handler)Handler function that executes the calculator tool logic: performs add, subtract, multiply, or divide on inputs a and b, handles division by zero, returns result in MCP text content format.async ({ operation, a, b }: { operation: 'add' | 'subtract' | 'multiply' | 'divide'; a: number; b: number }) => { let result: number; switch (operation) { case 'add': result = a + b break case 'subtract': result = a - b break case 'multiply': result = a * b break case 'divide': if (b === 0) { return { content: [ { type: 'text', text: '오류: 0으로 나눌 수 없습니다.' } ] } } result = a / b break } return { content: [ { type: 'text', text: `결과: ${result}` } ] } }
- src/index.ts:54-58 (schema)Zod schema defining the input parameters for the calculator tool: operation (enum), a (number), b (number).{ operation: z.enum(['add', 'subtract', 'multiply', 'divide']).describe('The operation to perform'), a: z.number().describe('First number'), b: z.number().describe('Second number') },
- src/index.ts:50-96 (registration)Registration of the calculator tool on the MCP server using server.tool(), including name, description, schema, and handler.// Define calculator tool server.tool( 'calculator', 'Perform basic arithmetic operations', { operation: z.enum(['add', 'subtract', 'multiply', 'divide']).describe('The operation to perform'), a: z.number().describe('First number'), b: z.number().describe('Second number') }, async ({ operation, a, b }: { operation: 'add' | 'subtract' | 'multiply' | 'divide'; a: number; b: number }) => { let result: number; switch (operation) { case 'add': result = a + b break case 'subtract': result = a - b break case 'multiply': result = a * b break case 'divide': if (b === 0) { return { content: [ { type: 'text', text: '오류: 0으로 나눌 수 없습니다.' } ] } } result = a / b break } return { content: [ { type: 'text', text: `결과: ${result}` } ] } } )
- src/index.ts:201-205 (helper)Helper documentation of the calculator tool's capabilities in the server-info resource.{ name: 'calculator', description: '기본 산술 연산을 수행합니다', supportedOperations: ['add', 'subtract', 'multiply', 'divide'] },