list_sections
Extract and list all sections from a Markdown file. Define document path and validation strictness (STRICT, NORMAL, PERMISSIVE) to ensure accurate section identification and organization.
Instructions
List all sections in the document.
Args:
document_path: Path to the Markdown file
validation_level: Validation strictness - "STRICT", "NORMAL", or "PERMISSIVE"
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| document_path | Yes | ||
| validation_level | No | NORMAL |
Implementation Reference
- Primary handler and registration for the 'list_sections' tool via @mcp.tool() decorator. Loads document, parses sections, extracts content previews, and returns structured list of sections.@self.mcp.tool() def list_sections(document_path: str, validation_level: str = "NORMAL") -> Dict[str, Any]: """ List all sections in the document. Args: document_path: Path to the Markdown file validation_level: Validation strictness - "STRICT", "NORMAL", or "PERMISSIVE" """ try: validation_map = {"STRICT": ValidationLevel.STRICT, "NORMAL": ValidationLevel.NORMAL, "PERMISSIVE": ValidationLevel.PERMISSIVE} validation_enum = validation_map.get(validation_level.upper(), ValidationLevel.NORMAL) editor = self.processor.load_document(document_path, validation_enum) sections = editor.get_sections() # Get full content to extract section content full_content = editor.to_markdown() lines = full_content.split('\n') # Build section list with content preview section_list = [] for section in sections: # Extract section content using line ranges try: section_lines = lines[section.line_start:section.line_end+1] section_content = '\n'.join(section_lines) content_preview = section_content[:100] + "..." if len(section_content) > 100 else section_content except (IndexError, AttributeError): # Fallback if line extraction fails content_preview = "Content preview not available" section_list.append({ "id": section.id, "title": section.title, "level": section.level, "line_start": section.line_start, "line_end": section.line_end, "content_preview": content_preview }) return { "success": True, "sections": section_list, "total_sections": len(sections) } except Exception as e: return self.processor.create_error_response(str(e), type(e).__name__)
- src/quantalogic_markdown_mcp/mcp_server.py:619-619 (registration)Registration of list_sections tool in the internal call_tool_sync method for testing purposes, mapping to _list_sections_impl."list_sections": self._list_sections_impl,
- Helper implementation of list_sections used in testing's call_tool_sync. Similar logic but returns EditResult format and used internally.def _list_sections_impl(self, document_path: str) -> Dict[str, Any]: """Implementation for list_sections tool.""" def operation(editor): from .safe_editor_types import EditResult, EditOperation sections = editor.get_sections() section_list = [] for section in sections: # Extract content for the section by using line ranges content = "" try: markdown_text = editor.to_markdown() lines = markdown_text.split('\n') # Extract section content from line_start to line_end if section.line_start < len(lines) and section.line_end < len(lines): content_lines = lines[section.line_start:section.line_end + 1] content = '\n'.join(content_lines) except Exception: content = "" section_list.append({ "id": section.id, "title": section.title, "level": section.level, "start_line": section.line_start, "end_line": section.line_end, "content": content }) return EditResult( success=True, operation=EditOperation.BATCH_OPERATIONS, # Or a custom operation modified_sections=[], errors=[], warnings=[], metadata={"sections": section_list} ) return self.processor.execute_operation(document_path, operation, auto_save=False)