count_chars
Counts the characters in a specified text file, excluding spaces and line breaks. Supports Windows or WSL/Linux file paths for accurate character analysis.
Instructions
ファイルの文字数を計測します。絶対パスを指定してください(Windows形式 C:\Users...、またはWSL/Linux形式 /c/Users/... のどちらも可)。スペースや改行を除いた実質的な文字数をカウントします。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | 文字数をカウントするファイルのパス(Windows形式かWSL/Linux形式の絶対パスを推奨) |
Implementation Reference
- src/index.ts:476-491 (handler)The handler function for the 'count_chars' tool. It resolves the file path, reads the file content using fs.readFileSync, and calls the countTextCharsImpl helper to compute the character count.async ({ filePath }) => { try { // ファイルパスを解決 const resolvedPath = resolveFilePath(filePath); const fileContent = fs.readFileSync(resolvedPath, 'utf8'); return this.countTextCharsImpl(fileContent, `ファイル '${resolvedPath}'`); } catch (error: any) { return { content: [{ type: 'text' as const, text: `ファイル読み込みエラー: ${error.message}` }], isError: true }; } }
- src/index.ts:473-475 (schema)Zod input schema defining the 'filePath' parameter for the tool.{ filePath: z.string().describe('文字数をカウントするファイルのパス(Windows形式かWSL/Linux形式の絶対パスを推奨)') },
- src/index.ts:470-492 (registration)Registration of the 'count_chars' tool on the McpServer instance using the tool() method, specifying name, description, schema, and handler.this.server.tool( 'count_chars', 'ファイルの文字数を計測します。絶対パスを指定してください(Windows形式 C:\\Users\\...、またはWSL/Linux形式 /c/Users/... のどちらも可)。スペースや改行を除いた実質的な文字数をカウントします。', { filePath: z.string().describe('文字数をカウントするファイルのパス(Windows形式かWSL/Linux形式の絶対パスを推奨)') }, async ({ filePath }) => { try { // ファイルパスを解決 const resolvedPath = resolveFilePath(filePath); const fileContent = fs.readFileSync(resolvedPath, 'utf8'); return this.countTextCharsImpl(fileContent, `ファイル '${resolvedPath}'`); } catch (error: any) { return { content: [{ type: 'text' as const, text: `ファイル読み込みエラー: ${error.message}` }], isError: true }; } } );
- src/index.ts:180-201 (helper)Helper method that implements the core character counting logic, excluding spaces, newlines, and returns formatted text response.private countTextCharsImpl(text: string, sourceName: string = 'テキスト') { try { // 改行とスペースを除外した文字数 const contentWithoutSpacesAndNewlines = text.replace(/[\s\n\r]/g, ''); const effectiveCharCount = contentWithoutSpacesAndNewlines.length; return { content: [{ type: 'text' as const, text: `${sourceName}の文字数: ${effectiveCharCount}文字(改行・スペース除外)` }] }; } catch (error: any) { return { content: [{ type: 'text' as const, text: `エラーが発生しました: ${error.message}` }], isError: true }; } }