@abraca/mcp 2.38.0 → 2.40.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/src/tools/tree.ts CHANGED
@@ -53,6 +53,51 @@ function descendantsOf(entries: TreeEntry[], id: string | null): TreeEntry[] {
53
53
  return result
54
54
  }
55
55
 
56
+ /**
57
+ * Evaluate one `where` predicate value against a doc's actual value. The
58
+ * predicate is a small string DSL so agents can filter on meta without a
59
+ * structured query language:
60
+ * - `>=N` `<=N` `>N` `<N` → numeric comparison (value coerced to number)
61
+ * - `contains:X` → array membership OR string substring
62
+ * - `exists` / `!exists` → presence / absence (null counts as absent)
63
+ * - `=X` or bare `X` → loose equality (string-coerced)
64
+ */
65
+ export function matchPredicate(actual: unknown, pred: string): boolean {
66
+ const p = pred.trim()
67
+ if (p === 'exists') return actual !== undefined && actual !== null
68
+ if (p === '!exists') return actual === undefined || actual === null
69
+ const cmp = p.match(/^(>=|<=|>|<)\s*(-?\d+(?:\.\d+)?)$/)
70
+ if (cmp) {
71
+ const n = Number(actual)
72
+ if (Number.isNaN(n)) return false
73
+ const rhs = Number(cmp[2])
74
+ switch (cmp[1]) {
75
+ case '>=': return n >= rhs
76
+ case '<=': return n <= rhs
77
+ case '>': return n > rhs
78
+ case '<': return n < rhs
79
+ }
80
+ }
81
+ if (p.startsWith('contains:')) {
82
+ const needle = p.slice('contains:'.length)
83
+ if (Array.isArray(actual)) return actual.map(String).includes(needle)
84
+ if (typeof actual === 'string') return actual.toLowerCase().includes(needle.toLowerCase())
85
+ return false
86
+ }
87
+ const want = p.startsWith('=') ? p.slice(1) : p
88
+ return String(actual) === want
89
+ }
90
+
91
+ /** True when an entry satisfies EVERY predicate in `where`. The special key
92
+ * `type` matches the entry's page type; every other key matches `entry.meta[key]`. */
93
+ export function matchesWhere(entry: TreeEntry, where: Record<string, string>): boolean {
94
+ for (const [key, pred] of Object.entries(where)) {
95
+ const actual = key === 'type' ? entry.type : (entry.meta as Record<string, unknown> | undefined)?.[key]
96
+ if (!matchPredicate(actual, pred)) return false
97
+ }
98
+ return true
99
+ }
100
+
56
101
  export function buildTree(entries: TreeEntry[], rootId: string | null, maxDepth: number, currentDepth = 0, visited = new Set<string>()): any[] {
57
102
  if (maxDepth >= 0 && currentDepth >= maxDepth) return []
58
103
  const children = childrenOf(entries, rootId)
@@ -133,14 +178,24 @@ export function registerTreeTools(
133
178
 
134
179
  mcp.tool(
135
180
  'find_document',
136
- 'Search for documents by name across the entire tree. Returns matching documents with their full ancestor path. Use this when you\'re looking for a specific document and don\'t know where it is in the hierarchy.',
181
+ '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.',
137
182
  {
138
- query: z.string().describe('Search query — matched case-insensitively against document labels (substring match).'),
183
+ query: z.string().optional().describe('Case-insensitive substring match against document labels. Optional when `where` is given.'),
184
+ where: z.record(z.string(), 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"}.'),
139
185
  rootId: z.string().optional().describe('Restrict search to descendants of this document. Omit to search the entire tree.'),
140
186
  },
141
- async ({ query, rootId }) => {
187
+ async ({ query, where, rootId }) => {
188
+ const label = query ?? (where ? Object.keys(where).join(',') : 'find')
142
189
  server.setAutoStatus('searching')
143
- server.setActiveToolCall({ name: 'find_document', target: query })
190
+ server.setActiveToolCall({ name: 'find_document', target: label })
191
+
192
+ if (!query && (!where || Object.keys(where).length === 0)) {
193
+ return {
194
+ content: [{ type: 'text', text: 'find_document needs at least one of `query` (label substring) or `where` (metadata predicates).' }],
195
+ isError: true,
196
+ }
197
+ }
198
+
144
199
  await server.ensureSpaceActive(rootId)
145
200
  const treeMap = server.getTreeMap()
146
201
  if (!treeMap) {
@@ -148,16 +203,20 @@ export function registerTreeTools(
148
203
  }
149
204
 
150
205
  const entries = readEntries(treeMap, server.rootDocId)
151
- const lowerQuery = query.toLowerCase()
206
+ const lowerQuery = query?.toLowerCase()
152
207
 
153
208
  // If rootId specified, restrict to descendants only
154
209
  const normalizedRoot = normalizeRootId(rootId, server)
155
210
  const searchSet = normalizedRoot ? descendantsOf(entries, normalizedRoot) : entries
156
211
 
157
- // Find matches
158
- const matches = searchSet.filter(e =>
159
- e.label.toLowerCase().includes(lowerQuery)
160
- )
212
+ // Find matches — label substring (when `query` given) AND every `where`
213
+ // predicate (when given). Both are optional individually but the tool
214
+ // requires at least one (guarded above).
215
+ const matches = searchSet.filter(e => {
216
+ if (lowerQuery !== undefined && !e.label.toLowerCase().includes(lowerQuery)) return false
217
+ if (where && !matchesWhere(e, where)) return false
218
+ return true
219
+ })
161
220
 
162
221
  // Build ancestor path for each match
163
222
  const byId = new Map(entries.map(e => [e.id, e]))
@@ -182,8 +241,9 @@ export function registerTreeTools(
182
241
  })
183
242
 
184
243
  if (results.length === 0) {
244
+ const crit = [query ? `label~"${query}"` : null, where ? `where=${JSON.stringify(where)}` : null].filter(Boolean).join(' AND ')
185
245
  return {
186
- content: [{ type: 'text', text: `No documents found matching "${query}". Try get_document_tree to see the full hierarchy.` }],
246
+ content: [{ type: 'text', text: `No documents found matching ${crit}. Try get_document_tree to see the full hierarchy.` }],
187
247
  }
188
248
  }
189
249