text_diff
Compare two text strings to identify line-by-line differences, showing additions with '+' and deletions with '-'.
Instructions
Compare two strings and show a simple line-by-line diff. Lines prefixed with '+' are additions, '-' are deletions, ' ' are unchanged.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text1 | Yes | First text (original) | |
| text2 | Yes | Second text (modified) |
Implementation Reference
- src/tools/text.ts:274-311 (handler)The implementation of the text_diff tool, including its schema definition and the logic to compare two texts and produce a line-by-line diff.
server.tool( "text_diff", "Compare two strings and show a simple line-by-line diff. Lines prefixed with '+' are additions, '-' are deletions, ' ' are unchanged.", { text1: z.string().describe("First text (original)"), text2: z.string().describe("Second text (modified)"), }, async ({ text1, text2 }) => { const lines1 = text1.split("\n"); const lines2 = text2.split("\n"); const maxLen = Math.max(lines1.length, lines2.length); const diffLines: string[] = []; for (let i = 0; i < maxLen; i++) { const l1 = i < lines1.length ? lines1[i] : undefined; const l2 = i < lines2.length ? lines2[i] : undefined; if (l1 === l2) { diffLines.push(` ${l1}`); } else { if (l1 !== undefined) diffLines.push(`- ${l1}`); if (l2 !== undefined) diffLines.push(`+ ${l2}`); } } const summary = { lines_in_original: lines1.length, lines_in_modified: lines2.length, additions: diffLines.filter((l) => l.startsWith("+ ")).length, deletions: diffLines.filter((l) => l.startsWith("- ")).length, unchanged: diffLines.filter((l) => l.startsWith(" ")).length, }; const output = `${JSON.stringify(summary, null, 2)}\n\n--- Diff ---\n${diffLines.join("\n")}`; return { content: [{ type: "text" as const, text: output }] }; } );