text_diff
Compare two texts and output their differences in unified diff format. Customize with labels and context lines for precise change tracking.
Instructions
对比两段文本的差异,输出 unified diff 格式。
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"text_a": "<text_a>",
"text_b": "<text_b>"
},
"name": "text_diff"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text_a | Yes | 原始文本 | |
| text_b | Yes | 修改后文本 | |
| label_a | No | 原始文本标签 | 原始 |
| label_b | No | 修改后标签 | 修改后 |
| context | No | 显示上下文行数(默认 3) |
Implementation Reference
- The _text_diff function implements the core logic of the 'text_diff' tool. It takes two text strings (text_a, text_b), splits them into lines, generates a unified diff using Python's difflib, and returns the result with added/removed line counts.
def _text_diff(args: dict) -> list[types.TextContent]: a = args["text_a"].splitlines(keepends=True) b = args["text_b"].splitlines(keepends=True) label_a = args.get("label_a", "原始") label_b = args.get("label_b", "修改后") context = int(args.get("context", 3)) diff = list(difflib.unified_diff( a, b, fromfile=label_a, tofile=label_b, n=context, )) if not diff: return [types.TextContent(type="text", text="✅ 两段文本完全相同,无差异")] # 统计变更 added = sum(1 for line in diff if line.startswith("+") and not line.startswith("+++")) removed = sum(1 for line in diff if line.startswith("-") and not line.startswith("---")) result = "".join(diff) return [types.TextContent(type="text", text=( f"📝 差异对比 新增 {added} 行 / 删除 {removed} 行\n\n" f"```diff\n{result}\n```" ))] - The Tool definition for 'text_diff' including its inputSchema which requires 'text_a' and 'text_b' strings, with optional defaults for 'label_a', 'label_b', and 'context' (context lines).
types.Tool( name="text_diff", description="对比两段文本的差异,输出 unified diff 格式。", inputSchema={ "type": "object", "properties": { "text_a": {"type": "string", "description": "原始文本"}, "text_b": {"type": "string", "description": "修改后文本"}, "label_a": {"type": "string", "description": "原始文本标签", "default": "原始"}, "label_b": {"type": "string", "description": "修改后标签", "default": "修改后"}, "context": { "type": "integer", "description": "显示上下文行数(默认 3)", "default": 3, }, }, "required": ["text_a", "text_b"], }, ), - src/onion_mcp_server/server.py:54-55 (registration)Registration of 'text_diff' tool in the server's handler routing: loops over TEXT_TOOLS and maps each tool name (including 'text_diff') to the handle_text dispatcher.
for _t in TEXT_TOOLS: _HANDLERS[_t.name] = handle_text - The handle_text dispatcher function that routes tool calls to the appropriate handler function (_text_diff for 'text_diff').
async def handle_text(name: str, arguments: dict) -> list[types.TextContent]: handlers = { "text_format": _text_format, "text_diff": _text_diff, "text_template": _text_template, "text_count": _text_count, "text_clean": _text_clean, } fn = handlers.get(name) if fn is None: raise ValueError(f"未知 text 工具: {name}") return fn(arguments)