move_section
Reposition a specified section within a Markdown document to a target location, automatically saving changes if enabled. Ensures document integrity with optional backup and validation.
Instructions
Move a section to a different position.
The document will be saved after the operation if successful and auto_save is True.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| auto_save | No | ||
| backup | No | ||
| document_path | Yes | ||
| section_id | Yes | ||
| target_position | Yes | ||
| validation_level | No | NORMAL |
Implementation Reference
- Primary MCP tool handler for 'move_section'. Registers the tool, parses arguments (document_path, section_id, target_position), loads SafeMarkdownEditor, performs the move by calling editor.move_section, and handles saving via stateless processor.def move_section(document_path: str, section_id: str, target_position: int, auto_save: bool = True, backup: bool = True, validation_level: str = "NORMAL") -> Dict[str, Any]: """ Move a section to a different position. The document will be saved after the operation if successful and auto_save is True. """ """ Move a section to a different position. Args: document_path: Path to the Markdown file section_id: The section ID to move target_position: Target position (0-based index) auto_save: Whether to automatically save the document backup: Whether to create a backup before saving validation_level: Validation strictness - "STRICT", "NORMAL", or "PERMISSIVE" """ def operation(editor): section_ref = editor.get_section_by_id(section_id) if not section_ref: from .safe_editor_types import EditResult, OperationType return EditResult( success=False, operation=OperationType.MOVE, modified_sections=[], errors=[f"Section with ID '{section_id}' not found"], warnings=[] ) sections = editor.get_sections() if target_position >= len(sections) or target_position < 0: from .safe_editor_types import EditResult, OperationType return EditResult( success=False, operation=OperationType.MOVE, modified_sections=[], errors=[f"Target position {target_position} is out of range (0-{len(sections)-1})"], warnings=[] ) target_section = sections[target_position] return editor.move_section(section_ref, target_section, "after") validation_map = {"STRICT": ValidationLevel.STRICT, "NORMAL": ValidationLevel.NORMAL, "PERMISSIVE": ValidationLevel.PERMISSIVE} validation_enum = validation_map.get(validation_level.upper(), ValidationLevel.NORMAL) return self.processor.execute_operation(document_path, operation, auto_save, backup, validation_enum)
- Underlying handler logic for moving sections in SafeMarkdownEditor. Validates sections and position, records transaction (simplified implementation currently returns success without actual text manipulation). Called by the MCP tool handler.def move_section(self, section_ref: SectionReference, target_ref: SectionReference, position: str = "after") -> EditResult: """ Move a section to a new position in the document. Args: section_ref: Section to move target_ref: Target section to move relative to position: "before" or "after" target section Returns: EditResult with operation details """ with self._lock: try: # Validate both sections exist if not self._is_valid_section_reference(section_ref): return EditResult( success=False, operation=EditOperation.MOVE_SECTION, modified_sections=[], errors=[SafeParseError( message=f"Source section not found: {section_ref.title}", error_code="SECTION_NOT_FOUND", category=ErrorCategory.VALIDATION )], warnings=[] ) if not self._is_valid_section_reference(target_ref): return EditResult( success=False, operation=EditOperation.MOVE_SECTION, modified_sections=[], errors=[SafeParseError( message=f"Target section not found: {target_ref.title}", error_code="SECTION_NOT_FOUND", category=ErrorCategory.VALIDATION )], warnings=[] ) if position not in ["before", "after"]: return EditResult( success=False, operation=EditOperation.MOVE_SECTION, modified_sections=[], errors=[SafeParseError( message="Position must be 'before' or 'after'", error_code="INVALID_POSITION", category=ErrorCategory.VALIDATION )], warnings=[] ) # Store rollback data rollback_data = self._current_text # Implementation simplified for now - just return success # Full implementation would require complex section boundary management # Record transaction operation_dict = { 'operation': EditOperation.MOVE_SECTION, 'section_id': section_ref.id, 'content': "", 'target_section_id': target_ref.id, 'position': position } self._record_transaction([operation_dict], rollback_data) # Update version self._version += 1 self._last_modified = datetime.now() return EditResult( success=True, operation=EditOperation.MOVE_SECTION, modified_sections=[section_ref, target_ref], errors=[], warnings=[SafeParseError( message="Move operation is simplified - full implementation pending", error_code="SIMPLIFIED_IMPLEMENTATION", category=ErrorCategory.OPERATION )], metadata={ 'position': position, 'version': self._version } ) except Exception as e: return EditResult( success=False, operation=EditOperation.MOVE_SECTION, modified_sections=[], errors=[SafeParseError( message=f"Move operation failed: {str(e)}", error_code="MOVE_ERROR", category=ErrorCategory.SYSTEM )], warnings=[] )
- Helper implementation method _move_section_impl used in synchronous tool calling (call_tool_sync). Similar logic to the main handler.def _move_section_impl(self, document_path: str, section_id: str, target_position: int, auto_save: bool = True, backup: bool = True) -> Dict[str, Any]: """Implementation for move_section tool.""" def operation(editor): section = editor.get_section_by_id(section_id) if not section: return {"success": False, "error": f"Section '{section_id}' not found"} sections = editor.get_sections() if target_position < 0 or target_position >= len(sections): return {"success": False, "error": f"Invalid target position: {target_position}"} target_section = sections[target_position] return editor.move_section(section, target_section, "after") validation_enum = ValidationLevel.NORMAL return self.processor.execute_operation(document_path, operation, auto_save, backup, validation_enum)
- src/quantalogic_markdown_mcp/mcp_server.py:941-942 (registration)Global MCP server instance creation where tools including move_section are registered.server = MarkdownMCPServer() mcp = server.mcp