count_letter
Count occurrences of a specific letter in text, providing positions, visual breakdown, and density percentage. Analyze text at character level to overcome tokenization limitations.
Instructions
Count occurrences of a specific letter in text.
Returns the count, positions, a visual breakdown showing where each letter appears, and a density percentage.
Args:
text (string): The text to analyze
letter (string): The single letter to count
case_sensitive (boolean): Whether to match case exactly (default: false)
Returns: count, positions array, visual breakdown, and density summary.
Example: count_letter("strawberry", "r") → count: 3, positions: [2, 5, 8]
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text to analyze | |
| letter | Yes | The letter to count | |
| case_sensitive | No | Match case exactly |
Implementation Reference
- src/tools/counting.ts:27-53 (handler)Core implementation of the count_letter tool logic: normalizes case if needed, finds all positions of the letter via loop, computes visual breakdown and density summary using helpers, returns structured output.export function countLetter(input: CountLetterInput): CountLetterOutput { const { text, letter, case_sensitive } = input; // Normalize the letter to search for const searchLetter = case_sensitive ? letter : letter.toLowerCase(); const searchText = case_sensitive ? text : text.toLowerCase(); const positions: number[] = []; for (let i = 0; i < searchText.length; i++) { if (searchText[i] === searchLetter) { positions.push(i); } } const visual = createVisualBreakdown(text, positions, letter); const density = createDensitySummary(positions.length, text.length, letter); return { text, letter, count: positions.length, case_sensitive, positions, visual, density, }; }
- src/tools/counting.ts:11-25 (schema)TypeScript interfaces defining the input schema (text, letter, case_sensitive) and output schema (including count, positions, visual, density) for the countLetter function.export interface CountLetterInput { text: string; letter: string; case_sensitive: boolean; } export interface CountLetterOutput { text: string; letter: string; count: number; case_sensitive: boolean; positions: number[]; visual: string; density: string; }
- src/index.ts:52-88 (registration)MCP server registration of the 'count_letter' tool, including title, description, Zod input schema validation, annotations, and async handler that invokes countLetter and formats MCP response with density, visual, count, and positions.server.registerTool( "count_letter", { title: "Count Letter", description: `Count occurrences of a specific letter in text. Returns the count, positions, a visual breakdown showing where each letter appears, and a density percentage. Args: - text (string): The text to analyze - letter (string): The single letter to count - case_sensitive (boolean): Whether to match case exactly (default: false) Returns: count, positions array, visual breakdown, and density summary. Example: count_letter("strawberry", "r") → count: 3, positions: [2, 5, 8]`, inputSchema: z.object({ text: z.string().min(1).describe("The text to analyze"), letter: z.string().length(1).describe("The letter to count"), case_sensitive: z.boolean().default(false).describe("Match case exactly"), }).strict(), annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }, }, async (params) => { const result = countLetter({ text: params.text, letter: params.letter, case_sensitive: params.case_sensitive, }); return { content: [{ type: "text" as const, text: `${result.density}\n\n${result.visual}\n\nResult: ${JSON.stringify({ count: result.count, positions: result.positions })}` }], }; } );