Skip to main content
Glama
cablate

Simple Document Processing MCP Server

text_formatter

Format text files by applying proper indentation and line spacing to improve readability and structure.

Instructions

Format text with proper indentation and line spacing

Input Schema

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

Implementation Reference

  • Implements the core logic for the 'text_formatter' tool: reads UTF-8 text file, formats by trimming lines and removing consecutive empty lines, generates unique output filename, saves to specified 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",
        };
      }
    }
  • Defines the Tool object for 'text_formatter' including name, description, and inputSchema specifying required inputPath and outputDir parameters.
    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)
    Registers and dispatches 'text_formatter' tool calls within the main CallToolRequest handler by invoking the formatText function and formatting the response.
    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,
      };
    }
  • Includes TEXT_FORMAT_TOOL in the exported 'tools' array used by ListToolsRequest to advertise available tools.
    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 to generate unique IDs for output filenames, used in the text_formatter handler.
    function generateUniqueId(): string {
      return randomBytes(9).toString("hex");
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool formats text but doesn't explain what 'proper indentation and line spacing' means, whether it modifies files in-place or creates new ones, or any error handling. This is inadequate for a tool with file operations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence with no wasted words. It's front-loaded with the core purpose, making it easy to scan and understand quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of file formatting operations, no annotations, and no output schema, the description is incomplete. It lacks details on behavior, error cases, or output specifics, which are crucial for an agent to use this tool effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters (inputPath and outputDir). The description doesn't add any meaning beyond this, such as file format expectations or output naming conventions, resulting in the baseline score of 3.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Format text with proper indentation and line spacing.' It specifies the action (format) and the resource (text), but doesn't differentiate it from sibling tools like 'html_formatter' or 'text_splitter', which prevents a score of 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'html_formatter' for HTML-specific formatting or 'text_splitter' for dividing text, leaving the agent without context for tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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

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