Skip to main content
Glama

Simple Document Processing MCP Server

text_formatter

Apply consistent indentation and line spacing to text files. Specify input file path and output directory to generate cleanly formatted documents.

Instructions

Format text with proper indentation and line spacing

Input Schema

NameRequiredDescriptionDefault
inputPathYesPath to the input text file
outputDirYesDirectory where formatted file should be saved

Input Schema (JSON Schema)

{ "properties": { "inputPath": { "description": "Path to the input text file", "type": "string" }, "outputDir": { "description": "Directory where formatted file should be saved", "type": "string" } }, "required": [ "inputPath", "outputDir" ], "type": "object" }

Implementation Reference

  • The main handler function that reads a text file, formats it by trimming lines, removing consecutive empty lines, and saves the result to a new file in the output directory.
    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", }; } }
  • The Tool definition including name, description, and inputSchema for validation.
    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/index.ts:265-281 (registration)
    The dispatch handler in the main server that routes calls to 'text_formatter' to the formatText function.
    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, }; }
  • Import of TEXT_FORMAT_TOOL and inclusion in the tools array used for listing available tools via ListToolsRequest.
    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];
  • Helper function used in formatText to generate unique filename IDs.
    function generateUniqueId(): string { return randomBytes(9).toString("hex"); }

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/cablate/mcp-doc-forge'

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