text_diff
Compare two text files to identify differences and save the result in a specified directory for easy analysis and document processing.
Instructions
Compare two text files and show differences
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file1Path | Yes | Path to the first text file | |
| file2Path | Yes | Path to the second text file | |
| outputDir | Yes | Directory where diff result should be saved |
Input Schema (JSON Schema)
{
"properties": {
"file1Path": {
"description": "Path to the first text file",
"type": "string"
},
"file2Path": {
"description": "Path to the second text file",
"type": "string"
},
"outputDir": {
"description": "Directory where diff result should be saved",
"type": "string"
}
},
"required": [
"file1Path",
"file2Path",
"outputDir"
],
"type": "object"
}
Implementation Reference
- src/tools/txtTools.ts:204-252 (handler)The handler function compareTexts that reads two text files, computes line-by-line differences using the 'diff' library's diffLines function, formats the diff with prefixes (+ for added, - for removed, space for unchanged), writes it to a file in outputDir, and returns success/error info.export async function compareTexts( file1Path: string, file2Path: string, outputDir: string ) { try { console.error(`Starting text comparison...`); console.error(`File 1: ${file1Path}`); console.error(`File 2: ${file2Path}`); 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 text1 = await fs.readFile(file1Path, "utf-8"); const text2 = await fs.readFile(file2Path, "utf-8"); const diff = diffLines(text1, text2); const diffResult = diff .map((part) => { const prefix = part.added ? "+ " : part.removed ? "- " : " "; return prefix + part.value; }) .join(""); const outputPath = path.join(outputDir, `diff_${uniqueId}.txt`); await fs.writeFile(outputPath, diffResult); console.error(`Written diff result to ${outputPath}`); return { success: true, data: `Successfully compared texts: ${outputPath}`, }; } catch (error) { console.error(`Error in compareTexts:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
- src/tools/txtTools.ts:61-82 (schema)Defines the Tool object for 'text_diff' including name, description, and inputSchema specifying required paths for two input files and output directory.export const TEXT_DIFF_TOOL: Tool = { name: "text_diff", description: "Compare two text files and show differences", inputSchema: { type: "object", properties: { file1Path: { type: "string", description: "Path to the first text file", }, file2Path: { type: "string", description: "Path to the second text file", }, outputDir: { type: "string", description: "Directory where diff result should be saved", }, }, required: ["file1Path", "file2Path", "outputDir"], }, };
- src/index.ts:283-300 (registration)In the main server request handler for CallToolRequestSchema, checks if name === 'text_diff', extracts arguments, calls the compareTexts handler, and formats the response.if (name === "text_diff") { const { file1Path, file2Path, outputDir } = args as { file1Path: string; file2Path: string; outputDir: string; }; const result = await compareTexts(file1Path, file2Path, outputDir); 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:9-9 (registration)Includes TEXT_DIFF_TOOL in the exported 'tools' array used by ListToolsRequestSchema handler to list 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];
- src/tools/txtTools.ts:8-10 (helper)Helper function generateUniqueId used to create unique filenames for output diff files.function generateUniqueId(): string { return randomBytes(9).toString("hex"); }