Skip to main content
Glama
camiloluvino

Roam Research MCP Server

by camiloluvino

roam_fetch_page_by_title

Retrieve Roam Research page content by title in markdown or JSON format for accessing knowledge graph information.

Instructions

Fetch page by title. Returns content in the specified format.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
titleYesTitle of the page. For date pages, use ordinal date formats such as January 2nd, 2025
formatNoFormat output as markdown or JSON. 'markdown' returns as string; 'raw' returns JSON string of the page's blocksraw

Implementation Reference

  • The main handler function that queries the Roam graph for the page by title (with case variations), retrieves all descendant blocks, resolves references, builds the hierarchical structure of blocks, and returns either raw JSON array of blocks or formatted Markdown.
    async fetchPageByTitle(
      title: string,
      format: 'markdown' | 'raw' = 'raw'
    ): Promise<string | RoamBlock[]> {
      if (!title) {
        throw new McpError(ErrorCode.InvalidRequest, 'title is required');
      }
    
      // Try different case variations
      // Generate variations to check
      const variations = [
        title, // Original
        capitalizeWords(title), // Each word capitalized
        title.toLowerCase() // All lowercase
      ];
    
      // Create OR clause for query
      const orClause = variations.map(v => `[?e :node/title "${v}"]`).join(' ');
    
      const searchQuery = `[:find ?uid .
                          :where [?e :block/uid ?uid]
                                 (or ${orClause})]`;
    
      const result = await q(this.graph, searchQuery, []);
      const uid = (result === null || result === undefined) ? null : String(result);
    
      if (!uid) {
        throw new McpError(
          ErrorCode.InvalidRequest,
          `Page with title "${title}" not found (tried original, capitalized words, and lowercase)`
        );
      }
    
      // Define ancestor rule for traversing block hierarchy
      const ancestorRule = `[
        [ (ancestor ?b ?a)
          [?a :block/children ?b] ]
        [ (ancestor ?b ?a)
          [?parent :block/children ?b]
          (ancestor ?parent ?a) ]
      ]`;
    
      // Get all blocks under this page using ancestor rule
      const blocksQuery = `[:find ?block-uid ?block-str ?order ?parent-uid
                          :in $ % ?page-title
                          :where [?page :node/title ?page-title]
                                 [?block :block/string ?block-str]
                                 [?block :block/uid ?block-uid]
                                 [?block :block/order ?order]
                                 (ancestor ?block ?page)
                                 [?parent :block/children ?block]
                                 [?parent :block/uid ?parent-uid]]`;
      const blocks = await q(this.graph, blocksQuery, [ancestorRule, title]);
    
      if (!blocks || blocks.length === 0) {
        if (format === 'raw') {
          return [];
        }
        return `${title} (no content found)`;
      }
    
      // Get heading information for blocks that have it
      const headingsQuery = `[:find ?block-uid ?heading
                            :in $ % ?page-title
                            :where [?page :node/title ?page-title]
                                   [?block :block/uid ?block-uid]
                                   [?block :block/heading ?heading]
                                   (ancestor ?block ?page)]`;
      const headings = await q(this.graph, headingsQuery, [ancestorRule, title]);
    
      // Create a map of block UIDs to heading levels
      const headingMap = new Map<string, number>();
      if (headings) {
        for (const [blockUid, heading] of headings) {
          headingMap.set(blockUid, heading as number);
        }
      }
    
      // Create a map of all blocks
      const blockMap = new Map<string, RoamBlock>();
      const rootBlocks: RoamBlock[] = [];
    
      // First pass: Create all block objects
      for (const [blockUid, blockStr, order, parentUid] of blocks) {
        const resolvedString = await resolveRefs(this.graph, blockStr);
        const block = {
          uid: blockUid,
          string: resolvedString,
          order: order as number,
          heading: headingMap.get(blockUid) || null,
          children: []
        };
        blockMap.set(blockUid, block);
    
        // If no parent or parent is the page itself, it's a root block
        if (!parentUid || parentUid === uid) {
          rootBlocks.push(block);
        }
      }
    
      // Second pass: Build parent-child relationships
      for (const [blockUid, _, __, parentUid] of blocks) {
        if (parentUid && parentUid !== uid) {
          const child = blockMap.get(blockUid);
          const parent = blockMap.get(parentUid);
          if (child && parent && !parent.children.includes(child)) {
            parent.children.push(child);
          }
        }
      }
    
      // Sort blocks recursively
      const sortBlocks = (blocks: RoamBlock[]) => {
        blocks.sort((a, b) => a.order - b.order);
        blocks.forEach(block => {
          if (block.children.length > 0) {
            sortBlocks(block.children);
          }
        });
      };
      sortBlocks(rootBlocks);
    
      if (format === 'raw') {
        return JSON.stringify(rootBlocks);
      }
    
      // Convert to markdown with proper nesting
      const toMarkdown = (blocks: RoamBlock[], level: number = 0): string => {
        return blocks
          .map(block => {
            const indent = '  '.repeat(level);
            let md: string;
    
            // Check block heading level and format accordingly
            if (block.heading && block.heading > 0) {
              // Format as heading with appropriate number of hashtags
              const hashtags = '#'.repeat(block.heading);
              md = `${indent}${hashtags} ${block.string}`;
            } else {
              // No heading, use bullet point (current behavior)
              md = `${indent}- ${block.string}`;
            }
    
            if (block.children.length > 0) {
              md += '\n' + toMarkdown(block.children, level + 1);
            }
            return md;
          })
          .join('\n');
      };
    
      return `# ${title}\n\n${toMarkdown(rootBlocks)}`;
    }
  • Defines the tool schema including name, description, and input schema (title required, format optional: 'markdown' or 'raw').
    [toolName(BASE_TOOL_NAMES.FETCH_PAGE_BY_TITLE)]: {
      name: toolName(BASE_TOOL_NAMES.FETCH_PAGE_BY_TITLE),
      description: 'Fetch page by title. Returns content in the specified format.',
      inputSchema: {
        type: 'object',
        properties: {
          title: {
            type: 'string',
            description:
              'Title of the page. For date pages, use ordinal date formats such as January 2nd, 2025'
          },
          format: {
            type: 'string',
            enum: ['markdown', 'raw'],
            default: 'raw',
            description:
              "Format output as markdown or JSON. 'markdown' returns as string; 'raw' returns JSON string of the page's blocks"
          }
        },
        required: ['title']
      },
    },
  • Tool registration in the MCP server request handler switch statement, dispatching tool calls to ToolHandlers.fetchPageByTitle.
    case BASE_TOOL_NAMES.FETCH_PAGE_BY_TITLE: {
      const { title, format } = request.params.arguments as {
        title: string;
        format?: 'markdown' | 'raw';
      };
      const content = await this.toolHandlers.fetchPageByTitle(title, format);
      return {
        content: [{ type: 'text', text: content }],
      };
    }
  • Facade method in ToolHandlers class that delegates the fetchPageByTitle call to PageOperations instance.
    async fetchPageByTitle(title: string, format?: 'markdown' | 'raw') {
      return this.pageOps.fetchPageByTitle(title, format);
    }
  • Constant definition mapping FETCH_PAGE_BY_TITLE to the tool name 'roam_fetch_page_by_title' used throughout the codebase.
    FETCH_PAGE_BY_TITLE: 'roam_fetch_page_by_title',
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It mentions the return format options but doesn't disclose critical behavioral traits: whether this requires authentication, what happens if the title doesn't exist (error vs. null), rate limits, or performance characteristics. 'Fetch' implies read-only, but this isn't explicitly stated, leaving gaps for a tool with no annotation coverage.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise with two short sentences that are front-loaded: the first states the core purpose, the second adds key behavioral detail about output format. Every word earns its place with zero waste or redundancy, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 2 parameters with full schema coverage but no annotations or output schema, the description is minimally adequate. It covers the basic purpose and output format, but for a fetch operation with potential complexities (e.g., title matching, error handling), it lacks completeness. Without annotations, more behavioral context would be helpful, though the concise nature keeps it at a baseline level.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with both parameters well-documented in the schema. The description adds minimal value beyond the schema, only mentioning 'content in the specified format' which echoes the schema's format parameter. It doesn't provide additional context like examples or edge cases, so baseline 3 is appropriate given the comprehensive schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'fetch' and resource 'page by title', specifying what the tool does. It distinguishes from siblings like roam_search_by_text or roam_search_by_date by focusing on exact title matching rather than search operations. However, it doesn't explicitly contrast with roam_fetch_block_with_children, which might be a similar fetch operation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With many sibling search tools (roam_search_by_text, roam_search_by_date, etc.), there's no indication whether this is for exact title matches versus broader searches, or when to prefer this over roam_fetch_block_with_children. The agent must infer usage from the name alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/camiloluvino/roamMCP'

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