get_document_structure
Analyze Markdown files to generate a hierarchical outline of headings and document elements for structured navigation and editing.
Instructions
Parses the Markdown file and returns a tree of headings and elements. Use this first to navigate.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Absolute path to the .md file | |
| depth | No | Maximum depth of headings to return |
Implementation Reference
- Core handler logic: Document.get_structure recursively serializes the parsed element tree into a structured JSON tree up to the specified depth.def get_structure(self, depth: Optional[int] = None) -> List[dict]: """Get document structure""" def element_to_struct(el: Element, current_depth: int = 0) -> dict: result = { "type": el.type, "path": el.path, "preview": el.content[:100] + "..." if len(el.content) > 100 else el.content } if el.level: result["level"] = el.level if el.children and (depth is None or current_depth < depth): result["children"] = [element_to_struct(c, current_depth + 1) for c in el.children] return result return [element_to_struct(el) for el in self.elements]
- Top-level async handler function registered with MCP server, delegates to EditTool instance.async def get_document_structure(file_path: str, depth: Optional[int] = None): return await _instance.get_structure(file_path, depth)
- EditTool.get_structure loads or creates Document instance and calls its get_structure method.async def get_structure(self, file_path: str, depth: Optional[int] = None) -> List[dict]: doc = self.get_doc(file_path) return doc.get_structure(depth=depth)
- src/markdown_editor/server.py:82-113 (schema)JSON schema definition for input (file_path required, depth optional) and output (structure array).Tool( name="get_document_structure", title="Get Document Structure", description="Parses the Markdown file and returns a tree of headings and elements. Use this first to navigate.", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Absolute path to the .md file", "examples": ["/path/to/document.md", "./README.md"] }, "depth": { "type": "integer", "description": "Maximum depth of headings to return", "default": 2, "examples": [2, 3, 5] } }, "required": ["file_path"], "additionalProperties": False }, outputSchema={ "type": "object", "properties": { "structure": { "type": "array", "description": "Tree of document elements" } } } ),
- src/markdown_editor/server.py:546-548 (registration)Dispatch registration in @app.call_tool(): matches tool name and invokes the handler.if name == "get_document_structure": res = await get_document_structure(file_path, arguments.get("depth", 2)) return {"content": [TextContent(type="text", text="Structure extracted")], "structuredContent": {"structure": res}, "isError": False}
- src/markdown_editor/server.py:14-25 (registration)Import of the handler function for registration in MCP server.from .tools.edit_tools import ( get_document_structure, read_element, replace_content, insert_element, delete_element, undo_changes, search_in_document, get_element_context, move_document_element, update_document_metadata )