Skip to main content
Glama

calculate

Perform basic mathematical operations like addition, subtraction, multiplication, and division using two numerical inputs.

Instructions

Perform basic mathematical operations

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
operationYesMathematical operation to perform
xYesFirst operand
yYesSecond operand

Implementation Reference

  • demo.ts:132-188 (handler)
    The handler for the 'calculate' tool within the CallToolRequestSchema handler. It destructures the arguments, performs the specified arithmetic operation (add, subtract, multiply, divide) on x and y, handles division by zero and invalid operations, and returns a formatted result text.
    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:65-83 (schema)
    The input schema for the 'calculate' tool, defining the required parameters: operation (enum of arithmetic ops), and numbers x and y.
    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"], },
  • demo.ts:62-84 (registration)
    Registration of the 'calculate' tool in the ListToolsRequestSchema handler's tools array, including its name, description, and input schema.
    { 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"], }, },
Install Server

Other Tools

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/joeguo911/mcp-demo'

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