analyze_sentence
Count how many times a specific letter appears in each word of a sentence, with per-word breakdown and position tracking.
Instructions
Analyze a sentence word-by-word for a specific letter.
Shows exactly how many times a letter appears in each word.
Args:
text (string): The sentence to analyze
letter (string): The letter to count
case_sensitive (boolean): Match case exactly (default: false)
Returns: Per-word breakdown with counts and positions.
Example: analyze_sentence("The strawberry was very ripe", "r") → per-word counts
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The sentence to analyze | |
| letter | Yes | The letter to count | |
| case_sensitive | No | Match case exactly |
Implementation Reference
- src/tools/analysis.ts:125-163 (handler)The core handler function that implements the logic for analyzing a sentence word-by-word, counting occurrences of a specific letter in each word, computing totals, and generating a breakdown table.export function analyzeSentence(input: AnalyzeSentenceInput): AnalyzeSentenceOutput { const { text, letter, case_sensitive } = input; // Split into words while tracking positions const wordMatches = [...text.matchAll(/\S+/g)]; const words: WordAnalysis[] = wordMatches.map((match, idx) => { const word = match[0]; const result = countLetter({ text: word, letter, case_sensitive }); return { word, position: idx + 1, length: [...word].length, letter_count: result.count, letter_positions: result.positions, }; }); const total_count = words.reduce((sum, w) => sum + w.letter_count, 0); // Create breakdown table const breakdown_table = words .map(w => `${w.position}. "${w.word}" (${w.length} chars): ${w.letter_count} '${letter}'${w.letter_positions.length > 0 ? ` at positions [${w.letter_positions.join(', ')}]` : ''}`) .join('\n'); const summary = `Found ${total_count} occurrence${total_count === 1 ? '' : 's'} of '${letter}' across ${words.length} word${words.length === 1 ? '' : 's'}.`; return { text, letter, case_sensitive, total_count, word_count: words.length, words, summary, breakdown_table, }; }
- src/tools/analysis.ts:100-123 (schema)TypeScript interfaces defining the input parameters, per-word analysis structure, and output format for the analyze_sentence tool.export interface AnalyzeSentenceInput { text: string; letter: string; case_sensitive: boolean; } export interface WordAnalysis { word: string; position: number; length: number; letter_count: number; letter_positions: number[]; } export interface AnalyzeSentenceOutput { text: string; letter: string; case_sensitive: boolean; total_count: number; word_count: number; words: WordAnalysis[]; summary: string; breakdown_table: string; }
- src/index.ts:412-445 (registration)Registers the 'analyze_sentence' tool with the MCP server, including Zod input schema for validation, tool metadata, and an async wrapper that calls the analyzeSentence handler and formats the response.server.registerTool( "analyze_sentence", { title: "Analyze Sentence", description: `Analyze a sentence word-by-word for a specific letter. Shows exactly how many times a letter appears in each word. Args: - text (string): The sentence to analyze - letter (string): The letter to count - case_sensitive (boolean): Match case exactly (default: false) Returns: Per-word breakdown with counts and positions. Example: analyze_sentence("The strawberry was very ripe", "r") → per-word counts`, inputSchema: z.object({ text: z.string().min(1).describe("The sentence 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 = analyzeSentence({ text: params.text, letter: params.letter, case_sensitive: params.case_sensitive, }); return { content: [{ type: "text" as const, text: `${result.summary}\n\n${result.breakdown_table}` }], }; } );