Skip to main content
Glama

get_document_stats

Analyze document structure and extract statistics from loaded documents to understand content organization and key metrics.

Instructions

Get statistics about a loaded document.

Args:
    doc_id: Document identifier

Returns:
    Document statistics and structure info

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
doc_idYes

Implementation Reference

  • The main handler function for the 'get_document_stats' tool, decorated with @mcp.tool() for registration. It retrieves document statistics including node counts, headings, paragraphs, token counts, and heading level breakdown using the DocumentNavigator.
    @mcp.tool()
    def get_document_stats(doc_id: str) -> str:
        """Get statistics about a loaded document.
    
        Args:
            doc_id: Document identifier
    
        Returns:
            Document statistics and structure info
        """
        document = navigator.get_document(doc_id)
        if not document:
            return f"Document '{doc_id}' not found"
    
        headings = []
        paragraphs = []
        if document.index:
            headings = [node for node in document.index.values() if node.type == "heading"]
            paragraphs = [
                node for node in document.index.values() if node.type == "paragraph"
            ]
    
        stats = f"Document: {doc_id}\n"
        stats += f"Total nodes: {len(document.index) if document.index else 0}\n"
        stats += f"Headings: {len(headings)}\n"
        stats += f"Paragraphs: {len(paragraphs)}\n"
    
        # Token statistics
        token_stats = navigator.get_document_tokens(doc_id)
        if token_stats:
            stats += f"Total tokens: {token_stats['total_tokens']}\n"
            # stats += f"Content tokens: {token_stats['content_tokens']}\n"
    
        # Heading level breakdown
        level_counts = {}
        for heading in headings:
            level = heading.level or 0
            level_counts[level] = level_counts.get(level, 0) + 1
    
        if level_counts:
            stats += "Heading levels:\n"
            for level in sorted(level_counts.keys()):
                stats += f"  H{level}: {level_counts[level]}\n"
    
        return stats

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/shenyimings/DocNav-MCP'

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