word_length
Measure text length with detailed character analysis. Calculate total characters, length without spaces, space count, and word count to overcome tokenization limitations in text processing.
Instructions
Get the exact length of text with detailed breakdown.
Args:
text (string): The text to measure
count_spaces (boolean): Include spaces in length (default: true)
Returns: Total length, length without spaces, space count, word count.
Example: word_length("hello world") → 11 total, 10 without spaces, 2 words
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text to measure | |
| count_spaces | No | Include spaces in length |
Implementation Reference
- src/tools/spelling.ts:180-196 (handler)The main handler function implementing the word_length tool logic, computing text length with and without spaces, word count, and space count.export function wordLength(input: WordLengthInput): WordLengthOutput { const { text, count_spaces } = input; const characters = [...text]; const spaceCount = characters.filter(c => c === ' ').length; const words = text.split(/\s+/).filter(w => w.length > 0); const length = count_spaces ? characters.length : characters.length - spaceCount; return { text, length, length_without_spaces: characters.length - spaceCount, space_count: spaceCount, word_count: words.length, description: `"${text}" has ${characters.length} total characters (${characters.length - spaceCount} letters, ${spaceCount} spaces).`, }; }
- src/tools/spelling.ts:163-178 (schema)TypeScript interfaces defining input and output types for the wordLength handler.// word_length - Get the exact length of text // ============================================================================ export interface WordLengthInput { text: string; count_spaces: boolean; } export interface WordLengthOutput { text: string; length: number; length_without_spaces: number; space_count: number; word_count: number; description: string; }
- src/index.ts:311-339 (registration)MCP tool registration for 'word_length', including Zod input schema, description, and handler wrapper that calls the wordLength function.server.registerTool( "word_length", { title: "Word Length", description: `Get the exact length of text with detailed breakdown. Args: - text (string): The text to measure - count_spaces (boolean): Include spaces in length (default: true) Returns: Total length, length without spaces, space count, word count. Example: word_length("hello world") → 11 total, 10 without spaces, 2 words`, inputSchema: z.object({ text: z.string().describe("The text to measure"), count_spaces: z.boolean().default(true).describe("Include spaces in length"), }).strict(), annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }, }, async (params) => { const result = wordLength({ text: params.text, count_spaces: params.count_spaces, }); return { content: [{ type: "text" as const, text: result.description }], }; } );