Skip to main content
Glama
camiloluvino

Roam Research MCP Server

by camiloluvino

roam_datomic_query

Execute custom Datomic queries on Roam Research graphs for advanced data retrieval with complex filtering, boolean logic, sorting, and proximity search capabilities.

Instructions

Execute a custom Datomic query on the Roam graph for advanced data retrieval beyond the available search tools. This provides direct access to Roam's query engine. Note: Roam graph is case-sensitive.

Optimal Use Cases for roam_datomic_query:

  • Advanced Filtering (including Regex): Use for scenarios requiring complex filtering, including regex matching on results post-query, which Datalog does not natively support for all data types. It can fetch broader results for client-side post-processing.

  • Highly Complex Boolean Logic: Ideal for intricate combinations of "AND", "OR", and "NOT" conditions across multiple terms or attributes.

  • Arbitrary Sorting Criteria: The go-to for highly customized sorting needs beyond default options.

  • Proximity Search: For advanced search capabilities involving proximity, which are difficult to implement efficiently with simpler tools.

List of some of Roam's data model Namespaces and Attributes: ancestor (descendants), attrs (lookup), block (children, heading, open, order, page, parents, props, refs, string, text-align, uid), children (view-type), create (email, time), descendant (ancestors), edit (email, seen-by, time), entity (attrs), log (id), node (title), page (uid, title), refs (text). Predicates (clojure.string/includes?, clojure.string/starts-with?, clojure.string/ends-with?, <, >, <=, >=, =, not=, !=). Aggregates (distinct, count, sum, max, min, avg, limit). Tips: Use :block/parents for all ancestor levels, :block/children for direct descendants only; combine clojure.string for complex matching, use distinct to deduplicate, leverage Pull patterns for hierarchies, handle case-sensitivity carefully, and chain ancestry rules for multi-level queries.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe Datomic query to execute (in Datalog syntax). Example: `[:find ?block-string :where [?block :block/string ?block-string] (or [(clojure.string/includes? ?block-string "hypnosis")] [(clojure.string/includes? ?block-string "trance")] [(clojure.string/includes? ?block-string "suggestion")]) :limit 25]`
inputsNoOptional array of input parameters for the query
regexFilterNoOptional: A regex pattern to filter the results client-side after the Datomic query. Applied to JSON.stringify(result) or specific fields if regexTargetField is provided.
regexFlagsNoOptional: Flags for the regex filter (e.g., "i" for case-insensitive, "g" for global).
regexTargetFieldNoOptional: An array of field paths (e.g., ["block_string", "page_title"]) within each Datomic result object to apply the regex filter to. If not provided, the regex is applied to the stringified full result.

