Skip to main content
Glama

mode

Identifies the most frequently occurring number in a given list of numbers, enabling quick and accurate statistical analysis for data-driven tasks.

Instructions

Finds the most common number in a list of numbers

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
numbersYesArray of numbers to find the mode of

Implementation Reference

  • Handler function that executes the 'mode' tool: invokes Statistics.mode on input numbers and returns MCP-formatted text response with mode values and frequency.
    }, async ({ numbers }) => {
        const value = Statistics.mode(numbers);
        return {
            content: [{
                    type: "text",
                    text: `Entries (${value.modeResult.join(', ')}) appeared ${value.maxFrequency} times`
                }]
        };
    });
  • build/index.js:148-158 (registration)
    Registration of the 'mode' MCP tool on the mathServer, including name, description, input schema, and handler function.
    mathServer.tool("mode", "Finds the most common number in a list of numbers", {
        numbers: z.array(z.number()).describe("Array of numbers to find the mode of")
    }, async ({ numbers }) => {
        const value = Statistics.mode(numbers);
        return {
            content: [{
                    type: "text",
                    text: `Entries (${value.modeResult.join(', ')}) appeared ${value.maxFrequency} times`
                }]
        };
    });
  • Zod input schema for the 'mode' tool: requires an array of numbers.
    numbers: z.array(z.number()).describe("Array of numbers to find the mode of")
  • Helper function Statistics.mode that computes the mode(s) of an array: uses Map for frequency count, identifies max frequency, collects all values with that frequency.
    static mode(numbers) {
        const modeMap = new Map();
        //Set each entry parameter into the map and assign it the number of times it appears in the list
        numbers.forEach((value) => {
            if (modeMap.has(value)) {
                modeMap.set(value, modeMap.get(value) + 1);
            }
            else {
                modeMap.set(value, 1);
            }
        });
        //Find the max frequency in the map
        let maxFrequency = 0;
        for (const numberFrequency of modeMap.values()) {
            if (numberFrequency > maxFrequency) {
                maxFrequency = numberFrequency;
            }
        }
        const modeResult = [];
        //Find the entries with the highest frequency
        for (const [key, value] of modeMap.entries()) {
            if (value === maxFrequency) {
                modeResult.push(key);
            }
        }
        return {
            modeResult: modeResult,
            maxFrequency: maxFrequency
        };
    }
Install Server

Other Tools

Related 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/EthanHenrickson/math-mcp'

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