text_diff
Compare two text files to identify differences and generate a detailed diff report for document analysis and version tracking.
Instructions
Compare two text files and show differences
Input Schema
TableJSON 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 |
Implementation Reference
- src/tools/txtTools.ts:204-252 (handler)Implements the text_diff tool: reads two UTF-8 text files, computes line-level differences using the 'diff' library, formats the diff output, and saves it to a new file in the specified output directory.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 input schema specifying two file paths 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 central CallToolRequestSchema handler, dispatches 'text_diff' calls to the compareTexts function, handles the result, 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/index.ts:47-49 (registration)Registers the list of all tools (including text_diff via imported tools array) for the ListToolsRequestSchema.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools, }));
- src/tools/_index.ts:9-9 (registration)Aggregates and exports the array of all Tool objects, including TEXT_DIFF_TOOL, which is used by the server for listing 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];