analyze_text
Count words and characters in a text document to analyze its structure and size. Use the tool to obtain precise statistics for any text file, aiding in effective document analysis.
Instructions
Count words and characters in a text document
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Path to the text file to analyze |
Input Schema (JSON Schema)
{
"properties": {
"filePath": {
"description": "Path to the text file to analyze",
"type": "string"
}
},
"required": [
"filePath"
],
"type": "object"
}
Implementation Reference
- src/wordCounter.ts:12-19 (handler)Core handler function that reads the file content and computes word count, total characters, and non-space characters.public async analyzeFile(filePath: string) { const text = await fs.readFile(filePath, 'utf-8'); return { words: this.countWords(text), characters: this.countCharacters(text), charactersNoSpaces: this.countCharactersNoSpaces(text) }; }
- src/index.ts:48-88 (handler)MCP server request handler for tool execution (CallToolRequestSchema) that specifically handles 'analyze_text' tool calls, including name check, parameter validation, delegation to WordCounter, response formatting, and error handling.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== "analyze_text") { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } const filePath = request.params.arguments?.filePath; if (!filePath || typeof filePath !== "string") { throw new McpError( ErrorCode.InvalidParams, "File path parameter is required and must be a string" ); } try { const stats = await wordCounter.analyzeFile(filePath); return { content: [ { type: "text", text: `Analysis Results: • Word count: ${stats.words} • Character count (including spaces): ${stats.characters} • Character count (excluding spaces): ${stats.charactersNoSpaces}` } ] }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error analyzing file: ${error instanceof Error ? error.message : String(error)}` } ] }; } });
- src/index.ts:30-45 (schema)Tool schema definition including name, description, and input schema requiring a 'filePath' string.{ name: "analyze_text", description: "Count words and characters in a text document", inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the text file to analyze" } }, required: ["filePath"] } } ] }));
- src/index.ts:28-45 (registration)Registers the ListToolsRequestHandler which exposes the 'analyze_text' tool with its schema.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: "analyze_text", description: "Count words and characters in a text document", inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the text file to analyze" } }, required: ["filePath"] } } ] }));
- src/wordCounter.ts:25-34 (helper)Helper method for counting words by splitting text on whitespace sequences.private countWords(text: string): number { // Handle empty or whitespace-only strings if (!text.trim()) { return 0; } // Split by whitespace and filter out empty strings const words = text.trim().split(/\s+/).filter(word => word.length > 0); return words.length; }