Skip to main content
Glama
codewithmsunke

MCP Math Server

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
NameRequiredDescriptionDefault
aYes
bYes

Implementation Reference

  • 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}` }
        ]
      };
    }
  • 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}` }
          ]
        };
      }
    );
  • 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;
    };
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/codewithmsunke/MCPMathTools'

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