2025-06-23 12:39:27 -04:00
|
|
|
import { extractEnvVariable } from 'librechat-data-provider';
|
|
|
|
|
import type { TUser, MCPOptions } from 'librechat-data-provider';
|
2025-08-16 20:45:55 -04:00
|
|
|
import type { RequestBody } from '~/types';
|
2025-06-23 12:39:27 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List of allowed user fields that can be used in MCP environment variables.
|
|
|
|
|
* These are non-sensitive string/boolean fields from the IUser interface.
|
|
|
|
|
*/
|
|
|
|
|
const ALLOWED_USER_FIELDS = [
|
|
|
|
|
'id',
|
|
|
|
|
'name',
|
|
|
|
|
'username',
|
|
|
|
|
'email',
|
|
|
|
|
'provider',
|
|
|
|
|
'role',
|
|
|
|
|
'googleId',
|
|
|
|
|
'facebookId',
|
|
|
|
|
'openidId',
|
|
|
|
|
'samlId',
|
|
|
|
|
'ldapId',
|
|
|
|
|
'githubId',
|
|
|
|
|
'discordId',
|
|
|
|
|
'appleId',
|
|
|
|
|
'emailVerified',
|
|
|
|
|
'twoFactorEnabled',
|
|
|
|
|
'termsAccepted',
|
|
|
|
|
] as const;
|
|
|
|
|
|
2025-08-16 20:45:55 -04:00
|
|
|
/**
|
|
|
|
|
* List of allowed request body fields that can be used in header placeholders.
|
|
|
|
|
* These are common fields from the request body that are safe to expose in headers.
|
|
|
|
|
*/
|
|
|
|
|
const ALLOWED_BODY_FIELDS = ['conversationId', 'parentMessageId', 'messageId'] as const;
|
|
|
|
|
|
2025-06-23 12:39:27 -04:00
|
|
|
/**
|
|
|
|
|
* Processes a string value to replace user field placeholders
|
|
|
|
|
* @param value - The string value to process
|
|
|
|
|
* @param user - The user object
|
|
|
|
|
* @returns The processed string with placeholders replaced
|
|
|
|
|
*/
|
|
|
|
|
function processUserPlaceholders(value: string, user?: TUser): string {
|
|
|
|
|
if (!user || typeof value !== 'string') {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const field of ALLOWED_USER_FIELDS) {
|
|
|
|
|
const placeholder = `{{LIBRECHAT_USER_${field.toUpperCase()}}}`;
|
|
|
|
|
if (!value.includes(placeholder)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fieldValue = user[field as keyof TUser];
|
|
|
|
|
|
|
|
|
|
// Skip replacement if field doesn't exist in user object
|
|
|
|
|
if (!(field in user)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Special case for 'id' field: skip if undefined or empty
|
|
|
|
|
if (field === 'id' && (fieldValue === undefined || fieldValue === '')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replacementValue = fieldValue == null ? '' : String(fieldValue);
|
|
|
|
|
value = value.replace(new RegExp(placeholder, 'g'), replacementValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 20:45:55 -04:00
|
|
|
/**
|
|
|
|
|
* Replaces request body field placeholders within a string.
|
|
|
|
|
* Recognized placeholders: `{{LIBRECHAT_BODY_<FIELD>}}` where `<FIELD>` ∈ ALLOWED_BODY_FIELDS.
|
|
|
|
|
* If a body field is absent or null/undefined, it is replaced with an empty string.
|
|
|
|
|
*
|
|
|
|
|
* @param value - The string value to process
|
|
|
|
|
* @param body - The request body object
|
|
|
|
|
* @returns The processed string with placeholders replaced
|
|
|
|
|
*/
|
|
|
|
|
function processBodyPlaceholders(value: string, body: RequestBody): string {
|
|
|
|
|
for (const field of ALLOWED_BODY_FIELDS) {
|
|
|
|
|
const placeholder = `{{LIBRECHAT_BODY_${field.toUpperCase()}}}`;
|
|
|
|
|
if (!value.includes(placeholder)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fieldValue = body[field];
|
|
|
|
|
const replacementValue = fieldValue == null ? '' : String(fieldValue);
|
|
|
|
|
value = value.replace(new RegExp(placeholder, 'g'), replacementValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 12:39:27 -04:00
|
|
|
/**
|
|
|
|
|
* Processes a single string value by replacing various types of placeholders
|
|
|
|
|
* @param originalValue - The original string value to process
|
|
|
|
|
* @param customUserVars - Optional custom user variables to replace placeholders
|
|
|
|
|
* @param user - Optional user object for replacing user field placeholders
|
2025-08-16 20:45:55 -04:00
|
|
|
* @param body - Optional request body object for replacing body field placeholders
|
2025-06-23 12:39:27 -04:00
|
|
|
* @returns The processed string with all placeholders replaced
|
|
|
|
|
*/
|
|
|
|
|
function processSingleValue({
|
|
|
|
|
originalValue,
|
|
|
|
|
customUserVars,
|
|
|
|
|
user,
|
2025-08-16 20:45:55 -04:00
|
|
|
body = undefined,
|
2025-06-23 12:39:27 -04:00
|
|
|
}: {
|
|
|
|
|
originalValue: string;
|
|
|
|
|
customUserVars?: Record<string, string>;
|
|
|
|
|
user?: TUser;
|
2025-08-16 20:45:55 -04:00
|
|
|
body?: RequestBody;
|
2025-06-23 12:39:27 -04:00
|
|
|
}): string {
|
|
|
|
|
let value = originalValue;
|
|
|
|
|
|
|
|
|
|
// 1. Replace custom user variables
|
|
|
|
|
if (customUserVars) {
|
|
|
|
|
for (const [varName, varVal] of Object.entries(customUserVars)) {
|
|
|
|
|
/** Escaped varName for use in regex to avoid issues with special characters */
|
|
|
|
|
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
|
const placeholderRegex = new RegExp(`\\{\\{${escapedVarName}\\}\\}`, 'g');
|
|
|
|
|
value = value.replace(placeholderRegex, varVal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Replace user field placeholders (e.g., {{LIBRECHAT_USER_EMAIL}}, {{LIBRECHAT_USER_ID}})
|
|
|
|
|
value = processUserPlaceholders(value, user);
|
|
|
|
|
|
2025-08-16 20:45:55 -04:00
|
|
|
// 3. Replace body field placeholders (e.g., {{LIBRECHAT_BODY_CONVERSATIONID}}, {{LIBRECHAT_BODY_PARENTMESSAGEID}})
|
|
|
|
|
if (body) {
|
|
|
|
|
value = processBodyPlaceholders(value, body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Replace system environment variables
|
2025-06-23 12:39:27 -04:00
|
|
|
value = extractEnvVariable(value);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recursively processes an object to replace environment variables in string values
|
2025-08-16 20:45:55 -04:00
|
|
|
* @param params - Processing parameters
|
|
|
|
|
* @param params.options - The MCP options to process
|
|
|
|
|
* @param params.user - The user object containing all user fields
|
|
|
|
|
* @param params.customUserVars - vars that user set in settings
|
|
|
|
|
* @param params.body - the body of the request that is being processed
|
2025-06-23 12:39:27 -04:00
|
|
|
* @returns - The processed object with environment variables replaced
|
|
|
|
|
*/
|
2025-08-16 20:45:55 -04:00
|
|
|
export function processMCPEnv(params: {
|
|
|
|
|
options: Readonly<MCPOptions>;
|
|
|
|
|
user?: TUser;
|
|
|
|
|
customUserVars?: Record<string, string>;
|
|
|
|
|
body?: RequestBody;
|
|
|
|
|
}): MCPOptions {
|
|
|
|
|
const { options, user, customUserVars, body } = params;
|
|
|
|
|
|
|
|
|
|
if (options === null || options === undefined) {
|
|
|
|
|
return options;
|
2025-06-23 12:39:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-16 20:45:55 -04:00
|
|
|
const newObj: MCPOptions = structuredClone(options);
|
2025-06-23 12:39:27 -04:00
|
|
|
|
|
|
|
|
if ('env' in newObj && newObj.env) {
|
|
|
|
|
const processedEnv: Record<string, string> = {};
|
|
|
|
|
for (const [key, originalValue] of Object.entries(newObj.env)) {
|
2025-08-16 20:45:55 -04:00
|
|
|
processedEnv[key] = processSingleValue({ originalValue, customUserVars, user, body });
|
2025-06-23 12:39:27 -04:00
|
|
|
}
|
|
|
|
|
newObj.env = processedEnv;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 11:37:56 -07:00
|
|
|
if ('args' in newObj && newObj.args) {
|
|
|
|
|
const processedArgs: string[] = [];
|
|
|
|
|
for (const originalValue of newObj.args) {
|
2025-08-16 20:45:55 -04:00
|
|
|
processedArgs.push(processSingleValue({ originalValue, customUserVars, user, body }));
|
2025-07-30 11:37:56 -07:00
|
|
|
}
|
|
|
|
|
newObj.args = processedArgs;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 12:39:27 -04:00
|
|
|
// Process headers if they exist (for WebSocket, SSE, StreamableHTTP types)
|
|
|
|
|
// Note: `env` and `headers` are on different branches of the MCPOptions union type.
|
|
|
|
|
if ('headers' in newObj && newObj.headers) {
|
|
|
|
|
const processedHeaders: Record<string, string> = {};
|
|
|
|
|
for (const [key, originalValue] of Object.entries(newObj.headers)) {
|
2025-08-16 20:45:55 -04:00
|
|
|
processedHeaders[key] = processSingleValue({ originalValue, customUserVars, user, body });
|
2025-06-23 12:39:27 -04:00
|
|
|
}
|
|
|
|
|
newObj.headers = processedHeaders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process URL if it exists (for WebSocket, SSE, StreamableHTTP types)
|
|
|
|
|
if ('url' in newObj && newObj.url) {
|
2025-08-16 20:45:55 -04:00
|
|
|
newObj.url = processSingleValue({ originalValue: newObj.url, customUserVars, user, body });
|
2025-06-23 12:39:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-16 20:45:55 -04:00
|
|
|
* Resolves header values by replacing user placeholders, body variables, custom variables, and environment variables.
|
|
|
|
|
*
|
|
|
|
|
* @param options - Optional configuration object.
|
|
|
|
|
* @param options.headers - The headers object to process.
|
|
|
|
|
* @param options.user - Optional user object for replacing user field placeholders (can be partial with just id).
|
|
|
|
|
* @param options.body - Optional request body object for replacing body field placeholders.
|
|
|
|
|
* @param options.customUserVars - Optional custom user variables to replace placeholders.
|
|
|
|
|
* @returns The processed headers with all placeholders replaced.
|
2025-06-23 12:39:27 -04:00
|
|
|
*/
|
2025-08-16 20:45:55 -04:00
|
|
|
export function resolveHeaders(options?: {
|
|
|
|
|
headers: Record<string, string> | undefined;
|
|
|
|
|
user?: Partial<TUser> | { id: string };
|
|
|
|
|
body?: RequestBody;
|
|
|
|
|
customUserVars?: Record<string, string>;
|
|
|
|
|
}) {
|
|
|
|
|
const { headers, user, body, customUserVars } = options ?? {};
|
|
|
|
|
const inputHeaders = headers ?? {};
|
|
|
|
|
|
|
|
|
|
const resolvedHeaders: Record<string, string> = { ...inputHeaders };
|
|
|
|
|
|
|
|
|
|
if (inputHeaders && typeof inputHeaders === 'object' && !Array.isArray(inputHeaders)) {
|
|
|
|
|
Object.keys(inputHeaders).forEach((key) => {
|
2025-06-23 12:39:27 -04:00
|
|
|
resolvedHeaders[key] = processSingleValue({
|
2025-08-16 20:45:55 -04:00
|
|
|
originalValue: inputHeaders[key],
|
2025-06-23 12:39:27 -04:00
|
|
|
customUserVars,
|
|
|
|
|
user: user as TUser,
|
2025-08-16 20:45:55 -04:00
|
|
|
body,
|
2025-06-23 12:39:27 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolvedHeaders;
|
|
|
|
|
}
|