Skip to main content
Glama
codewithmsunke

MCP Math Server

index.js8.47 kB
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; async function main() { // Create the MCP server (with name + version metadata) const server = new McpServer({ name: "demo-mcp-server", version: "0.1.0", }); // Register basic math tools // Addition server.registerTool("add", { title: "Addition tool", description: "Add two numbers a + b", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const sum = a + b; return { content: [ { type: "text", text: `The sum of ${a} and ${b} is ${sum}` } ] }; }); // Square Root server.registerTool("sqrt", { title: "Square Root tool", description: "Calculate the square root of a number (rounded to two decimals)", inputSchema: { x: z.number() }, }, async ({ x }) => { if (x < 0) { return { content: [ { type: "text", text: `Error: Square root of negative number is not allowed.` } ], isError: true }; } const result = Math.sqrt(x); const rounded = Math.round(result * 100) / 100; return { content: [ { type: "text", text: `The square root of ${x} is ${rounded}` } ] }; }); // Subtraction server.registerTool("subtract", { title: "Subtraction tool", description: "Subtract b from a (a - b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const diff = a - b; return { content: [ { type: "text", text: `The difference of ${a} and ${b} is ${diff}` } ] }; }); // Square server.registerTool("square", { title: "Square tool", description: "Calculate the square of a number", inputSchema: { a: z.number() }, }, async ({ a }) => { const result = a * a; return { content: [ { type: "text", text: `The square of ${a} is ${result}` } ] }; }); // Multiplication server.registerTool("multiply", { title: "Multiplication tool", description: "Multiply two numbers a * b", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const product = a * b; return { content: [ { type: "text", text: `The product of ${a} and ${b} is ${product}` } ] }; }); // GCM (Greatest Common Measure) / GCD (Greatest Common Divisor) server.registerTool("gcm", { title: "GCM tool", description: "Calculate the greatest common measure (GCD) of two numbers", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const gcd = (x, y) => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; const result = gcd(a, b); return { content: [ { type: "text", text: `The greatest common measure of ${a} and ${b} is ${result}` } ] }; }); // LCM (Least Common Multiple) server.registerTool("lcm", { title: "LCM tool", description: "Calculate the least common multiple of two numbers", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const gcd = (x, y) => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; const lcm = (a, b) => (a * b) / gcd(a, b); return { content: [ { type: "text", text: `The least common multiple of ${a} and ${b} is ${lcm(a, b)}` } ] }; }); // Division server.registerTool("divide", { title: "Division tool", description: "Divide a by b (a / b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { if (b === 0) { return { content: [ { type: "text", text: `Error: Division by zero is not allowed.` } ], isError: true }; } const quotient = a / b; return { content: [ { type: "text", text: `The quotient of ${a} divided by ${b} is ${quotient}` } ] }; }); // Number Comparison Tools // Less Than server.registerTool("lessThan", { title: "Less Than comparison tool", description: "Compare if a is less than b (a < b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const result = a < b; return { content: [ { type: "text", text: `${a} is ${result ? '' : 'not '}less than ${b}` } ] }; }); // Greater Than server.registerTool("greaterThan", { title: "Greater Than comparison tool", description: "Compare if a is greater than b (a > b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const result = a > b; return { content: [ { type: "text", text: `${a} is ${result ? '' : 'not '}greater than ${b}` } ] }; }); // Less Than or Equal server.registerTool("lessThanOrEqual", { title: "Less Than or Equal comparison tool", description: "Compare if a is less than or equal to b (a <= b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const result = a <= b; return { content: [ { type: "text", text: `${a} is ${result ? '' : 'not '}less than or equal to ${b}` } ] }; }); // Greater Than or Equal server.registerTool("greaterThanOrEqual", { title: "Greater Than or Equal comparison tool", description: "Compare if a is greater than or equal to b (a >= b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const result = a >= b; return { content: [ { type: "text", text: `${a} is ${result ? '' : 'not '}greater than or equal to ${b}` } ] }; }); // General Compare server.registerTool("compare", { title: "General comparison tool", description: "Compare two numbers and return -1, 0, or 1 (-1 if a < b, 0 if a = b, 1 if a > b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { let result; let description; if (a < b) { result = -1; description = `${a} is less than ${b}`; } else if (a > b) { result = 1; description = `${a} is greater than ${b}`; } else { result = 0; description = `${a} is equal to ${b}`; } return { content: [ { type: "text", text: `Comparison result: ${result} (${description})` } ] }; }); // Register a “greet” resource: returns a greeting based on name parameter server.registerResource("greet", new ResourceTemplate("greet://{name}", { list: undefined }), { title: "Greeting Resource", description: "Generate a greeting for a given name" }, async (uri, { name }) => { return { contents: [ { uri: uri.href, text: `Hello, ${name}!` } ] }; }); // Set up transport (stdio in this case, so MCP messages come via stdin/stdout) const transport = new StdioServerTransport(); // Connect and start handling requests await server.connect(transport); console.log("MCP server started (stdio transport)"); } main().catch(err => { console.error("Error starting MCP server:", err); process.exit(1); });

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/codewithmsunke/MCPMathTools'

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