Implementation Reference

  • The core handler implementation for executing custom Datomic queries on the Roam graph, including optional client-side regex filtering on results.
    export class DatomicSearchHandler extends BaseSearchHandler {
      constructor(
        graph: Graph,
        private params: DatomicSearchParams
      ) {
        super(graph);
      }
    
      async execute(): Promise<SearchResult> {
        try {
          // Execute the datomic query using the Roam API
          let results = await q(this.graph, this.params.query, this.params.inputs || []) as unknown[];
    
          if (this.params.regexFilter) {
            let regex: RegExp;
            try {
              regex = new RegExp(this.params.regexFilter, this.params.regexFlags);
            } catch (e) {
              return {
                success: false,
                matches: [],
                message: `Invalid regex filter provided: ${e instanceof Error ? e.message : String(e)}`
              };
            }
    
            results = results.filter(result => {
              if (this.params.regexTargetField && this.params.regexTargetField.length > 0) {
                for (const field of this.params.regexTargetField) {
                  // Access nested fields if path is provided (e.g., "prop.nested")
                  const fieldPath = field.split('.');
                  let value: any = result;
                  for (const part of fieldPath) {
                    if (typeof value === 'object' && value !== null && part in value) {
                      value = value[part];
                    } else {
                      value = undefined; // Field not found
                      break;
                    }
                  }
                  if (typeof value === 'string' && regex.test(value)) {
                    return true;
                  }
                }
                return false;
              } else {
                // Default to stringifying the whole result if no target field is specified
                return regex.test(JSON.stringify(result));
              }
            });
          }
    
          return {
            success: true,
            matches: results.map(result => ({
              content: JSON.stringify(result),
              block_uid: '', // Datomic queries may not always return block UIDs
              page_title: '' // Datomic queries may not always return page titles
            })),
            message: `Query executed successfully. Found ${results.length} results.`
          };
        } catch (error) {
          return {
            success: false,
            matches: [],
            message: `Failed to execute query: ${error instanceof Error ? error.message : String(error)}`
          };
        }
      }
    }
  • The tool schema defining the name, description, and input validation for roam_datomic_query.
    [toolName(BASE_TOOL_NAMES.DATOMIC_QUERY)]: {
      name: toolName(BASE_TOOL_NAMES.DATOMIC_QUERY),
      description: 'Execute a custom Datomic query on the Roam graph for advanced data retrieval beyond the available search tools. This provides direct access to Roam\'s query engine. Note: Roam graph is case-sensitive.\n\n__Optimal Use Cases for `roam_datomic_query`:__\n- __Advanced Filtering (including Regex):__ Use for scenarios requiring complex filtering, including regex matching on results post-query, which Datalog does not natively support for all data types. It can fetch broader results for client-side post-processing.\n- __Highly Complex Boolean Logic:__ Ideal for intricate combinations of "AND", "OR", and "NOT" conditions across multiple terms or attributes.\n- __Arbitrary Sorting Criteria:__ The go-to for highly customized sorting needs beyond default options.\n- __Proximity Search:__ For advanced search capabilities involving proximity, which are difficult to implement efficiently with simpler tools.\n\nList of some of Roam\'s data model Namespaces and Attributes: ancestor (descendants), attrs (lookup), block (children, heading, open, order, page, parents, props, refs, string, text-align, uid), children (view-type), create (email, time), descendant (ancestors), edit (email, seen-by, time), entity (attrs), log (id), node (title), page (uid, title), refs (text).\nPredicates (clojure.string/includes?, clojure.string/starts-with?, clojure.string/ends-with?, <, >, <=, >=, =, not=, !=).\nAggregates (distinct, count, sum, max, min, avg, limit).\nTips: Use :block/parents for all ancestor levels, :block/children for direct descendants only; combine clojure.string for complex matching, use distinct to deduplicate, leverage Pull patterns for hierarchies, handle case-sensitivity carefully, and chain ancestry rules for multi-level queries.',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'The Datomic query to execute (in Datalog syntax). Example: `[:find ?block-string :where [?block :block/string ?block-string] (or [(clojure.string/includes? ?block-string "hypnosis")] [(clojure.string/includes? ?block-string "trance")] [(clojure.string/includes? ?block-string "suggestion")]) :limit 25]`'
          },
          inputs: {
            type: 'array',
            description: 'Optional array of input parameters for the query',
            items: {
              type: 'string'
            }
          },
          regexFilter: {
            type: 'string',
            description: 'Optional: A regex pattern to filter the results client-side after the Datomic query. Applied to JSON.stringify(result) or specific fields if regexTargetField is provided.'
          },
          regexFlags: {
            type: 'string',
            description: 'Optional: Flags for the regex filter (e.g., "i" for case-insensitive, "g" for global).',
          },
          regexTargetField: {
            type: 'array',
            items: {
              type: 'string'
            },
            description: 'Optional: An array of field paths (e.g., ["block_string", "page_title"]) within each Datomic result object to apply the regex filter to. If not provided, the regex is applied to the stringified full result.'
          }
        },
        required: ['query']
      }
    },
  • Registration and dispatch logic in the MCP server\'s tool call handler that routes calls to roam_datomic_query to the appropriate handler method.
    case BASE_TOOL_NAMES.DATOMIC_QUERY: {
      const { query, inputs } = request.params.arguments as {
        query: string;
        inputs?: unknown[];
      };
      const result = await this.toolHandlers.executeDatomicQuery({ query, inputs });
      return {
        content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
      };
    }
  • Intermediate handler method in ToolHandlers class that instantiates and calls the DatomicSearchHandlerImpl.
    // Datomic query
    async executeDatomicQuery(params: { query: string; inputs?: unknown[] }) {
      const handler = new DatomicSearchHandlerImpl(this.graph, params);
      return handler.execute();
    }
  • Definition of the base tool name constant used for handler routing and registration.
    DATOMIC_QUERY: 'roam_datomic_query',
Behavior4/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key behavioral traits: it provides 'direct access to Roam's query engine,' notes that 'Roam graph is case-sensitive,' explains that regex filtering is applied 'client-side after the Datomic query,' and offers tips on query construction (e.g., using :block/parents for ancestors, handling case-sensitivity). It doesn't cover rate limits, authentication needs, or error handling, but provides substantial operational context.

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

Conciseness4/5

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

The description is well-structured with clear sections (purpose, optimal use cases, data model reference, tips) and front-loaded with the core purpose. However, it includes extensive details like the full list of namespaces/attributes and predicates/aggregates, which might be excessive for a tool description—some of this could be referenced elsewhere. Most sentences earn their place by providing actionable guidance.

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

Completeness4/5

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

Given the tool's complexity (custom query execution with 5 parameters) and no annotations or output schema, the description does a good job of providing context. It explains the tool's role, when to use it, behavioral notes (case-sensitivity, client-side filtering), and includes tips for effective querying. It could benefit from examples of return values or error cases, but overall it's quite complete for guiding usage.

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%, so the schema already documents all 5 parameters thoroughly. The description adds some context by mentioning 'regex matching on results post-query' and 'client-side post-processing,' which relates to the regexFilter parameter, but doesn't provide significant additional semantic meaning beyond what's in the schema descriptions. This meets the baseline of 3 for high schema coverage.

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

Purpose5/5

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

The description explicitly states the tool's purpose: 'Execute a custom Datomic query on the Roam graph for advanced data retrieval beyond the available search tools.' This clearly specifies the verb (execute), resource (custom Datomic query on Roam graph), and distinguishes it from sibling tools by emphasizing it's for 'advanced data retrieval beyond the available search tools.' The title being null doesn't affect this clarity.

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

Usage Guidelines5/5

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

The description provides explicit guidance on when to use this tool vs alternatives through the 'Optimal Use Cases' section, which lists four specific scenarios (Advanced Filtering, Highly Complex Boolean Logic, Arbitrary Sorting Criteria, Proximity Search). It also implies when not to use it by stating it's for 'advanced data retrieval beyond the available search tools,' suggesting simpler sibling tools like roam_search_by_text or roam_search_hierarchy should be used for basic queries.

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