mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 02:10:15 +01:00
🚦 feat: Auto-reinitialize MCP Servers on Request (#9226)
This commit is contained in:
parent
ac608ded46
commit
c827fdd10e
28 changed files with 871 additions and 312 deletions
|
|
@ -7,33 +7,56 @@ import { getPluginAuthMap } from '~/agents/auth';
|
|||
export async function getUserMCPAuthMap({
|
||||
userId,
|
||||
tools,
|
||||
servers,
|
||||
toolInstances,
|
||||
findPluginAuthsByKeys,
|
||||
}: {
|
||||
userId: string;
|
||||
tools: GenericTool[] | undefined;
|
||||
tools?: (string | undefined)[];
|
||||
servers?: (string | undefined)[];
|
||||
toolInstances?: (GenericTool | null)[];
|
||||
findPluginAuthsByKeys: PluginAuthMethods['findPluginAuthsByKeys'];
|
||||
}) {
|
||||
if (!tools || tools.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const uniqueMcpServers = new Set<string>();
|
||||
|
||||
for (const tool of tools) {
|
||||
const mcpTool = tool as GenericTool & { mcpRawServerName?: string };
|
||||
if (mcpTool.mcpRawServerName) {
|
||||
uniqueMcpServers.add(`${Constants.mcp_prefix}${mcpTool.mcpRawServerName}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (uniqueMcpServers.size === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const mcpPluginKeysToFetch = Array.from(uniqueMcpServers);
|
||||
|
||||
let allMcpCustomUserVars: Record<string, Record<string, string>> = {};
|
||||
let mcpPluginKeysToFetch: string[] = [];
|
||||
try {
|
||||
const uniqueMcpServers = new Set<string>();
|
||||
|
||||
if (servers != null && servers.length) {
|
||||
for (const serverName of servers) {
|
||||
if (!serverName) {
|
||||
continue;
|
||||
}
|
||||
uniqueMcpServers.add(`${Constants.mcp_prefix}${serverName}`);
|
||||
}
|
||||
} else if (tools != null && tools.length) {
|
||||
for (const toolName of tools) {
|
||||
if (!toolName) {
|
||||
continue;
|
||||
}
|
||||
const delimiterIndex = toolName.indexOf(Constants.mcp_delimiter);
|
||||
if (delimiterIndex === -1) continue;
|
||||
const mcpServer = toolName.slice(delimiterIndex + Constants.mcp_delimiter.length);
|
||||
if (!mcpServer) continue;
|
||||
uniqueMcpServers.add(`${Constants.mcp_prefix}${mcpServer}`);
|
||||
}
|
||||
} else if (toolInstances != null && toolInstances.length) {
|
||||
for (const tool of toolInstances) {
|
||||
if (!tool) {
|
||||
continue;
|
||||
}
|
||||
const mcpTool = tool as GenericTool & { mcpRawServerName?: string };
|
||||
if (mcpTool.mcpRawServerName) {
|
||||
uniqueMcpServers.add(`${Constants.mcp_prefix}${mcpTool.mcpRawServerName}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (uniqueMcpServers.size === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
mcpPluginKeysToFetch = Array.from(uniqueMcpServers);
|
||||
allMcpCustomUserVars = await getPluginAuthMap({
|
||||
userId,
|
||||
pluginKeys: mcpPluginKeysToFetch,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue