🔧 fix: Plugin Method Undefined in Agent Tool Closure (#8413)

This commit is contained in:
Danny Avila 2025-07-11 13:16:59 -04:00 committed by GitHub
parent 1e4f1f780c
commit 929b433662
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 50 deletions

View file

@ -1,5 +1,7 @@
require('events').EventEmitter.defaultMaxListeners = 100; require('events').EventEmitter.defaultMaxListeners = 100;
const { logger } = require('@librechat/data-schemas'); const { logger } = require('@librechat/data-schemas');
const { DynamicStructuredTool } = require('@langchain/core/tools');
const { getBufferString, HumanMessage } = require('@langchain/core/messages');
const { const {
sendEvent, sendEvent,
createRun, createRun,
@ -31,13 +33,16 @@ const {
bedrockInputSchema, bedrockInputSchema,
removeNullishValues, removeNullishValues,
} = require('librechat-data-provider'); } = require('librechat-data-provider');
const { DynamicStructuredTool } = require('@langchain/core/tools'); const {
const { getBufferString, HumanMessage } = require('@langchain/core/messages'); findPluginAuthsByKeys,
const { createGetMCPAuthMap, checkCapability } = require('~/server/services/Config'); getFormattedMemories,
deleteMemory,
setMemory,
} = require('~/models');
const { getMCPAuthMap, checkCapability, hasCustomUserVars } = require('~/server/services/Config');
const { addCacheControl, createContextHandlers } = require('~/app/clients/prompts'); const { addCacheControl, createContextHandlers } = require('~/app/clients/prompts');
const { initializeAgent } = require('~/server/services/Endpoints/agents/agent'); const { initializeAgent } = require('~/server/services/Endpoints/agents/agent');
const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens'); const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens');
const { getFormattedMemories, deleteMemory, setMemory } = require('~/models');
const { encodeAndFormat } = require('~/server/services/Files/images/encode'); const { encodeAndFormat } = require('~/server/services/Files/images/encode');
const { getProviderConfig } = require('~/server/services/Endpoints'); const { getProviderConfig } = require('~/server/services/Endpoints');
const BaseClient = require('~/app/clients/BaseClient'); const BaseClient = require('~/app/clients/BaseClient');
@ -701,8 +706,6 @@ class AgentClient extends BaseClient {
version: 'v2', version: 'v2',
}; };
const getUserMCPAuthMap = await createGetMCPAuthMap();
const toolSet = new Set((this.options.agent.tools ?? []).map((tool) => tool && tool.name)); const toolSet = new Set((this.options.agent.tools ?? []).map((tool) => tool && tool.name));
let { messages: initialMessages, indexTokenCountMap } = formatAgentMessages( let { messages: initialMessages, indexTokenCountMap } = formatAgentMessages(
payload, payload,
@ -823,10 +826,11 @@ class AgentClient extends BaseClient {
} }
try { try {
if (getUserMCPAuthMap) { if (await hasCustomUserVars()) {
config.configurable.userMCPAuthMap = await getUserMCPAuthMap({ config.configurable.userMCPAuthMap = await getMCPAuthMap({
tools: agent.tools, tools: agent.tools,
userId: this.options.req.user.id, userId: this.options.req.user.id,
findPluginAuthsByKeys,
}); });
} }
} catch (err) { } catch (err) {

View file

@ -1,10 +1,9 @@
const { logger } = require('@librechat/data-schemas'); const { logger } = require('@librechat/data-schemas');
const { getUserMCPAuthMap } = require('@librechat/api'); const { isEnabled, getUserMCPAuthMap } = require('@librechat/api');
const { CacheKeys, EModelEndpoint } = require('librechat-data-provider'); const { CacheKeys, EModelEndpoint } = require('librechat-data-provider');
const { normalizeEndpointName, isEnabled } = require('~/server/utils'); const { normalizeEndpointName } = require('~/server/utils');
const loadCustomConfig = require('./loadCustomConfig'); const loadCustomConfig = require('./loadCustomConfig');
const { getCachedTools } = require('./getCachedTools'); const { getCachedTools } = require('./getCachedTools');
const { findPluginAuthsByKeys } = require('~/models');
const getLogStores = require('~/cache/getLogStores'); const getLogStores = require('~/cache/getLogStores');
/** /**
@ -55,21 +54,14 @@ const getCustomEndpointConfig = async (endpoint) => {
); );
}; };
async function createGetMCPAuthMap() {
const customConfig = await getCustomConfig();
const mcpServers = customConfig?.mcpServers;
const hasCustomUserVars = Object.values(mcpServers ?? {}).some((server) => server.customUserVars);
if (!hasCustomUserVars) {
return;
}
/** /**
* @param {Object} params * @param {Object} params
* @param {GenericTool[]} [params.tools]
* @param {string} params.userId * @param {string} params.userId
* @param {GenericTool[]} [params.tools]
* @param {import('@librechat/data-schemas').PluginAuthMethods['findPluginAuthsByKeys']} params.findPluginAuthsByKeys
* @returns {Promise<Record<string, Record<string, string>> | undefined>} * @returns {Promise<Record<string, Record<string, string>> | undefined>}
*/ */
return async function ({ tools, userId }) { async function getMCPAuthMap({ userId, tools, findPluginAuthsByKeys }) {
try { try {
if (!tools || tools.length === 0) { if (!tools || tools.length === 0) {
return; return;
@ -89,12 +81,21 @@ async function createGetMCPAuthMap() {
err, err,
); );
} }
}; }
/**
* @returns {Promise<boolean>}
*/
async function hasCustomUserVars() {
const customConfig = await getCustomConfig();
const mcpServers = customConfig?.mcpServers;
return Object.values(mcpServers ?? {}).some((server) => server.customUserVars);
} }
module.exports = { module.exports = {
getMCPAuthMap,
getCustomConfig, getCustomConfig,
getBalanceConfig, getBalanceConfig,
createGetMCPAuthMap, hasCustomUserVars,
getCustomEndpointConfig, getCustomEndpointConfig,
}; };

View file

@ -76,6 +76,11 @@ export async function getPluginAuthMap({
await Promise.all(decryptionPromises); await Promise.all(decryptionPromises);
return authMap; return authMap;
} catch (error) { } catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
const plugins = pluginKeys?.join(', ') ?? 'all requested';
logger.warn(
`[getPluginAuthMap] Failed to fetch auth values for userId ${userId}, plugins: ${plugins}: ${message}`,
);
if (!throwError) { if (!throwError) {
/** Empty objects for each plugin key on error */ /** Empty objects for each plugin key on error */
return pluginKeys.reduce((acc, key) => { return pluginKeys.reduce((acc, key) => {
@ -83,11 +88,6 @@ export async function getPluginAuthMap({
return acc; return acc;
}, {} as PluginAuthMap); }, {} as PluginAuthMap);
} }
const message = error instanceof Error ? error.message : 'Unknown error';
logger.error(
`[getPluginAuthMap] Failed to fetch auth values for userId ${userId}, plugins: ${pluginKeys.join(', ')}: ${message}`,
);
throw error; throw error;
} }
} }