roam-research-mcp 5.0.1 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cli/batch/resolver.js +2 -1
- package/build/query/generator.js +7 -10
- package/build/search/tag-search.js +3 -2
- package/build/search/text-search.js +3 -2
- package/build/search/utils.js +5 -4
- package/build/shared/datalog.js +16 -0
- package/build/tools/helpers/page-resolution.js +3 -2
- package/build/tools/operations/block-retrieval.js +2 -1
- package/build/tools/operations/blocks.js +2 -1
- package/build/tools/operations/outline.js +5 -4
- package/build/tools/operations/pages.js +5 -4
- package/package.json +1 -1
|
@@ -5,6 +5,7 @@ import { q } from '@roam-research/roam-api-sdk';
|
|
|
5
5
|
import { capitalizeWords } from '../../tools/helpers/text.js';
|
|
6
6
|
import { formatRoamDate } from '../../utils/helpers.js';
|
|
7
7
|
import { pageUidCache } from '../../cache/page-uid-cache.js';
|
|
8
|
+
import { ednString } from '../../shared/datalog.js';
|
|
8
9
|
/**
|
|
9
10
|
* Resolve a page title to its UID, trying multiple case variations
|
|
10
11
|
*/
|
|
@@ -20,7 +21,7 @@ export async function resolvePageUid(graph, title) {
|
|
|
20
21
|
capitalizeWords(title),
|
|
21
22
|
title.toLowerCase()
|
|
22
23
|
];
|
|
23
|
-
const orClause = variations.map(v => `[?e :node/title
|
|
24
|
+
const orClause = variations.map(v => `[?e :node/title ${ednString(v)}]`).join(' ');
|
|
24
25
|
const searchQuery = `[:find ?uid .
|
|
25
26
|
:where [?e :block/uid ?uid]
|
|
26
27
|
(or ${orClause})]`;
|
package/build/query/generator.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Converts parsed query nodes into Datalog WHERE clauses
|
|
5
5
|
*/
|
|
6
|
+
import { ednString } from '../shared/datalog.js';
|
|
6
7
|
export class DatalogGenerator {
|
|
7
8
|
constructor() {
|
|
8
9
|
this.refCounter = 0;
|
|
@@ -117,7 +118,7 @@ export class DatalogGenerator {
|
|
|
117
118
|
const refVar = `?ref-${this.refCounter++}`;
|
|
118
119
|
// A tag reference means the block has :block/refs pointing to a page with that title
|
|
119
120
|
const where = [
|
|
120
|
-
`[${refVar} :node/title
|
|
121
|
+
`[${refVar} :node/title ${ednString(tagName)}]`,
|
|
121
122
|
`[${blockVar} :block/refs ${refVar}]`
|
|
122
123
|
];
|
|
123
124
|
return { where, inputs: [], inputValues: [] };
|
|
@@ -127,7 +128,7 @@ export class DatalogGenerator {
|
|
|
127
128
|
// This can be through :block/refs or embedded in the string
|
|
128
129
|
const refVar = `?block-ref-${this.refCounter++}`;
|
|
129
130
|
const where = [
|
|
130
|
-
`[${refVar} :block/uid
|
|
131
|
+
`[${refVar} :block/uid ${ednString(uid)}]`,
|
|
131
132
|
`[${blockVar} :block/refs ${refVar}]`
|
|
132
133
|
];
|
|
133
134
|
return { where, inputs: [], inputValues: [] };
|
|
@@ -135,7 +136,7 @@ export class DatalogGenerator {
|
|
|
135
136
|
generateSearch(text, blockVar) {
|
|
136
137
|
// Search uses clojure.string/includes? to find text in block content
|
|
137
138
|
const where = [
|
|
138
|
-
`[(clojure.string/includes? ?block-str
|
|
139
|
+
`[(clojure.string/includes? ?block-str ${ednString(text)})]`
|
|
139
140
|
];
|
|
140
141
|
return { where, inputs: [], inputValues: [] };
|
|
141
142
|
}
|
|
@@ -152,20 +153,19 @@ export class DatalogGenerator {
|
|
|
152
153
|
generateBy(user, blockVar) {
|
|
153
154
|
// "by" matches blocks created OR edited by the user
|
|
154
155
|
// Uses or-join to match either condition
|
|
155
|
-
const escapedUser = this.escapeString(user);
|
|
156
156
|
const where = [
|
|
157
157
|
`(or-join [${blockVar}]
|
|
158
158
|
(and [${blockVar} :create/user ?by-creator]
|
|
159
|
-
[?by-creator :user/display-name
|
|
159
|
+
[?by-creator :user/display-name ${ednString(user)}])
|
|
160
160
|
(and [${blockVar} :edit/user ?by-editor]
|
|
161
|
-
[?by-editor :user/display-name
|
|
161
|
+
[?by-editor :user/display-name ${ednString(user)}]))`
|
|
162
162
|
];
|
|
163
163
|
return { where, inputs: [], inputValues: [] };
|
|
164
164
|
}
|
|
165
165
|
generateUserClause(user, blockVar, attribute, varName) {
|
|
166
166
|
const where = [
|
|
167
167
|
`[${blockVar} ${attribute} ?${varName}]`,
|
|
168
|
-
`[?${varName} :user/display-name
|
|
168
|
+
`[?${varName} :user/display-name ${ednString(user)}]`
|
|
169
169
|
];
|
|
170
170
|
return { where, inputs: [], inputValues: [] };
|
|
171
171
|
}
|
|
@@ -277,9 +277,6 @@ export class DatalogGenerator {
|
|
|
277
277
|
return date.getTime();
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
|
-
escapeString(str) {
|
|
281
|
-
return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
282
|
-
}
|
|
283
280
|
}
|
|
284
281
|
/**
|
|
285
282
|
* Build a complete Datalog query from generated clauses
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { q } from '@roam-research/roam-api-sdk';
|
|
2
2
|
import { BaseSearchHandler } from './types.js';
|
|
3
3
|
import { SearchUtils } from './utils.js';
|
|
4
|
+
import { ednString } from '../shared/datalog.js';
|
|
4
5
|
export class TagSearchHandler extends BaseSearchHandler {
|
|
5
6
|
constructor(graph, params) {
|
|
6
7
|
super(graph);
|
|
@@ -55,12 +56,12 @@ export class TagSearchHandler extends BaseSearchHandler {
|
|
|
55
56
|
if (nearTagUid) {
|
|
56
57
|
queryWhereClauses += `
|
|
57
58
|
[?b :block/refs ?near-tag-page]
|
|
58
|
-
[?near-tag-page :block/uid
|
|
59
|
+
[?near-tag-page :block/uid ${ednString(nearTagUid)}]`;
|
|
59
60
|
}
|
|
60
61
|
if (excludeTagUid) {
|
|
61
62
|
queryWhereClauses += `
|
|
62
63
|
(not [?b :block/refs ?exclude-tag-page])
|
|
63
|
-
[?exclude-tag-page :block/uid
|
|
64
|
+
[?exclude-tag-page :block/uid ${ednString(excludeTagUid)}]`;
|
|
64
65
|
}
|
|
65
66
|
if (targetPageUid) {
|
|
66
67
|
inClause += ` ?target-page-uid`;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { q } from '@roam-research/roam-api-sdk';
|
|
2
2
|
import { BaseSearchHandler } from './types.js';
|
|
3
3
|
import { SearchUtils } from './utils.js';
|
|
4
|
+
import { ednString } from '../shared/datalog.js';
|
|
4
5
|
export class TextSearchHandler extends BaseSearchHandler {
|
|
5
6
|
constructor(graph, params) {
|
|
6
7
|
super(graph);
|
|
@@ -90,14 +91,14 @@ export class TextSearchHandler extends BaseSearchHandler {
|
|
|
90
91
|
:where
|
|
91
92
|
[?e :node/title ?title]
|
|
92
93
|
[?e :block/uid ?uid]
|
|
93
|
-
[(clojure.string/starts-with? ?title
|
|
94
|
+
[(clojure.string/starts-with? ?title ${ednString(namespace)})]]`;
|
|
94
95
|
const rawResults = await q(this.graph, queryStr, []);
|
|
95
96
|
// Get total count
|
|
96
97
|
const countQueryStr = `[:find (count ?e)
|
|
97
98
|
:in $
|
|
98
99
|
:where
|
|
99
100
|
[?e :node/title ?title]
|
|
100
|
-
[(clojure.string/starts-with? ?title
|
|
101
|
+
[(clojure.string/starts-with? ?title ${ednString(namespace)})]]`;
|
|
101
102
|
const totalCountResults = await q(this.graph, countQueryStr, []);
|
|
102
103
|
const totalCount = totalCountResults[0] ? totalCountResults[0][0] : 0;
|
|
103
104
|
// Format results: page UID as block_uid, title as content
|
package/build/search/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
|
|
2
2
|
import { q } from '@roam-research/roam-api-sdk';
|
|
3
3
|
import { normalizeToRoamDate } from '../utils/helpers.js';
|
|
4
|
+
import { ednString } from '../shared/datalog.js';
|
|
4
5
|
export class SearchUtils {
|
|
5
6
|
/**
|
|
6
7
|
* Find a page by title or UID.
|
|
@@ -22,7 +23,7 @@ export class SearchUtils {
|
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
// Try as UID
|
|
25
|
-
const uidQuery = `[:find ?uid :where [?e :block/uid
|
|
26
|
+
const uidQuery = `[:find ?uid :where [?e :block/uid ${ednString(titleOrUid)}] [?e :block/uid ?uid]]`;
|
|
26
27
|
const uidResults = await q(graph, uidQuery, []);
|
|
27
28
|
if (!uidResults || uidResults.length === 0) {
|
|
28
29
|
throw new McpError(ErrorCode.InvalidRequest, `Page with title/UID "${titleOrUid}" not found`);
|
|
@@ -117,7 +118,7 @@ export class SearchUtils {
|
|
|
117
118
|
return new Map();
|
|
118
119
|
}
|
|
119
120
|
// Build OR clause for all UIDs
|
|
120
|
-
const uidClauses = blockUids.map(uid => `[?b :block/uid
|
|
121
|
+
const uidClauses = blockUids.map(uid => `[?b :block/uid ${ednString(uid)}]`).join(' ');
|
|
121
122
|
const queryStr = `[:find ?block-uid ?tag-title
|
|
122
123
|
:where
|
|
123
124
|
(or ${uidClauses})
|
|
@@ -159,7 +160,7 @@ export class SearchUtils {
|
|
|
159
160
|
*/
|
|
160
161
|
static buildTextSearchClause(text, variable, caseSensitive = false) {
|
|
161
162
|
const variations = this.getCaseVariations(text, caseSensitive);
|
|
162
|
-
const clauses = variations.map(term => `[(clojure.string/includes? ${variable}
|
|
163
|
+
const clauses = variations.map(term => `[(clojure.string/includes? ${variable} ${ednString(term)})]`);
|
|
163
164
|
return `(or ${clauses.join(' ')})`;
|
|
164
165
|
}
|
|
165
166
|
/**
|
|
@@ -171,7 +172,7 @@ export class SearchUtils {
|
|
|
171
172
|
*/
|
|
172
173
|
static buildTagMatchClause(tag, variable, caseSensitive = false) {
|
|
173
174
|
const variations = this.getCaseVariations(tag, caseSensitive);
|
|
174
|
-
const clauses = variations.map(t => `[${variable} :node/title
|
|
175
|
+
const clauses = variations.map(t => `[${variable} :node/title ${ednString(t)}]`);
|
|
175
176
|
return `(or ${clauses.join(' ')})`;
|
|
176
177
|
}
|
|
177
178
|
/**
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Datalog query text helpers.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Render a value as a Datalog (EDN) string literal, quotes included.
|
|
6
|
+
*
|
|
7
|
+
* Every value placed inside a query string goes through this. Interpolating
|
|
8
|
+
* it raw lets a `"` close the literal early: `Alex "Sandy" Pentland` becomes
|
|
9
|
+
* `"Alex "Sandy" Pentland"`, which Roam rejects with IndexOutOfBoundsException,
|
|
10
|
+
* and a `\` is read as the start of an escape. Prefer binding the value with
|
|
11
|
+
* `:in` where the query shape allows; this is for the places it does not,
|
|
12
|
+
* such as an `or` over several case variations.
|
|
13
|
+
*/
|
|
14
|
+
export function ednString(value) {
|
|
15
|
+
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
16
|
+
}
|
|
@@ -6,6 +6,7 @@ import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
|
6
6
|
import { formatRoamDate, normalizeToRoamDate } from '../../utils/helpers.js';
|
|
7
7
|
import { pageUidCache } from '../../cache/page-uid-cache.js';
|
|
8
8
|
import { capitalizeWords } from './text.js';
|
|
9
|
+
import { ednString } from '../../shared/datalog.js';
|
|
9
10
|
/**
|
|
10
11
|
* Find a page UID by title with case variation matching and caching.
|
|
11
12
|
* Tries: original, capitalized words, lowercase.
|
|
@@ -31,7 +32,7 @@ export async function getPageUid(graph, title) {
|
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
// Query database with OR clause for all variations
|
|
34
|
-
const orClause = variations.map(v => `[?e :node/title
|
|
35
|
+
const orClause = variations.map(v => `[?e :node/title ${ednString(v)}]`).join(' ');
|
|
35
36
|
const searchQuery = `[:find ?uid .
|
|
36
37
|
:where [?e :block/uid ?uid]
|
|
37
38
|
(or ${orClause})]`;
|
|
@@ -110,7 +111,7 @@ export async function findOrCreatePage(graph, titleOrUid, options = {}) {
|
|
|
110
111
|
}
|
|
111
112
|
// If not found as title, try as UID
|
|
112
113
|
const uidQuery = `[:find ?uid
|
|
113
|
-
:where [?e :block/uid
|
|
114
|
+
:where [?e :block/uid ${ednString(titleOrUid)}]
|
|
114
115
|
[?e :block/uid ?uid]]`;
|
|
115
116
|
const uidResult = await q(graph, uidQuery, []);
|
|
116
117
|
if (uidResult && uidResult.length > 0) {
|
|
@@ -3,6 +3,7 @@ import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
|
3
3
|
import { resolveBlockRefs } from '../helpers/refs.js';
|
|
4
4
|
import { fetchChildrenByDepth } from '../helpers/fetch-children.js';
|
|
5
5
|
import { pruneHiddenBlocks, isHiddenBlockString } from '../helpers/hidden.js';
|
|
6
|
+
import { ednString } from '../../shared/datalog.js';
|
|
6
7
|
export class BlockRetrievalOperations {
|
|
7
8
|
constructor(graph) {
|
|
8
9
|
this.graph = graph;
|
|
@@ -41,7 +42,7 @@ export class BlockRetrievalOperations {
|
|
|
41
42
|
if (include_ancestors) {
|
|
42
43
|
const ancestorQuery = `[:find (pull ?b [:block/uid :block/string
|
|
43
44
|
{:block/parents [:block/uid :block/string :node/title]}])
|
|
44
|
-
:where [?b :block/uid
|
|
45
|
+
:where [?b :block/uid ${ednString(block_uid)}]]`;
|
|
45
46
|
const ancestorResults = await q(this.graph, ancestorQuery, []);
|
|
46
47
|
if (ancestorResults && ancestorResults.length > 0) {
|
|
47
48
|
const blockData = ancestorResults[0][0] || ancestorResults[0];
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { q, moveBlock as moveRoamBlock } from '@roam-research/roam-api-sdk';
|
|
2
2
|
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
3
|
+
import { ednString } from '../../shared/datalog.js';
|
|
3
4
|
export class BlockOperations {
|
|
4
5
|
constructor(graph) {
|
|
5
6
|
this.graph = graph;
|
|
@@ -13,7 +14,7 @@ export class BlockOperations {
|
|
|
13
14
|
}
|
|
14
15
|
// Verify the block exists
|
|
15
16
|
const blockQuery = `[:find ?uid .
|
|
16
|
-
:where [?b :block/uid
|
|
17
|
+
:where [?b :block/uid ${ednString(block_uid)}]
|
|
17
18
|
[?b :block/uid ?uid]]`;
|
|
18
19
|
const blockExists = await q(this.graph, blockQuery, []);
|
|
19
20
|
if (!blockExists) {
|
|
@@ -5,6 +5,7 @@ import { findOrCreatePage, getPageUid, getOrCreateTodayPage } from '../helpers/p
|
|
|
5
5
|
import { executeBatch } from '../helpers/batch-utils.js';
|
|
6
6
|
import { parseMarkdown, nestUnderHeadings, convertToRoamActions, convertToRoamActionsWithBlocks, convertToRoamMarkdown } from '../../markdown-utils.js';
|
|
7
7
|
import { executeStagedBatch } from '../../shared/staged-batch.js';
|
|
8
|
+
import { ednString } from '../../shared/datalog.js';
|
|
8
9
|
// Threshold for skipping child fetch during verification
|
|
9
10
|
const VERIFICATION_THRESHOLD = 5;
|
|
10
11
|
/**
|
|
@@ -38,9 +39,9 @@ export class OutlineOperations {
|
|
|
38
39
|
async findBlockWithRetry(pageUid, blockString, maxRetries = 2, initialDelay = 1000) {
|
|
39
40
|
// Use only the most reliable query strategy (direct page and string match)
|
|
40
41
|
const query = `[:find ?b-uid ?order
|
|
41
|
-
:where [?p :block/uid
|
|
42
|
+
:where [?p :block/uid ${ednString(pageUid)}]
|
|
42
43
|
[?b :block/page ?p]
|
|
43
|
-
[?b :block/string
|
|
44
|
+
[?b :block/string ${ednString(blockString)}]
|
|
44
45
|
[?b :block/order ?order]
|
|
45
46
|
[?b :block/uid ?b-uid]]`;
|
|
46
47
|
for (let retry = 0; retry < maxRetries; retry++) {
|
|
@@ -201,7 +202,7 @@ export class OutlineOperations {
|
|
|
201
202
|
if (this.isValidUid(block_text_uid)) {
|
|
202
203
|
// First try to find block by UID
|
|
203
204
|
const uidQuery = `[:find ?uid
|
|
204
|
-
:where [?e :block/uid
|
|
205
|
+
:where [?e :block/uid ${ednString(block_text_uid)}]
|
|
205
206
|
[?e :block/uid ?uid]]`;
|
|
206
207
|
const uidResult = await q(this.graph, uidQuery, []);
|
|
207
208
|
if (uidResult && uidResult.length > 0) {
|
|
@@ -321,7 +322,7 @@ export class OutlineOperations {
|
|
|
321
322
|
let targetPageUid = page_uid;
|
|
322
323
|
// If page_uid is provided, verify it exists
|
|
323
324
|
if (page_uid) {
|
|
324
|
-
const verifyQuery = `[:find ?uid :where [?e :block/uid
|
|
325
|
+
const verifyQuery = `[:find ?uid :where [?e :block/uid ${ednString(page_uid)}] [?e :block/uid ?uid]]`;
|
|
325
326
|
const verifyResult = await q(this.graph, verifyQuery, []);
|
|
326
327
|
if (!verifyResult || verifyResult.length === 0) {
|
|
327
328
|
throw new McpError(ErrorCode.InvalidRequest, `Page/block with UID "${page_uid}" not found`);
|
|
@@ -12,6 +12,7 @@ import { pageUidCache } from '../../cache/page-uid-cache.js';
|
|
|
12
12
|
import { buildTableActions } from './table.js';
|
|
13
13
|
import { BatchOperations } from './batch.js';
|
|
14
14
|
import { parseExistingBlocks, pruneHiddenExistingBlocks, countHiddenExistingBlocks, flattenExistingBlocks, markdownToBlocks, diffBlockTrees, generateBatchActions, getDiffStats, isDiffEmpty, summarizeActions, } from '../../diff/index.js';
|
|
15
|
+
import { ednString } from '../../shared/datalog.js';
|
|
15
16
|
// Helper to get ordinal suffix for dates
|
|
16
17
|
function getOrdinalSuffix(day) {
|
|
17
18
|
if (day > 3 && day < 21)
|
|
@@ -105,7 +106,7 @@ export class PageOperations {
|
|
|
105
106
|
const year = today.getFullYear();
|
|
106
107
|
const dailyPageTitle = `${month} ${day}${getOrdinalSuffix(day)}, ${year}`;
|
|
107
108
|
// Get or create daily page UID
|
|
108
|
-
const dailyPageQuery = `[:find ?uid . :where [?e :node/title
|
|
109
|
+
const dailyPageQuery = `[:find ?uid . :where [?e :node/title ${ednString(dailyPageTitle)}] [?e :block/uid ?uid]]`;
|
|
109
110
|
let dailyPageUid = await q(this.graph, dailyPageQuery, []);
|
|
110
111
|
if (!dailyPageUid) {
|
|
111
112
|
// Create daily page first
|
|
@@ -149,7 +150,7 @@ export class PageOperations {
|
|
|
149
150
|
// Idempotency check: If page already has content, skip adding more
|
|
150
151
|
// This prevents duplicate content when the tool is called twice
|
|
151
152
|
const existingBlocksQuery = `[:find (count ?b) .
|
|
152
|
-
:where [?p :block/uid
|
|
153
|
+
:where [?p :block/uid ${ednString(pageUid)}]
|
|
153
154
|
[?p :block/children ?b]]`;
|
|
154
155
|
const existingBlockCountResult = await q(this.graph, existingBlocksQuery, []);
|
|
155
156
|
const existingBlockCount = typeof existingBlockCountResult === 'number' ? existingBlockCountResult : 0;
|
|
@@ -343,7 +344,7 @@ export class PageOperations {
|
|
|
343
344
|
return null;
|
|
344
345
|
}
|
|
345
346
|
// First get the page title
|
|
346
|
-
const titleQuery = `[:find ?title . :where [?e :block/uid
|
|
347
|
+
const titleQuery = `[:find ?title . :where [?e :block/uid ${ednString(uid)}] [?e :node/title ?title]]`;
|
|
347
348
|
const title = await q(this.graph, titleQuery, []);
|
|
348
349
|
if (!title) {
|
|
349
350
|
return null;
|
|
@@ -675,7 +676,7 @@ export class PageOperations {
|
|
|
675
676
|
:block/heading
|
|
676
677
|
{:block/children ...}
|
|
677
678
|
]) .
|
|
678
|
-
:where [?page :block/uid
|
|
679
|
+
:where [?page :block/uid ${ednString(pageUid)}]]`;
|
|
679
680
|
const pageData = await q(this.graph, blocksQuery, []);
|
|
680
681
|
if (!pageData) {
|
|
681
682
|
throw new McpError(ErrorCode.InternalError, `Failed to fetch page data for "${title}"`);
|