text_formatter
Format text files by applying proper indentation and line spacing. Specify input file and output directory to produce consistently formatted documents.
Instructions
Format text with proper indentation and line spacing
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Path to the input text file | |
| outputDir | Yes | Directory where formatted file should be saved |
Implementation Reference
- src/tools/txtTools.ts:160-201 (handler)The formatText function is the core handler for the text_formatter tool. It reads a text file, trims whitespace from each line, removes consecutive blank lines, and writes the formatted result to a new file.
export async function formatText(inputPath: string, outputDir: string) { try { console.error(`Starting text formatting...`); console.error(`Input file: ${inputPath}`); console.error(`Output directory: ${outputDir}`); // 確保輸出目錄存在 try { await fs.access(outputDir); console.error(`Output directory exists: ${outputDir}`); } catch { console.error(`Creating output directory: ${outputDir}`); await fs.mkdir(outputDir, { recursive: true }); console.error(`Created output directory: ${outputDir}`); } const uniqueId = generateUniqueId(); const content = await fs.readFile(inputPath, "utf-8"); // 基本格式化:移除多餘空白行,統一縮排 const formatted = content .split("\n") .map((line) => line.trim()) .filter((line, index, array) => !(line === "" && array[index - 1] === "")) .join("\n"); const outputPath = path.join(outputDir, `formatted_${uniqueId}.txt`); await fs.writeFile(outputPath, formatted); console.error(`Written formatted text to ${outputPath}`); return { success: true, data: `Successfully formatted text: ${outputPath}`, }; } catch (error) { console.error(`Error in formatText:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } } - src/tools/txtTools.ts:40-58 (schema)The TEXT_FORMAT_TOOL constant defines the schema for text_formatter: name 'text_formatter', description, and inputSchema requiring 'inputPath' (string) and 'outputDir' (string).
// 文字格式化工具 export const TEXT_FORMAT_TOOL: Tool = { name: "text_formatter", description: "Format text with proper indentation and line spacing", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input text file", }, outputDir: { type: "string", description: "Directory where formatted file should be saved", }, }, required: ["inputPath", "outputDir"], }, }; - src/tools/_index.ts:7-9 (registration)TEXT_FORMAT_TOOL is imported from txtTools.ts and included in the exported 'tools' array that registers all tools with the MCP server.
import { TEXT_DIFF_TOOL, TEXT_ENCODING_CONVERT_TOOL, TEXT_FORMAT_TOOL, TEXT_SPLIT_TOOL } from "./txtTools.js"; export const tools = [DOCUMENT_READER_TOOL, PDF_MERGE_TOOL, PDF_SPLIT_TOOL, DOCX_TO_PDF_TOOL, DOCX_TO_HTML_TOOL, HTML_CLEAN_TOOL, HTML_TO_TEXT_TOOL, HTML_TO_MARKDOWN_TOOL, HTML_EXTRACT_RESOURCES_TOOL, HTML_FORMAT_TOOL, TEXT_DIFF_TOOL, TEXT_SPLIT_TOOL, TEXT_FORMAT_TOOL, TEXT_ENCODING_CONVERT_TOOL, EXCEL_READ_TOOL, FORMAT_CONVERTER_TOOL]; - src/index.ts:265-281 (handler)The server's CallToolRequestSchema handler dispatches to formatText when name === 'text_formatter', extracting inputPath and outputDir from args and returning the result.
if (name === "text_formatter") { const { inputPath, outputDir } = args as { inputPath: string; outputDir: string; }; const result = await formatText(inputPath, outputDir); if (!result.success) { return { content: [{ type: "text", text: `Error: ${result.error}` }], isError: true, }; } return { content: [{ type: "text", text: fileOperationResponse(result.data) }], isError: false, }; }