letter_frequency
Analyze character frequency in text to identify patterns, count occurrences, and understand distribution with customizable options for case sensitivity and character types.
Instructions
Get frequency distribution of all characters in text.
Provides a complete breakdown of character frequencies.
Args:
text (string): The text to analyze
case_sensitive (boolean): Distinguish upper/lowercase (default: false)
include_spaces (boolean): Include spaces in count (default: false)
include_punctuation (boolean): Include punctuation (default: false)
letters_only (boolean): Only count a-z letters (default: true)
Returns: Frequency map, sorted list, most/least common characters.
Example: letter_frequency("hello") → h: 1, e: 1, l: 2, o: 1
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text to analyze | |
| case_sensitive | No | Distinguish upper/lowercase | |
| include_spaces | No | Include spaces | |
| include_punctuation | No | Include punctuation | |
| letters_only | No | Only count a-z letters |
Implementation Reference
- src/tools/counting.ts:191-219 (handler)The core handler function that implements the letter frequency analysis logic, processing the input text according to options and computing frequency distribution, most/least common characters.export function letterFrequency(input: LetterFrequencyInput): LetterFrequencyOutput { const { text, case_sensitive, include_spaces, include_punctuation, letters_only } = input; const frequency: Record<string, number> = {}; const processedText = case_sensitive ? text : text.toLowerCase(); for (const char of processedText) { // Filter based on options if (letters_only && !/[a-zA-Z]/.test(char)) continue; if (!include_spaces && char === ' ') continue; if (!include_punctuation && /[^\w\s]/.test(char)) continue; frequency[char] = (frequency[char] || 0) + 1; } const entries = Object.entries(frequency).sort((a, b) => b[1] - a[1]); const most_common = entries.slice(0, 5).map(([char, count]) => ({ char, count })); const least_common = entries.slice(-5).reverse().map(([char, count]) => ({ char, count })); return { text, total_characters: Object.values(frequency).reduce((a, b) => a + b, 0), unique_characters: Object.keys(frequency).length, frequency, frequency_table: formatFrequencyTable(frequency), most_common, least_common, }; }
- src/tools/counting.ts:173-189 (schema)TypeScript interfaces defining the input parameters and output structure for the letterFrequency handler.export interface LetterFrequencyInput { text: string; case_sensitive: boolean; include_spaces: boolean; include_punctuation: boolean; letters_only: boolean; } export interface LetterFrequencyOutput { text: string; total_characters: number; unique_characters: number; frequency: Record<string, number>; frequency_table: string; most_common: Array<{ char: string; count: number }>; least_common: Array<{ char: string; count: number }>; }
- src/index.ts:166-205 (registration)MCP server registration of the 'letter_frequency' tool, including Zod input schema validation and wrapper that calls the handler and formats response.server.registerTool( "letter_frequency", { title: "Letter Frequency", description: `Get frequency distribution of all characters in text. Provides a complete breakdown of character frequencies. Args: - text (string): The text to analyze - case_sensitive (boolean): Distinguish upper/lowercase (default: false) - include_spaces (boolean): Include spaces in count (default: false) - include_punctuation (boolean): Include punctuation (default: false) - letters_only (boolean): Only count a-z letters (default: true) Returns: Frequency map, sorted list, most/least common characters. Example: letter_frequency("hello") → h: 1, e: 1, l: 2, o: 1`, inputSchema: z.object({ text: z.string().min(1).describe("The text to analyze"), case_sensitive: z.boolean().default(false).describe("Distinguish upper/lowercase"), include_spaces: z.boolean().default(false).describe("Include spaces"), include_punctuation: z.boolean().default(false).describe("Include punctuation"), letters_only: z.boolean().default(true).describe("Only count a-z letters"), }).strict(), annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }, }, async (params) => { const result = letterFrequency({ text: params.text, case_sensitive: params.case_sensitive, include_spaces: params.include_spaces, include_punctuation: params.include_punctuation, letters_only: params.letters_only, }); return { content: [{ type: "text" as const, text: `Frequency analysis of "${result.text}":\n\n${result.frequency_table}\n\nMost common: ${result.most_common.map(c => `'${c.char}': ${c.count}`).join(', ')}` }], }; } );