Skip to content

Commit f2ed73c

Browse files
Merge branch 'danny-avila:main' into main
2 parents 8430f76 + 4f3683f commit f2ed73c

File tree

6 files changed

+386
-36
lines changed

6 files changed

+386
-36
lines changed

api/app/clients/tools/util/handleTools.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
const { logger } = require('@librechat/data-schemas');
22
const { SerpAPI } = require('@langchain/community/tools/serpapi');
33
const { Calculator } = require('@langchain/community/tools/calculator');
4-
const { mcpToolPattern, loadWebSearchAuth, checkAccess } = require('@librechat/api');
54
const { EnvVar, createCodeExecutionTool, createSearchTool } = require('@librechat/agents');
5+
const {
6+
checkAccess,
7+
createSafeUser,
8+
mcpToolPattern,
9+
loadWebSearchAuth,
10+
} = require('@librechat/api');
611
const {
712
Tools,
813
Constants,
@@ -410,6 +415,7 @@ Current Date & Time: ${replaceSpecialVars({ text: '{{iso_datetime}}' })}
410415
/** MCP server tools are initialized sequentially by server */
411416
let index = -1;
412417
const failedMCPServers = new Set();
418+
const safeUser = createSafeUser(options.req?.user);
413419
for (const [serverName, toolConfigs] of Object.entries(requestedMCPTools)) {
414420
index++;
415421
/** @type {LCAvailableTools} */
@@ -420,14 +426,14 @@ Current Date & Time: ${replaceSpecialVars({ text: '{{iso_datetime}}' })}
420426
continue;
421427
}
422428
const mcpParams = {
423-
res: options.res,
424-
userId: user,
425429
index,
426-
serverName: config.serverName,
430+
signal,
431+
user: safeUser,
427432
userMCPAuthMap,
433+
res: options.res,
428434
model: agent?.model ?? model,
435+
serverName: config.serverName,
429436
provider: agent?.provider ?? endpoint,
430-
signal,
431437
};
432438

433439
if (config.type === 'all' && toolConfigs.length === 1) {

api/server/routes/mcp.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const { Router } = require('express');
22
const { logger } = require('@librechat/data-schemas');
33
const { CacheKeys, Constants } = require('librechat-data-provider');
4-
const { MCPOAuthHandler, MCPTokenStorage, getUserMCPAuthMap } = require('@librechat/api');
4+
const {
5+
createSafeUser,
6+
MCPOAuthHandler,
7+
MCPTokenStorage,
8+
getUserMCPAuthMap,
9+
} = require('@librechat/api');
510
const { getMCPManager, getFlowStateManager, getOAuthReconnectionManager } = require('~/config');
611
const { getMCPSetupData, getServerConnectionStatus } = require('~/server/services/MCP');
712
const { findToken, updateToken, createToken, deleteTokens } = require('~/models');
@@ -335,9 +340,9 @@ router.post('/oauth/cancel/:serverName', requireJwtAuth, async (req, res) => {
335340
router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
336341
try {
337342
const { serverName } = req.params;
338-
const userId = req.user?.id;
343+
const user = createSafeUser(req.user);
339344

340-
if (!userId) {
345+
if (!user.id) {
341346
return res.status(401).json({ error: 'User not authenticated' });
342347
}
343348

@@ -351,7 +356,7 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
351356
});
352357
}
353358

354-
await mcpManager.disconnectUserConnection(userId, serverName);
359+
await mcpManager.disconnectUserConnection(user.id, serverName);
355360
logger.info(
356361
`[MCP Reinitialize] Disconnected existing user connection for server: ${serverName}`,
357362
);
@@ -360,14 +365,14 @@ router.post('/:serverName/reinitialize', requireJwtAuth, async (req, res) => {
360365
let userMCPAuthMap;
361366
if (serverConfig.customUserVars && typeof serverConfig.customUserVars === 'object') {
362367
userMCPAuthMap = await getUserMCPAuthMap({
363-
userId,
368+
userId: user.id,
364369
servers: [serverName],
365370
findPluginAuthsByKeys,
366371
});
367372
}
368373

369374
const result = await reinitMCPServer({
370-
userId,
375+
user,
371376
serverName,
372377
userMCPAuthMap,
373378
});

api/server/services/MCP.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,17 @@ function createOAuthCallback({ runStepEmitter, runStepDeltaEmitter }) {
153153
/**
154154
* @param {Object} params
155155
* @param {ServerResponse} params.res - The Express response object for sending events.
156-
* @param {string} params.userId - The user ID from the request object.
156+
* @param {IUser} params.user - The user from the request object.
157157
* @param {string} params.serverName
158158
* @param {AbortSignal} params.signal
159159
* @param {string} params.model
160160
* @param {number} [params.index]
161161
* @param {Record<string, Record<string, string>>} [params.userMCPAuthMap]
162162
* @returns { Promise<Array<typeof tool | { _call: (toolInput: Object | string) => unknown}>> } An object with `_call` method to execute the tool input.
163163
*/
164-
async function reconnectServer({ res, userId, index, signal, serverName, userMCPAuthMap }) {
164+
async function reconnectServer({ res, user, index, signal, serverName, userMCPAuthMap }) {
165165
const runId = Constants.USE_PRELIM_RESPONSE_MESSAGE_ID;
166-
const flowId = `${userId}:${serverName}:${Date.now()}`;
166+
const flowId = `${user.id}:${serverName}:${Date.now()}`;
167167
const flowManager = getFlowStateManager(getLogStores(CacheKeys.FLOWS));
168168
const stepId = 'step_oauth_login_' + serverName;
169169
const toolCall = {
@@ -192,7 +192,7 @@ async function reconnectServer({ res, userId, index, signal, serverName, userMCP
192192
flowManager,
193193
});
194194
return await reinitMCPServer({
195-
userId,
195+
user,
196196
signal,
197197
serverName,
198198
oauthStart,
@@ -212,7 +212,7 @@ async function reconnectServer({ res, userId, index, signal, serverName, userMCP
212212
*
213213
* @param {Object} params
214214
* @param {ServerResponse} params.res - The Express response object for sending events.
215-
* @param {string} params.userId - The user ID from the request object.
215+
* @param {IUser} params.user - The user from the request object.
216216
* @param {string} params.serverName
217217
* @param {string} params.model
218218
* @param {Providers | EModelEndpoint} params.provider - The provider for the tool.
@@ -221,16 +221,8 @@ async function reconnectServer({ res, userId, index, signal, serverName, userMCP
221221
* @param {Record<string, Record<string, string>>} [params.userMCPAuthMap]
222222
* @returns { Promise<Array<typeof tool | { _call: (toolInput: Object | string) => unknown}>> } An object with `_call` method to execute the tool input.
223223
*/
224-
async function createMCPTools({
225-
res,
226-
userId,
227-
index,
228-
signal,
229-
serverName,
230-
provider,
231-
userMCPAuthMap,
232-
}) {
233-
const result = await reconnectServer({ res, userId, index, signal, serverName, userMCPAuthMap });
224+
async function createMCPTools({ res, user, index, signal, serverName, provider, userMCPAuthMap }) {
225+
const result = await reconnectServer({ res, user, index, signal, serverName, userMCPAuthMap });
234226
if (!result || !result.tools) {
235227
logger.warn(`[MCP][${serverName}] Failed to reinitialize MCP server.`);
236228
return;
@@ -240,7 +232,7 @@ async function createMCPTools({
240232
for (const tool of result.tools) {
241233
const toolInstance = await createMCPTool({
242234
res,
243-
userId,
235+
user,
244236
provider,
245237
userMCPAuthMap,
246238
availableTools: result.availableTools,
@@ -258,7 +250,7 @@ async function createMCPTools({
258250
* Creates a single tool from the specified MCP Server via `toolKey`.
259251
* @param {Object} params
260252
* @param {ServerResponse} params.res - The Express response object for sending events.
261-
* @param {string} params.userId - The user ID from the request object.
253+
* @param {IUser} params.user - The user from the request object.
262254
* @param {string} params.toolKey - The toolKey for the tool.
263255
* @param {string} params.model - The model for the tool.
264256
* @param {number} [params.index]
@@ -270,7 +262,7 @@ async function createMCPTools({
270262
*/
271263
async function createMCPTool({
272264
res,
273-
userId,
265+
user,
274266
index,
275267
signal,
276268
toolKey,
@@ -288,7 +280,7 @@ async function createMCPTool({
288280
);
289281
const result = await reconnectServer({
290282
res,
291-
userId,
283+
user,
292284
index,
293285
signal,
294286
serverName,

0 commit comments

Comments
 (0)