word_length
Measure text length with detailed breakdown including total characters, length without spaces, space count, and word count. Configure whether to include spaces in the calculation.
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 core handler function implementing the word_length tool logic: computes total length (with/without spaces), space count, word count, and generates a description.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:166-178 (schema)TypeScript interfaces defining the input schema (text and count_spaces option) and output schema (lengths, counts, description) for the word_length tool.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 server registration of the 'word_length' tool, including title, description, Zod inputSchema validation, annotations, and async wrapper calling the wordLength handler.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 }], }; } );