Skip to main content
Glama
cablate

Simple Document Processing MCP Server

text_splitter

Split large text files into smaller segments using line count or custom delimiters for easier processing and management.

Instructions

Split text file by specified delimiter or line count

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputPathYesPath to the input text file
outputDirYesDirectory where split files should be saved
splitByYesSplit method: by line count or delimiter
valueYesLine count (number) or delimiter string

Implementation Reference

  • The core handler function `splitText` that executes the text splitting logic: reads the input file, splits content by lines or delimiter, and writes split parts to numbered files in the output directory.
    export async function splitText( inputPath: string, outputDir: string, splitBy: "lines" | "delimiter", value: string ) { try { console.error(`Starting text splitting...`); console.error(`Input file: ${inputPath}`); console.error(`Output directory: ${outputDir}`); console.error(`Split by: ${splitBy}`); console.error(`Value: ${value}`); // 確保輸出目錄存在 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 parts: string[] = []; if (splitBy === "lines") { const lineCount = parseInt(value, 10); if (isNaN(lineCount) || lineCount <= 0) { throw new Error("Invalid line count"); } const lines = content.split("\n"); for (let i = 0; i < lines.length; i += lineCount) { parts.push(lines.slice(i, i + lineCount).join("\n")); } } else { parts.push(...content.split(value)); } const results: string[] = []; for (let i = 0; i < parts.length; i++) { const outputPath = path.join(outputDir, `part_${uniqueId}_${i + 1}.txt`); await fs.writeFile(outputPath, parts[i]); results.push(outputPath); console.error(`Written part ${i + 1} to ${outputPath}`); } return { success: true, data: `Successfully split text into ${parts.length} parts: ${results.join( ", " )}`, }; } catch (error) { console.error(`Error in splitText:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
  • Tool schema definition for 'text_splitter', including name, description, and inputSchema specifying parameters for input file, output directory, split method, and value.
    export const TEXT_SPLIT_TOOL: Tool = { name: "text_splitter", description: "Split text file by specified delimiter or line count", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input text file", }, outputDir: { type: "string", description: "Directory where split files should be saved", }, splitBy: { type: "string", enum: ["lines", "delimiter"], description: "Split method: by line count or delimiter", }, value: { type: "string", description: "Line count (number) or delimiter string", }, }, required: ["inputPath", "outputDir", "splitBy", "value"], }, };
  • Registration of the text_splitter tool: imports TEXT_SPLIT_TOOL from txtTools.ts and includes it in the exported `tools` array, which is used by the MCP server to list available tools.
    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:302-320 (registration)
    Tool dispatch/registration in the main MCP server request handler: matches tool name 'text_splitter', extracts arguments, calls the `splitText` handler, and formats the response.
    if (name === "text_splitter") { const { inputPath, outputDir, splitBy, value } = args as { inputPath: string; outputDir: string; splitBy: "lines" | "delimiter"; value: string; }; const result = await splitText(inputPath, outputDir, splitBy, value); if (!result.success) { return { content: [{ type: "text", text: `Error: ${result.error}` }], isError: true, }; } return { content: [{ type: "text", text: fileOperationResponse(result.data) }], isError: false, }; }

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