Skip to main content
Glama

obsidian_get_file_contents

Read complete contents of Obsidian vault files to analyze Zettelkasten notes, understand their structure, and identify connections for creating new atomic notes.

Instructions

Read the complete contents of a single file from the vault.

Use this to read existing Zettelkasten notes, understand their structure,
and find connections for creating new atomic notes.

Args:
    params (GetFileInput): Contains:
        - filepath (str): Path to file relative to vault root

Returns:
    str: File contents including frontmatter and body
    
Example:
    For filepath="Zettelkasten/202411061234.md", returns the full note content.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsYes

Implementation Reference

  • The decorated handler function implementing the core logic of obsidian_get_file_contents tool: reads file via client, formats output with header, truncates long content, handles API errors.
    @mcp.tool(
        name="obsidian_get_file_contents",
        annotations={
            "title": "Get File Contents",
            "readOnlyHint": True,
            "destructiveHint": False,
            "idempotentHint": True,
            "openWorldHint": False
        }
    )
    async def get_file_contents(params: GetFileInput) -> str:
        """Read the complete contents of a single file from the vault.
        
        Use this to read existing Zettelkasten notes, understand their structure,
        and find connections for creating new atomic notes.
        
        Args:
            params (GetFileInput): Contains:
                - filepath (str): Path to file relative to vault root
        
        Returns:
            str: File contents including frontmatter and body
            
        Example:
            For filepath="Zettelkasten/202411061234.md", returns the full note content.
        """
        try:
            content = await obsidian_client.read_file(params.filepath)
            
            # Add filepath header for context
            output = f"# File: {params.filepath}\n\n{content}"
            
            return truncate_response(output, f"file {params.filepath}")
            
        except ObsidianAPIError as e:
            return json.dumps({
                "error": str(e),
                "filepath": params.filepath,
                "success": False
            }, indent=2)
  • Pydantic model defining the input schema for the tool, specifying the required filepath parameter with validation.
    class GetFileInput(BaseModel):
        """Input for getting file contents."""
        model_config = ConfigDict(str_strip_whitespace=True, extra='forbid')
        
        filepath: str = Field(
            description="Path to the file relative to vault root (e.g., 'Notes/zettelkasten/202411061234.md')",
            min_length=1,
            max_length=500
        )
  • Helper function used by the tool handler to truncate responses exceeding 25000 characters.
    def truncate_response(content: str, description: str = "response") -> str:
        """Truncate content if it exceeds CHARACTER_LIMIT."""
        if len(content) <= CHARACTER_LIMIT:
            return content
        
        truncated = content[:CHARACTER_LIMIT]
        message = f"\n\n[Response truncated at {CHARACTER_LIMIT} characters. Original {description} was {len(content)} characters. Use filters or pagination to reduce results.]"
        return truncated + message
  • Supporting method in ObsidianClient that performs the actual API GET request to retrieve file contents, called by the tool handler.
    async def read_file(self, filepath: str) -> str:
        """Read file content from the vault."""
        result = await self.get(f"/vault/{filepath}")
        return result.get("content", "")
  • The @mcp.tool decorator registering the function as an MCP tool with name and annotations.
    @mcp.tool(
        name="obsidian_get_file_contents",
        annotations={
            "title": "Get File Contents",
            "readOnlyHint": True,
            "destructiveHint": False,
            "idempotentHint": True,
            "openWorldHint": 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/Shepherd-Creative/obsidian-mcp'

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