Skip to main content
Glama
by qpd-v

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

NameRequiredDescriptionDefault
filePathYesPath 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

  • 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) }; }
  • 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)}` } ] }; } });
  • 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"] } } ] }));
  • 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; }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/qpd-v/mcp-wordcounter'

If you have feedback or need assistance with the MCP directory API, please join our Discord server