Skip to main content
Glama
cablate

Simple Document Processing MCP Server

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
NameRequiredDescriptionDefault
file1PathYesPath to the first text file
file2PathYesPath to the second text file
outputDirYesDirectory where diff result should be saved

Implementation Reference

  • 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", }; } }
  • 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, }));
  • 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];

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