2024-12-17 13:12:57 -05:00
|
|
|
import { z } from 'zod';
|
2025-03-01 07:51:12 -05:00
|
|
|
import { extractEnvVariable } from './utils';
|
2024-12-17 13:12:57 -05:00
|
|
|
|
|
|
|
|
const BaseOptionsSchema = z.object({
|
|
|
|
|
iconPath: z.string().optional(),
|
2025-03-06 11:02:43 -06:00
|
|
|
timeout: z.number().optional(),
|
2025-03-19 06:47:02 +01:00
|
|
|
initTimeout: z.number().optional(),
|
2025-05-08 12:12:36 -04:00
|
|
|
/** Controls visibility in chat dropdown menu (MCPSelect) */
|
|
|
|
|
chatMenu: z.boolean().optional(),
|
2024-12-17 13:12:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const StdioOptionsSchema = BaseOptionsSchema.extend({
|
|
|
|
|
type: z.literal('stdio').optional(),
|
|
|
|
|
/**
|
|
|
|
|
* The executable to run to start the server.
|
|
|
|
|
*/
|
|
|
|
|
command: z.string(),
|
|
|
|
|
/**
|
|
|
|
|
* Command line arguments to pass to the executable.
|
|
|
|
|
*/
|
|
|
|
|
args: z.array(z.string()),
|
|
|
|
|
/**
|
|
|
|
|
* The environment to use when spawning the process.
|
|
|
|
|
*
|
|
|
|
|
* If not specified, the result of getDefaultEnvironment() will be used.
|
2025-03-01 07:51:12 -05:00
|
|
|
* Environment variables can be referenced using ${VAR_NAME} syntax.
|
2024-12-17 13:12:57 -05:00
|
|
|
*/
|
2025-03-01 07:51:12 -05:00
|
|
|
env: z
|
|
|
|
|
.record(z.string(), z.string())
|
|
|
|
|
.optional()
|
|
|
|
|
.transform((env) => {
|
|
|
|
|
if (!env) {
|
|
|
|
|
return env;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const processedEnv: Record<string, string> = {};
|
|
|
|
|
for (const [key, value] of Object.entries(env)) {
|
|
|
|
|
processedEnv[key] = extractEnvVariable(value);
|
|
|
|
|
}
|
|
|
|
|
return processedEnv;
|
|
|
|
|
}),
|
2024-12-17 13:12:57 -05:00
|
|
|
/**
|
|
|
|
|
* How to handle stderr of the child process. This matches the semantics of Node's `child_process.spawn`.
|
|
|
|
|
*
|
|
|
|
|
* @type {import('node:child_process').IOType | import('node:stream').Stream | number}
|
|
|
|
|
*
|
|
|
|
|
* The default is "inherit", meaning messages to stderr will be printed to the parent process's stderr.
|
|
|
|
|
*/
|
|
|
|
|
stderr: z.any().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const WebSocketOptionsSchema = BaseOptionsSchema.extend({
|
|
|
|
|
type: z.literal('websocket').optional(),
|
|
|
|
|
url: z
|
|
|
|
|
.string()
|
2025-05-20 01:37:21 +02:00
|
|
|
.transform((val: string) => extractEnvVariable(val))
|
|
|
|
|
.pipe(z.string().url())
|
2024-12-17 13:12:57 -05:00
|
|
|
.refine(
|
2025-05-20 01:37:21 +02:00
|
|
|
(val: string) => {
|
2024-12-17 13:12:57 -05:00
|
|
|
const protocol = new URL(val).protocol;
|
|
|
|
|
return protocol === 'ws:' || protocol === 'wss:';
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
message: 'WebSocket URL must start with ws:// or wss://',
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const SSEOptionsSchema = BaseOptionsSchema.extend({
|
|
|
|
|
type: z.literal('sse').optional(),
|
2025-03-28 15:21:10 -04:00
|
|
|
headers: z.record(z.string(), z.string()).optional(),
|
2024-12-17 13:12:57 -05:00
|
|
|
url: z
|
|
|
|
|
.string()
|
2025-05-20 01:37:21 +02:00
|
|
|
.transform((val: string) => extractEnvVariable(val))
|
|
|
|
|
.pipe(z.string().url())
|
2024-12-17 13:12:57 -05:00
|
|
|
.refine(
|
2025-05-20 01:37:21 +02:00
|
|
|
(val: string) => {
|
2024-12-17 13:12:57 -05:00
|
|
|
const protocol = new URL(val).protocol;
|
|
|
|
|
return protocol !== 'ws:' && protocol !== 'wss:';
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
message: 'SSE URL must not start with ws:// or wss://',
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-13 19:14:15 +02:00
|
|
|
export const StreamableHTTPOptionsSchema = BaseOptionsSchema.extend({
|
|
|
|
|
type: z.literal('streamable-http'),
|
|
|
|
|
headers: z.record(z.string(), z.string()).optional(),
|
2025-05-15 12:17:17 -04:00
|
|
|
url: z
|
|
|
|
|
.string()
|
2025-05-20 01:37:21 +02:00
|
|
|
.transform((val: string) => extractEnvVariable(val))
|
|
|
|
|
.pipe(z.string().url())
|
2025-05-15 12:17:17 -04:00
|
|
|
.refine(
|
2025-05-20 01:37:21 +02:00
|
|
|
(val: string) => {
|
2025-05-13 19:14:15 +02:00
|
|
|
const protocol = new URL(val).protocol;
|
|
|
|
|
return protocol !== 'ws:' && protocol !== 'wss:';
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
message: 'Streamable HTTP URL must not start with ws:// or wss://',
|
|
|
|
|
},
|
2025-05-15 12:17:17 -04:00
|
|
|
),
|
2025-05-13 19:14:15 +02:00
|
|
|
});
|
|
|
|
|
|
2024-12-17 13:12:57 -05:00
|
|
|
export const MCPOptionsSchema = z.union([
|
|
|
|
|
StdioOptionsSchema,
|
|
|
|
|
WebSocketOptionsSchema,
|
|
|
|
|
SSEOptionsSchema,
|
2025-05-13 19:14:15 +02:00
|
|
|
StreamableHTTPOptionsSchema,
|
2024-12-17 13:12:57 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
export const MCPServersSchema = z.record(z.string(), MCPOptionsSchema);
|
2025-03-18 23:16:45 -04:00
|
|
|
|
|
|
|
|
export type MCPOptions = z.infer<typeof MCPOptionsSchema>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recursively processes an object to replace environment variables in string values
|
|
|
|
|
* @param {MCPOptions} obj - The object to process
|
2025-03-28 15:21:10 -04:00
|
|
|
* @param {string} [userId] - The user ID
|
2025-03-18 23:16:45 -04:00
|
|
|
* @returns {MCPOptions} - The processed object with environment variables replaced
|
|
|
|
|
*/
|
2025-05-06 10:29:05 -04:00
|
|
|
export function processMCPEnv(obj: Readonly<MCPOptions>, userId?: string): MCPOptions {
|
2025-03-18 23:16:45 -04:00
|
|
|
if (obj === null || obj === undefined) {
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 10:29:05 -04:00
|
|
|
const newObj: MCPOptions = structuredClone(obj);
|
|
|
|
|
|
|
|
|
|
if ('env' in newObj && newObj.env) {
|
2025-03-18 23:16:45 -04:00
|
|
|
const processedEnv: Record<string, string> = {};
|
2025-05-06 10:29:05 -04:00
|
|
|
for (const [key, value] of Object.entries(newObj.env)) {
|
2025-03-18 23:16:45 -04:00
|
|
|
processedEnv[key] = extractEnvVariable(value);
|
|
|
|
|
}
|
2025-05-06 10:29:05 -04:00
|
|
|
newObj.env = processedEnv;
|
|
|
|
|
} else if ('headers' in newObj && newObj.headers) {
|
2025-03-28 15:21:10 -04:00
|
|
|
const processedHeaders: Record<string, string> = {};
|
2025-05-06 10:29:05 -04:00
|
|
|
for (const [key, value] of Object.entries(newObj.headers)) {
|
2025-03-28 15:21:10 -04:00
|
|
|
if (value === '{{LIBRECHAT_USER_ID}}' && userId != null && userId) {
|
|
|
|
|
processedHeaders[key] = userId;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
processedHeaders[key] = extractEnvVariable(value);
|
|
|
|
|
}
|
2025-05-06 10:29:05 -04:00
|
|
|
newObj.headers = processedHeaders;
|
2025-03-18 23:16:45 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 01:37:21 +02:00
|
|
|
if ('url' in newObj && newObj.url) {
|
|
|
|
|
newObj.url = extractEnvVariable(newObj.url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 10:29:05 -04:00
|
|
|
return newObj;
|
2025-03-18 23:16:45 -04:00
|
|
|
}
|