@abraca/mcp 2.38.0 → 2.39.0
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/dist/abracadabra-mcp.cjs +348 -11
- package/dist/abracadabra-mcp.cjs.map +1 -1
- package/dist/abracadabra-mcp.esm.js +352 -12
- package/dist/abracadabra-mcp.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/tools/content.ts +350 -4
- package/src/tools/tree.ts +70 -10
package/dist/abracadabra-mcp.cjs
CHANGED
|
@@ -21405,6 +21405,76 @@ function registerChannelTools(mcp, server) {
|
|
|
21405
21405
|
/**
|
|
21406
21406
|
* Content tools — read/write document content via markdown ↔ Y.js conversion.
|
|
21407
21407
|
*/
|
|
21408
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
21409
|
+
/** Clamp per-block streaming delay to a sane window. */
|
|
21410
|
+
const STREAM_MS_DEFAULT = 90;
|
|
21411
|
+
const STREAM_MS_MAX = 2e3;
|
|
21412
|
+
/**
|
|
21413
|
+
* Collect every body Y.XmlText in a fragment, skipping the `documentHeader`
|
|
21414
|
+
* and `documentMeta` elements — the title/meta are synced to the tree entry
|
|
21415
|
+
* elsewhere, so a surgical body edit must never touch them (it would desync
|
|
21416
|
+
* the header from the tree `label`).
|
|
21417
|
+
*/
|
|
21418
|
+
function collectBodyXmlText(frag, out) {
|
|
21419
|
+
for (const child of frag.toArray()) if (child instanceof yjs.XmlText) out.push(child);
|
|
21420
|
+
else if (child instanceof yjs.XmlElement) {
|
|
21421
|
+
const tag = child.nodeName;
|
|
21422
|
+
if (tag === "documentHeader" || tag === "documentMeta") continue;
|
|
21423
|
+
collectXmlTextDeep(child, out);
|
|
21424
|
+
}
|
|
21425
|
+
}
|
|
21426
|
+
function collectXmlTextDeep(node, out) {
|
|
21427
|
+
for (const child of node.toArray()) if (child instanceof yjs.XmlText) out.push(child);
|
|
21428
|
+
else if (child instanceof yjs.XmlElement) collectXmlTextDeep(child, out);
|
|
21429
|
+
}
|
|
21430
|
+
/**
|
|
21431
|
+
* Plain text of a Y.XmlText aligned with its Y character indices. Critically
|
|
21432
|
+
* NOT `text.toString()` — that serialises marks as XML tags
|
|
21433
|
+
* (`hello <bold>WORLD</bold>`), which throws off `indexOf` offsets. Each
|
|
21434
|
+
* non-string embed (inline atom) counts as one character, matching how Yjs
|
|
21435
|
+
* indexes them, so `delete`/`insert` offsets stay correct.
|
|
21436
|
+
*/
|
|
21437
|
+
function plainTextForIndex(text) {
|
|
21438
|
+
const delta = text.toDelta();
|
|
21439
|
+
let s = "";
|
|
21440
|
+
for (const op of delta) s += typeof op.insert === "string" ? op.insert : "";
|
|
21441
|
+
return s;
|
|
21442
|
+
}
|
|
21443
|
+
/** Marks/attributes active at a character offset inside a Y.XmlText, so a
|
|
21444
|
+
* replacement can re-use the surrounding formatting (bold, link, etc.). */
|
|
21445
|
+
function attrsAtOffset(text, offset) {
|
|
21446
|
+
const delta = text.toDelta();
|
|
21447
|
+
let pos = 0;
|
|
21448
|
+
for (const op of delta) {
|
|
21449
|
+
const len = typeof op.insert === "string" ? op.insert.length : 1;
|
|
21450
|
+
if (offset < pos + len) return op.attributes;
|
|
21451
|
+
pos += len;
|
|
21452
|
+
}
|
|
21453
|
+
}
|
|
21454
|
+
/**
|
|
21455
|
+
* Replace `find` with `replace` inside a single Y.XmlText, preserving the
|
|
21456
|
+
* formatting at each match's start. Splices right-to-left so earlier offsets
|
|
21457
|
+
* stay valid. Returns the number of replacements made. Caller wraps the whole
|
|
21458
|
+
* pass in one transaction.
|
|
21459
|
+
*/
|
|
21460
|
+
function surgicalReplaceInText(text, find, replace, all) {
|
|
21461
|
+
const s = plainTextForIndex(text);
|
|
21462
|
+
const offsets = [];
|
|
21463
|
+
let idx = s.indexOf(find);
|
|
21464
|
+
while (idx !== -1) {
|
|
21465
|
+
offsets.push(idx);
|
|
21466
|
+
if (!all) break;
|
|
21467
|
+
idx = s.indexOf(find, idx + find.length);
|
|
21468
|
+
}
|
|
21469
|
+
for (let i = offsets.length - 1; i >= 0; i--) {
|
|
21470
|
+
const at = offsets[i];
|
|
21471
|
+
const attrs = attrsAtOffset(text, at);
|
|
21472
|
+
text.delete(at, find.length);
|
|
21473
|
+
if (replace.length > 0) if (attrs && Object.keys(attrs).length > 0) text.insert(at, replace, attrs);
|
|
21474
|
+
else text.insert(at, replace);
|
|
21475
|
+
}
|
|
21476
|
+
return offsets.length;
|
|
21477
|
+
}
|
|
21408
21478
|
function registerContentTools(mcp, server) {
|
|
21409
21479
|
mcp.tool("read_document", "Read a document's content as markdown, along with its immediate children. IMPORTANT: A document's full content is its body text PLUS all its children. An empty body does not mean empty content — the children ARE the content (sub-docs, kanban columns, table columns, calendar events, etc.). Always check the returned \"children\" array and read child documents too.", { docId: zod.z.string().describe("Document ID to read.") }, async ({ docId }) => {
|
|
21410
21480
|
try {
|
|
@@ -21469,8 +21539,10 @@ function registerContentTools(mcp, server) {
|
|
|
21469
21539
|
mcp.tool("write_document", "Write markdown content to a document. Parses the markdown and writes it to the Y.js CRDT document, which syncs in real-time to all connected clients. Supports optional YAML frontmatter for title and metadata. Use ![[docId]] to embed another document as a block, or [[docId|label]] for inline doc links.", {
|
|
21470
21540
|
docId: zod.z.string().describe("Document ID to write to."),
|
|
21471
21541
|
markdown: zod.z.string().describe("Markdown content to write. Can include YAML frontmatter with title and metadata fields."),
|
|
21472
|
-
mode: zod.z.enum(["replace", "append"]).optional().describe("Write mode. \"replace\" clears existing content first (default). \"append\" adds to the end.")
|
|
21473
|
-
|
|
21542
|
+
mode: zod.z.enum(["replace", "append"]).optional().describe("Write mode. \"replace\" clears existing content first (default). \"append\" adds to the end."),
|
|
21543
|
+
stream: zod.z.boolean().optional().describe("When true, insert the content block-by-block (typewriter effect) so collaborators watching the document see it appear progressively, with the agent cursor advancing. The final document is identical to a non-streamed write. Default false."),
|
|
21544
|
+
streamMs: zod.z.number().optional().describe("Delay in milliseconds between streamed blocks (only used when stream=true). Default 90, clamped to [0, 2000].")
|
|
21545
|
+
}, async ({ docId, markdown, mode, stream, streamMs }) => {
|
|
21474
21546
|
try {
|
|
21475
21547
|
server.setAutoStatus("writing", docId);
|
|
21476
21548
|
server.setActiveToolCall({
|
|
@@ -21506,14 +21578,28 @@ function registerContentTools(mcp, server) {
|
|
|
21506
21578
|
});
|
|
21507
21579
|
const contentToWrite = body || markdown;
|
|
21508
21580
|
const existingLabel = server.getDocSummary(docId)?.label;
|
|
21509
|
-
|
|
21581
|
+
const fallbackTitle = title || existingLabel || "Untitled";
|
|
21582
|
+
if (stream) {
|
|
21583
|
+
const { header, meta: metaNode, body: bodyNodes } = (0, _abraca_convert.markdownToFragmentNodes)(contentToWrite, fallbackTitle);
|
|
21584
|
+
if (writeMode !== "append") {
|
|
21585
|
+
const lead = [header, metaNode].filter((n) => !!n);
|
|
21586
|
+
if (lead.length) (0, _abraca_convert.appendJsonNodes)(fragment, lead);
|
|
21587
|
+
}
|
|
21588
|
+
const delay = Math.max(0, Math.min(streamMs ?? STREAM_MS_DEFAULT, STREAM_MS_MAX));
|
|
21589
|
+
for (const node of bodyNodes) {
|
|
21590
|
+
(0, _abraca_convert.appendJsonNodes)(fragment, [node]);
|
|
21591
|
+
server.setFocusedDoc(docId);
|
|
21592
|
+
server.setDocCursor(docId, fragment.length);
|
|
21593
|
+
if (delay > 0) await sleep(delay);
|
|
21594
|
+
}
|
|
21595
|
+
} else (0, _abraca_convert.populateYDocFromMarkdown)(fragment, contentToWrite, fallbackTitle);
|
|
21510
21596
|
const coverTree = server.getTreeMap();
|
|
21511
21597
|
if (coverTree) (0, _abraca_dabra.reconcileDocCover)(coverTree, docId, fragment);
|
|
21512
21598
|
server.setFocusedDoc(docId);
|
|
21513
21599
|
server.setDocCursor(docId, fragment.length);
|
|
21514
21600
|
return { content: [{
|
|
21515
21601
|
type: "text",
|
|
21516
|
-
text: `Document ${docId} updated (${writeMode} mode)`
|
|
21602
|
+
text: `Document ${docId} updated (${writeMode} mode${stream ? ", streamed" : ""})`
|
|
21517
21603
|
}] };
|
|
21518
21604
|
} catch (error) {
|
|
21519
21605
|
return {
|
|
@@ -21525,6 +21611,204 @@ function registerContentTools(mcp, server) {
|
|
|
21525
21611
|
};
|
|
21526
21612
|
}
|
|
21527
21613
|
});
|
|
21614
|
+
mcp.tool("patch_document", "Make a targeted edit to a document by find-and-replace on its markdown, WITHOUT resending the whole document. Reads the current content as markdown, replaces an exact substring, and writes it back. Use this for small/surgical edits (fix a typo, update one line, insert a paragraph next to an anchor) instead of read_document + write_document. The `find` string must match the current markdown exactly (whitespace included); the tool errors if it is not found so you never silently no-op.", {
|
|
21615
|
+
docId: zod.z.string().describe("Document ID to patch."),
|
|
21616
|
+
find: zod.z.string().describe("Exact substring of the current markdown to replace. Must be present verbatim. Returns an error listing nothing-matched if not found."),
|
|
21617
|
+
replace: zod.z.string().describe("Replacement text. Use an empty string to delete the matched text."),
|
|
21618
|
+
all: zod.z.boolean().optional().describe("Replace every occurrence (true) or only the first (false, default).")
|
|
21619
|
+
}, async ({ docId, find, replace, all }) => {
|
|
21620
|
+
try {
|
|
21621
|
+
server.setAutoStatus("writing", docId);
|
|
21622
|
+
server.setActiveToolCall({
|
|
21623
|
+
name: "patch_document",
|
|
21624
|
+
target: docId
|
|
21625
|
+
});
|
|
21626
|
+
if (find.length === 0) return {
|
|
21627
|
+
content: [{
|
|
21628
|
+
type: "text",
|
|
21629
|
+
text: "Error patching document: `find` must be a non-empty string."
|
|
21630
|
+
}],
|
|
21631
|
+
isError: true
|
|
21632
|
+
};
|
|
21633
|
+
await server.ensureSpaceActive(docId);
|
|
21634
|
+
const doc = (await server.getChildProvider(docId)).document;
|
|
21635
|
+
const fragment = doc.getXmlFragment("default");
|
|
21636
|
+
const textNodes = [];
|
|
21637
|
+
collectBodyXmlText(fragment, textNodes);
|
|
21638
|
+
let surgicalCount = 0;
|
|
21639
|
+
doc.transact(() => {
|
|
21640
|
+
for (const tn of textNodes) {
|
|
21641
|
+
surgicalCount += surgicalReplaceInText(tn, find, replace, !!all);
|
|
21642
|
+
if (!all && surgicalCount > 0) break;
|
|
21643
|
+
}
|
|
21644
|
+
});
|
|
21645
|
+
if (surgicalCount > 0) {
|
|
21646
|
+
server.setFocusedDoc(docId);
|
|
21647
|
+
return { content: [{
|
|
21648
|
+
type: "text",
|
|
21649
|
+
text: `Patched ${docId}: ${surgicalCount} replacement${surgicalCount === 1 ? "" : "s"} (surgical).`
|
|
21650
|
+
}] };
|
|
21651
|
+
}
|
|
21652
|
+
const treeMap = server.getTreeMap();
|
|
21653
|
+
let label = "Untitled";
|
|
21654
|
+
let type;
|
|
21655
|
+
let meta;
|
|
21656
|
+
if (treeMap) {
|
|
21657
|
+
const entry = (0, _abraca_dabra.toPlain)(treeMap.get(docId));
|
|
21658
|
+
if (entry && typeof entry === "object") {
|
|
21659
|
+
label = entry.label || label;
|
|
21660
|
+
type = entry.type;
|
|
21661
|
+
meta = entry.meta;
|
|
21662
|
+
}
|
|
21663
|
+
}
|
|
21664
|
+
const current = (0, _abraca_convert.yjsToMarkdown)(fragment, label, meta, type);
|
|
21665
|
+
if (!current.includes(find)) return {
|
|
21666
|
+
content: [{
|
|
21667
|
+
type: "text",
|
|
21668
|
+
text: `Error patching document: \`find\` text not present in ${docId}. Read the document first to see its exact current markdown.`
|
|
21669
|
+
}],
|
|
21670
|
+
isError: true
|
|
21671
|
+
};
|
|
21672
|
+
let count = 0;
|
|
21673
|
+
let next;
|
|
21674
|
+
if (all) {
|
|
21675
|
+
next = current.split(find).join(replace);
|
|
21676
|
+
count = current.split(find).length - 1;
|
|
21677
|
+
} else {
|
|
21678
|
+
next = current.replace(find, replace);
|
|
21679
|
+
count = 1;
|
|
21680
|
+
}
|
|
21681
|
+
const { title, meta: fmMeta, body } = (0, _abraca_convert.parseFrontmatter)(next);
|
|
21682
|
+
if (title || Object.keys(fmMeta).length > 0) {
|
|
21683
|
+
const rootDoc = server.rootDocument;
|
|
21684
|
+
if (treeMap && rootDoc) {
|
|
21685
|
+
const rawEntry = treeMap.get(docId);
|
|
21686
|
+
if (rawEntry) {
|
|
21687
|
+
const cur = (0, _abraca_dabra.toPlain)(rawEntry);
|
|
21688
|
+
rootDoc.transact(() => {
|
|
21689
|
+
const patch = { updatedAt: Date.now() };
|
|
21690
|
+
if (title) patch.label = title;
|
|
21691
|
+
if (Object.keys(fmMeta).length > 0) patch.meta = {
|
|
21692
|
+
...cur.meta ?? {},
|
|
21693
|
+
...fmMeta
|
|
21694
|
+
};
|
|
21695
|
+
(0, _abraca_dabra.patchEntry)(treeMap, docId, patch);
|
|
21696
|
+
});
|
|
21697
|
+
}
|
|
21698
|
+
}
|
|
21699
|
+
}
|
|
21700
|
+
doc.transact(() => {
|
|
21701
|
+
while (fragment.length > 0) fragment.delete(0);
|
|
21702
|
+
});
|
|
21703
|
+
(0, _abraca_convert.populateYDocFromMarkdown)(fragment, body || next, title || label || "Untitled");
|
|
21704
|
+
const coverTree = server.getTreeMap();
|
|
21705
|
+
if (coverTree) (0, _abraca_dabra.reconcileDocCover)(coverTree, docId, fragment);
|
|
21706
|
+
server.setFocusedDoc(docId);
|
|
21707
|
+
server.setDocCursor(docId, fragment.length);
|
|
21708
|
+
return { content: [{
|
|
21709
|
+
type: "text",
|
|
21710
|
+
text: `Patched ${docId}: ${count} replacement${count === 1 ? "" : "s"} (re-rendered).`
|
|
21711
|
+
}] };
|
|
21712
|
+
} catch (error) {
|
|
21713
|
+
return {
|
|
21714
|
+
content: [{
|
|
21715
|
+
type: "text",
|
|
21716
|
+
text: `Error patching document: ${error.message}`
|
|
21717
|
+
}],
|
|
21718
|
+
isError: true
|
|
21719
|
+
};
|
|
21720
|
+
}
|
|
21721
|
+
});
|
|
21722
|
+
mcp.tool("read_subtree", "Read a document AND its descendants' markdown in ONE call, as a nested tree. Use this instead of repeated read_document + get_document_tree walks when you need the actual content of a board/outline/checklist and its children (e.g. \"summarize this kanban\"). Each node carries {id,label,type,meta,markdown,children[]}. Bounded by `depth` and `maxNodes` so it can't run away on huge trees — nodes past the cap are listed with their label/type but `markdown:null` and `truncated:true`.", {
|
|
21723
|
+
rootId: zod.z.string().describe("Document ID to read from (its own content is included as the tree root)."),
|
|
21724
|
+
depth: zod.z.number().optional().describe("How many levels of descendants to include. Default 2. Use 0 for just the root doc, -1 for unlimited (still capped by maxNodes)."),
|
|
21725
|
+
maxNodes: zod.z.number().optional().describe("Maximum number of documents whose content is loaded. Default 40, hard cap 200. Protects against loading hundreds of child providers.")
|
|
21726
|
+
}, async ({ rootId, depth, maxNodes }) => {
|
|
21727
|
+
try {
|
|
21728
|
+
server.setAutoStatus("reading", rootId);
|
|
21729
|
+
server.setActiveToolCall({
|
|
21730
|
+
name: "read_subtree",
|
|
21731
|
+
target: rootId
|
|
21732
|
+
});
|
|
21733
|
+
await server.ensureSpaceActive(rootId);
|
|
21734
|
+
const treeMap = server.getTreeMap();
|
|
21735
|
+
if (!treeMap) return {
|
|
21736
|
+
content: [{
|
|
21737
|
+
type: "text",
|
|
21738
|
+
text: "Not connected"
|
|
21739
|
+
}],
|
|
21740
|
+
isError: true
|
|
21741
|
+
};
|
|
21742
|
+
const targetId = (0, _abraca_dabra.normalizeRootId)(rootId, server.rootDocId);
|
|
21743
|
+
const maxDepth = depth ?? 2;
|
|
21744
|
+
const cap = Math.max(1, Math.min(maxNodes ?? 40, 200));
|
|
21745
|
+
const entries = (0, _abraca_dabra.readEntries)(treeMap, server.rootDocId);
|
|
21746
|
+
const byParent = /* @__PURE__ */ new Map();
|
|
21747
|
+
for (const e of entries) {
|
|
21748
|
+
const list = byParent.get(e.parentId) ?? [];
|
|
21749
|
+
list.push(e);
|
|
21750
|
+
byParent.set(e.parentId, list);
|
|
21751
|
+
}
|
|
21752
|
+
const childrenOf = (id) => (byParent.get(id) ?? []).slice().sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
21753
|
+
let loaded = 0;
|
|
21754
|
+
const visited = /* @__PURE__ */ new Set();
|
|
21755
|
+
const readMarkdown = async (id, label, meta, type) => {
|
|
21756
|
+
if (loaded >= cap) return {
|
|
21757
|
+
markdown: null,
|
|
21758
|
+
truncated: true
|
|
21759
|
+
};
|
|
21760
|
+
loaded++;
|
|
21761
|
+
return { markdown: (0, _abraca_convert.yjsToMarkdown)((await server.getChildProvider(id)).document.getXmlFragment("default"), label, meta, type) };
|
|
21762
|
+
};
|
|
21763
|
+
const build = async (id, curDepth) => {
|
|
21764
|
+
if (maxDepth >= 0 && curDepth > maxDepth) return [];
|
|
21765
|
+
const out = [];
|
|
21766
|
+
for (const child of childrenOf(id)) {
|
|
21767
|
+
if (visited.has(child.id)) continue;
|
|
21768
|
+
visited.add(child.id);
|
|
21769
|
+
const { markdown, truncated } = await readMarkdown(child.id, child.label, child.meta, child.type);
|
|
21770
|
+
out.push({
|
|
21771
|
+
id: child.id,
|
|
21772
|
+
label: child.label,
|
|
21773
|
+
type: child.type,
|
|
21774
|
+
meta: child.meta,
|
|
21775
|
+
markdown,
|
|
21776
|
+
...truncated ? { truncated: true } : {},
|
|
21777
|
+
children: await build(child.id, curDepth + 1)
|
|
21778
|
+
});
|
|
21779
|
+
}
|
|
21780
|
+
return out;
|
|
21781
|
+
};
|
|
21782
|
+
const rootEntry = (0, _abraca_dabra.toPlain)(treeMap.get(targetId ?? ""));
|
|
21783
|
+
const rootLabel = rootEntry && rootEntry.label || "Untitled";
|
|
21784
|
+
const rootType = rootEntry?.type;
|
|
21785
|
+
const rootMeta = rootEntry?.meta;
|
|
21786
|
+
visited.add(targetId ?? rootId);
|
|
21787
|
+
const rootContent = targetId === null ? { markdown: null } : await readMarkdown(targetId, rootLabel, rootMeta, rootType);
|
|
21788
|
+
const result = {
|
|
21789
|
+
id: targetId ?? rootId,
|
|
21790
|
+
label: rootLabel,
|
|
21791
|
+
type: rootType,
|
|
21792
|
+
meta: rootMeta,
|
|
21793
|
+
markdown: rootContent.markdown,
|
|
21794
|
+
...rootContent.truncated ? { truncated: true } : {},
|
|
21795
|
+
children: await build(targetId, 1),
|
|
21796
|
+
...loaded >= cap ? { note: `Stopped loading content after ${cap} nodes (maxNodes). Increase maxNodes or narrow rootId to see more.` } : {}
|
|
21797
|
+
};
|
|
21798
|
+
return { content: [{
|
|
21799
|
+
type: "text",
|
|
21800
|
+
text: JSON.stringify(result, null, 2)
|
|
21801
|
+
}] };
|
|
21802
|
+
} catch (error) {
|
|
21803
|
+
return {
|
|
21804
|
+
content: [{
|
|
21805
|
+
type: "text",
|
|
21806
|
+
text: `Error reading subtree: ${error.message}`
|
|
21807
|
+
}],
|
|
21808
|
+
isError: true
|
|
21809
|
+
};
|
|
21810
|
+
}
|
|
21811
|
+
});
|
|
21528
21812
|
}
|
|
21529
21813
|
|
|
21530
21814
|
//#endregion
|
|
@@ -21961,6 +22245,46 @@ function descendantsOf(entries, id) {
|
|
|
21961
22245
|
collect(id);
|
|
21962
22246
|
return result;
|
|
21963
22247
|
}
|
|
22248
|
+
/**
|
|
22249
|
+
* Evaluate one `where` predicate value against a doc's actual value. The
|
|
22250
|
+
* predicate is a small string DSL so agents can filter on meta without a
|
|
22251
|
+
* structured query language:
|
|
22252
|
+
* - `>=N` `<=N` `>N` `<N` → numeric comparison (value coerced to number)
|
|
22253
|
+
* - `contains:X` → array membership OR string substring
|
|
22254
|
+
* - `exists` / `!exists` → presence / absence (null counts as absent)
|
|
22255
|
+
* - `=X` or bare `X` → loose equality (string-coerced)
|
|
22256
|
+
*/
|
|
22257
|
+
function matchPredicate(actual, pred) {
|
|
22258
|
+
const p = pred.trim();
|
|
22259
|
+
if (p === "exists") return actual !== void 0 && actual !== null;
|
|
22260
|
+
if (p === "!exists") return actual === void 0 || actual === null;
|
|
22261
|
+
const cmp = p.match(/^(>=|<=|>|<)\s*(-?\d+(?:\.\d+)?)$/);
|
|
22262
|
+
if (cmp) {
|
|
22263
|
+
const n = Number(actual);
|
|
22264
|
+
if (Number.isNaN(n)) return false;
|
|
22265
|
+
const rhs = Number(cmp[2]);
|
|
22266
|
+
switch (cmp[1]) {
|
|
22267
|
+
case ">=": return n >= rhs;
|
|
22268
|
+
case "<=": return n <= rhs;
|
|
22269
|
+
case ">": return n > rhs;
|
|
22270
|
+
case "<": return n < rhs;
|
|
22271
|
+
}
|
|
22272
|
+
}
|
|
22273
|
+
if (p.startsWith("contains:")) {
|
|
22274
|
+
const needle = p.slice(9);
|
|
22275
|
+
if (Array.isArray(actual)) return actual.map(String).includes(needle);
|
|
22276
|
+
if (typeof actual === "string") return actual.toLowerCase().includes(needle.toLowerCase());
|
|
22277
|
+
return false;
|
|
22278
|
+
}
|
|
22279
|
+
const want = p.startsWith("=") ? p.slice(1) : p;
|
|
22280
|
+
return String(actual) === want;
|
|
22281
|
+
}
|
|
22282
|
+
/** True when an entry satisfies EVERY predicate in `where`. The special key
|
|
22283
|
+
* `type` matches the entry's page type; every other key matches `entry.meta[key]`. */
|
|
22284
|
+
function matchesWhere(entry, where) {
|
|
22285
|
+
for (const [key, pred] of Object.entries(where)) if (!matchPredicate(key === "type" ? entry.type : entry.meta?.[key], pred)) return false;
|
|
22286
|
+
return true;
|
|
22287
|
+
}
|
|
21964
22288
|
function buildTree(entries, rootId, maxDepth, currentDepth = 0, visited = /* @__PURE__ */ new Set()) {
|
|
21965
22289
|
if (maxDepth >= 0 && currentDepth >= maxDepth) return [];
|
|
21966
22290
|
return childrenOf(entries, rootId).filter((e) => !visited.has(e.id)).map((entry) => {
|
|
@@ -22013,15 +22337,24 @@ function registerTreeTools(mcp, server, validator = null) {
|
|
|
22013
22337
|
text: JSON.stringify(tree, null, 2)
|
|
22014
22338
|
}] };
|
|
22015
22339
|
});
|
|
22016
|
-
mcp.tool("find_document", "Search for documents by name
|
|
22017
|
-
query: zod.z.string().describe("
|
|
22340
|
+
mcp.tool("find_document", "Search for documents across the tree by name and/or metadata. Returns matching documents with their full ancestor path. Use this when looking for a document and you don't know where it is. At least one of `query` or `where` is required. `where` filters on metadata — e.g. find every high-priority card or every doc tagged \"bug\" without knowing labels.", {
|
|
22341
|
+
query: zod.z.string().optional().describe("Case-insensitive substring match against document labels. Optional when `where` is given."),
|
|
22342
|
+
where: zod.z.record(zod.z.string(), zod.z.string()).optional().describe("Metadata predicates, ALL of which must match. Key is a meta field (or the special key \"type\" for page type); value is a tiny predicate: \">=3\" / \"<=1\" (numeric, e.g. priority), \"contains:bug\" (array membership or string substring, e.g. tags), \"exists\" / \"!exists\", or a bare/\"=value\" for equality. Example: {\"priority\":\">=3\",\"type\":\"kanban\"}."),
|
|
22018
22343
|
rootId: zod.z.string().optional().describe("Restrict search to descendants of this document. Omit to search the entire tree.")
|
|
22019
|
-
}, async ({ query, rootId }) => {
|
|
22344
|
+
}, async ({ query, where, rootId }) => {
|
|
22345
|
+
const label = query ?? (where ? Object.keys(where).join(",") : "find");
|
|
22020
22346
|
server.setAutoStatus("searching");
|
|
22021
22347
|
server.setActiveToolCall({
|
|
22022
22348
|
name: "find_document",
|
|
22023
|
-
target:
|
|
22349
|
+
target: label
|
|
22024
22350
|
});
|
|
22351
|
+
if (!query && (!where || Object.keys(where).length === 0)) return {
|
|
22352
|
+
content: [{
|
|
22353
|
+
type: "text",
|
|
22354
|
+
text: "find_document needs at least one of `query` (label substring) or `where` (metadata predicates)."
|
|
22355
|
+
}],
|
|
22356
|
+
isError: true
|
|
22357
|
+
};
|
|
22025
22358
|
await server.ensureSpaceActive(rootId);
|
|
22026
22359
|
const treeMap = server.getTreeMap();
|
|
22027
22360
|
if (!treeMap) return { content: [{
|
|
@@ -22029,9 +22362,13 @@ function registerTreeTools(mcp, server, validator = null) {
|
|
|
22029
22362
|
text: "Not connected"
|
|
22030
22363
|
}] };
|
|
22031
22364
|
const entries = (0, _abraca_dabra.readEntries)(treeMap, server.rootDocId);
|
|
22032
|
-
const lowerQuery = query
|
|
22365
|
+
const lowerQuery = query?.toLowerCase();
|
|
22033
22366
|
const normalizedRoot = normalizeRootId(rootId, server);
|
|
22034
|
-
const matches = (normalizedRoot ? descendantsOf(entries, normalizedRoot) : entries).filter((e) =>
|
|
22367
|
+
const matches = (normalizedRoot ? descendantsOf(entries, normalizedRoot) : entries).filter((e) => {
|
|
22368
|
+
if (lowerQuery !== void 0 && !e.label.toLowerCase().includes(lowerQuery)) return false;
|
|
22369
|
+
if (where && !matchesWhere(e, where)) return false;
|
|
22370
|
+
return true;
|
|
22371
|
+
});
|
|
22035
22372
|
const byId = new Map(entries.map((e) => [e.id, e]));
|
|
22036
22373
|
const results = matches.map((entry) => {
|
|
22037
22374
|
const path = [];
|
|
@@ -22054,7 +22391,7 @@ function registerTreeTools(mcp, server, validator = null) {
|
|
|
22054
22391
|
});
|
|
22055
22392
|
if (results.length === 0) return { content: [{
|
|
22056
22393
|
type: "text",
|
|
22057
|
-
text: `No documents found matching "${query}". Try get_document_tree to see the full hierarchy.`
|
|
22394
|
+
text: `No documents found matching ${[query ? `label~"${query}"` : null, where ? `where=${JSON.stringify(where)}` : null].filter(Boolean).join(" AND ")}. Try get_document_tree to see the full hierarchy.`
|
|
22058
22395
|
}] };
|
|
22059
22396
|
return { content: [{
|
|
22060
22397
|
type: "text",
|