UNPKG

mcp-wordcounter

Version:

A Model Context Protocol server for analyzing text documents with word and character counting capabilities

45 lines (44 loc) 1.33 kB
import * as fs from 'fs/promises'; /** * A class that provides text analysis functionality */ export class WordCounter { /** * Analyzes text from a file to count words and characters * @param filePath Path to the file to analyze * @returns Object containing word and character counts */ async analyzeFile(filePath) { const text = await fs.readFile(filePath, 'utf-8'); return { words: this.countWords(text), characters: this.countCharacters(text), charactersNoSpaces: this.countCharactersNoSpaces(text) }; } /** * Counts the number of words in a text * Words are defined as sequences of characters separated by whitespace */ countWords(text) { // 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; } /** * Counts all characters including whitespace */ countCharacters(text) { return text.length; } /** * Counts characters excluding whitespace */ countCharactersNoSpaces(text) { return text.replace(/\s/g, '').length; } }