Skip to main content
Glama
codewithmsunke

MCP Math Server

lcm

Calculate the least common multiple of two numbers to find the smallest positive integer divisible by both values.

Instructions

Calculate the least common multiple of two numbers

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
aYes
bYes

Implementation Reference

  • Handler function for the 'lcm' tool that computes the least common multiple using an inline GCD function and returns a formatted text response.
    async ({ a, b }) => { const gcd = (x: number, y: number): number => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; const lcm = (a: number, b: number): number => (a * b) / gcd(a, b); return { content: [ { type: "text", text: `The least common multiple of ${a} and ${b} is ${lcm(a, b)}` } ] }; }
  • Input schema for the 'lcm' tool defining parameters a and b as numbers using Zod validation.
    { title: "LCM tool", description: "Calculate the least common multiple of two numbers", inputSchema: { a: z.number(), b: z.number() }, },
  • src/index.ts:140-166 (registration)
    Registration of the 'lcm' MCP tool via server.registerTool, including schema, title, description, and inline handler implementation.
    // 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: number, y: number): number => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; const lcm = (a: number, b: number): number => (a * b) / gcd(a, b); return { content: [ { type: "text", text: `The least common multiple of ${a} and ${b} is ${lcm(a, b)}` } ] }; } );
  • Inline helper function 'gcd' implementing the Euclidean algorithm for greatest common divisor, used in LCM calculation.
    const gcd = (x: number, y: number): number => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; };
  • Inline helper function 'lcm' that computes least common multiple using the formula LCM(a,b) = |a*b| / GCD(a,b).
    const lcm = (a: number, b: number): number => (a * b) / gcd(a, b);

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