Skip to main content
Glama

analyze_document

Analyze Markdown document structure and provide validation insights to identify issues and improve content quality.

Instructions

        Analyze document structure and provide insights.
        
        Args:
            document_path: Path to the Markdown file
            validation_level: Validation strictness - "STRICT", "NORMAL", or "PERMISSIVE"
        

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
document_pathYes
validation_levelNoNORMAL

Implementation Reference

  • Primary MCP tool handler for 'analyze_document'. Decorated with @self.mcp.tool() for automatic registration and schema inference. Loads document, analyzes structure (sections, heading levels), computes content statistics (lines, words, characters), and returns detailed analysis.
    @self.mcp.tool()
    def analyze_document(document_path: str, validation_level: str = "NORMAL") -> Dict[str, Any]:
        """
        Analyze document structure and provide insights.
        
        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()
            content = editor.to_markdown()
            
            # Analyze heading levels
            heading_levels = {}
            for section in sections:
                level = section.level
                heading_levels[level] = heading_levels.get(level, 0) + 1
            
            # Content statistics
            lines = content.split('\n')
            words = len(content.split())
            
            return {
                "success": True,
                "document_path": document_path,
                "analysis": {
                    "structure": {
                        "total_sections": len(sections),
                        "heading_levels": heading_levels,
                        "max_depth": max(heading_levels.keys()) if heading_levels else 0,
                        "min_depth": min(heading_levels.keys()) if heading_levels else 0
                    },
                    "content": {
                        "total_lines": len(lines),
                        "non_empty_lines": len([line for line in lines if line.strip()]),
                        "total_words": words,
                        "total_characters": len(content),
                        "average_section_length": words // len(sections) if sections else 0
                    },
                    "validation": {
                        "level": validation_level,
                        "is_valid": True  # If we got here, it loaded successfully
                    }
                }
            }
            
        except Exception as e:
            return self.processor.create_error_response(str(e), type(e).__name__)
  • Explicit registration of 'analyze_document' tool (mapped to _analyze_document_impl) in the internal call_tool_sync method for synchronous testing.
        "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,
    }
  • Helper implementation for analyze_document used in synchronous tool calls. Performs similar document analysis using the processor.execute_operation pattern, returning EditResult with analysis metadata.
    def _analyze_document_impl(self, document_path: str) -> Dict[str, Any]:
        """Implementation for analyze_document tool."""
        def operation(editor):
            from .safe_editor_types import EditResult, EditOperation
            
            content = editor.to_markdown()
            sections = editor.get_sections()
            
            # Basic document analysis
            lines = content.split('\n')
            word_count = len(content.split())
            char_count = len(content)
            
            # Section analysis
            section_levels = {}
            for section in sections:
                level = section.level
                section_levels[level] = section_levels.get(level, 0) + 1
            
            analysis_data = {
                "analysis": {
                    "total_sections": len(sections),
                    "section_levels": section_levels,
                    "word_count": word_count,
                    "character_count": char_count,
                    "line_count": len(lines),
                    "heading_structure": [
                        {
                            "id": section.id,
                            "title": section.title,
                            "level": section.level
                        }
                        for section in sections
                    ]
                }
            }
            
            return EditResult(
                success=True,
                operation=EditOperation.BATCH_OPERATIONS,
                modified_sections=[],
                errors=[],
                warnings=[],
                metadata=analysis_data
            )
        
        return self.processor.execute_operation(document_path, operation, auto_save=False)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/quantalogic/quantalogic_markdown_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server