gcm
Calculate the greatest common divisor (GCD) of two numbers to find the largest integer that divides both without a remainder.
Instructions
Calculate the greatest common measure (GCD) of two numbers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | ||
| b | Yes |
Implementation Reference
- src/index.ts:120-137 (handler)Handler function that executes the GCD (Greatest Common Divisor) logic using the Euclidean algorithm, takes inputs a and b, 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 result = gcd(a, b); return { content: [ { type: "text", text: `The greatest common measure of ${a} and ${b} is ${result}` } ] }; }
- src/index.ts:115-119 (schema)Input schema and metadata (title, description) for the gcm tool, validating two numeric inputs a and b using Zod.{ title: "GCM tool", description: "Calculate the greatest common measure (GCD) of two numbers", inputSchema: { a: z.number(), b: z.number() }, },
- src/index.ts:113-138 (registration)Registers the 'gcm' tool with the MCP server, specifying name, schema, and inline handler function.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: 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 result = gcd(a, b); return { content: [ { type: "text", text: `The greatest common measure of ${a} and ${b} is ${result}` } ] }; } );
- src/index.ts:121-130 (helper)Inner helper function implementing the Euclidean algorithm for GCD computation.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; };