get_section
Retrieve a specific section from a Markdown file by its ID, with configurable validation levels for accuracy and compatibility. Simplifies targeted content extraction.
Instructions
Get a specific section by ID.
Args:
document_path: Path to the Markdown file
section_id: The section ID to retrieve
validation_level: Validation strictness - "STRICT", "NORMAL", or "PERMISSIVE"
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| document_path | Yes | ||
| section_id | Yes | ||
| validation_level | No | NORMAL |
Implementation Reference
- Primary MCP tool handler for 'get_section'. Decorated with @self.mcp.tool() for automatic registration and schema generation from signature/docstring. Loads document via StatelessMarkdownProcessor, retrieves section by ID using SafeMarkdownEditor.get_section_by_id(), extracts full content using line ranges from to_markdown(), and returns structured response.def get_section(document_path: str, section_id: str, validation_level: str = "NORMAL") -> Dict[str, Any]: """ Get a specific section by ID. Args: document_path: Path to the Markdown file section_id: The section ID to retrieve 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) section_ref = editor.get_section_by_id(section_id) if not section_ref: return { "success": False, "error": f"Section with ID '{section_id}' not found", "suggestions": ["Use list_sections to see available sections"] } # Extract section content using line ranges full_content = editor.to_markdown() lines = full_content.split('\n') section_lines = lines[section_ref.line_start:section_ref.line_end+1] section_content = '\n'.join(section_lines) return { "success": True, "section": { "id": section_ref.id, "title": section_ref.title, "level": section_ref.level, "content": section_content, "line_start": section_ref.line_start, "line_end": section_ref.line_end } } except Exception as e: return self.processor.create_error_response(str(e), type(e).__name__)
- Alternative/internal handler implementation for 'get_section' used by call_tool_sync. Similar logic: loads editor, gets section by ID, extracts content via line ranges, wraps in EditResult.def _get_section_impl(self, document_path: str, section_id: str) -> Dict[str, Any]: """Implementation for get_section tool.""" def operation(editor): from .safe_editor_types import EditResult, EditOperation, SafeParseError, ErrorCategory section = editor.get_section_by_id(section_id) if not section: return EditResult( success=False, operation=EditOperation.BATCH_OPERATIONS, modified_sections=[], errors=[SafeParseError( message=f"Section '{section_id}' not found", error_code="SECTION_NOT_FOUND", category=ErrorCategory.VALIDATION )], warnings=[] ) # Extract content for the section 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_data = { "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, modified_sections=[], errors=[], warnings=[], metadata={"section": section_data} ) return self.processor.execute_operation(document_path, operation, auto_save=False)
- src/quantalogic_markdown_mcp/mcp_server.py:617-628 (registration)Internal tool registration dictionary in call_tool_sync method, mapping "get_section" to _get_section_impl for synchronous testing calls.tools = { "load_document": self._load_document_impl, "list_sections": self._list_sections_impl, "get_section": self._get_section_impl, "insert_section": self._insert_section_impl, "update_section": self._update_section_impl, "delete_section": self._delete_section_impl, "move_section": self._move_section_impl, "get_document": self._get_document_impl, "save_document": self._save_document_impl, "analyze_document": self._analyze_document_impl, }
- Core helper method in SafeMarkdownEditor used by get_section tool handlers. Iterates over current sections to find and return SectionReference by stable ID.def get_section_by_id(self, section_id: str) -> Optional[SectionReference]: """ Find section by stable identifier. Args: section_id: Stable section identifier Returns: SectionReference if found, None otherwise Complexity: O(n) where n is number of sections """ with self._lock: sections = self._build_section_references() for section in sections: if section.id == section_id: return section return None
- Handler for get_section in enhanced MCP server variant. Similar to primary but returns section metadata without full content extraction.def get_section(document_path: str, section_id: str, validation_level: str = "NORMAL") -> Dict[str, Any]: """Get a specific section (stateless only).""" 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) section = editor.get_section_by_id(section_id) if section: return { "success": True, "section": { "id": section.id, "title": section.title, "level": section.level, "line_start": section.line_start, "line_end": section.line_end, "path": section.path }, "stateless": True } else: return { "success": False, "error": f"Section not found: {section_id}" } except Exception as e: return self.processor.create_error_response(str(e), type(e).__name__)