mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-06 15:33:56 +00:00
* feat(web-search): route outbound search and scrape requests through the SSRF-safe agent Build the SSRF-safe agents at the web-search tool-assembly site and pass them into the search tool config so outbound search and scrape connections are validated at connect time against their resolved IP, on every hop including redirects, consistent with the other outbound clients. Add allowedAddresses to webSearchSchema, reusing allowedAddressesSchema, so self-hosters can permit a deliberately-private search or scrape endpoint (for example a private SearXNG instance). The field is resolved directly from the webSearch config at the createSearchTool call site, not through loadWebSearchAuth, because it is config and not an auth credential. webSearchSchema is flat (providers are chosen by enums, not by counting keys), so the field is inert with respect to provider selection. Document the field and its operator warning in librechat.example.yaml, and assert the wiring in handleTools.test.js: the SSRF-safe agents are threaded into the search tool config, allowedAddresses is passed through when set, and omitting it still threads the agents with no exemptions. TODO awaits @librechat/agents release with the httpAgent hook: this consumes optional httpAgent/httpsAgent fields on the search-tool config that are not yet in a published @librechat/agents. package.json is intentionally left at the current version; bump it to the release that ships the hook before this lands. Validated locally against a revendored @librechat/agents build, not a published release. * 🛡️ fix: Apply allowedAddresses to the Web Search SSRF Preflight The connect-time SSRF agent already honors webSearch.allowedAddresses, but loadWebSearchAuth ran the isSSRFUrl preflight without it, so an admin-permitted private search or scrape URL was stripped before the agent could ever use it. Thread allowedAddresses and the URL's effective port through isSSRFTarget and resolveHostnameSSRF so the exemption is consistent across both SSRF layers. * 🛡️ fix: Validate Web Search Destinations and Defer to Configured Proxies Handing agents to createSearchTool covered only the connect-time DNS lookup, which Node skips for IP-literal hosts, and a configured proxy connects on our behalf without running that check. A literal private target such as http://169.254.169.254 could therefore reach the network. Route every resolved web-search destination through the existing applySSRFSafeAgentIfDirect contract so a blocked literal target throws before any request is made, and withhold the agents when a proxy owns egress, since one agent pair is shared by every provider and a direct-connect agent on a proxied connection would break the request while asserting protection the proxy's network context cannot provide. * 🛡️ fix: Keep Web Search SSRF Agents Under a Proxy and Restore Pooling Withholding the agents whenever a proxy was configured removed protection from every direct and NO_PROXY destination in exchange for preventing a failure that cannot occur: for an https target Axios substitutes its own CONNECT tunnel, so the injected agent is never used for the proxy connection. Only a plaintext http target keeps our agent and repoints it at the proxy, and only a proxy whose hostname resolves private then trips the connect-time check. Always pass the agents and exempt the proxy endpoint instead, deriving host:port from the same PROXY, HTTP_PROXY, and HTTPS_PROXY resolution the rest of LibreChat uses so the proxy hop stays reachable while destinations remain guarded. Axios already applies NO_PROXY per request, so bypassed routes keep enforcement with no extra logic. Drop the load-time destination validation. It duplicated the isSSRFTarget preflight for user-provided URLs, rejected admin values that were previously legal, and threw from inside loadTools, where both loader wrappers swallow the error and drop every tool for the turn rather than degrading web search alone. Build the agents with keepAlive and cache them per exemption list. A bare http.Agent does not pool, so the previous code replaced the pooled global agents for every search, scrape, and rerank call and allocated a fresh pair per turn. * 🛡️ fix: Reject IP-Literal Private Targets on Web Search Connections Node resolves nothing for a literal host, so the connect-time lookup never saw one: a destination or a redirect target given as http://169.254.169.254 reached the network. Redirect hops pass through the same createConnection, so checking the literal there covers both cases and removes the need for a maxRedirects control that createSearchTool cannot accept. Gate it behind blockLiteralHosts so only web search opts in. A caller that reaches a proxy or a deliberate private service by literal address must exempt it first, and the merged consumers of createSSRFSafeAgents have no such exemption, so enabling this everywhere would break configurations that work today. * 🛡️ fix: Keep IPv6 Brackets on Derived Proxy Exemptions The exemption parser accepts an IPv6 entry only as [ipv6]:port, so stripping the brackets produced fd00::1:3128, which carries three colons and is dropped as malformed. An IPv6 proxy therefore stayed unexempted and the connect-time check rejected it, failing every web-search request routed through it. Use the URL hostname as parsed, which already carries the brackets. * 🛡️ fix: Exempt Proxies Configured Through ALL_PROXY Axios resolves a proxy through proxy-from-env, which falls back to all_proxy in either case after <protocol>_proxy, so ALL_PROXY on its own is enough to route a request through a proxy. Exemptions were derived from PROXY, HTTP_PROXY, and HTTPS_PROXY only, leaving such a proxy unexempted and rejected with ESSRF. Derive the exemptions from the full set of variables that can put a proxy in front of these requests instead. The installed proxy-from-env 2.1.0 reads no npm_config variables, so those are deliberately not included. * 🛡️ fix: Drop the Unearned PROXY Exemption and Harden the Web Search Guard Nothing on this path consumes PROXY: Axios resolves proxies through proxy-from-env, which reads only <protocol>_proxy and all_proxy, and web search never calls applyAxiosProxyConfig. Exempting it therefore granted a bypass rather than preserving a working route, and a user-settable search URL that redirects to that address reached it and returned the body. Remove PROXY and proxy, and skip a socks endpoint for the same reason, since Axios cannot proxy through one. Tolerate a non-array allowedAddresses instead of spreading it, which threw out of loadTools and dropped every tool for the turn. The YAML path is schema-validated but the admin override path merges without parsing, so the value is reachable. Separate cache keys with NUL rather than a newline, so an entry containing a newline cannot collide with two separate entries, and bound the cache. Give the agents the idle timeout the global agents carry, which keepAlive alone did not restore. Reject a unix socket, which carries no host to validate. Also treat fec0::/10 site-local as private, matching the fe80::/10 handling beside it. Exercise the real resolver in handleTools.test.js rather than mocking it, so the wiring test now fails if the agents it threads do not actually block a private target. * 🛡️ fix: Derive Proxy Exemptions Through Axios's Own Resolver Unioning every populated proxy variable exempted addresses that never carry a request. proxy-from-env picks a protocol-specific variable before all_proxy and lowercase before uppercase, so an ignored value became a trusted host:port that a redirect onto a direct route could reach. It also normalizes a scheme-less value such as proxy.internal:3128 to an http URL, where parsing the raw string yielded an empty hostname and no exemption at all, breaking the proxy hop. Resolve through getProxyForUrl, the entry point Axios itself calls, so precedence, scheme normalization, and NO_PROXY match exactly and cannot drift. NO_PROXY covering everything now yields no exemption, since nothing is proxied. Declared locally rather than adding a types package, alongside the existing declaration in the same directory. Also revert the fec0::/10 site-local change. domain.spec asserts that boundary deliberately to prove the fe80::/10 mask does not over-reach, and the shared address schema still classifies fec0 as public, so a runtime block there would leave operators unable to configure the exemption. It belongs with those two together, not in this PR. * 🛡️ fix: Resolve Proxy Exemptions Against the Real Destinations Resolving against placeholder probe hosts applied destination-specific NO_PROXY rules to a host nobody dials. With NO_PROXY matching the probe domain but not a real provider, no exemption was derived even though Axios still proxied the actual request, so the agent rejected the private proxy hop with ESSRF. Resolve per configured destination instead, passing the values loadWebSearchAuth already resolved. Only plaintext http destinations are considered, since for an https destination Axios substitutes its own CONNECT tunnel and never uses the injected agent for the proxy connection, which is also why provider defaults need no exemption: every one of them is https. * 🛡️ fix: Accept Embedded-IPv4 IPv6 Forms in the Address Exemption Schema The runtime guard blocks 6to4, NAT64, and Teredo addresses whose embedded IPv4 is private, but the schema's local copy recognized only ULA, link-local, and the dotted IPv4-mapped form, so an entry such as [64:ff9b::a00:1]:8080 was dropped as a public literal. An operator reaching a private endpoint that way could not configure the exemption at all. Mirror hasPrivateEmbeddedIPv4 in the schema helper, which the surrounding comment already asks to keep in sync. Public embedded addresses stay rejected, since an exemption there has no defensive purpose.
652 lines
23 KiB
JavaScript
652 lines
23 KiB
JavaScript
const { logger } = require('@librechat/data-schemas');
|
|
const { Calculator, createSearchTool, createCodeExecutionTool } = require('@librechat/agents');
|
|
const {
|
|
checkAccess,
|
|
toolkitParent,
|
|
createSafeUser,
|
|
mcpToolPattern,
|
|
loadWebSearchAuth,
|
|
splitMCPToolKey,
|
|
buildServerNameAliases,
|
|
findShadowedServerNames,
|
|
isNormalizationSensitiveName,
|
|
buildInlineMemoryTool,
|
|
getCodeApiAuthHeaders,
|
|
buildImageToolContext,
|
|
SET_MEMORY_TOOL_NAME,
|
|
buildWebSearchContext,
|
|
DELETE_MEMORY_TOOL_NAME,
|
|
createAskUserQuestionTool,
|
|
ASK_USER_QUESTION_TOOL_NAME,
|
|
resolveWebSearchSSRFAgents,
|
|
buildWebSearchDynamicContext,
|
|
} = require('@librechat/api');
|
|
const {
|
|
Tools,
|
|
Constants,
|
|
Permissions,
|
|
EToolResources,
|
|
PermissionTypes,
|
|
AgentCapabilities,
|
|
} = require('librechat-data-provider');
|
|
const {
|
|
availableTools,
|
|
manifestToolMap,
|
|
// Basic Tools
|
|
GoogleSearchAPI,
|
|
// Structured Tools
|
|
DALLE3,
|
|
FluxAPI,
|
|
OpenWeather,
|
|
StructuredSD,
|
|
StructuredACS,
|
|
TraversaalSearch,
|
|
StructuredWolfram,
|
|
TavilySearchResults,
|
|
createGeminiImageTool,
|
|
createOpenAIImageTools,
|
|
} = require('../');
|
|
const {
|
|
createMCPTool,
|
|
createMCPTools,
|
|
createMCPPermissionContext,
|
|
resolveMcpServerContext,
|
|
resolveCollisionAuditNames,
|
|
} = require('~/server/services/MCP');
|
|
const { getMCPRequestContext } = require('~/server/services/MCPRequestContext');
|
|
const { createFileSearchTool, primeFiles: primeSearchFiles } = require('./fileSearch');
|
|
const { primeFiles: primeCodeFiles } = require('~/server/services/Files/Code/process');
|
|
const { getUserPluginAuthValue } = require('~/server/services/PluginService');
|
|
const { loadAuthValues } = require('~/server/services/Tools/credentials');
|
|
const { getMCPServerTools, checkCapability } = require('~/server/services/Config');
|
|
const { getMCPServersRegistry } = require('~/config');
|
|
const { getRoleByName, setMemory, deleteMemory, getFormattedMemories } = require('~/models');
|
|
|
|
/**
|
|
* Validates the availability and authentication of tools for a user based on environment variables or user-specific plugin authentication values.
|
|
* Tools without required authentication or with valid authentication are considered valid.
|
|
*
|
|
* @param {Object} user The user object for whom to validate tool access.
|
|
* @param {Array<string>} tools An array of tool identifiers to validate. Defaults to an empty array.
|
|
* @returns {Promise<Array<string>>} A promise that resolves to an array of valid tool identifiers.
|
|
*/
|
|
const validateTools = async (user, tools = []) => {
|
|
try {
|
|
const validToolsSet = new Set(tools);
|
|
const availableToolsToValidate = availableTools.filter((tool) =>
|
|
validToolsSet.has(tool.pluginKey),
|
|
);
|
|
|
|
/**
|
|
* Validates the credentials for a given auth field or set of alternate auth fields for a tool.
|
|
* If valid admin or user authentication is found, the function returns early. Otherwise, it removes the tool from the set of valid tools.
|
|
*
|
|
* @param {string} authField The authentication field or fields (separated by "||" for alternates) to validate.
|
|
* @param {string} toolName The identifier of the tool being validated.
|
|
*/
|
|
const validateCredentials = async (authField, toolName) => {
|
|
const fields = authField.split('||');
|
|
for (const field of fields) {
|
|
const adminAuth = process.env[field];
|
|
if (adminAuth && adminAuth.length > 0) {
|
|
return;
|
|
}
|
|
|
|
let userAuth = null;
|
|
try {
|
|
userAuth = await getUserPluginAuthValue(user, field);
|
|
} catch (err) {
|
|
if (field === fields[fields.length - 1] && !userAuth) {
|
|
throw err;
|
|
}
|
|
}
|
|
if (userAuth && userAuth.length > 0) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
validToolsSet.delete(toolName);
|
|
};
|
|
|
|
for (const tool of availableToolsToValidate) {
|
|
if (!tool.authConfig || tool.authConfig.length === 0) {
|
|
continue;
|
|
}
|
|
|
|
for (const auth of tool.authConfig) {
|
|
await validateCredentials(auth.authField, tool.pluginKey);
|
|
}
|
|
}
|
|
|
|
return Array.from(validToolsSet.values());
|
|
} catch (err) {
|
|
logger.error('[validateTools] There was a problem validating tools', err);
|
|
throw new Error(err);
|
|
}
|
|
};
|
|
|
|
/** @typedef {typeof import('@librechat/agents/langchain/tools').Tool} ToolConstructor */
|
|
/** @typedef {import('@librechat/agents/langchain/tools').Tool} Tool */
|
|
|
|
/**
|
|
* Initializes a tool with authentication values for the given user, supporting alternate authentication fields.
|
|
* Authentication fields can have alternates separated by "||", and the first defined variable will be used.
|
|
*
|
|
* @param {string} userId The user ID for which the tool is being loaded.
|
|
* @param {Array<string>} authFields Array of strings representing the authentication fields. Supports alternate fields delimited by "||".
|
|
* @param {ToolConstructor} ToolConstructor The constructor function for the tool to be initialized.
|
|
* @param {Object} options Optional parameters to be passed to the tool constructor alongside authentication values.
|
|
* @returns {() => Promise<Tool>} An Async function that, when called, asynchronously initializes and returns an instance of the tool with authentication.
|
|
*/
|
|
const loadToolWithAuth = (userId, authFields, ToolConstructor, options = {}) => {
|
|
return async function () {
|
|
const authValues = await loadAuthValues({ userId, authFields });
|
|
return new ToolConstructor({ ...options, ...authValues, userId });
|
|
};
|
|
};
|
|
|
|
/**
|
|
* @param {string} toolKey
|
|
* @returns {Array<string>}
|
|
*/
|
|
const getAuthFields = (toolKey) => {
|
|
return manifestToolMap[toolKey]?.authConfig.map((auth) => auth.authField) ?? [];
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param {object} params
|
|
* @param {string} params.user
|
|
* @param {Record<string, Record<string, string>>} [object.userMCPAuthMap]
|
|
* @param {AbortSignal} [object.signal]
|
|
* @param {Pick<Agent, 'id' | 'provider' | 'model'>} [params.agent]
|
|
* @param {string} [params.model]
|
|
* @param {EModelEndpoint} [params.endpoint]
|
|
* @param {LoadToolOptions} [params.options]
|
|
* @param {boolean} [params.useSpecs]
|
|
* @param {Array<string>} params.tools
|
|
* @param {boolean} [params.functions]
|
|
* @param {boolean} [params.returnMap]
|
|
* @param {AppConfig['webSearch']} [params.webSearch]
|
|
* @param {AppConfig['fileStrategy']} [params.fileStrategy]
|
|
* @param {AppConfig['imageOutputType']} [params.imageOutputType]
|
|
* @returns {Promise<{ loadedTools: Tool[], toolContextMap: Object<string, any>, dynamicToolContextMap?: Object<string, any> } | Record<string,Tool>>}
|
|
*/
|
|
const loadTools = async ({
|
|
user,
|
|
agent,
|
|
model,
|
|
signal,
|
|
endpoint,
|
|
userMCPAuthMap,
|
|
tools = [],
|
|
options = {},
|
|
functions = true,
|
|
returnMap = false,
|
|
webSearch,
|
|
fileStrategy,
|
|
imageOutputType,
|
|
}) => {
|
|
const toolConstructors = {
|
|
flux: FluxAPI,
|
|
calculator: Calculator,
|
|
google: GoogleSearchAPI,
|
|
open_weather: OpenWeather,
|
|
wolfram: StructuredWolfram,
|
|
'stable-diffusion': StructuredSD,
|
|
'azure-ai-search': StructuredACS,
|
|
traversaal_search: TraversaalSearch,
|
|
tavily_search_results_json: TavilySearchResults,
|
|
};
|
|
|
|
const customConstructors = {
|
|
image_gen_oai: async (_toolContextMap, dynamicToolContextMap) => {
|
|
const authFields = getAuthFields('image_gen_oai');
|
|
const authValues = await loadAuthValues({ userId: user, authFields });
|
|
const imageFiles = options.tool_resources?.[EToolResources.image_edit]?.files ?? [];
|
|
const toolContext = buildImageToolContext({
|
|
imageFiles,
|
|
toolName: `${EToolResources.image_edit}_oai`,
|
|
contextDescription: 'image editing',
|
|
});
|
|
if (toolContext) {
|
|
dynamicToolContextMap.image_edit_oai = toolContext;
|
|
}
|
|
return createOpenAIImageTools({
|
|
...authValues,
|
|
isAgent: !!agent,
|
|
req: options.req,
|
|
imageOutputType,
|
|
fileStrategy,
|
|
imageFiles,
|
|
});
|
|
},
|
|
gemini_image_gen: async (_toolContextMap, dynamicToolContextMap) => {
|
|
const authFields = getAuthFields('gemini_image_gen');
|
|
const authValues = await loadAuthValues({ userId: user, authFields, throwError: false });
|
|
const imageFiles = options.tool_resources?.[EToolResources.image_edit]?.files ?? [];
|
|
const toolContext = buildImageToolContext({
|
|
imageFiles,
|
|
toolName: 'gemini_image_gen',
|
|
contextDescription: 'image context',
|
|
});
|
|
if (toolContext) {
|
|
dynamicToolContextMap.gemini_image_gen = toolContext;
|
|
}
|
|
return createGeminiImageTool({
|
|
...authValues,
|
|
isAgent: !!agent,
|
|
req: options.req,
|
|
imageFiles,
|
|
userId: user,
|
|
fileStrategy,
|
|
});
|
|
},
|
|
};
|
|
|
|
const requestedTools = {};
|
|
const hasMCPTools = tools.some((toolName) => toolName && mcpToolPattern.test(toolName));
|
|
const mcpPermissionContext =
|
|
options.mcpPermissionContext ?? createMCPPermissionContext(options.req);
|
|
const canUseMCP = hasMCPTools
|
|
? await mcpPermissionContext.canUseServers(options.req?.user)
|
|
: true;
|
|
let loggedMCPDenied = false;
|
|
|
|
if (functions === true) {
|
|
toolConstructors.dalle = DALLE3;
|
|
}
|
|
|
|
/** @type {ImageGenOptions} */
|
|
const imageGenOptions = {
|
|
isAgent: !!agent,
|
|
req: options.req,
|
|
fileStrategy,
|
|
processFileURL: options.processFileURL,
|
|
returnMetadata: options.returnMetadata,
|
|
uploadImageBuffer: options.uploadImageBuffer,
|
|
};
|
|
|
|
const toolOptions = {
|
|
flux: imageGenOptions,
|
|
dalle: imageGenOptions,
|
|
'stable-diffusion': imageGenOptions,
|
|
gemini_image_gen: imageGenOptions,
|
|
};
|
|
|
|
/** @type {Record<string, string>} */
|
|
const toolContextMap = {};
|
|
/** @type {Record<string, string>} */
|
|
const dynamicToolContextMap = {};
|
|
/**
|
|
* @type {import('@librechat/agents').CodeEnvFile[] | undefined}
|
|
* Captured by the `execute_code` factory when files are primed. Surfaced
|
|
* out of `loadTools` so client.js can seed `Graph.sessions[EXECUTE_CODE]`
|
|
* before run start — without that seed, the first `execute_code` /
|
|
* `bash_tool` call lands with empty `_injected_files` and the sandbox
|
|
* can't see the prior turn's generated artifacts.
|
|
*/
|
|
let primedCodeFiles;
|
|
const requestedMCPTools = {};
|
|
|
|
/** Resolve config-source servers for the current user/tenant context */
|
|
let configServers;
|
|
/** All configured names, in the normalized form tool keys carry */
|
|
let mcpServerNames = [];
|
|
/** All configured names in raw config form, for normalized→raw resolution */
|
|
let mcpRawServerNames = [];
|
|
if (hasMCPTools && canUseMCP) {
|
|
/** Reuse the caller's context when it already resolved one, so the chat
|
|
* startup path reads the request app config once. */
|
|
({
|
|
configServers,
|
|
serverNames: mcpServerNames,
|
|
rawServerNames: mcpRawServerNames = [],
|
|
} = options.mcpServerContext ?? (await resolveMcpServerContext(options.req)));
|
|
}
|
|
/**
|
|
* Collision guards need the FULL accessible set (operator + user DB): a
|
|
* cross-tier collision (DB `foo` vs operator `foo!`) is invisible to the
|
|
* operator-config names alone. The caller's heal may have already fetched
|
|
* it (threaded via `mcpServerContext.accessibleServerNames`); otherwise it
|
|
* is fetched ONLY when a configured name actually needs normalizing. When
|
|
* the full set was needed but unavailable, normalization-sensitive
|
|
* references FAIL CLOSED below rather than auditing operator names alone.
|
|
*/
|
|
const collisionAudit = hasMCPTools
|
|
? await resolveCollisionAuditNames({
|
|
rawServerNames: mcpRawServerNames,
|
|
/** Load-time callers thread the audit inside `mcpServerContext`;
|
|
* deferred execution threads initialization's snapshot as a bare
|
|
* `accessibleMcpServerNames` (it resolves no server context). */
|
|
accessibleServerNames:
|
|
options.mcpServerContext?.accessibleServerNames ?? options.accessibleMcpServerNames,
|
|
userId: user,
|
|
role: options.req?.user?.role,
|
|
})
|
|
: { names: [], complete: true };
|
|
const serverNameAliases = buildServerNameAliases(collisionAudit.names);
|
|
const shadowedServers = findShadowedServerNames(collisionAudit.names);
|
|
|
|
for (const tool of tools) {
|
|
if (tool === Tools.execute_code) {
|
|
requestedTools[tool] = async () => {
|
|
const { files, toolContext } = await primeCodeFiles({
|
|
...options,
|
|
agentId: agent?.id,
|
|
});
|
|
if (toolContext) {
|
|
dynamicToolContextMap[tool] = toolContext;
|
|
}
|
|
if (files?.length) {
|
|
primedCodeFiles = files;
|
|
}
|
|
/* Hedge the execute_code description toward persistence only when the
|
|
* admin `stateful_code_sessions` capability is on AND the agent opted
|
|
* in via the builder (off by default); the matching wire hint is set
|
|
* in the run config. Older @librechat/agents ignore the param. */
|
|
const statefulSessions =
|
|
agent?.stateful_code_sessions === true &&
|
|
(await checkCapability(options.req, AgentCapabilities.stateful_code_sessions));
|
|
return createCodeExecutionTool({
|
|
user_id: user,
|
|
files,
|
|
authHeaders: () => getCodeApiAuthHeaders(options.req),
|
|
statefulSessions,
|
|
});
|
|
};
|
|
continue;
|
|
} else if (tool === Tools.file_search) {
|
|
requestedTools[tool] = async () => {
|
|
const { files, toolContext } = await primeSearchFiles({
|
|
...options,
|
|
agentId: agent?.id,
|
|
});
|
|
if (toolContext) {
|
|
dynamicToolContextMap[tool] = toolContext;
|
|
}
|
|
|
|
/** @type {boolean | undefined} Check if user has FILE_CITATIONS permission */
|
|
let fileCitations;
|
|
if (fileCitations == null && options.req?.user != null) {
|
|
try {
|
|
fileCitations = await checkAccess({
|
|
user: options.req.user,
|
|
permissionType: PermissionTypes.FILE_CITATIONS,
|
|
permissions: [Permissions.USE],
|
|
getRoleByName,
|
|
});
|
|
} catch (error) {
|
|
logger.error('[handleTools] FILE_CITATIONS permission check failed:', error);
|
|
fileCitations = false;
|
|
}
|
|
}
|
|
|
|
return createFileSearchTool({
|
|
userId: user,
|
|
files,
|
|
entity_id: agent?.id,
|
|
fileCitations,
|
|
});
|
|
};
|
|
continue;
|
|
} else if (tool === Tools.web_search) {
|
|
const result = await loadWebSearchAuth({
|
|
userId: user,
|
|
loadAuthValues,
|
|
webSearchConfig: webSearch,
|
|
});
|
|
const { onSearchResults, onGetHighlights } = options?.[Tools.web_search] ?? {};
|
|
const { httpAgent, httpsAgent } = resolveWebSearchSSRFAgents(
|
|
result.authResult,
|
|
webSearch?.allowedAddresses,
|
|
);
|
|
requestedTools[tool] = async () => {
|
|
toolContextMap[tool] = buildWebSearchContext();
|
|
dynamicToolContextMap[tool] = buildWebSearchDynamicContext(
|
|
options.req?.conversationCreatedAt,
|
|
);
|
|
return createSearchTool({
|
|
...result.authResult,
|
|
httpAgent,
|
|
httpsAgent,
|
|
onSearchResults,
|
|
onGetHighlights,
|
|
logger,
|
|
});
|
|
};
|
|
continue;
|
|
} else if (tool === ASK_USER_QUESTION_TOOL_NAME) {
|
|
requestedTools[tool] = async () => createAskUserQuestionTool();
|
|
continue;
|
|
} else if (tool === SET_MEMORY_TOOL_NAME || tool === DELETE_MEMORY_TOOL_NAME) {
|
|
requestedTools[tool] = () =>
|
|
buildInlineMemoryTool({
|
|
toolName: tool,
|
|
req: options.req,
|
|
agent,
|
|
userId: user,
|
|
memoryMethods: { setMemory, deleteMemory, getFormattedMemories },
|
|
getRoleByName,
|
|
});
|
|
continue;
|
|
} else if (tool && mcpToolPattern.test(tool)) {
|
|
if (!canUseMCP) {
|
|
if (!loggedMCPDenied) {
|
|
logger.warn(
|
|
`[handleTools] User ${options.req?.user?.id} lacks MCP server use permission`,
|
|
);
|
|
loggedMCPDenied = true;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
/** Keys carry the normalized server name (raw in pre-normalization data),
|
|
* so both spellings resolve the boundary; everything downstream — the
|
|
* registry, config maps, cache, and auth rows — is keyed by the RAW name. */
|
|
const [toolName, parsedServerName] = splitMCPToolKey(tool, [
|
|
...mcpServerNames,
|
|
...serverNameAliases.values(),
|
|
]);
|
|
if (toolName === Constants.mcp_server) {
|
|
/** Placeholder used for UI purposes */
|
|
continue;
|
|
}
|
|
/** DIRECT-FIRST: a server resolving under the parsed name as-is wins
|
|
* (a user-DB server may be named exactly like an operator server's
|
|
* normalized form); only when nothing resolves is the parsed name
|
|
* treated as a normalized spelling of a raw config name. */
|
|
let serverName = parsedServerName;
|
|
let serverConfig = serverName
|
|
? await getMCPServersRegistry().getServerConfig(serverName, user, configServers)
|
|
: null;
|
|
if (!serverConfig && serverName != null) {
|
|
const aliasedName = serverNameAliases.get(serverName);
|
|
if (aliasedName != null && aliasedName !== serverName) {
|
|
serverConfig = await getMCPServersRegistry().getServerConfig(
|
|
aliasedName,
|
|
user,
|
|
configServers,
|
|
);
|
|
if (serverConfig) {
|
|
serverName = aliasedName;
|
|
}
|
|
}
|
|
}
|
|
/** A shadowed server's instances (wildcard-expanded or single) get the
|
|
* SAME normalized names as the winning server's — in-run dispatch
|
|
* could execute either. Fail closed at execution too, since legacy
|
|
* raw keys and `mcp_all` tokens bypass catalog filtering. Under an
|
|
* incomplete audit, any normalization-sensitive reference is
|
|
* potentially shadowed and fails closed the same way. */
|
|
if (
|
|
serverName != null &&
|
|
(shadowedServers.has(serverName) ||
|
|
(!collisionAudit.complete && isNormalizationSensitiveName(serverName, mcpRawServerNames)))
|
|
) {
|
|
logger.warn(
|
|
`[handleTools] Skipping MCP tool "${tool}": server "${serverName}" is shadowed by a name collision (or the collision audit is unavailable); rename one server or retry.`,
|
|
);
|
|
continue;
|
|
}
|
|
if (!serverConfig) {
|
|
logger.warn(
|
|
`MCP server "${serverName}" for "${toolName}" tool is not configured${agent?.id != null && agent.id ? ` but attached to "${agent.id}"` : ''}`,
|
|
);
|
|
continue;
|
|
}
|
|
if (toolName === Constants.mcp_all) {
|
|
requestedMCPTools[serverName] = [
|
|
{
|
|
type: 'all',
|
|
serverName,
|
|
config: serverConfig,
|
|
},
|
|
];
|
|
continue;
|
|
}
|
|
|
|
requestedMCPTools[serverName] = requestedMCPTools[serverName] || [];
|
|
requestedMCPTools[serverName].push({
|
|
type: 'single',
|
|
toolKey: tool,
|
|
serverName,
|
|
config: serverConfig,
|
|
});
|
|
continue;
|
|
}
|
|
|
|
const toolKey = customConstructors[tool] ? tool : toolkitParent[tool];
|
|
if (toolKey && customConstructors[toolKey]) {
|
|
if (!requestedTools[toolKey]) {
|
|
let cached;
|
|
requestedTools[toolKey] = async () => {
|
|
cached ??= customConstructors[toolKey](toolContextMap, dynamicToolContextMap);
|
|
return cached;
|
|
};
|
|
}
|
|
requestedTools[tool] = requestedTools[toolKey];
|
|
continue;
|
|
}
|
|
|
|
if (toolConstructors[tool]) {
|
|
const options = toolOptions[tool] || {};
|
|
const toolInstance = loadToolWithAuth(
|
|
user,
|
|
getAuthFields(tool),
|
|
toolConstructors[tool],
|
|
options,
|
|
);
|
|
requestedTools[tool] = toolInstance;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (returnMap) {
|
|
return requestedTools;
|
|
}
|
|
|
|
const toolPromises = [];
|
|
for (const tool of tools) {
|
|
const validTool = requestedTools[tool];
|
|
if (validTool) {
|
|
toolPromises.push(
|
|
validTool().catch((error) => {
|
|
logger.error(`Error loading tool ${tool}:`, error);
|
|
return null;
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
const loadedTools = (await Promise.all(toolPromises)).flatMap((plugin) => plugin || []);
|
|
const mcpToolPromises = [];
|
|
/** MCP server tools are initialized sequentially by server */
|
|
let index = -1;
|
|
const failedMCPServers = new Set();
|
|
const safeUser = createSafeUser(options.req?.user);
|
|
const requestScopedConnections =
|
|
options.requestScopedConnections ?? getMCPRequestContext(options.req, options.res);
|
|
|
|
for (const [serverName, toolConfigs] of Object.entries(requestedMCPTools)) {
|
|
index++;
|
|
/** @type {LCAvailableTools} */
|
|
let availableTools = options.mcpAvailableTools?.[serverName];
|
|
for (const config of toolConfigs) {
|
|
try {
|
|
if (failedMCPServers.has(serverName)) {
|
|
continue;
|
|
}
|
|
const mcpParams = {
|
|
mcpPermissionContext,
|
|
index,
|
|
signal,
|
|
user: safeUser,
|
|
userMCPAuthMap,
|
|
configServers,
|
|
requestBody: options.req?.body,
|
|
requestScopedConnections,
|
|
res: options.res,
|
|
streamId: options.req?._resumableStreamId || null,
|
|
jobCreatedAt: options.jobCreatedAt,
|
|
model: agent?.model ?? model,
|
|
serverName: config.serverName,
|
|
provider: agent?.provider ?? endpoint,
|
|
config: config.config,
|
|
};
|
|
|
|
if (config.type === 'all' && toolConfigs.length === 1) {
|
|
/** Handle async loading for single 'all' tool config */
|
|
mcpToolPromises.push(
|
|
createMCPTools(mcpParams).catch((error) => {
|
|
logger.error(`Error loading ${serverName} tools:`, error);
|
|
return null;
|
|
}),
|
|
);
|
|
continue;
|
|
}
|
|
if (!availableTools) {
|
|
try {
|
|
availableTools = await getMCPServerTools(safeUser.id, serverName, config.config);
|
|
} catch (error) {
|
|
logger.error(`Error fetching available tools for MCP server ${serverName}:`, error);
|
|
}
|
|
}
|
|
|
|
/** Handle synchronous loading */
|
|
const mcpTool =
|
|
config.type === 'all'
|
|
? await createMCPTools(mcpParams)
|
|
: await createMCPTool({
|
|
...mcpParams,
|
|
availableTools,
|
|
toolKey: config.toolKey,
|
|
onAvailableTools: (tools) => {
|
|
availableTools = tools;
|
|
},
|
|
});
|
|
|
|
if (Array.isArray(mcpTool)) {
|
|
loadedTools.push(...mcpTool);
|
|
} else if (mcpTool) {
|
|
loadedTools.push(mcpTool);
|
|
} else {
|
|
failedMCPServers.add(serverName);
|
|
logger.warn(
|
|
`MCP tool creation failed for "${config.toolKey}", server may be unavailable or unauthenticated.`,
|
|
);
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error loading MCP tool for server ${serverName}:`, error);
|
|
}
|
|
}
|
|
}
|
|
loadedTools.push(...(await Promise.all(mcpToolPromises)).flatMap((plugin) => plugin || []));
|
|
return { loadedTools, toolContextMap, dynamicToolContextMap, primedCodeFiles };
|
|
};
|
|
|
|
module.exports = {
|
|
loadToolWithAuth,
|
|
validateTools,
|
|
loadTools,
|
|
};
|