undo
Revert recent editing operations in Markdown documents to correct mistakes or restore previous content states.
Instructions
Reverts the last N operations on the document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| count | No |
Implementation Reference
- src/markdown_editor/server.py:370-397 (registration)MCP tool registration for 'undo', including name, title, description, inputSchema (file_path required, count optional), and outputSchema (success, reverted_count).Tool( name="undo", title="Undo Last Changes", description="Reverts the last N operations on the document.", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "examples": ["./document.md"] }, "count": { "type": "integer", "default": 1, "examples": [1, 2, 5] } }, "required": ["file_path"], "additionalProperties": False }, outputSchema={ "type": "object", "properties": { "success": {"type": "boolean"}, "reverted_count": {"type": "integer", "description": "Number of operations reverted"} } } ),
- src/markdown_editor/server.py:582-584 (handler)Handler dispatch in MCP server call_tool: extracts file_path, calls undo_changes with optional count, returns structured response.elif name == "undo": res = await undo_changes(file_path, arguments.get("count", 1)) return {"content": [TextContent(type="text", text="Undo performed")], "structuredContent": res, "isError": "error" in res}
- Wrapper function undo_changes called by server: delegates to EditTool instance's undo method.async def undo_changes(file_path: str, count: int = 1): return await _instance.undo(file_path, count)
- EditTool.undo: loads Document instance, calls doc.undo(count), persists changes to file if successful.async def undo(self, file_path: str, count: int = 1) -> Dict[str, Any]: doc = self.get_doc(file_path) result = doc.undo(count) if "success" in result: with open(file_path, 'w', encoding='utf-8') as f: f.write(doc.get_content()) return result
- Core Document.undo implementation: pops last N journal entries, reverses simple operations like replace by restoring old_value, rebuilds raw_content from structure, saves journal.def undo(self, count: int = 1) -> Dict[str, Any]: """Undo last operations""" if not self.journal: return {"error": "Journal is empty"} undone = [] for _ in range(min(count, len(self.journal))): entry = self.journal.pop() # Apply reverse operation if entry.operation == "replace": element = self.find_by_path(entry.path) if element and entry.old_value is not None: element.content = entry.old_value elif entry.operation == "delete" and entry.old_value: # Restore deleted element — complex, skipping for simplicity pass elif entry.operation in ("insert_after", "insert_before"): # Remove inserted element pass undone.append(entry.operation) self._rebuild_raw_content() self._save_journal() return {"success": True, "undone_operations": undone}