calculate
Evaluate math expressions directly using the tool on my-mcp-server. Input any valid equation like '2 + 2' to get instant results for basic calculations.
Instructions
Perform basic math calculations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Math expression to evaluate (e.g., '2 + 2') |
Implementation Reference
- server1.js:87-110 (handler)The handler for the 'calculate' tool. It sanitizes the input math expression by removing disallowed characters, evaluates it using eval(), and returns the result or an error message in the tool response format.case "calculate": try { // Simple math evaluation (sanitized) const sanitized = args.expression.replace(/[^0-9+\-*/().\s]/g, ""); const result = eval(sanitized); return { content: [ { type: "text", text: `${args.expression} = ${result}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error calculating "${args.expression}": ${error.message}`, }, ], isError: true, }; }
- server1.js:40-53 (registration)Registration of the 'calculate' tool in the ListToolsRequestSchema handler, including its name, description, and input schema.{ name: "calculate", description: "Perform basic math calculations", inputSchema: { type: "object", properties: { expression: { type: "string", description: "Math expression to evaluate (e.g., '2 + 2')", }, }, required: ["expression"], }, },
- server1.js:43-52 (schema)Input schema definition for the 'calculate' tool, specifying an object with a required 'expression' string property.inputSchema: { type: "object", properties: { expression: { type: "string", description: "Math expression to evaluate (e.g., '2 + 2')", }, }, required: ["expression"], },