Skip to main content
Glama
lethain

Library MCP

by lethain

get_by_tag

Retrieve blog content by tag from local Markdown files using the Library MCP server. Specify a tag and optional limit to filter and access relevant posts.

Instructions

Get blog content by its tag.

Args: tag: the tag associated with content limit: the number of results to include

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tagYes
limitNo

Implementation Reference

  • main.py:428-441 (handler)
    MCP tool handler for 'get_by_tag' with @mcp.tool() decorator (registration). Includes schema via args and docstring. Executes core logic via content_manager and formats output.
    @mcp.tool()
    async def get_by_tag(tag: str, limit: int = 50) -> str:
        """Get blog content by its tag.
        
        Args:
            tag: the tag associated with content
            limit: the number of results to include
        """
        if content_manager is None:
            return "Content has not been loaded. Please ensure the server is properly initialized."
        
        matching_content = content_manager.get_by_tag(tag, limit)
        return format_content_for_output(matching_content)
  • Core helper method in HugoContentManager that implements the tag-based search logic: normalizes tags, finds exact/partial matches case-insensitively, sorts by date descending, applies limit.
    def get_by_tag(self, tag: str, limit: int = 50) -> List[ContentFile]:
        """Find all files with a given tag"""
        matches = []
        tag_lower = tag.lower()
        
        debug_print(f"Searching for tag: '{tag_lower}'")
        for file_path, content_file in self.path_to_content.items():
            raw_tags = content_file.meta.get('tags', [])
            tags = self._normalize_tags(raw_tags)
            
            # Debug
            if tags:
                debug_print(f"File: {os.path.basename(file_path)} - Tags: {tags}")
            
            # Check for exact tag match (case insensitive)
            if any(tag_lower == t.lower() for t in tags):
                debug_print(f"Found exact tag match in {os.path.basename(file_path)}")
                matches.append(content_file)
                continue
            
            # Check if the tag is part of a tag
            for t in tags:
                if tag_lower in t.lower():
                    debug_print(f"Found partial tag match in {os.path.basename(file_path)}: '{t}'")
                    matches.append(content_file)
                    break
        
        debug_print(f"Found {len(matches)} files with tag '{tag}'")
        
        # Sort by date (most recent first)
        def get_sort_key(content_file):
            date = content_file.date
            if date is None:
                return datetime.min
            # Make date naive if it has timezone info
            if hasattr(date, 'tzinfo') and date.tzinfo is not None:
                date = date.replace(tzinfo=None)
            return date
            
        matches.sort(key=get_sort_key, reverse=True)
        
        return matches[:limit]
  • Helper function to format the list of ContentFile objects into a readable string output for the tool response.
    def format_content_for_output(content_files: List[ContentFile]) -> str:
        """Format the content files for output"""
        if not content_files:
            return "No matching content found."
        
        result = []
        
        for i, file in enumerate(content_files):
            result.append(f"File: {file.path}")
            result.append("Metadata:")
            for key, value in file.meta.items():
                result.append(f"  {key}: {value}")
            
            # Include the full content
            result.append("Content:")
            result.append(file.data.strip())
            
            # Add separator between entries, but not after the last one
            if i < len(content_files) - 1:
                result.append("-" * 50)
        
        return "\n".join(result)

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/lethain/library-mcp'

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