move_element
Move document elements to new locations using hierarchical paths. Reposition sections, paragraphs, or content blocks within Markdown files by specifying source and target paths with before/after placement options.
Instructions
Moves an element (and its children) to a new location in the document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| source_path | Yes | ||
| target_path | Yes | ||
| where | No | after |
Implementation Reference
- Core handler implementing move_element logic: locates source and destination elements by path, detaches source from its parent, inserts it before or after the destination in the document tree, then rebuilds the raw Markdown content.def move_element(self, src_path: str, dst_path: str, where: str = "after") -> Dict[str, Any]: """Move element""" src = self.find_by_path(src_path) dst = self.find_by_path(dst_path) if not src or not dst: return {"error": "Source or target path not found"} # Remove from old location if src.parent: src.parent.children.remove(src) else: self.elements.remove(src) # Insert into new one if dst.parent: siblings = dst.parent.children idx = siblings.index(dst) if where == "after": siblings.insert(idx + 1, src) else: siblings.insert(idx, src) src.parent = dst.parent else: idx = self.elements.index(dst) if where == "after": self.elements.insert(idx + 1, src) else: self.elements.insert(idx, src) src.parent = None self._rebuild_raw_content() return {"success": True, "new_path": src.path}
- src/markdown_editor/server.py:279-314 (registration)Registers the 'move_element' tool with the MCP server via @app.list_tools(), including title, description, full inputSchema and outputSchema.Tool( name="move_element", title="Move Structural Block", description="Moves an element (and its children) to a new location in the document.", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "examples": ["./document.md"] }, "source_path": { "type": "string", "examples": ["Old Section", "Introduction > paragraph 2"] }, "target_path": { "type": "string", "examples": ["Conclusion", "Features"] }, "where": { "type": "string", "enum": ["before", "after"], "default": "after", "examples": ["after", "before"] } }, "required": ["file_path", "source_path", "target_path"], "additionalProperties": False }, outputSchema={ "type": "object", "properties": { "success": {"type": "boolean"} } } ),
- src/markdown_editor/server.py:570-572 (handler)Tool dispatch handler in @app.call_tool(): extracts file_path and arguments, calls the move_document_element wrapper from edit_tools, formats response.elif name == "move_element": res = await move_document_element(file_path, arguments["source_path"], arguments["target_path"], arguments.get("where", "after")) return {"content": [TextContent(type="text", text="Element moved")], "structuredContent": res, "isError": "error" in res}
- Helper wrapper in EditTool class: loads or creates Document instance, delegates to doc.move_element(), persists changes to file if successful.async def move(self, file_path: str, src_path: str, dst_path: str, where: str = "after") -> Dict[str, Any]: doc = self.get_doc(file_path) result = doc.move_element(src_path, dst_path, where) if "success" in result: with open(file_path, 'w', encoding='utf-8') as f: f.write(doc.get_content()) return result
- Async wrapper function exported from edit_tools.py, used by server.py dispatcher; delegates to singleton EditTool.move() method.async def move_document_element(file_path: str, src_path: str, dst_path: str, where: str = "after"): return await _instance.move(file_path, src_path, dst_path, where)