text_splitter
Split text files by line count or delimiter into multiple output files. Specify input path, output directory, and split method for efficient document segmentation.
Instructions
Split text file by specified delimiter or line count
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Path to the input text file | |
| outputDir | Yes | Directory where split files should be saved | |
| splitBy | Yes | Split method: by line count or delimiter | |
| value | Yes | Line count (number) or delimiter string |
Implementation Reference
- src/tools/txtTools.ts:254-317 (handler)The main handler function for the text_splitter tool. Reads a text file and splits it either by line count (chunking every N lines) or by a delimiter string. Each part is written to a separate file (part_{id}_{n}.txt).
// 文字分割實作 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", }; } } - src/tools/txtTools.ts:84-111 (schema)The input schema definition for the text_splitter tool. Defines required parameters: inputPath (string), outputDir (string), splitBy ('lines'|'delimiter'), and value (string - either line count or delimiter).
// 文字分割工具 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"], }, }; - src/index.ts:302-320 (registration)The tool call handler in the MCP server that dispatches to the splitText function when name is 'text_splitter'. It destructures args (inputPath, outputDir, splitBy, value) and calls splitText(...).
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, }; } - src/tools/_index.ts:7-17 (registration)Re-exports TEXT_SPLIT_TOOL from txtTools.ts and includes it in the tools array used by the MCP server's ListToolsRequestSchema handler.
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]; export * from "./documentReader.js"; export * from "./docxTools.js"; export * from "./excelTools.js"; export * from "./formatConverterPlus.js"; export * from "./htmlTools.js"; export * from "./pdfTools.js"; export * from "./txtTools.js";