word_count
Count words and characters in text strings. Optionally calculate unique word counts for text analysis.
Instructions
Count words and characters in a string. Optionally count unique words.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text to analyse | |
| unique | No | Count unique words only (default: false) |
Implementation Reference
- src/services/text-service.ts:18-29 (handler)The logic implementation of the word_count tool.
wordCount(input: WordCountInput): WordCountResult { const words = input.text.trim().split(/\s+/).filter(Boolean); const result: WordCountResult = { text: input.text, wordCount: words.length, characterCount: input.text.length, }; if (input.unique) { result.uniqueWords = new Set(words.map((w) => w.toLowerCase())).size; } return result; } - src/types.ts:13-26 (schema)Type definitions for the input and output of the word_count tool.
/** Input for the word_count tool. */ export interface WordCountInput { text: string; /** Count unique words only. Defaults to false. */ unique?: boolean; } /** Result from the word_count tool. */ export interface WordCountResult { text: string; wordCount: number; characterCount: number; uniqueWords?: number; } - src/tools/text-tools.ts:27-44 (registration)Tool registration and handler invocation for word_count.
{ name: 'word_count', description: 'Count words and characters in a string. Optionally count unique words.', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'The text to analyse' }, unique: { type: 'boolean', description: 'Count unique words only (default: false)' }, }, required: ['text'], }, handler: async (args) => { return service.wordCount({ text: args.text as string, unique: args.unique as boolean | undefined, }); }, },