@librechat/agents 3.0.67 → 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 +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/main.cjs +6 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +175 -8
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/esm/tools/ToolSearch.mjs +170 -9
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +3 -1
- package/dist/types/tools/ToolSearch.d.ts +45 -2
- package/dist/types/types/tools.d.ts +3 -1
- package/package.json +1 -1
- package/src/common/enum.ts +2 -0
- package/src/tools/ToolSearch.ts +209 -9
- package/src/tools/__tests__/ToolSearch.test.ts +294 -1
- package/src/types/tools.ts +3 -1
|
@@ -22,10 +22,15 @@ const SEARCH_TIMEOUT = 5000;
|
|
|
22
22
|
*/
|
|
23
23
|
function createToolSearchSchema(mode) {
|
|
24
24
|
const queryDescription = mode === 'local'
|
|
25
|
-
? 'Search term to find in tool names and descriptions. Case-insensitive substring matching.'
|
|
26
|
-
: 'Regex pattern to search tool names and descriptions.
|
|
25
|
+
? 'Search term to find in tool names and descriptions. Case-insensitive substring matching. Optional if mcp_server is provided.'
|
|
26
|
+
: 'Regex pattern to search tool names and descriptions. Optional if mcp_server is provided.';
|
|
27
27
|
return z.object({
|
|
28
|
-
query: z
|
|
28
|
+
query: z
|
|
29
|
+
.string()
|
|
30
|
+
.max(MAX_PATTERN_LENGTH)
|
|
31
|
+
.optional()
|
|
32
|
+
.default('')
|
|
33
|
+
.describe(queryDescription),
|
|
29
34
|
fields: z
|
|
30
35
|
.array(z.enum(['name', 'description', 'parameters']))
|
|
31
36
|
.optional()
|
|
@@ -39,8 +44,62 @@ function createToolSearchSchema(mode) {
|
|
|
39
44
|
.optional()
|
|
40
45
|
.default(10)
|
|
41
46
|
.describe('Maximum number of matching tools to return'),
|
|
47
|
+
mcp_server: z
|
|
48
|
+
.union([z.string(), z.array(z.string())])
|
|
49
|
+
.optional()
|
|
50
|
+
.describe('Filter to tools from specific MCP server(s). Can be a single server name or array of names. If provided without a query, lists all tools from those servers.'),
|
|
42
51
|
});
|
|
43
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Extracts the MCP server name from a tool name.
|
|
55
|
+
* MCP tools follow the pattern: toolName_mcp_serverName
|
|
56
|
+
* @param toolName - The full tool name
|
|
57
|
+
* @returns The server name if it's an MCP tool, undefined otherwise
|
|
58
|
+
*/
|
|
59
|
+
function extractMcpServerName(toolName) {
|
|
60
|
+
const delimiterIndex = toolName.indexOf(Constants.MCP_DELIMITER);
|
|
61
|
+
if (delimiterIndex === -1) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
return toolName.substring(delimiterIndex + Constants.MCP_DELIMITER.length);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks if a tool belongs to a specific MCP server.
|
|
68
|
+
* @param toolName - The full tool name
|
|
69
|
+
* @param serverName - The server name to match
|
|
70
|
+
* @returns True if the tool belongs to the specified server
|
|
71
|
+
*/
|
|
72
|
+
function isFromMcpServer(toolName, serverName) {
|
|
73
|
+
const toolServer = extractMcpServerName(toolName);
|
|
74
|
+
return toolServer === serverName;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Checks if a tool belongs to any of the specified MCP servers.
|
|
78
|
+
* @param toolName - The full tool name
|
|
79
|
+
* @param serverNames - Array of server names to match
|
|
80
|
+
* @returns True if the tool belongs to any of the specified servers
|
|
81
|
+
*/
|
|
82
|
+
function isFromAnyMcpServer(toolName, serverNames) {
|
|
83
|
+
const toolServer = extractMcpServerName(toolName);
|
|
84
|
+
if (toolServer === undefined) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
return serverNames.includes(toolServer);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Normalizes server filter input to always be an array.
|
|
91
|
+
* @param serverFilter - String, array of strings, or undefined
|
|
92
|
+
* @returns Array of server names (empty if none specified)
|
|
93
|
+
*/
|
|
94
|
+
function normalizeServerFilter(serverFilter) {
|
|
95
|
+
if (serverFilter === undefined) {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
if (typeof serverFilter === 'string') {
|
|
99
|
+
return serverFilter === '' ? [] : [serverFilter];
|
|
100
|
+
}
|
|
101
|
+
return serverFilter.filter((s) => s !== '');
|
|
102
|
+
}
|
|
44
103
|
/**
|
|
45
104
|
* Escapes special regex characters in a string to use as a literal pattern.
|
|
46
105
|
* @param pattern - The string to escape
|
|
@@ -334,6 +393,64 @@ function formatSearchResults(searchResponse) {
|
|
|
334
393
|
response += `Pattern used: ${pattern_used}`;
|
|
335
394
|
return response;
|
|
336
395
|
}
|
|
396
|
+
/**
|
|
397
|
+
* Extracts the base tool name (without MCP server suffix) from a full tool name.
|
|
398
|
+
* @param toolName - The full tool name
|
|
399
|
+
* @returns The base tool name without server suffix
|
|
400
|
+
*/
|
|
401
|
+
function getBaseToolName(toolName) {
|
|
402
|
+
const delimiterIndex = toolName.indexOf(Constants.MCP_DELIMITER);
|
|
403
|
+
if (delimiterIndex === -1) {
|
|
404
|
+
return toolName;
|
|
405
|
+
}
|
|
406
|
+
return toolName.substring(0, delimiterIndex);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Formats a server listing response when listing all tools from MCP server(s).
|
|
410
|
+
* Provides a cohesive view of all tools grouped by server.
|
|
411
|
+
* NOTE: This is a PREVIEW only - tools are NOT discovered/loaded.
|
|
412
|
+
* @param tools - Array of tool metadata from the server(s)
|
|
413
|
+
* @param serverNames - The MCP server name(s)
|
|
414
|
+
* @returns Formatted string showing all tools from the server(s)
|
|
415
|
+
*/
|
|
416
|
+
function formatServerListing(tools, serverNames) {
|
|
417
|
+
const servers = Array.isArray(serverNames) ? serverNames : [serverNames];
|
|
418
|
+
if (tools.length === 0) {
|
|
419
|
+
return `No tools found from MCP server(s): ${servers.join(', ')}.`;
|
|
420
|
+
}
|
|
421
|
+
const toolsByServer = new Map();
|
|
422
|
+
for (const tool of tools) {
|
|
423
|
+
const server = extractMcpServerName(tool.name) ?? 'unknown';
|
|
424
|
+
const existing = toolsByServer.get(server) ?? [];
|
|
425
|
+
existing.push(tool);
|
|
426
|
+
toolsByServer.set(server, existing);
|
|
427
|
+
}
|
|
428
|
+
let response = servers.length === 1
|
|
429
|
+
? `## Tools from MCP server: ${servers[0]}\n\n`
|
|
430
|
+
: `## Tools from MCP servers: ${servers.join(', ')}\n\n`;
|
|
431
|
+
response += `Found ${tools.length} tool(s) (preview only - not yet loaded):\n\n`;
|
|
432
|
+
for (const [server, serverTools] of toolsByServer) {
|
|
433
|
+
if (servers.length > 1) {
|
|
434
|
+
response += `### ${server}\n\n`;
|
|
435
|
+
}
|
|
436
|
+
for (const tool of serverTools) {
|
|
437
|
+
const baseName = getBaseToolName(tool.name);
|
|
438
|
+
response += `- **${baseName}**`;
|
|
439
|
+
if (tool.description) {
|
|
440
|
+
const shortDesc = tool.description.length > 80
|
|
441
|
+
? tool.description.substring(0, 77) + '...'
|
|
442
|
+
: tool.description;
|
|
443
|
+
response += `: ${shortDesc}`;
|
|
444
|
+
}
|
|
445
|
+
response += '\n';
|
|
446
|
+
}
|
|
447
|
+
if (servers.length > 1) {
|
|
448
|
+
response += '\n';
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
response += `\n_To use a tool, search for it by name (e.g., query: "${getBaseToolName(tools[0]?.name ?? 'tool_name')}") to load it._`;
|
|
452
|
+
return response;
|
|
453
|
+
}
|
|
337
454
|
/**
|
|
338
455
|
* Creates a Tool Search tool for discovering tools from a large registry.
|
|
339
456
|
*
|
|
@@ -377,6 +494,13 @@ function createToolSearch(initParams = {}) {
|
|
|
377
494
|
}
|
|
378
495
|
const baseEndpoint = initParams.baseUrl ?? getCodeBaseURL();
|
|
379
496
|
const EXEC_ENDPOINT = `${baseEndpoint}/exec`;
|
|
497
|
+
const mcpInstructions = `
|
|
498
|
+
|
|
499
|
+
MCP Server Tools:
|
|
500
|
+
- Tools from MCP servers follow the naming convention: toolName${Constants.MCP_DELIMITER}serverName
|
|
501
|
+
- Example: "get_weather${Constants.MCP_DELIMITER}weather-api" is the "get_weather" tool from the "weather-api" server
|
|
502
|
+
- Use mcp_server parameter to filter by server (e.g., mcp_server: "weather-api")
|
|
503
|
+
- If mcp_server is provided without a query, lists ALL tools from that server`;
|
|
380
504
|
const description = mode === 'local'
|
|
381
505
|
? `
|
|
382
506
|
Searches through available tools to find ones matching your search term.
|
|
@@ -387,6 +511,7 @@ Usage:
|
|
|
387
511
|
- Use this when you need to discover tools for a specific task.
|
|
388
512
|
- Results include tool names, match quality scores, and snippets showing where the match occurred.
|
|
389
513
|
- Higher scores (0.95+) indicate name matches, medium scores (0.70+) indicate description matches.
|
|
514
|
+
${mcpInstructions}
|
|
390
515
|
`.trim()
|
|
391
516
|
: `
|
|
392
517
|
Searches through available tools to find ones matching your query pattern.
|
|
@@ -396,14 +521,18 @@ Usage:
|
|
|
396
521
|
- Use this when you need to discover tools for a specific task.
|
|
397
522
|
- Results include tool names, match quality scores, and snippets showing where the match occurred.
|
|
398
523
|
- Higher scores (0.9+) indicate name matches, medium scores (0.7+) indicate description matches.
|
|
524
|
+
${mcpInstructions}
|
|
399
525
|
`.trim();
|
|
400
526
|
return tool(async (params, config) => {
|
|
401
|
-
const { query, fields = ['name', 'description'], max_results = 10, } = params;
|
|
402
|
-
const { toolRegistry: paramToolRegistry, onlyDeferred: paramOnlyDeferred, } = config.toolCall ?? {};
|
|
527
|
+
const { query, fields = ['name', 'description'], max_results = 10, mcp_server, } = params;
|
|
528
|
+
const { toolRegistry: paramToolRegistry, onlyDeferred: paramOnlyDeferred, mcpServer: paramMcpServer, } = config.toolCall ?? {};
|
|
403
529
|
const toolRegistry = paramToolRegistry ?? initParams.toolRegistry;
|
|
404
530
|
const onlyDeferred = paramOnlyDeferred !== undefined
|
|
405
531
|
? paramOnlyDeferred
|
|
406
532
|
: defaultOnlyDeferred;
|
|
533
|
+
const rawServerFilter = mcp_server ?? paramMcpServer ?? initParams.mcpServer;
|
|
534
|
+
const serverFilters = normalizeServerFilter(rawServerFilter);
|
|
535
|
+
const hasServerFilter = serverFilters.length > 0;
|
|
407
536
|
if (toolRegistry == null) {
|
|
408
537
|
return [
|
|
409
538
|
'Error: No tool registry provided. Configure toolRegistry at agent level or initialization.',
|
|
@@ -419,18 +548,49 @@ Usage:
|
|
|
419
548
|
}
|
|
420
549
|
const toolsArray = Array.from(toolRegistry.values());
|
|
421
550
|
const deferredTools = toolsArray
|
|
422
|
-
.filter((lcTool) =>
|
|
551
|
+
.filter((lcTool) => {
|
|
552
|
+
if (onlyDeferred === true && lcTool.defer_loading !== true) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
if (hasServerFilter &&
|
|
556
|
+
!isFromAnyMcpServer(lcTool.name, serverFilters)) {
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
559
|
+
return true;
|
|
560
|
+
})
|
|
423
561
|
.map((lcTool) => ({
|
|
424
562
|
name: lcTool.name,
|
|
425
563
|
description: lcTool.description ?? '',
|
|
426
564
|
parameters: simplifyParametersForSearch(lcTool.parameters),
|
|
427
565
|
}));
|
|
428
566
|
if (deferredTools.length === 0) {
|
|
567
|
+
const serverMsg = hasServerFilter
|
|
568
|
+
? ` from MCP server(s): ${serverFilters.join(', ')}`
|
|
569
|
+
: '';
|
|
429
570
|
return [
|
|
430
|
-
|
|
571
|
+
`No tools available to search${serverMsg}. The tool registry is empty or no matching deferred tools are registered.`,
|
|
431
572
|
{
|
|
432
573
|
tool_references: [],
|
|
433
|
-
metadata: {
|
|
574
|
+
metadata: {
|
|
575
|
+
total_searched: 0,
|
|
576
|
+
pattern: query,
|
|
577
|
+
mcp_server: serverFilters,
|
|
578
|
+
},
|
|
579
|
+
},
|
|
580
|
+
];
|
|
581
|
+
}
|
|
582
|
+
const isServerListing = hasServerFilter && query === '';
|
|
583
|
+
if (isServerListing) {
|
|
584
|
+
const formattedOutput = formatServerListing(deferredTools, serverFilters);
|
|
585
|
+
return [
|
|
586
|
+
formattedOutput,
|
|
587
|
+
{
|
|
588
|
+
tool_references: [],
|
|
589
|
+
metadata: {
|
|
590
|
+
total_available: deferredTools.length,
|
|
591
|
+
mcp_server: serverFilters,
|
|
592
|
+
listing_mode: true,
|
|
593
|
+
},
|
|
434
594
|
},
|
|
435
595
|
];
|
|
436
596
|
}
|
|
@@ -444,6 +604,7 @@ Usage:
|
|
|
444
604
|
metadata: {
|
|
445
605
|
total_searched: searchResponse.total_tools_searched,
|
|
446
606
|
pattern: searchResponse.pattern_used,
|
|
607
|
+
mcp_server: serverFilters.length > 0 ? serverFilters : undefined,
|
|
447
608
|
},
|
|
448
609
|
},
|
|
449
610
|
];
|
|
@@ -531,5 +692,5 @@ Usage:
|
|
|
531
692
|
});
|
|
532
693
|
}
|
|
533
694
|
|
|
534
|
-
export { countNestedGroups, createToolSearch, escapeRegexSpecialChars, hasNestedQuantifiers, isDangerousPattern, performLocalSearch, sanitizeRegex };
|
|
695
|
+
export { countNestedGroups, createToolSearch, escapeRegexSpecialChars, extractMcpServerName, formatServerListing, getBaseToolName, hasNestedQuantifiers, isDangerousPattern, isFromAnyMcpServer, isFromMcpServer, normalizeServerFilter, performLocalSearch, sanitizeRegex };
|
|
535
696
|
//# sourceMappingURL=ToolSearch.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToolSearch.mjs","sources":["../../../src/tools/ToolSearch.ts"],"sourcesContent":["// src/tools/ToolSearch.ts\nimport { z } from 'zod';\nimport { config } from 'dotenv';\nimport fetch, { RequestInit } from 'node-fetch';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as t from '@/types';\nimport { getCodeBaseURL } from './CodeExecutor';\nimport { EnvVar, Constants } from '@/common';\n\nconfig();\n\n/** Maximum allowed regex pattern length */\nconst MAX_PATTERN_LENGTH = 200;\n\n/** Maximum allowed regex nesting depth */\nconst MAX_REGEX_COMPLEXITY = 5;\n\n/** Default search timeout in milliseconds */\nconst SEARCH_TIMEOUT = 5000;\n\n/** Zod schema type for tool search parameters */\ntype ToolSearchSchema = z.ZodObject<{\n query: z.ZodString;\n fields: z.ZodDefault<\n z.ZodOptional<z.ZodArray<z.ZodEnum<['name', 'description', 'parameters']>>>\n >;\n max_results: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;\n}>;\n\n/**\n * Creates the Zod schema with dynamic query description based on mode.\n * @param mode - The search mode determining query interpretation\n * @returns Zod schema for tool search parameters\n */\nfunction createToolSearchSchema(mode: t.ToolSearchMode): ToolSearchSchema {\n const queryDescription =\n mode === 'local'\n ? 'Search term to find in tool names and descriptions. Case-insensitive substring matching.'\n : 'Regex pattern to search tool names and descriptions. Special regex characters will be sanitized for safety.';\n\n return z.object({\n query: z.string().min(1).max(MAX_PATTERN_LENGTH).describe(queryDescription),\n fields: z\n .array(z.enum(['name', 'description', 'parameters']))\n .optional()\n .default(['name', 'description'])\n .describe('Which fields to search. Default: name and description'),\n max_results: z\n .number()\n .int()\n .min(1)\n .max(50)\n .optional()\n .default(10)\n .describe('Maximum number of matching tools to return'),\n });\n}\n\n/**\n * Escapes special regex characters in a string to use as a literal pattern.\n * @param pattern - The string to escape\n * @returns The escaped string safe for use in a RegExp\n */\nfunction escapeRegexSpecialChars(pattern: string): string {\n return pattern.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\n/**\n * Counts the maximum nesting depth of groups in a regex pattern.\n * @param pattern - The regex pattern to analyze\n * @returns The maximum nesting depth\n */\nfunction countNestedGroups(pattern: string): number {\n let maxDepth = 0;\n let currentDepth = 0;\n\n for (let i = 0; i < pattern.length; i++) {\n if (pattern[i] === '(' && (i === 0 || pattern[i - 1] !== '\\\\')) {\n currentDepth++;\n maxDepth = Math.max(maxDepth, currentDepth);\n } else if (pattern[i] === ')' && (i === 0 || pattern[i - 1] !== '\\\\')) {\n currentDepth = Math.max(0, currentDepth - 1);\n }\n }\n\n return maxDepth;\n}\n\n/**\n * Detects nested quantifiers that can cause catastrophic backtracking.\n * Patterns like (a+)+, (a*)*, (a+)*, etc.\n * @param pattern - The regex pattern to check\n * @returns True if nested quantifiers are detected\n */\nfunction hasNestedQuantifiers(pattern: string): boolean {\n const nestedQuantifierPattern = /\\([^)]*[+*][^)]*\\)[+*?]/;\n return nestedQuantifierPattern.test(pattern);\n}\n\n/**\n * Checks if a regex pattern contains potentially dangerous constructs.\n * @param pattern - The regex pattern to validate\n * @returns True if the pattern is dangerous\n */\nfunction isDangerousPattern(pattern: string): boolean {\n if (hasNestedQuantifiers(pattern)) {\n return true;\n }\n\n if (countNestedGroups(pattern) > MAX_REGEX_COMPLEXITY) {\n return true;\n }\n\n const dangerousPatterns = [\n /\\.\\{1000,\\}/, // Excessive wildcards\n /\\(\\?=\\.\\{100,\\}\\)/, // Runaway lookaheads\n /\\([^)]*\\|\\s*\\){20,}/, // Excessive alternation (rough check)\n /\\(\\.\\*\\)\\+/, // (.*)+\n /\\(\\.\\+\\)\\+/, // (.+)+\n /\\(\\.\\*\\)\\*/, // (.*)*\n /\\(\\.\\+\\)\\*/, // (.+)*\n ];\n\n for (const dangerous of dangerousPatterns) {\n if (dangerous.test(pattern)) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Sanitizes a regex pattern for safe execution.\n * If the pattern is dangerous, it will be escaped to a literal string search.\n * @param pattern - The regex pattern to sanitize\n * @returns Object containing the safe pattern and whether it was escaped\n */\nfunction sanitizeRegex(pattern: string): { safe: string; wasEscaped: boolean } {\n if (isDangerousPattern(pattern)) {\n return {\n safe: escapeRegexSpecialChars(pattern),\n wasEscaped: true,\n };\n }\n\n try {\n new RegExp(pattern);\n return { safe: pattern, wasEscaped: false };\n } catch {\n return {\n safe: escapeRegexSpecialChars(pattern),\n wasEscaped: true,\n };\n }\n}\n\n/**\n * Simplifies tool parameters for search purposes.\n * Extracts only the essential structure needed for parameter name searching.\n * @param parameters - The tool's JSON schema parameters\n * @returns Simplified parameters object\n */\nfunction simplifyParametersForSearch(\n parameters?: t.JsonSchemaType\n): t.JsonSchemaType | undefined {\n if (!parameters) {\n return undefined;\n }\n\n if (parameters.properties) {\n return {\n type: parameters.type,\n properties: Object.fromEntries(\n Object.entries(parameters.properties).map(([key, value]) => [\n key,\n { type: (value as t.JsonSchemaType).type },\n ])\n ),\n } as t.JsonSchemaType;\n }\n\n return { type: parameters.type };\n}\n\n/**\n * Performs safe local substring search without regex.\n * Uses case-insensitive String.includes() for complete safety against ReDoS.\n * @param tools - Array of tool metadata to search\n * @param query - The search term (treated as literal substring)\n * @param fields - Which fields to search\n * @param maxResults - Maximum results to return\n * @returns Search response with matching tools\n */\nfunction performLocalSearch(\n tools: t.ToolMetadata[],\n query: string,\n fields: string[],\n maxResults: number\n): t.ToolSearchResponse {\n const lowerQuery = query.toLowerCase();\n const results: t.ToolSearchResult[] = [];\n\n for (const tool of tools) {\n let bestScore = 0;\n let matchedField = '';\n let snippet = '';\n\n if (fields.includes('name')) {\n const lowerName = tool.name.toLowerCase();\n if (lowerName.includes(lowerQuery)) {\n const isExactMatch = lowerName === lowerQuery;\n const startsWithQuery = lowerName.startsWith(lowerQuery);\n bestScore = isExactMatch ? 1.0 : startsWithQuery ? 0.95 : 0.85;\n matchedField = 'name';\n snippet = tool.name;\n }\n }\n\n if (fields.includes('description') && tool.description) {\n const lowerDesc = tool.description.toLowerCase();\n if (lowerDesc.includes(lowerQuery) && bestScore === 0) {\n bestScore = 0.7;\n matchedField = 'description';\n snippet = tool.description.substring(0, 100);\n }\n }\n\n if (fields.includes('parameters') && tool.parameters?.properties) {\n const paramNames = Object.keys(tool.parameters.properties)\n .join(' ')\n .toLowerCase();\n if (paramNames.includes(lowerQuery) && bestScore === 0) {\n bestScore = 0.55;\n matchedField = 'parameters';\n snippet = Object.keys(tool.parameters.properties).join(' ');\n }\n }\n\n if (bestScore > 0) {\n results.push({\n tool_name: tool.name,\n match_score: bestScore,\n matched_field: matchedField,\n snippet,\n });\n }\n }\n\n results.sort((a, b) => b.match_score - a.match_score);\n const topResults = results.slice(0, maxResults);\n\n return {\n tool_references: topResults,\n total_tools_searched: tools.length,\n pattern_used: query,\n };\n}\n\n/**\n * Generates the JavaScript search script to be executed in the sandbox.\n * Uses plain JavaScript for maximum compatibility with the Code API.\n * @param deferredTools - Array of tool metadata to search through\n * @param fields - Which fields to search\n * @param maxResults - Maximum number of results to return\n * @param sanitizedPattern - The sanitized regex pattern\n * @returns The JavaScript code string\n */\nfunction generateSearchScript(\n deferredTools: t.ToolMetadata[],\n fields: string[],\n maxResults: number,\n sanitizedPattern: string\n): string {\n const lines = [\n '// Tool definitions (injected)',\n 'var tools = ' + JSON.stringify(deferredTools) + ';',\n 'var searchFields = ' + JSON.stringify(fields) + ';',\n 'var maxResults = ' + maxResults + ';',\n 'var pattern = ' + JSON.stringify(sanitizedPattern) + ';',\n '',\n '// Compile regex (pattern is sanitized client-side)',\n 'var regex;',\n 'try {',\n ' regex = new RegExp(pattern, \\'i\\');',\n '} catch (e) {',\n ' regex = new RegExp(pattern.replace(/[.*+?^${}()[\\\\]\\\\\\\\|]/g, \"\\\\\\\\$&\"), \"i\");',\n '}',\n '',\n '// Search logic',\n 'var results = [];',\n '',\n 'for (var j = 0; j < tools.length; j++) {',\n ' var tool = tools[j];',\n ' var bestScore = 0;',\n ' var matchedField = \\'\\';',\n ' var snippet = \\'\\';',\n '',\n ' // Search name (highest priority)',\n ' if (searchFields.indexOf(\\'name\\') >= 0 && regex.test(tool.name)) {',\n ' bestScore = 0.95;',\n ' matchedField = \\'name\\';',\n ' snippet = tool.name;',\n ' }',\n '',\n ' // Search description (medium priority)',\n ' if (searchFields.indexOf(\\'description\\') >= 0 && tool.description && regex.test(tool.description)) {',\n ' if (bestScore === 0) {',\n ' bestScore = 0.75;',\n ' matchedField = \\'description\\';',\n ' snippet = tool.description.substring(0, 100);',\n ' }',\n ' }',\n '',\n ' // Search parameter names (lower priority)',\n ' if (searchFields.indexOf(\\'parameters\\') >= 0 && tool.parameters && tool.parameters.properties) {',\n ' var paramNames = Object.keys(tool.parameters.properties).join(\\' \\');',\n ' if (regex.test(paramNames)) {',\n ' if (bestScore === 0) {',\n ' bestScore = 0.60;',\n ' matchedField = \\'parameters\\';',\n ' snippet = paramNames;',\n ' }',\n ' }',\n ' }',\n '',\n ' if (bestScore > 0) {',\n ' results.push({',\n ' tool_name: tool.name,',\n ' match_score: bestScore,',\n ' matched_field: matchedField,',\n ' snippet: snippet',\n ' });',\n ' }',\n '}',\n '',\n '// Sort by score (descending) and limit results',\n 'results.sort(function(a, b) { return b.match_score - a.match_score; });',\n 'var topResults = results.slice(0, maxResults);',\n '',\n '// Output as JSON',\n 'console.log(JSON.stringify({',\n ' tool_references: topResults.map(function(r) {',\n ' return {',\n ' tool_name: r.tool_name,',\n ' match_score: r.match_score,',\n ' matched_field: r.matched_field,',\n ' snippet: r.snippet',\n ' };',\n ' }),',\n ' total_tools_searched: tools.length,',\n ' pattern_used: pattern',\n '}));',\n ];\n return lines.join('\\n');\n}\n\n/**\n * Parses the search results from stdout JSON.\n * @param stdout - The stdout string containing JSON results\n * @returns Parsed search response\n */\nfunction parseSearchResults(stdout: string): t.ToolSearchResponse {\n const jsonMatch = stdout.trim();\n const parsed = JSON.parse(jsonMatch) as t.ToolSearchResponse;\n return parsed;\n}\n\n/**\n * Formats search results into a human-readable string.\n * @param searchResponse - The parsed search response\n * @returns Formatted string for LLM consumption\n */\nfunction formatSearchResults(searchResponse: t.ToolSearchResponse): string {\n const { tool_references, total_tools_searched, pattern_used } =\n searchResponse;\n\n if (tool_references.length === 0) {\n return `No tools matched the pattern \"${pattern_used}\".\\nTotal tools searched: ${total_tools_searched}`;\n }\n\n let response = `Found ${tool_references.length} matching tools:\\n\\n`;\n\n for (const ref of tool_references) {\n response += `- ${ref.tool_name} (score: ${ref.match_score.toFixed(2)})\\n`;\n response += ` Matched in: ${ref.matched_field}\\n`;\n response += ` Snippet: ${ref.snippet}\\n\\n`;\n }\n\n response += `Total tools searched: ${total_tools_searched}\\n`;\n response += `Pattern used: ${pattern_used}`;\n\n return response;\n}\n\n/**\n * Creates a Tool Search tool for discovering tools from a large registry.\n *\n * This tool enables AI agents to dynamically discover tools from a large library\n * without loading all tool definitions into the LLM context window. The agent\n * can search for relevant tools on-demand.\n *\n * **Modes:**\n * - `code_interpreter` (default): Uses external sandbox for regex search. Safer for complex patterns.\n * - `local`: Uses safe substring matching locally. No network call, faster, completely safe from ReDoS.\n *\n * The tool registry can be provided either:\n * 1. At initialization time via params.toolRegistry\n * 2. At runtime via config.configurable.toolRegistry when invoking\n *\n * @param params - Configuration parameters for the tool (toolRegistry is optional)\n * @returns A LangChain DynamicStructuredTool for tool searching\n *\n * @example\n * // Option 1: Code interpreter mode (regex via sandbox)\n * const tool = createToolSearch({ apiKey, toolRegistry });\n * await tool.invoke({ query: 'expense.*report' });\n *\n * @example\n * // Option 2: Local mode (safe substring search, no API key needed)\n * const tool = createToolSearch({ mode: 'local', toolRegistry });\n * await tool.invoke({ query: 'expense' });\n */\nfunction createToolSearch(\n initParams: t.ToolSearchParams = {}\n): DynamicStructuredTool<ReturnType<typeof createToolSearchSchema>> {\n const mode: t.ToolSearchMode = initParams.mode ?? 'code_interpreter';\n const defaultOnlyDeferred = initParams.onlyDeferred ?? true;\n const schema = createToolSearchSchema(mode);\n\n const apiKey: string =\n mode === 'code_interpreter'\n ? ((initParams[EnvVar.CODE_API_KEY] as string | undefined) ??\n initParams.apiKey ??\n getEnvironmentVariable(EnvVar.CODE_API_KEY) ??\n '')\n : '';\n\n if (mode === 'code_interpreter' && !apiKey) {\n throw new Error(\n 'No API key provided for tool search in code_interpreter mode. Use mode: \"local\" to search without an API key.'\n );\n }\n\n const baseEndpoint = initParams.baseUrl ?? getCodeBaseURL();\n const EXEC_ENDPOINT = `${baseEndpoint}/exec`;\n\n const description =\n mode === 'local'\n ? `\nSearches through available tools to find ones matching your search term.\n\nUsage:\n- Provide a search term to find in tool names and descriptions.\n- Uses case-insensitive substring matching (fast and safe).\n- Use this when you need to discover tools for a specific task.\n- Results include tool names, match quality scores, and snippets showing where the match occurred.\n- Higher scores (0.95+) indicate name matches, medium scores (0.70+) indicate description matches.\n`.trim()\n : `\nSearches through available tools to find ones matching your query pattern.\n\nUsage:\n- Provide a regex pattern to search tool names and descriptions.\n- Use this when you need to discover tools for a specific task.\n- Results include tool names, match quality scores, and snippets showing where the match occurred.\n- Higher scores (0.9+) indicate name matches, medium scores (0.7+) indicate description matches.\n`.trim();\n\n return tool<typeof schema>(\n async (params, config) => {\n const {\n query,\n fields = ['name', 'description'],\n max_results = 10,\n } = params;\n\n const {\n toolRegistry: paramToolRegistry,\n onlyDeferred: paramOnlyDeferred,\n } = config.toolCall ?? {};\n\n const toolRegistry = paramToolRegistry ?? initParams.toolRegistry;\n const onlyDeferred =\n paramOnlyDeferred !== undefined\n ? paramOnlyDeferred\n : defaultOnlyDeferred;\n\n if (toolRegistry == null) {\n return [\n 'Error: No tool registry provided. Configure toolRegistry at agent level or initialization.',\n {\n tool_references: [],\n metadata: {\n total_searched: 0,\n pattern: query,\n error: 'No tool registry provided',\n },\n },\n ];\n }\n\n const toolsArray: t.LCTool[] = Array.from(toolRegistry.values());\n const deferredTools: t.ToolMetadata[] = toolsArray\n .filter((lcTool) =>\n onlyDeferred === true ? lcTool.defer_loading === true : true\n )\n .map((lcTool) => ({\n name: lcTool.name,\n description: lcTool.description ?? '',\n parameters: simplifyParametersForSearch(lcTool.parameters),\n }));\n\n if (deferredTools.length === 0) {\n return [\n 'No tools available to search. The tool registry is empty or no deferred tools are registered.',\n {\n tool_references: [],\n metadata: { total_searched: 0, pattern: query },\n },\n ];\n }\n\n if (mode === 'local') {\n const searchResponse = performLocalSearch(\n deferredTools,\n query,\n fields,\n max_results\n );\n const formattedOutput = formatSearchResults(searchResponse);\n\n return [\n formattedOutput,\n {\n tool_references: searchResponse.tool_references,\n metadata: {\n total_searched: searchResponse.total_tools_searched,\n pattern: searchResponse.pattern_used,\n },\n },\n ];\n }\n\n const { safe: sanitizedPattern, wasEscaped } = sanitizeRegex(query);\n let warningMessage = '';\n if (wasEscaped) {\n warningMessage =\n 'Note: The provided pattern was converted to a literal search for safety.\\n\\n';\n }\n\n const searchScript = generateSearchScript(\n deferredTools,\n fields,\n max_results,\n sanitizedPattern\n );\n\n const postData = {\n lang: 'js',\n code: searchScript,\n timeout: SEARCH_TIMEOUT,\n };\n\n try {\n const fetchOptions: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'LibreChat/1.0',\n 'X-API-Key': apiKey,\n },\n body: JSON.stringify(postData),\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n\n const response = await fetch(EXEC_ENDPOINT, fetchOptions);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const result: t.ExecuteResult = await response.json();\n\n if (result.stderr && result.stderr.trim()) {\n // eslint-disable-next-line no-console\n console.warn('[ToolSearch] stderr:', result.stderr);\n }\n\n if (!result.stdout || !result.stdout.trim()) {\n return [\n `${warningMessage}No tools matched the pattern \"${sanitizedPattern}\".\\nTotal tools searched: ${deferredTools.length}`,\n {\n tool_references: [],\n metadata: {\n total_searched: deferredTools.length,\n pattern: sanitizedPattern,\n },\n },\n ];\n }\n\n const searchResponse = parseSearchResults(result.stdout);\n const formattedOutput = `${warningMessage}${formatSearchResults(searchResponse)}`;\n\n return [\n formattedOutput,\n {\n tool_references: searchResponse.tool_references,\n metadata: {\n total_searched: searchResponse.total_tools_searched,\n pattern: searchResponse.pattern_used,\n },\n },\n ];\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('[ToolSearch] Error:', error);\n\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return [\n `Tool search failed: ${errorMessage}\\n\\nSuggestion: Try a simpler search pattern or search for specific tool names.`,\n {\n tool_references: [],\n metadata: {\n total_searched: 0,\n pattern: sanitizedPattern,\n error: errorMessage,\n },\n },\n ];\n }\n },\n {\n name: Constants.TOOL_SEARCH,\n description,\n schema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n}\n\nexport {\n createToolSearch,\n performLocalSearch,\n sanitizeRegex,\n escapeRegexSpecialChars,\n isDangerousPattern,\n countNestedGroups,\n hasNestedQuantifiers,\n};\n"],"names":[],"mappings":";;;;;;;;;AAAA;AAWA,MAAM,EAAE;AAER;AACA,MAAM,kBAAkB,GAAG,GAAG;AAE9B;AACA,MAAM,oBAAoB,GAAG,CAAC;AAE9B;AACA,MAAM,cAAc,GAAG,IAAI;AAW3B;;;;AAIG;AACH,SAAS,sBAAsB,CAAC,IAAsB,EAAA;AACpD,IAAA,MAAM,gBAAgB,GACpB,IAAI,KAAK;AACP,UAAE;UACA,6GAA6G;IAEnH,OAAO,CAAC,CAAC,MAAM,CAAC;AACd,QAAA,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,QAAQ,CAAC,gBAAgB,CAAC;AAC3E,QAAA,MAAM,EAAE;AACL,aAAA,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,aAAa,EAAE,YAAY,CAAC,CAAC;AACnD,aAAA,QAAQ;AACR,aAAA,OAAO,CAAC,CAAC,MAAM,EAAE,aAAa,CAAC;aAC/B,QAAQ,CAAC,uDAAuD,CAAC;AACpE,QAAA,WAAW,EAAE;AACV,aAAA,MAAM;AACN,aAAA,GAAG;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,IAAI,EAAE,SAAS,CAAC,WAAW;QAC3B,WAAW;QACX,MAAM;QACN,cAAc,EAAE,SAAS,CAAC,oBAAoB;AAC/C,KAAA,CACF;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"ToolSearch.mjs","sources":["../../../src/tools/ToolSearch.ts"],"sourcesContent":["// src/tools/ToolSearch.ts\nimport { z } from 'zod';\nimport { config } from 'dotenv';\nimport fetch, { RequestInit } from 'node-fetch';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as t from '@/types';\nimport { getCodeBaseURL } from './CodeExecutor';\nimport { EnvVar, Constants } from '@/common';\n\nconfig();\n\n/** Maximum allowed regex pattern length */\nconst MAX_PATTERN_LENGTH = 200;\n\n/** Maximum allowed regex nesting depth */\nconst MAX_REGEX_COMPLEXITY = 5;\n\n/** Default search timeout in milliseconds */\nconst SEARCH_TIMEOUT = 5000;\n\n/** Zod schema type for tool search parameters */\ntype ToolSearchSchema = z.ZodObject<{\n query: z.ZodDefault<z.ZodOptional<z.ZodString>>;\n fields: z.ZodDefault<\n z.ZodOptional<z.ZodArray<z.ZodEnum<['name', 'description', 'parameters']>>>\n >;\n max_results: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;\n mcp_server: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString>]>>;\n}>;\n\n/**\n * Creates the Zod schema with dynamic query description based on mode.\n * @param mode - The search mode determining query interpretation\n * @returns Zod schema for tool search parameters\n */\nfunction createToolSearchSchema(mode: t.ToolSearchMode): ToolSearchSchema {\n const queryDescription =\n mode === 'local'\n ? 'Search term to find in tool names and descriptions. Case-insensitive substring matching. Optional if mcp_server is provided.'\n : 'Regex pattern to search tool names and descriptions. Optional if mcp_server is provided.';\n\n return z.object({\n query: z\n .string()\n .max(MAX_PATTERN_LENGTH)\n .optional()\n .default('')\n .describe(queryDescription),\n fields: z\n .array(z.enum(['name', 'description', 'parameters']))\n .optional()\n .default(['name', 'description'])\n .describe('Which fields to search. Default: name and description'),\n max_results: z\n .number()\n .int()\n .min(1)\n .max(50)\n .optional()\n .default(10)\n .describe('Maximum number of matching tools to return'),\n mcp_server: z\n .union([z.string(), z.array(z.string())])\n .optional()\n .describe(\n 'Filter to tools from specific MCP server(s). Can be a single server name or array of names. If provided without a query, lists all tools from those servers.'\n ),\n });\n}\n\n/**\n * Extracts the MCP server name from a tool name.\n * MCP tools follow the pattern: toolName_mcp_serverName\n * @param toolName - The full tool name\n * @returns The server name if it's an MCP tool, undefined otherwise\n */\nfunction extractMcpServerName(toolName: string): string | undefined {\n const delimiterIndex = toolName.indexOf(Constants.MCP_DELIMITER);\n if (delimiterIndex === -1) {\n return undefined;\n }\n return toolName.substring(delimiterIndex + Constants.MCP_DELIMITER.length);\n}\n\n/**\n * Checks if a tool belongs to a specific MCP server.\n * @param toolName - The full tool name\n * @param serverName - The server name to match\n * @returns True if the tool belongs to the specified server\n */\nfunction isFromMcpServer(toolName: string, serverName: string): boolean {\n const toolServer = extractMcpServerName(toolName);\n return toolServer === serverName;\n}\n\n/**\n * Checks if a tool belongs to any of the specified MCP servers.\n * @param toolName - The full tool name\n * @param serverNames - Array of server names to match\n * @returns True if the tool belongs to any of the specified servers\n */\nfunction isFromAnyMcpServer(toolName: string, serverNames: string[]): boolean {\n const toolServer = extractMcpServerName(toolName);\n if (toolServer === undefined) {\n return false;\n }\n return serverNames.includes(toolServer);\n}\n\n/**\n * Normalizes server filter input to always be an array.\n * @param serverFilter - String, array of strings, or undefined\n * @returns Array of server names (empty if none specified)\n */\nfunction normalizeServerFilter(\n serverFilter: string | string[] | undefined\n): string[] {\n if (serverFilter === undefined) {\n return [];\n }\n if (typeof serverFilter === 'string') {\n return serverFilter === '' ? [] : [serverFilter];\n }\n return serverFilter.filter((s) => s !== '');\n}\n\n/**\n * Escapes special regex characters in a string to use as a literal pattern.\n * @param pattern - The string to escape\n * @returns The escaped string safe for use in a RegExp\n */\nfunction escapeRegexSpecialChars(pattern: string): string {\n return pattern.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\n/**\n * Counts the maximum nesting depth of groups in a regex pattern.\n * @param pattern - The regex pattern to analyze\n * @returns The maximum nesting depth\n */\nfunction countNestedGroups(pattern: string): number {\n let maxDepth = 0;\n let currentDepth = 0;\n\n for (let i = 0; i < pattern.length; i++) {\n if (pattern[i] === '(' && (i === 0 || pattern[i - 1] !== '\\\\')) {\n currentDepth++;\n maxDepth = Math.max(maxDepth, currentDepth);\n } else if (pattern[i] === ')' && (i === 0 || pattern[i - 1] !== '\\\\')) {\n currentDepth = Math.max(0, currentDepth - 1);\n }\n }\n\n return maxDepth;\n}\n\n/**\n * Detects nested quantifiers that can cause catastrophic backtracking.\n * Patterns like (a+)+, (a*)*, (a+)*, etc.\n * @param pattern - The regex pattern to check\n * @returns True if nested quantifiers are detected\n */\nfunction hasNestedQuantifiers(pattern: string): boolean {\n const nestedQuantifierPattern = /\\([^)]*[+*][^)]*\\)[+*?]/;\n return nestedQuantifierPattern.test(pattern);\n}\n\n/**\n * Checks if a regex pattern contains potentially dangerous constructs.\n * @param pattern - The regex pattern to validate\n * @returns True if the pattern is dangerous\n */\nfunction isDangerousPattern(pattern: string): boolean {\n if (hasNestedQuantifiers(pattern)) {\n return true;\n }\n\n if (countNestedGroups(pattern) > MAX_REGEX_COMPLEXITY) {\n return true;\n }\n\n const dangerousPatterns = [\n /\\.\\{1000,\\}/, // Excessive wildcards\n /\\(\\?=\\.\\{100,\\}\\)/, // Runaway lookaheads\n /\\([^)]*\\|\\s*\\){20,}/, // Excessive alternation (rough check)\n /\\(\\.\\*\\)\\+/, // (.*)+\n /\\(\\.\\+\\)\\+/, // (.+)+\n /\\(\\.\\*\\)\\*/, // (.*)*\n /\\(\\.\\+\\)\\*/, // (.+)*\n ];\n\n for (const dangerous of dangerousPatterns) {\n if (dangerous.test(pattern)) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Sanitizes a regex pattern for safe execution.\n * If the pattern is dangerous, it will be escaped to a literal string search.\n * @param pattern - The regex pattern to sanitize\n * @returns Object containing the safe pattern and whether it was escaped\n */\nfunction sanitizeRegex(pattern: string): { safe: string; wasEscaped: boolean } {\n if (isDangerousPattern(pattern)) {\n return {\n safe: escapeRegexSpecialChars(pattern),\n wasEscaped: true,\n };\n }\n\n try {\n new RegExp(pattern);\n return { safe: pattern, wasEscaped: false };\n } catch {\n return {\n safe: escapeRegexSpecialChars(pattern),\n wasEscaped: true,\n };\n }\n}\n\n/**\n * Simplifies tool parameters for search purposes.\n * Extracts only the essential structure needed for parameter name searching.\n * @param parameters - The tool's JSON schema parameters\n * @returns Simplified parameters object\n */\nfunction simplifyParametersForSearch(\n parameters?: t.JsonSchemaType\n): t.JsonSchemaType | undefined {\n if (!parameters) {\n return undefined;\n }\n\n if (parameters.properties) {\n return {\n type: parameters.type,\n properties: Object.fromEntries(\n Object.entries(parameters.properties).map(([key, value]) => [\n key,\n { type: (value as t.JsonSchemaType).type },\n ])\n ),\n } as t.JsonSchemaType;\n }\n\n return { type: parameters.type };\n}\n\n/**\n * Performs safe local substring search without regex.\n * Uses case-insensitive String.includes() for complete safety against ReDoS.\n * @param tools - Array of tool metadata to search\n * @param query - The search term (treated as literal substring)\n * @param fields - Which fields to search\n * @param maxResults - Maximum results to return\n * @returns Search response with matching tools\n */\nfunction performLocalSearch(\n tools: t.ToolMetadata[],\n query: string,\n fields: string[],\n maxResults: number\n): t.ToolSearchResponse {\n const lowerQuery = query.toLowerCase();\n const results: t.ToolSearchResult[] = [];\n\n for (const tool of tools) {\n let bestScore = 0;\n let matchedField = '';\n let snippet = '';\n\n if (fields.includes('name')) {\n const lowerName = tool.name.toLowerCase();\n if (lowerName.includes(lowerQuery)) {\n const isExactMatch = lowerName === lowerQuery;\n const startsWithQuery = lowerName.startsWith(lowerQuery);\n bestScore = isExactMatch ? 1.0 : startsWithQuery ? 0.95 : 0.85;\n matchedField = 'name';\n snippet = tool.name;\n }\n }\n\n if (fields.includes('description') && tool.description) {\n const lowerDesc = tool.description.toLowerCase();\n if (lowerDesc.includes(lowerQuery) && bestScore === 0) {\n bestScore = 0.7;\n matchedField = 'description';\n snippet = tool.description.substring(0, 100);\n }\n }\n\n if (fields.includes('parameters') && tool.parameters?.properties) {\n const paramNames = Object.keys(tool.parameters.properties)\n .join(' ')\n .toLowerCase();\n if (paramNames.includes(lowerQuery) && bestScore === 0) {\n bestScore = 0.55;\n matchedField = 'parameters';\n snippet = Object.keys(tool.parameters.properties).join(' ');\n }\n }\n\n if (bestScore > 0) {\n results.push({\n tool_name: tool.name,\n match_score: bestScore,\n matched_field: matchedField,\n snippet,\n });\n }\n }\n\n results.sort((a, b) => b.match_score - a.match_score);\n const topResults = results.slice(0, maxResults);\n\n return {\n tool_references: topResults,\n total_tools_searched: tools.length,\n pattern_used: query,\n };\n}\n\n/**\n * Generates the JavaScript search script to be executed in the sandbox.\n * Uses plain JavaScript for maximum compatibility with the Code API.\n * @param deferredTools - Array of tool metadata to search through\n * @param fields - Which fields to search\n * @param maxResults - Maximum number of results to return\n * @param sanitizedPattern - The sanitized regex pattern\n * @returns The JavaScript code string\n */\nfunction generateSearchScript(\n deferredTools: t.ToolMetadata[],\n fields: string[],\n maxResults: number,\n sanitizedPattern: string\n): string {\n const lines = [\n '// Tool definitions (injected)',\n 'var tools = ' + JSON.stringify(deferredTools) + ';',\n 'var searchFields = ' + JSON.stringify(fields) + ';',\n 'var maxResults = ' + maxResults + ';',\n 'var pattern = ' + JSON.stringify(sanitizedPattern) + ';',\n '',\n '// Compile regex (pattern is sanitized client-side)',\n 'var regex;',\n 'try {',\n ' regex = new RegExp(pattern, \\'i\\');',\n '} catch (e) {',\n ' regex = new RegExp(pattern.replace(/[.*+?^${}()[\\\\]\\\\\\\\|]/g, \"\\\\\\\\$&\"), \"i\");',\n '}',\n '',\n '// Search logic',\n 'var results = [];',\n '',\n 'for (var j = 0; j < tools.length; j++) {',\n ' var tool = tools[j];',\n ' var bestScore = 0;',\n ' var matchedField = \\'\\';',\n ' var snippet = \\'\\';',\n '',\n ' // Search name (highest priority)',\n ' if (searchFields.indexOf(\\'name\\') >= 0 && regex.test(tool.name)) {',\n ' bestScore = 0.95;',\n ' matchedField = \\'name\\';',\n ' snippet = tool.name;',\n ' }',\n '',\n ' // Search description (medium priority)',\n ' if (searchFields.indexOf(\\'description\\') >= 0 && tool.description && regex.test(tool.description)) {',\n ' if (bestScore === 0) {',\n ' bestScore = 0.75;',\n ' matchedField = \\'description\\';',\n ' snippet = tool.description.substring(0, 100);',\n ' }',\n ' }',\n '',\n ' // Search parameter names (lower priority)',\n ' if (searchFields.indexOf(\\'parameters\\') >= 0 && tool.parameters && tool.parameters.properties) {',\n ' var paramNames = Object.keys(tool.parameters.properties).join(\\' \\');',\n ' if (regex.test(paramNames)) {',\n ' if (bestScore === 0) {',\n ' bestScore = 0.60;',\n ' matchedField = \\'parameters\\';',\n ' snippet = paramNames;',\n ' }',\n ' }',\n ' }',\n '',\n ' if (bestScore > 0) {',\n ' results.push({',\n ' tool_name: tool.name,',\n ' match_score: bestScore,',\n ' matched_field: matchedField,',\n ' snippet: snippet',\n ' });',\n ' }',\n '}',\n '',\n '// Sort by score (descending) and limit results',\n 'results.sort(function(a, b) { return b.match_score - a.match_score; });',\n 'var topResults = results.slice(0, maxResults);',\n '',\n '// Output as JSON',\n 'console.log(JSON.stringify({',\n ' tool_references: topResults.map(function(r) {',\n ' return {',\n ' tool_name: r.tool_name,',\n ' match_score: r.match_score,',\n ' matched_field: r.matched_field,',\n ' snippet: r.snippet',\n ' };',\n ' }),',\n ' total_tools_searched: tools.length,',\n ' pattern_used: pattern',\n '}));',\n ];\n return lines.join('\\n');\n}\n\n/**\n * Parses the search results from stdout JSON.\n * @param stdout - The stdout string containing JSON results\n * @returns Parsed search response\n */\nfunction parseSearchResults(stdout: string): t.ToolSearchResponse {\n const jsonMatch = stdout.trim();\n const parsed = JSON.parse(jsonMatch) as t.ToolSearchResponse;\n return parsed;\n}\n\n/**\n * Formats search results into a human-readable string.\n * @param searchResponse - The parsed search response\n * @returns Formatted string for LLM consumption\n */\nfunction formatSearchResults(searchResponse: t.ToolSearchResponse): string {\n const { tool_references, total_tools_searched, pattern_used } =\n searchResponse;\n\n if (tool_references.length === 0) {\n return `No tools matched the pattern \"${pattern_used}\".\\nTotal tools searched: ${total_tools_searched}`;\n }\n\n let response = `Found ${tool_references.length} matching tools:\\n\\n`;\n\n for (const ref of tool_references) {\n response += `- ${ref.tool_name} (score: ${ref.match_score.toFixed(2)})\\n`;\n response += ` Matched in: ${ref.matched_field}\\n`;\n response += ` Snippet: ${ref.snippet}\\n\\n`;\n }\n\n response += `Total tools searched: ${total_tools_searched}\\n`;\n response += `Pattern used: ${pattern_used}`;\n\n return response;\n}\n\n/**\n * Extracts the base tool name (without MCP server suffix) from a full tool name.\n * @param toolName - The full tool name\n * @returns The base tool name without server suffix\n */\nfunction getBaseToolName(toolName: string): string {\n const delimiterIndex = toolName.indexOf(Constants.MCP_DELIMITER);\n if (delimiterIndex === -1) {\n return toolName;\n }\n return toolName.substring(0, delimiterIndex);\n}\n\n/**\n * Formats a server listing response when listing all tools from MCP server(s).\n * Provides a cohesive view of all tools grouped by server.\n * NOTE: This is a PREVIEW only - tools are NOT discovered/loaded.\n * @param tools - Array of tool metadata from the server(s)\n * @param serverNames - The MCP server name(s)\n * @returns Formatted string showing all tools from the server(s)\n */\nfunction formatServerListing(\n tools: t.ToolMetadata[],\n serverNames: string | string[]\n): string {\n const servers = Array.isArray(serverNames) ? serverNames : [serverNames];\n\n if (tools.length === 0) {\n return `No tools found from MCP server(s): ${servers.join(', ')}.`;\n }\n\n const toolsByServer = new Map<string, t.ToolMetadata[]>();\n for (const tool of tools) {\n const server = extractMcpServerName(tool.name) ?? 'unknown';\n const existing = toolsByServer.get(server) ?? [];\n existing.push(tool);\n toolsByServer.set(server, existing);\n }\n\n let response =\n servers.length === 1\n ? `## Tools from MCP server: ${servers[0]}\\n\\n`\n : `## Tools from MCP servers: ${servers.join(', ')}\\n\\n`;\n\n response += `Found ${tools.length} tool(s) (preview only - not yet loaded):\\n\\n`;\n\n for (const [server, serverTools] of toolsByServer) {\n if (servers.length > 1) {\n response += `### ${server}\\n\\n`;\n }\n for (const tool of serverTools) {\n const baseName = getBaseToolName(tool.name);\n response += `- **${baseName}**`;\n if (tool.description) {\n const shortDesc =\n tool.description.length > 80\n ? tool.description.substring(0, 77) + '...'\n : tool.description;\n response += `: ${shortDesc}`;\n }\n response += '\\n';\n }\n if (servers.length > 1) {\n response += '\\n';\n }\n }\n\n response += `\\n_To use a tool, search for it by name (e.g., query: \"${getBaseToolName(tools[0]?.name ?? 'tool_name')}\") to load it._`;\n\n return response;\n}\n\n/**\n * Creates a Tool Search tool for discovering tools from a large registry.\n *\n * This tool enables AI agents to dynamically discover tools from a large library\n * without loading all tool definitions into the LLM context window. The agent\n * can search for relevant tools on-demand.\n *\n * **Modes:**\n * - `code_interpreter` (default): Uses external sandbox for regex search. Safer for complex patterns.\n * - `local`: Uses safe substring matching locally. No network call, faster, completely safe from ReDoS.\n *\n * The tool registry can be provided either:\n * 1. At initialization time via params.toolRegistry\n * 2. At runtime via config.configurable.toolRegistry when invoking\n *\n * @param params - Configuration parameters for the tool (toolRegistry is optional)\n * @returns A LangChain DynamicStructuredTool for tool searching\n *\n * @example\n * // Option 1: Code interpreter mode (regex via sandbox)\n * const tool = createToolSearch({ apiKey, toolRegistry });\n * await tool.invoke({ query: 'expense.*report' });\n *\n * @example\n * // Option 2: Local mode (safe substring search, no API key needed)\n * const tool = createToolSearch({ mode: 'local', toolRegistry });\n * await tool.invoke({ query: 'expense' });\n */\nfunction createToolSearch(\n initParams: t.ToolSearchParams = {}\n): DynamicStructuredTool<ReturnType<typeof createToolSearchSchema>> {\n const mode: t.ToolSearchMode = initParams.mode ?? 'code_interpreter';\n const defaultOnlyDeferred = initParams.onlyDeferred ?? true;\n const schema = createToolSearchSchema(mode);\n\n const apiKey: string =\n mode === 'code_interpreter'\n ? ((initParams[EnvVar.CODE_API_KEY] as string | undefined) ??\n initParams.apiKey ??\n getEnvironmentVariable(EnvVar.CODE_API_KEY) ??\n '')\n : '';\n\n if (mode === 'code_interpreter' && !apiKey) {\n throw new Error(\n 'No API key provided for tool search in code_interpreter mode. Use mode: \"local\" to search without an API key.'\n );\n }\n\n const baseEndpoint = initParams.baseUrl ?? getCodeBaseURL();\n const EXEC_ENDPOINT = `${baseEndpoint}/exec`;\n\n const mcpInstructions = `\n\nMCP Server Tools:\n- Tools from MCP servers follow the naming convention: toolName${Constants.MCP_DELIMITER}serverName\n- Example: \"get_weather${Constants.MCP_DELIMITER}weather-api\" is the \"get_weather\" tool from the \"weather-api\" server\n- Use mcp_server parameter to filter by server (e.g., mcp_server: \"weather-api\")\n- If mcp_server is provided without a query, lists ALL tools from that server`;\n\n const description =\n mode === 'local'\n ? `\nSearches through available tools to find ones matching your search term.\n\nUsage:\n- Provide a search term to find in tool names and descriptions.\n- Uses case-insensitive substring matching (fast and safe).\n- Use this when you need to discover tools for a specific task.\n- Results include tool names, match quality scores, and snippets showing where the match occurred.\n- Higher scores (0.95+) indicate name matches, medium scores (0.70+) indicate description matches.\n${mcpInstructions}\n`.trim()\n : `\nSearches through available tools to find ones matching your query pattern.\n\nUsage:\n- Provide a regex pattern to search tool names and descriptions.\n- Use this when you need to discover tools for a specific task.\n- Results include tool names, match quality scores, and snippets showing where the match occurred.\n- Higher scores (0.9+) indicate name matches, medium scores (0.7+) indicate description matches.\n${mcpInstructions}\n`.trim();\n\n return tool<typeof schema>(\n async (params, config) => {\n const {\n query,\n fields = ['name', 'description'],\n max_results = 10,\n mcp_server,\n } = params;\n\n const {\n toolRegistry: paramToolRegistry,\n onlyDeferred: paramOnlyDeferred,\n mcpServer: paramMcpServer,\n } = config.toolCall ?? {};\n\n const toolRegistry = paramToolRegistry ?? initParams.toolRegistry;\n const onlyDeferred =\n paramOnlyDeferred !== undefined\n ? paramOnlyDeferred\n : defaultOnlyDeferred;\n const rawServerFilter =\n mcp_server ?? paramMcpServer ?? initParams.mcpServer;\n const serverFilters = normalizeServerFilter(rawServerFilter);\n const hasServerFilter = serverFilters.length > 0;\n\n if (toolRegistry == null) {\n return [\n 'Error: No tool registry provided. Configure toolRegistry at agent level or initialization.',\n {\n tool_references: [],\n metadata: {\n total_searched: 0,\n pattern: query,\n error: 'No tool registry provided',\n },\n },\n ];\n }\n\n const toolsArray: t.LCTool[] = Array.from(toolRegistry.values());\n const deferredTools: t.ToolMetadata[] = toolsArray\n .filter((lcTool) => {\n if (onlyDeferred === true && lcTool.defer_loading !== true) {\n return false;\n }\n if (\n hasServerFilter &&\n !isFromAnyMcpServer(lcTool.name, serverFilters)\n ) {\n return false;\n }\n return true;\n })\n .map((lcTool) => ({\n name: lcTool.name,\n description: lcTool.description ?? '',\n parameters: simplifyParametersForSearch(lcTool.parameters),\n }));\n\n if (deferredTools.length === 0) {\n const serverMsg = hasServerFilter\n ? ` from MCP server(s): ${serverFilters.join(', ')}`\n : '';\n return [\n `No tools available to search${serverMsg}. The tool registry is empty or no matching deferred tools are registered.`,\n {\n tool_references: [],\n metadata: {\n total_searched: 0,\n pattern: query,\n mcp_server: serverFilters,\n },\n },\n ];\n }\n\n const isServerListing = hasServerFilter && query === '';\n\n if (isServerListing) {\n const formattedOutput = formatServerListing(\n deferredTools,\n serverFilters\n );\n\n return [\n formattedOutput,\n {\n tool_references: [],\n metadata: {\n total_available: deferredTools.length,\n mcp_server: serverFilters,\n listing_mode: true,\n },\n },\n ];\n }\n\n if (mode === 'local') {\n const searchResponse = performLocalSearch(\n deferredTools,\n query,\n fields,\n max_results\n );\n const formattedOutput = formatSearchResults(searchResponse);\n\n return [\n formattedOutput,\n {\n tool_references: searchResponse.tool_references,\n metadata: {\n total_searched: searchResponse.total_tools_searched,\n pattern: searchResponse.pattern_used,\n mcp_server: serverFilters.length > 0 ? serverFilters : undefined,\n },\n },\n ];\n }\n\n const { safe: sanitizedPattern, wasEscaped } = sanitizeRegex(query);\n let warningMessage = '';\n if (wasEscaped) {\n warningMessage =\n 'Note: The provided pattern was converted to a literal search for safety.\\n\\n';\n }\n\n const searchScript = generateSearchScript(\n deferredTools,\n fields,\n max_results,\n sanitizedPattern\n );\n\n const postData = {\n lang: 'js',\n code: searchScript,\n timeout: SEARCH_TIMEOUT,\n };\n\n try {\n const fetchOptions: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'LibreChat/1.0',\n 'X-API-Key': apiKey,\n },\n body: JSON.stringify(postData),\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n\n const response = await fetch(EXEC_ENDPOINT, fetchOptions);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const result: t.ExecuteResult = await response.json();\n\n if (result.stderr && result.stderr.trim()) {\n // eslint-disable-next-line no-console\n console.warn('[ToolSearch] stderr:', result.stderr);\n }\n\n if (!result.stdout || !result.stdout.trim()) {\n return [\n `${warningMessage}No tools matched the pattern \"${sanitizedPattern}\".\\nTotal tools searched: ${deferredTools.length}`,\n {\n tool_references: [],\n metadata: {\n total_searched: deferredTools.length,\n pattern: sanitizedPattern,\n },\n },\n ];\n }\n\n const searchResponse = parseSearchResults(result.stdout);\n const formattedOutput = `${warningMessage}${formatSearchResults(searchResponse)}`;\n\n return [\n formattedOutput,\n {\n tool_references: searchResponse.tool_references,\n metadata: {\n total_searched: searchResponse.total_tools_searched,\n pattern: searchResponse.pattern_used,\n },\n },\n ];\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('[ToolSearch] Error:', error);\n\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return [\n `Tool search failed: ${errorMessage}\\n\\nSuggestion: Try a simpler search pattern or search for specific tool names.`,\n {\n tool_references: [],\n metadata: {\n total_searched: 0,\n pattern: sanitizedPattern,\n error: errorMessage,\n },\n },\n ];\n }\n },\n {\n name: Constants.TOOL_SEARCH,\n description,\n schema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n}\n\nexport {\n createToolSearch,\n performLocalSearch,\n extractMcpServerName,\n isFromMcpServer,\n isFromAnyMcpServer,\n normalizeServerFilter,\n getBaseToolName,\n formatServerListing,\n sanitizeRegex,\n escapeRegexSpecialChars,\n isDangerousPattern,\n countNestedGroups,\n hasNestedQuantifiers,\n};\n"],"names":[],"mappings":";;;;;;;;;AAAA;AAWA,MAAM,EAAE;AAER;AACA,MAAM,kBAAkB,GAAG,GAAG;AAE9B;AACA,MAAM,oBAAoB,GAAG,CAAC;AAE9B;AACA,MAAM,cAAc,GAAG,IAAI;AAY3B;;;;AAIG;AACH,SAAS,sBAAsB,CAAC,IAAsB,EAAA;AACpD,IAAA,MAAM,gBAAgB,GACpB,IAAI,KAAK;AACP,UAAE;UACA,0FAA0F;IAEhG,OAAO,CAAC,CAAC,MAAM,CAAC;AACd,QAAA,KAAK,EAAE;AACJ,aAAA,MAAM;aACN,GAAG,CAAC,kBAAkB;AACtB,aAAA,QAAQ;aACR,OAAO,CAAC,EAAE;aACV,QAAQ,CAAC,gBAAgB,CAAC;AAC7B,QAAA,MAAM,EAAE;AACL,aAAA,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,aAAa,EAAE,YAAY,CAAC,CAAC;AACnD,aAAA,QAAQ;AACR,aAAA,OAAO,CAAC,CAAC,MAAM,EAAE,aAAa,CAAC;aAC/B,QAAQ,CAAC,uDAAuD,CAAC;AACpE,QAAA,WAAW,EAAE;AACV,aAAA,MAAM;AACN,aAAA,GAAG;aACH,GAAG,CAAC,CAAC;aACL,GAAG,CAAC,EAAE;AACN,aAAA,QAAQ;aACR,OAAO,CAAC,EAAE;aACV,QAAQ,CAAC,4CAA4C,CAAC;AACzD,QAAA,UAAU,EAAE;AACT,aAAA,KAAK,CAAC,CAAC,CAAC,CAAC,MAA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AO;gBACL,eAAe;AACf,gBAAA;oBACE,eAAe,EAAE,cAAc,CAAC,eAAe;AAC/C,oBAAA,QAAQ,EAAE;wBACR,cAAc,EAAE,cAAc,CAAC,oBAAoB;wBACnD,OAAO,EAAE,cAAc,CAAC,YAAY;AACrC,qBAAA;AACF,iBAAA;aACF;;QACD,OAAO,KAAK,EAAE;;AAEd,YAAA,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC;AAE3C,YAAA,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;YACxD,OAAO;AACL,gBAAA,CAAA,oBAAA,EAAuB,YAAY,CAAiF,+EAAA,CAAA;AACpH,gBAAA;AACE,oBAAA,eAAe,EAAE,EAAE;AACnB,oBAAA,QAAQ,EAAE;AACR,wBAAA,cAAc,EAAE,CAAC;AACjB,wBAAA,OAAO,EAAE,gBAAgB;AACzB,wBAAA,KAAK,EAAE,YAAY;AACpB,qBAAA;AACF,iBAAA;aACF;;AAEL,KAAC,EACD;QACE,IAAI,EAAE,SAAS,CAAC,WAAW;QAC3B,WAAW;QACX,MAAM;QACN,cAAc,EAAE,SAAS,CAAC,oBAAoB;AAC/C,KAAA,CACF;AACH;;;;"}
|
|
@@ -118,7 +118,9 @@ export declare enum Constants {
|
|
|
118
118
|
PROGRAMMATIC_TOOL_CALLING = "run_tools_with_code",
|
|
119
119
|
WEB_SEARCH = "web_search",
|
|
120
120
|
CONTENT_AND_ARTIFACT = "content_and_artifact",
|
|
121
|
-
LC_TRANSFER_TO_ = "lc_transfer_to_"
|
|
121
|
+
LC_TRANSFER_TO_ = "lc_transfer_to_",
|
|
122
|
+
/** Delimiter for MCP tools: toolName_mcp_serverName */
|
|
123
|
+
MCP_DELIMITER = "_mcp_"
|
|
122
124
|
}
|
|
123
125
|
export declare enum TitleMethod {
|
|
124
126
|
STRUCTURED = "structured",
|
|
@@ -3,9 +3,10 @@ import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
|
3
3
|
import type * as t from '@/types';
|
|
4
4
|
/** Zod schema type for tool search parameters */
|
|
5
5
|
type ToolSearchSchema = z.ZodObject<{
|
|
6
|
-
query: z.ZodString
|
|
6
|
+
query: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
7
7
|
fields: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodEnum<['name', 'description', 'parameters']>>>>;
|
|
8
8
|
max_results: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
9
|
+
mcp_server: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString>]>>;
|
|
9
10
|
}>;
|
|
10
11
|
/**
|
|
11
12
|
* Creates the Zod schema with dynamic query description based on mode.
|
|
@@ -13,6 +14,33 @@ type ToolSearchSchema = z.ZodObject<{
|
|
|
13
14
|
* @returns Zod schema for tool search parameters
|
|
14
15
|
*/
|
|
15
16
|
declare function createToolSearchSchema(mode: t.ToolSearchMode): ToolSearchSchema;
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the MCP server name from a tool name.
|
|
19
|
+
* MCP tools follow the pattern: toolName_mcp_serverName
|
|
20
|
+
* @param toolName - The full tool name
|
|
21
|
+
* @returns The server name if it's an MCP tool, undefined otherwise
|
|
22
|
+
*/
|
|
23
|
+
declare function extractMcpServerName(toolName: string): string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Checks if a tool belongs to a specific MCP server.
|
|
26
|
+
* @param toolName - The full tool name
|
|
27
|
+
* @param serverName - The server name to match
|
|
28
|
+
* @returns True if the tool belongs to the specified server
|
|
29
|
+
*/
|
|
30
|
+
declare function isFromMcpServer(toolName: string, serverName: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Checks if a tool belongs to any of the specified MCP servers.
|
|
33
|
+
* @param toolName - The full tool name
|
|
34
|
+
* @param serverNames - Array of server names to match
|
|
35
|
+
* @returns True if the tool belongs to any of the specified servers
|
|
36
|
+
*/
|
|
37
|
+
declare function isFromAnyMcpServer(toolName: string, serverNames: string[]): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Normalizes server filter input to always be an array.
|
|
40
|
+
* @param serverFilter - String, array of strings, or undefined
|
|
41
|
+
* @returns Array of server names (empty if none specified)
|
|
42
|
+
*/
|
|
43
|
+
declare function normalizeServerFilter(serverFilter: string | string[] | undefined): string[];
|
|
16
44
|
/**
|
|
17
45
|
* Escapes special regex characters in a string to use as a literal pattern.
|
|
18
46
|
* @param pattern - The string to escape
|
|
@@ -58,6 +86,21 @@ declare function sanitizeRegex(pattern: string): {
|
|
|
58
86
|
* @returns Search response with matching tools
|
|
59
87
|
*/
|
|
60
88
|
declare function performLocalSearch(tools: t.ToolMetadata[], query: string, fields: string[], maxResults: number): t.ToolSearchResponse;
|
|
89
|
+
/**
|
|
90
|
+
* Extracts the base tool name (without MCP server suffix) from a full tool name.
|
|
91
|
+
* @param toolName - The full tool name
|
|
92
|
+
* @returns The base tool name without server suffix
|
|
93
|
+
*/
|
|
94
|
+
declare function getBaseToolName(toolName: string): string;
|
|
95
|
+
/**
|
|
96
|
+
* Formats a server listing response when listing all tools from MCP server(s).
|
|
97
|
+
* Provides a cohesive view of all tools grouped by server.
|
|
98
|
+
* NOTE: This is a PREVIEW only - tools are NOT discovered/loaded.
|
|
99
|
+
* @param tools - Array of tool metadata from the server(s)
|
|
100
|
+
* @param serverNames - The MCP server name(s)
|
|
101
|
+
* @returns Formatted string showing all tools from the server(s)
|
|
102
|
+
*/
|
|
103
|
+
declare function formatServerListing(tools: t.ToolMetadata[], serverNames: string | string[]): string;
|
|
61
104
|
/**
|
|
62
105
|
* Creates a Tool Search tool for discovering tools from a large registry.
|
|
63
106
|
*
|
|
@@ -87,4 +130,4 @@ declare function performLocalSearch(tools: t.ToolMetadata[], query: string, fiel
|
|
|
87
130
|
* await tool.invoke({ query: 'expense' });
|
|
88
131
|
*/
|
|
89
132
|
declare function createToolSearch(initParams?: t.ToolSearchParams): DynamicStructuredTool<ReturnType<typeof createToolSearchSchema>>;
|
|
90
|
-
export { createToolSearch, performLocalSearch, sanitizeRegex, escapeRegexSpecialChars, isDangerousPattern, countNestedGroups, hasNestedQuantifiers, };
|
|
133
|
+
export { createToolSearch, performLocalSearch, extractMcpServerName, isFromMcpServer, isFromAnyMcpServer, normalizeServerFilter, getBaseToolName, formatServerListing, sanitizeRegex, escapeRegexSpecialChars, isDangerousPattern, countNestedGroups, hasNestedQuantifiers, };
|
|
@@ -101,7 +101,7 @@ export type ProgrammaticCache = {
|
|
|
101
101
|
};
|
|
102
102
|
/** Search mode: code_interpreter uses external sandbox, local uses safe substring matching */
|
|
103
103
|
export type ToolSearchMode = 'code_interpreter' | 'local';
|
|
104
|
-
/** Parameters for creating a Tool Search
|
|
104
|
+
/** Parameters for creating a Tool Search tool */
|
|
105
105
|
export type ToolSearchParams = {
|
|
106
106
|
apiKey?: string;
|
|
107
107
|
toolRegistry?: LCToolRegistry;
|
|
@@ -109,6 +109,8 @@ export type ToolSearchParams = {
|
|
|
109
109
|
baseUrl?: string;
|
|
110
110
|
/** Search mode: 'code_interpreter' (default) uses sandbox for regex, 'local' uses safe substring matching */
|
|
111
111
|
mode?: ToolSearchMode;
|
|
112
|
+
/** Filter tools to only those from specific MCP server(s). Can be a single name or array of names. */
|
|
113
|
+
mcpServer?: string | string[];
|
|
112
114
|
[key: string]: unknown;
|
|
113
115
|
};
|
|
114
116
|
/** Simplified tool metadata for search purposes */
|
package/package.json
CHANGED
package/src/common/enum.ts
CHANGED
|
@@ -164,6 +164,8 @@ export enum Constants {
|
|
|
164
164
|
WEB_SEARCH = 'web_search',
|
|
165
165
|
CONTENT_AND_ARTIFACT = 'content_and_artifact',
|
|
166
166
|
LC_TRANSFER_TO_ = 'lc_transfer_to_',
|
|
167
|
+
/** Delimiter for MCP tools: toolName_mcp_serverName */
|
|
168
|
+
MCP_DELIMITER = '_mcp_',
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
export enum TitleMethod {
|