Skip to main content
Glama

fetch

Retrieve complete document contents from search results to access stored knowledge in your local Markdown files.

Instructions

Fetch the full contents of a search result document

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idYes

Implementation Reference

  • The handler function for the MCP tool named 'fetch'. It retrieves the full content of a document by its ID using the underlying 'read_note' tool, formats it into a JSON structure compatible with ChatGPT/OpenAI expectations, and returns it wrapped in the MCP content array format.
    @mcp.tool(description="Fetch the full contents of a search result document")
    async def fetch(
        id: str,
        context: Context | None = None,
    ) -> List[Dict[str, Any]]:
        """ChatGPT/OpenAI MCP fetch adapter returning a single text content item.
    
        Args:
            id: Document identifier (permalink, title, or memory URL)
            context: Optional FastMCP context passed through for auth/session data
    
        Returns:
            List with one dict: `{ "type": "text", "text": "{...JSON...}" }`
            where the JSON body includes `id`, `title`, `text`, `url`, and metadata.
        """
        track_mcp_tool("fetch")
        logger.info(f"ChatGPT fetch request: id='{id}'")
    
        try:
            # ChatGPT tools don't expose project parameter, so use default project
            config = ConfigManager().config
            default_project = config.default_project
    
            # Call underlying read_note function
            content = await read_note.fn(
                identifier=id,
                project=default_project,  # Use default project for ChatGPT
                page=1,
                page_size=10,  # Default pagination
                context=context,
            )
    
            # Format the document for ChatGPT
            document = _format_document_for_chatgpt(content, id)
    
            logger.info(f"Fetch completed: id='{id}', content_length={len(document.get('text', ''))}")
    
            # Return in MCP content array format as required by OpenAI
            return [{"type": "text", "text": json.dumps(document, ensure_ascii=False)}]
    
        except Exception as e:
            logger.error(f"ChatGPT fetch failed for id '{id}': {e}")
            error_document = {
                "id": id,
                "title": "Fetch Error",
                "text": f"Failed to fetch document: {str(e)[:200]}",
                "url": id,
                "metadata": {"error": "Fetch failed"},
            }
            return [{"type": "text", "text": json.dumps(error_document, ensure_ascii=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/basicmachines-co/basic-memory'

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