calculate
Perform basic mathematical operations like addition, subtraction, multiplication, and division using the MCP Demo Server. Execute calculations with ease by specifying operands and desired operation.
Instructions
Perform basic mathematical operations
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | Mathematical operation to perform | |
| x | Yes | First operand | |
| y | Yes | Second operand |
Input Schema (JSON Schema)
{
"properties": {
"operation": {
"description": "Mathematical operation to perform",
"enum": [
"add",
"subtract",
"multiply",
"divide"
],
"type": "string"
},
"x": {
"description": "First operand",
"type": "number"
},
"y": {
"description": "Second operand",
"type": "number"
}
},
"required": [
"operation",
"x",
"y"
],
"type": "object"
}
Implementation Reference
- demo.ts:132-188 (handler)Handler logic for the 'calculate' tool: performs add, subtract, multiply, divide operations on inputs x and y, handles division by zero and invalid operations.if (name === "calculate") { const { operation, x, y } = args as { operation: string; x: number; y: number; }; let result: number; let operationSymbol: string; switch (operation) { case "add": result = x + y; operationSymbol = "+"; break; case "subtract": result = x - y; operationSymbol = "-"; break; case "multiply": result = x * y; operationSymbol = "×"; break; case "divide": if (y === 0) { return { content: [ { type: "text", text: "Error: Division by zero is not allowed", }, ], }; } result = x / y; operationSymbol = "÷"; break; default: return { content: [ { type: "text", text: `Error: Unknown operation "${operation}"`, }, ], }; } return { content: [ { type: "text", text: `${x} ${operationSymbol} ${y} = ${result}`, }, ], }; }
- demo.ts:62-84 (schema)Input schema for the 'calculate' tool defining operation (add/subtract/multiply/divide), x, and y as required number operands.{ name: "calculate", description: "Perform basic mathematical operations", inputSchema: { type: "object", properties: { operation: { type: "string", description: "Mathematical operation to perform", enum: ["add", "subtract", "multiply", "divide"], }, x: { type: "number", description: "First operand", }, y: { type: "number", description: "Second operand", }, }, required: ["operation", "x", "y"], }, },