@librechat/agents 3.0.66 → 3.0.68
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/cjs/common/enum.cjs +3 -1
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/main.cjs +14 -7
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +2 -2
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +1 -1
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/{ToolSearchRegex.cjs → ToolSearch.cjs} +318 -63
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
- package/dist/esm/common/enum.mjs +3 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/esm/messages/tools.mjs +2 -2
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +1 -1
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/{ToolSearchRegex.mjs → ToolSearch.mjs} +311 -63
- package/dist/esm/tools/ToolSearch.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +4 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/tools/ToolSearch.d.ts +133 -0
- package/dist/types/types/tools.d.ts +8 -2
- package/package.json +2 -2
- package/src/common/enum.ts +3 -1
- package/src/index.ts +1 -1
- package/src/messages/__tests__/tools.test.ts +21 -21
- package/src/messages/tools.ts +2 -2
- package/src/scripts/programmatic_exec_agent.ts +4 -4
- package/src/scripts/{tool_search_regex.ts → tool_search.ts} +5 -5
- package/src/tools/ToolNode.ts +1 -1
- package/src/tools/{ToolSearchRegex.ts → ToolSearch.ts} +390 -69
- package/src/tools/__tests__/{ToolSearchRegex.integration.test.ts → ToolSearch.integration.test.ts} +6 -6
- package/src/tools/__tests__/ToolSearch.test.ts +734 -0
- package/src/types/tools.ts +9 -2
- package/dist/cjs/tools/ToolSearchRegex.cjs.map +0 -1
- package/dist/esm/tools/ToolSearchRegex.mjs.map +0 -1
- package/dist/types/tools/ToolSearchRegex.d.ts +0 -80
- package/src/tools/__tests__/ToolSearchRegex.test.ts +0 -232
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
// src/tools/__tests__/ToolSearch.test.ts
|
|
2
|
+
/**
|
|
3
|
+
* Unit tests for Tool Search.
|
|
4
|
+
* Tests helper functions and sanitization logic without hitting the API.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect } from '@jest/globals';
|
|
7
|
+
import {
|
|
8
|
+
sanitizeRegex,
|
|
9
|
+
escapeRegexSpecialChars,
|
|
10
|
+
isDangerousPattern,
|
|
11
|
+
countNestedGroups,
|
|
12
|
+
hasNestedQuantifiers,
|
|
13
|
+
performLocalSearch,
|
|
14
|
+
extractMcpServerName,
|
|
15
|
+
isFromMcpServer,
|
|
16
|
+
isFromAnyMcpServer,
|
|
17
|
+
normalizeServerFilter,
|
|
18
|
+
getBaseToolName,
|
|
19
|
+
formatServerListing,
|
|
20
|
+
} from '../ToolSearch';
|
|
21
|
+
import type { ToolMetadata } from '@/types';
|
|
22
|
+
|
|
23
|
+
describe('ToolSearch', () => {
|
|
24
|
+
describe('escapeRegexSpecialChars', () => {
|
|
25
|
+
it('escapes special regex characters', () => {
|
|
26
|
+
expect(escapeRegexSpecialChars('hello.world')).toBe('hello\\.world');
|
|
27
|
+
expect(escapeRegexSpecialChars('test*pattern')).toBe('test\\*pattern');
|
|
28
|
+
expect(escapeRegexSpecialChars('query+result')).toBe('query\\+result');
|
|
29
|
+
expect(escapeRegexSpecialChars('a?b')).toBe('a\\?b');
|
|
30
|
+
expect(escapeRegexSpecialChars('(group)')).toBe('\\(group\\)');
|
|
31
|
+
expect(escapeRegexSpecialChars('[abc]')).toBe('\\[abc\\]');
|
|
32
|
+
expect(escapeRegexSpecialChars('a|b')).toBe('a\\|b');
|
|
33
|
+
expect(escapeRegexSpecialChars('a^b$c')).toBe('a\\^b\\$c');
|
|
34
|
+
expect(escapeRegexSpecialChars('a{2,3}')).toBe('a\\{2,3\\}');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('handles empty string', () => {
|
|
38
|
+
expect(escapeRegexSpecialChars('')).toBe('');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('handles string with no special chars', () => {
|
|
42
|
+
expect(escapeRegexSpecialChars('hello_world')).toBe('hello_world');
|
|
43
|
+
expect(escapeRegexSpecialChars('test123')).toBe('test123');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('handles multiple consecutive special chars', () => {
|
|
47
|
+
expect(escapeRegexSpecialChars('...')).toBe('\\.\\.\\.');
|
|
48
|
+
expect(escapeRegexSpecialChars('***')).toBe('\\*\\*\\*');
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('countNestedGroups', () => {
|
|
53
|
+
it('counts simple nesting', () => {
|
|
54
|
+
expect(countNestedGroups('(a)')).toBe(1);
|
|
55
|
+
expect(countNestedGroups('((a))')).toBe(2);
|
|
56
|
+
expect(countNestedGroups('(((a)))')).toBe(3);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('counts maximum depth with multiple groups', () => {
|
|
60
|
+
expect(countNestedGroups('(a)(b)(c)')).toBe(1);
|
|
61
|
+
expect(countNestedGroups('(a(b)c)')).toBe(2);
|
|
62
|
+
expect(countNestedGroups('(a(b(c)))')).toBe(3);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('handles mixed nesting levels', () => {
|
|
66
|
+
expect(countNestedGroups('(a)((b)(c))')).toBe(2);
|
|
67
|
+
expect(countNestedGroups('((a)(b))((c))')).toBe(2);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('ignores escaped parentheses', () => {
|
|
71
|
+
expect(countNestedGroups('\\(not a group\\)')).toBe(0);
|
|
72
|
+
expect(countNestedGroups('(a\\(b\\)c)')).toBe(1);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('handles no groups', () => {
|
|
76
|
+
expect(countNestedGroups('abc')).toBe(0);
|
|
77
|
+
expect(countNestedGroups('test.*pattern')).toBe(0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('handles unbalanced groups', () => {
|
|
81
|
+
expect(countNestedGroups('((a)')).toBe(2);
|
|
82
|
+
expect(countNestedGroups('(a))')).toBe(1);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('hasNestedQuantifiers', () => {
|
|
87
|
+
it('detects nested quantifiers', () => {
|
|
88
|
+
expect(hasNestedQuantifiers('(a+)+')).toBe(true);
|
|
89
|
+
expect(hasNestedQuantifiers('(a*)*')).toBe(true);
|
|
90
|
+
expect(hasNestedQuantifiers('(a+)*')).toBe(true);
|
|
91
|
+
expect(hasNestedQuantifiers('(a*)?')).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('allows safe quantifiers', () => {
|
|
95
|
+
expect(hasNestedQuantifiers('a+')).toBe(false);
|
|
96
|
+
expect(hasNestedQuantifiers('(abc)+')).toBe(false);
|
|
97
|
+
expect(hasNestedQuantifiers('a+b*c?')).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('handles complex patterns', () => {
|
|
101
|
+
expect(hasNestedQuantifiers('(a|b)+')).toBe(false);
|
|
102
|
+
// Note: This pattern might not be detected by the simple regex check
|
|
103
|
+
const complexPattern = '((a|b)+)+';
|
|
104
|
+
const result = hasNestedQuantifiers(complexPattern);
|
|
105
|
+
// Just verify it doesn't crash - detection may vary
|
|
106
|
+
expect(typeof result).toBe('boolean');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('isDangerousPattern', () => {
|
|
111
|
+
it('detects nested quantifiers', () => {
|
|
112
|
+
expect(isDangerousPattern('(a+)+')).toBe(true);
|
|
113
|
+
expect(isDangerousPattern('(a*)*')).toBe(true);
|
|
114
|
+
expect(isDangerousPattern('(.+)+')).toBe(true);
|
|
115
|
+
expect(isDangerousPattern('(.*)*')).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('detects excessive nesting', () => {
|
|
119
|
+
expect(isDangerousPattern('((((((a))))))')).toBe(true); // Depth > 5
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('detects excessive wildcards', () => {
|
|
123
|
+
const pattern = '.{1000,}';
|
|
124
|
+
expect(isDangerousPattern(pattern)).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('allows safe patterns', () => {
|
|
128
|
+
expect(isDangerousPattern('weather')).toBe(false);
|
|
129
|
+
expect(isDangerousPattern('get_.*_data')).toBe(false);
|
|
130
|
+
expect(isDangerousPattern('(a|b|c)')).toBe(false);
|
|
131
|
+
expect(isDangerousPattern('test\\d+')).toBe(false);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('detects various dangerous patterns', () => {
|
|
135
|
+
expect(isDangerousPattern('(.*)+')).toBe(true);
|
|
136
|
+
expect(isDangerousPattern('(.+)*')).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('sanitizeRegex', () => {
|
|
141
|
+
it('returns safe pattern unchanged', () => {
|
|
142
|
+
const result = sanitizeRegex('weather');
|
|
143
|
+
expect(result.safe).toBe('weather');
|
|
144
|
+
expect(result.wasEscaped).toBe(false);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('escapes dangerous patterns', () => {
|
|
148
|
+
const result = sanitizeRegex('(a+)+');
|
|
149
|
+
expect(result.safe).toBe('\\(a\\+\\)\\+');
|
|
150
|
+
expect(result.wasEscaped).toBe(true);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('escapes invalid regex', () => {
|
|
154
|
+
const result = sanitizeRegex('(unclosed');
|
|
155
|
+
expect(result.wasEscaped).toBe(true);
|
|
156
|
+
expect(result.safe).toContain('\\(');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('allows complex but safe patterns', () => {
|
|
160
|
+
const result = sanitizeRegex('get_[a-z]+_data');
|
|
161
|
+
expect(result.safe).toBe('get_[a-z]+_data');
|
|
162
|
+
expect(result.wasEscaped).toBe(false);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('handles alternation patterns', () => {
|
|
166
|
+
const result = sanitizeRegex('weather|forecast');
|
|
167
|
+
expect(result.safe).toBe('weather|forecast');
|
|
168
|
+
expect(result.wasEscaped).toBe(false);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('Pattern Validation Edge Cases', () => {
|
|
173
|
+
it('handles empty pattern', () => {
|
|
174
|
+
expect(countNestedGroups('')).toBe(0);
|
|
175
|
+
expect(hasNestedQuantifiers('')).toBe(false);
|
|
176
|
+
expect(isDangerousPattern('')).toBe(false);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('handles pattern with only quantifiers', () => {
|
|
180
|
+
expect(hasNestedQuantifiers('+++')).toBe(false);
|
|
181
|
+
expect(hasNestedQuantifiers('***')).toBe(false);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('handles escaped special sequences', () => {
|
|
185
|
+
const result = sanitizeRegex('\\d+\\w*\\s?');
|
|
186
|
+
expect(result.wasEscaped).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('sanitizes exponential backtracking patterns', () => {
|
|
190
|
+
// These can cause catastrophic backtracking
|
|
191
|
+
expect(isDangerousPattern('(a+)+')).toBe(true);
|
|
192
|
+
expect(isDangerousPattern('(a*)*')).toBe(true);
|
|
193
|
+
expect(isDangerousPattern('(.*)*')).toBe(true);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('Real-World Pattern Examples', () => {
|
|
198
|
+
it('handles common search patterns safely', () => {
|
|
199
|
+
const safePatterns = [
|
|
200
|
+
'expense',
|
|
201
|
+
'weather|forecast',
|
|
202
|
+
'data.*query',
|
|
203
|
+
'_tool$',
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
for (const pattern of safePatterns) {
|
|
207
|
+
const result = sanitizeRegex(pattern);
|
|
208
|
+
expect(result.wasEscaped).toBe(false);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('escapes clearly dangerous patterns', () => {
|
|
213
|
+
const dangerousPatterns = ['(a+)+', '(.*)+', '(.+)*'];
|
|
214
|
+
|
|
215
|
+
for (const pattern of dangerousPatterns) {
|
|
216
|
+
const result = sanitizeRegex(pattern);
|
|
217
|
+
expect(result.wasEscaped).toBe(true);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('handles patterns that may or may not be escaped', () => {
|
|
222
|
+
// These patterns might be escaped depending on validation logic
|
|
223
|
+
const edgeCasePatterns = [
|
|
224
|
+
'(?i)email',
|
|
225
|
+
'^create_',
|
|
226
|
+
'get_[a-z]+_info',
|
|
227
|
+
'get_.*',
|
|
228
|
+
'((((((a))))))',
|
|
229
|
+
'(a|a)*',
|
|
230
|
+
];
|
|
231
|
+
|
|
232
|
+
for (const pattern of edgeCasePatterns) {
|
|
233
|
+
const result = sanitizeRegex(pattern);
|
|
234
|
+
// Just verify it returns a result without crashing
|
|
235
|
+
expect(typeof result.safe).toBe('string');
|
|
236
|
+
expect(typeof result.wasEscaped).toBe('boolean');
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe('performLocalSearch', () => {
|
|
242
|
+
const mockTools: ToolMetadata[] = [
|
|
243
|
+
{
|
|
244
|
+
name: 'get_weather',
|
|
245
|
+
description: 'Get current weather data',
|
|
246
|
+
parameters: undefined,
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
name: 'get_forecast',
|
|
250
|
+
description: 'Get weather forecast for multiple days',
|
|
251
|
+
parameters: undefined,
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
name: 'send_email',
|
|
255
|
+
description: 'Send an email message',
|
|
256
|
+
parameters: undefined,
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
name: 'get_expenses',
|
|
260
|
+
description: 'Retrieve expense reports',
|
|
261
|
+
parameters: undefined,
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
name: 'calculate_expense_totals',
|
|
265
|
+
description: 'Sum up expenses by category',
|
|
266
|
+
parameters: undefined,
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
name: 'run_database_query',
|
|
270
|
+
description: 'Execute a database query',
|
|
271
|
+
parameters: {
|
|
272
|
+
type: 'object',
|
|
273
|
+
properties: {
|
|
274
|
+
query: { type: 'string' },
|
|
275
|
+
timeout: { type: 'number' },
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
];
|
|
280
|
+
|
|
281
|
+
it('finds tools by exact name match', () => {
|
|
282
|
+
const result = performLocalSearch(mockTools, 'get_weather', ['name'], 10);
|
|
283
|
+
|
|
284
|
+
expect(result.tool_references.length).toBe(1);
|
|
285
|
+
expect(result.tool_references[0].tool_name).toBe('get_weather');
|
|
286
|
+
expect(result.tool_references[0].match_score).toBe(1.0);
|
|
287
|
+
expect(result.tool_references[0].matched_field).toBe('name');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('finds tools by partial name match (starts with)', () => {
|
|
291
|
+
const result = performLocalSearch(mockTools, 'get_', ['name'], 10);
|
|
292
|
+
|
|
293
|
+
expect(result.tool_references.length).toBe(3);
|
|
294
|
+
expect(result.tool_references[0].match_score).toBe(0.95);
|
|
295
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
296
|
+
'get_weather'
|
|
297
|
+
);
|
|
298
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
299
|
+
'get_forecast'
|
|
300
|
+
);
|
|
301
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
302
|
+
'get_expenses'
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('finds tools by substring match in name', () => {
|
|
307
|
+
const result = performLocalSearch(mockTools, 'expense', ['name'], 10);
|
|
308
|
+
|
|
309
|
+
expect(result.tool_references.length).toBe(2);
|
|
310
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
311
|
+
'get_expenses'
|
|
312
|
+
);
|
|
313
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
314
|
+
'calculate_expense_totals'
|
|
315
|
+
);
|
|
316
|
+
expect(result.tool_references[0].match_score).toBe(0.85);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('performs case-insensitive search', () => {
|
|
320
|
+
const result = performLocalSearch(
|
|
321
|
+
mockTools,
|
|
322
|
+
'WEATHER',
|
|
323
|
+
['name', 'description'],
|
|
324
|
+
10
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
expect(result.tool_references.length).toBe(2);
|
|
328
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
329
|
+
'get_weather'
|
|
330
|
+
);
|
|
331
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
332
|
+
'get_forecast'
|
|
333
|
+
);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('searches in description field', () => {
|
|
337
|
+
const result = performLocalSearch(
|
|
338
|
+
mockTools,
|
|
339
|
+
'email',
|
|
340
|
+
['description'],
|
|
341
|
+
10
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
expect(result.tool_references.length).toBe(1);
|
|
345
|
+
expect(result.tool_references[0].tool_name).toBe('send_email');
|
|
346
|
+
expect(result.tool_references[0].matched_field).toBe('description');
|
|
347
|
+
expect(result.tool_references[0].match_score).toBe(0.7);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('searches in parameter names', () => {
|
|
351
|
+
const result = performLocalSearch(mockTools, 'query', ['parameters'], 10);
|
|
352
|
+
|
|
353
|
+
expect(result.tool_references.length).toBe(1);
|
|
354
|
+
expect(result.tool_references[0].tool_name).toBe('run_database_query');
|
|
355
|
+
expect(result.tool_references[0].matched_field).toBe('parameters');
|
|
356
|
+
expect(result.tool_references[0].match_score).toBe(0.55);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('prioritizes name matches over description matches', () => {
|
|
360
|
+
const result = performLocalSearch(
|
|
361
|
+
mockTools,
|
|
362
|
+
'weather',
|
|
363
|
+
['name', 'description'],
|
|
364
|
+
10
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const weatherTool = result.tool_references.find(
|
|
368
|
+
(r) => r.tool_name === 'get_weather'
|
|
369
|
+
);
|
|
370
|
+
const forecastTool = result.tool_references.find(
|
|
371
|
+
(r) => r.tool_name === 'get_forecast'
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
expect(weatherTool?.matched_field).toBe('name');
|
|
375
|
+
expect(forecastTool?.matched_field).toBe('description');
|
|
376
|
+
expect(weatherTool!.match_score).toBeGreaterThan(
|
|
377
|
+
forecastTool!.match_score
|
|
378
|
+
);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('limits results to max_results', () => {
|
|
382
|
+
const result = performLocalSearch(mockTools, 'get', ['name'], 2);
|
|
383
|
+
|
|
384
|
+
expect(result.tool_references.length).toBe(2);
|
|
385
|
+
expect(result.total_tools_searched).toBe(mockTools.length);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it('returns empty array when no matches found', () => {
|
|
389
|
+
const result = performLocalSearch(
|
|
390
|
+
mockTools,
|
|
391
|
+
'nonexistent_xyz_123',
|
|
392
|
+
['name', 'description'],
|
|
393
|
+
10
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
expect(result.tool_references.length).toBe(0);
|
|
397
|
+
expect(result.total_tools_searched).toBe(mockTools.length);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it('sorts results by score descending', () => {
|
|
401
|
+
const result = performLocalSearch(
|
|
402
|
+
mockTools,
|
|
403
|
+
'expense',
|
|
404
|
+
['name', 'description'],
|
|
405
|
+
10
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
for (let i = 1; i < result.tool_references.length; i++) {
|
|
409
|
+
expect(
|
|
410
|
+
result.tool_references[i - 1].match_score
|
|
411
|
+
).toBeGreaterThanOrEqual(result.tool_references[i].match_score);
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it('handles empty tools array', () => {
|
|
416
|
+
const result = performLocalSearch([], 'test', ['name'], 10);
|
|
417
|
+
|
|
418
|
+
expect(result.tool_references.length).toBe(0);
|
|
419
|
+
expect(result.total_tools_searched).toBe(0);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('handles empty query gracefully', () => {
|
|
423
|
+
const result = performLocalSearch(mockTools, '', ['name'], 10);
|
|
424
|
+
|
|
425
|
+
expect(result.tool_references.length).toBe(mockTools.length);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('includes correct metadata in response', () => {
|
|
429
|
+
const result = performLocalSearch(mockTools, 'weather', ['name'], 10);
|
|
430
|
+
|
|
431
|
+
expect(result.total_tools_searched).toBe(mockTools.length);
|
|
432
|
+
expect(result.pattern_used).toBe('weather');
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('provides snippet in results', () => {
|
|
436
|
+
const result = performLocalSearch(
|
|
437
|
+
mockTools,
|
|
438
|
+
'database',
|
|
439
|
+
['description'],
|
|
440
|
+
10
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
expect(result.tool_references[0].snippet).toBeTruthy();
|
|
444
|
+
expect(result.tool_references[0].snippet.length).toBeGreaterThan(0);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
describe('extractMcpServerName', () => {
|
|
449
|
+
it('extracts server name from MCP tool name', () => {
|
|
450
|
+
expect(extractMcpServerName('get_weather_mcp_weather-server')).toBe(
|
|
451
|
+
'weather-server'
|
|
452
|
+
);
|
|
453
|
+
expect(extractMcpServerName('send_email_mcp_gmail')).toBe('gmail');
|
|
454
|
+
expect(extractMcpServerName('query_database_mcp_postgres-mcp')).toBe(
|
|
455
|
+
'postgres-mcp'
|
|
456
|
+
);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it('returns undefined for non-MCP tools', () => {
|
|
460
|
+
expect(extractMcpServerName('get_weather')).toBeUndefined();
|
|
461
|
+
expect(extractMcpServerName('send_email')).toBeUndefined();
|
|
462
|
+
expect(extractMcpServerName('regular_tool_name')).toBeUndefined();
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it('handles edge cases', () => {
|
|
466
|
+
expect(extractMcpServerName('_mcp_server')).toBe('server');
|
|
467
|
+
expect(extractMcpServerName('tool_mcp_')).toBe('');
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
describe('getBaseToolName', () => {
|
|
472
|
+
it('extracts base name from MCP tool name', () => {
|
|
473
|
+
expect(getBaseToolName('get_weather_mcp_weather-server')).toBe(
|
|
474
|
+
'get_weather'
|
|
475
|
+
);
|
|
476
|
+
expect(getBaseToolName('send_email_mcp_gmail')).toBe('send_email');
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it('returns full name for non-MCP tools', () => {
|
|
480
|
+
expect(getBaseToolName('get_weather')).toBe('get_weather');
|
|
481
|
+
expect(getBaseToolName('regular_tool')).toBe('regular_tool');
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
describe('isFromMcpServer', () => {
|
|
486
|
+
it('returns true for matching MCP server', () => {
|
|
487
|
+
expect(
|
|
488
|
+
isFromMcpServer('get_weather_mcp_weather-server', 'weather-server')
|
|
489
|
+
).toBe(true);
|
|
490
|
+
expect(isFromMcpServer('send_email_mcp_gmail', 'gmail')).toBe(true);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it('returns false for non-matching MCP server', () => {
|
|
494
|
+
expect(
|
|
495
|
+
isFromMcpServer('get_weather_mcp_weather-server', 'other-server')
|
|
496
|
+
).toBe(false);
|
|
497
|
+
expect(isFromMcpServer('send_email_mcp_gmail', 'outlook')).toBe(false);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it('returns false for non-MCP tools', () => {
|
|
501
|
+
expect(isFromMcpServer('get_weather', 'weather-server')).toBe(false);
|
|
502
|
+
expect(isFromMcpServer('regular_tool', 'any-server')).toBe(false);
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
describe('isFromAnyMcpServer', () => {
|
|
507
|
+
it('returns true if tool is from any of the specified servers', () => {
|
|
508
|
+
expect(
|
|
509
|
+
isFromAnyMcpServer('get_weather_mcp_weather-api', [
|
|
510
|
+
'weather-api',
|
|
511
|
+
'gmail',
|
|
512
|
+
])
|
|
513
|
+
).toBe(true);
|
|
514
|
+
expect(
|
|
515
|
+
isFromAnyMcpServer('send_email_mcp_gmail', ['weather-api', 'gmail'])
|
|
516
|
+
).toBe(true);
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('returns false if tool is not from any specified server', () => {
|
|
520
|
+
expect(
|
|
521
|
+
isFromAnyMcpServer('get_weather_mcp_weather-api', ['gmail', 'slack'])
|
|
522
|
+
).toBe(false);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it('returns false for non-MCP tools', () => {
|
|
526
|
+
expect(isFromAnyMcpServer('regular_tool', ['weather-api', 'gmail'])).toBe(
|
|
527
|
+
false
|
|
528
|
+
);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it('returns false for empty server list', () => {
|
|
532
|
+
expect(isFromAnyMcpServer('get_weather_mcp_weather-api', [])).toBe(false);
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
describe('normalizeServerFilter', () => {
|
|
537
|
+
it('converts string to single-element array', () => {
|
|
538
|
+
expect(normalizeServerFilter('gmail')).toEqual(['gmail']);
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
it('passes through arrays unchanged', () => {
|
|
542
|
+
expect(normalizeServerFilter(['gmail', 'slack'])).toEqual([
|
|
543
|
+
'gmail',
|
|
544
|
+
'slack',
|
|
545
|
+
]);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it('returns empty array for undefined', () => {
|
|
549
|
+
expect(normalizeServerFilter(undefined)).toEqual([]);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it('returns empty array for empty string', () => {
|
|
553
|
+
expect(normalizeServerFilter('')).toEqual([]);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
it('filters out empty strings from arrays', () => {
|
|
557
|
+
expect(normalizeServerFilter(['gmail', '', 'slack'])).toEqual([
|
|
558
|
+
'gmail',
|
|
559
|
+
'slack',
|
|
560
|
+
]);
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
describe('performLocalSearch with MCP tools', () => {
|
|
565
|
+
const mcpTools: ToolMetadata[] = [
|
|
566
|
+
{
|
|
567
|
+
name: 'get_weather_mcp_weather-server',
|
|
568
|
+
description: 'Get weather from MCP server',
|
|
569
|
+
parameters: undefined,
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
name: 'get_forecast_mcp_weather-server',
|
|
573
|
+
description: 'Get forecast from MCP server',
|
|
574
|
+
parameters: undefined,
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
name: 'send_email_mcp_gmail',
|
|
578
|
+
description: 'Send email via Gmail MCP',
|
|
579
|
+
parameters: undefined,
|
|
580
|
+
},
|
|
581
|
+
{
|
|
582
|
+
name: 'read_inbox_mcp_gmail',
|
|
583
|
+
description: 'Read inbox via Gmail MCP',
|
|
584
|
+
parameters: undefined,
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
name: 'get_weather',
|
|
588
|
+
description: 'Regular weather tool (not MCP)',
|
|
589
|
+
parameters: undefined,
|
|
590
|
+
},
|
|
591
|
+
];
|
|
592
|
+
|
|
593
|
+
it('searches across all tools including MCP tools', () => {
|
|
594
|
+
const result = performLocalSearch(
|
|
595
|
+
mcpTools,
|
|
596
|
+
'weather',
|
|
597
|
+
['name', 'description'],
|
|
598
|
+
10
|
|
599
|
+
);
|
|
600
|
+
|
|
601
|
+
expect(result.tool_references.length).toBe(3);
|
|
602
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
603
|
+
'get_weather_mcp_weather-server'
|
|
604
|
+
);
|
|
605
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
606
|
+
'get_weather'
|
|
607
|
+
);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it('finds MCP tools by searching the full name including server suffix', () => {
|
|
611
|
+
const result = performLocalSearch(mcpTools, 'gmail', ['name'], 10);
|
|
612
|
+
|
|
613
|
+
expect(result.tool_references.length).toBe(2);
|
|
614
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
615
|
+
'send_email_mcp_gmail'
|
|
616
|
+
);
|
|
617
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
618
|
+
'read_inbox_mcp_gmail'
|
|
619
|
+
);
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it('can search for tools by MCP delimiter', () => {
|
|
623
|
+
const result = performLocalSearch(mcpTools, '_mcp_', ['name'], 10);
|
|
624
|
+
|
|
625
|
+
expect(result.tool_references.length).toBe(4);
|
|
626
|
+
expect(result.tool_references.map((r) => r.tool_name)).not.toContain(
|
|
627
|
+
'get_weather'
|
|
628
|
+
);
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
describe('formatServerListing', () => {
|
|
633
|
+
const serverTools: ToolMetadata[] = [
|
|
634
|
+
{
|
|
635
|
+
name: 'get_weather_mcp_weather-api',
|
|
636
|
+
description: 'Get current weather conditions for a location',
|
|
637
|
+
parameters: undefined,
|
|
638
|
+
},
|
|
639
|
+
{
|
|
640
|
+
name: 'get_forecast_mcp_weather-api',
|
|
641
|
+
description: 'Get weather forecast for the next 7 days',
|
|
642
|
+
parameters: undefined,
|
|
643
|
+
},
|
|
644
|
+
];
|
|
645
|
+
|
|
646
|
+
it('formats server listing with tool names and descriptions', () => {
|
|
647
|
+
const result = formatServerListing(serverTools, 'weather-api');
|
|
648
|
+
|
|
649
|
+
expect(result).toContain('Tools from MCP server: weather-api');
|
|
650
|
+
expect(result).toContain('2 tool(s)');
|
|
651
|
+
expect(result).toContain('get_weather');
|
|
652
|
+
expect(result).toContain('get_forecast');
|
|
653
|
+
expect(result).toContain('preview only');
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it('includes hint to search for specific tool to load it', () => {
|
|
657
|
+
const result = formatServerListing(serverTools, 'weather-api');
|
|
658
|
+
|
|
659
|
+
expect(result).toContain('To use a tool, search for it by name');
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
it('uses base tool name (without MCP suffix) in display', () => {
|
|
663
|
+
const result = formatServerListing(serverTools, 'weather-api');
|
|
664
|
+
|
|
665
|
+
expect(result).toContain('**get_weather**');
|
|
666
|
+
expect(result).not.toContain('**get_weather_mcp_weather-api**');
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it('handles empty tools array', () => {
|
|
670
|
+
const result = formatServerListing([], 'empty-server');
|
|
671
|
+
|
|
672
|
+
expect(result).toContain('No tools found');
|
|
673
|
+
expect(result).toContain('empty-server');
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it('truncates long descriptions', () => {
|
|
677
|
+
const toolsWithLongDesc: ToolMetadata[] = [
|
|
678
|
+
{
|
|
679
|
+
name: 'long_tool_mcp_server',
|
|
680
|
+
description:
|
|
681
|
+
'This is a very long description that exceeds 80 characters and should be truncated to keep the listing compact and readable.',
|
|
682
|
+
parameters: undefined,
|
|
683
|
+
},
|
|
684
|
+
];
|
|
685
|
+
|
|
686
|
+
const result = formatServerListing(toolsWithLongDesc, 'server');
|
|
687
|
+
|
|
688
|
+
expect(result).toContain('...');
|
|
689
|
+
expect(result.length).toBeLessThan(
|
|
690
|
+
toolsWithLongDesc[0].description.length + 200
|
|
691
|
+
);
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it('handles multiple servers with grouped output', () => {
|
|
695
|
+
const multiServerTools: ToolMetadata[] = [
|
|
696
|
+
{
|
|
697
|
+
name: 'get_weather_mcp_weather-api',
|
|
698
|
+
description: 'Get weather',
|
|
699
|
+
parameters: undefined,
|
|
700
|
+
},
|
|
701
|
+
{
|
|
702
|
+
name: 'send_email_mcp_gmail',
|
|
703
|
+
description: 'Send email',
|
|
704
|
+
parameters: undefined,
|
|
705
|
+
},
|
|
706
|
+
{
|
|
707
|
+
name: 'read_inbox_mcp_gmail',
|
|
708
|
+
description: 'Read inbox',
|
|
709
|
+
parameters: undefined,
|
|
710
|
+
},
|
|
711
|
+
];
|
|
712
|
+
|
|
713
|
+
const result = formatServerListing(multiServerTools, [
|
|
714
|
+
'weather-api',
|
|
715
|
+
'gmail',
|
|
716
|
+
]);
|
|
717
|
+
|
|
718
|
+
expect(result).toContain('Tools from MCP servers: weather-api, gmail');
|
|
719
|
+
expect(result).toContain('3 tool(s)');
|
|
720
|
+
expect(result).toContain('### weather-api');
|
|
721
|
+
expect(result).toContain('### gmail');
|
|
722
|
+
expect(result).toContain('get_weather');
|
|
723
|
+
expect(result).toContain('send_email');
|
|
724
|
+
expect(result).toContain('read_inbox');
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
it('accepts single server as array', () => {
|
|
728
|
+
const result = formatServerListing(serverTools, ['weather-api']);
|
|
729
|
+
|
|
730
|
+
expect(result).toContain('Tools from MCP server: weather-api');
|
|
731
|
+
expect(result).not.toContain('###');
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
});
|