(
config: McpServerConfig,
userId: string,
workspaceId?: string,
options: ResolveMcpConfigOptions = {}
)
| 27 | * @returns Resolved config with env vars replaced |
| 28 | */ |
| 29 | export async function resolveMcpConfigEnvVars( |
| 30 | config: McpServerConfig, |
| 31 | userId: string, |
| 32 | workspaceId?: string, |
| 33 | options: ResolveMcpConfigOptions = {} |
| 34 | ): Promise<{ config: McpServerConfig; missingVars: string[] }> { |
| 35 | const { strict = true } = options |
| 36 | const allMissingVars: string[] = [] |
| 37 | |
| 38 | let envVars: Record<string, string> = {} |
| 39 | try { |
| 40 | envVars = await getEffectiveDecryptedEnv(userId, workspaceId) |
| 41 | } catch (error) { |
| 42 | logger.error('Failed to fetch environment variables for MCP config:', error) |
| 43 | return { config, missingVars: [] } |
| 44 | } |
| 45 | |
| 46 | const resolveValue = (value: string): string => { |
| 47 | const missingVars: string[] = [] |
| 48 | const resolved = resolveEnvVarReferences(value, envVars, { |
| 49 | missingKeys: missingVars, |
| 50 | }) as string |
| 51 | allMissingVars.push(...missingVars) |
| 52 | return resolved |
| 53 | } |
| 54 | |
| 55 | const resolvedConfig = { ...config } |
| 56 | |
| 57 | if (resolvedConfig.url) { |
| 58 | resolvedConfig.url = resolveValue(resolvedConfig.url) |
| 59 | } |
| 60 | |
| 61 | if (resolvedConfig.headers) { |
| 62 | const resolvedHeaders: Record<string, string> = {} |
| 63 | for (const [key, value] of Object.entries(resolvedConfig.headers)) { |
| 64 | resolvedHeaders[key] = resolveValue(value) |
| 65 | } |
| 66 | resolvedConfig.headers = resolvedHeaders |
| 67 | } |
| 68 | |
| 69 | // Handle missing vars based on strict mode |
| 70 | if (allMissingVars.length > 0) { |
| 71 | const uniqueMissing = Array.from(new Set(allMissingVars)) |
| 72 | |
| 73 | if (strict) { |
| 74 | throw new Error( |
| 75 | `Missing required environment variable${uniqueMissing.length > 1 ? 's' : ''}: ${uniqueMissing.join(', ')}. ` + |
| 76 | `Please set ${uniqueMissing.length > 1 ? 'these variables' : 'this variable'} in your workspace or personal environment settings.` |
| 77 | ) |
| 78 | } |
| 79 | uniqueMissing.forEach((envKey) => { |
| 80 | logger.warn(`Environment variable "${envKey}" not found in MCP config`) |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | return { config: resolvedConfig, missingVars: allMissingVars } |
| 85 | } |
no test coverage detected