MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / expandEnvRefs

Function expandEnvRefs

src/mcp/manager.ts:19–55  ·  view source on GitHub ↗
(config: MCPServerConfig, serverName: string)

Source from the content-addressed store, hash-verified

17 * the input is not mutated.
18 */
19export function expandEnvRefs(config: MCPServerConfig, serverName: string): MCPServerConfig {
20 const missing = new Set<string>();
21 const expand = (s: string): string =>
22 s.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_m, varName: string) => {
23 const v = process.env[varName];
24 if (v === undefined || v === '') { missing.add(varName); return ''; }
25 return v;
26 });
27
28 const out: MCPServerConfig = { ...config };
29
30 if (config.args) out.args = config.args.map(a => (typeof a === 'string' ? expand(a) : a));
31 if (config.url) out.url = expand(config.url);
32 if (config.env) {
33 const env: Record<string, string> = {};
34 for (const [k, val] of Object.entries(config.env)) {
35 env[k] = typeof val === 'string' ? expand(val) : (val as any);
36 }
37 out.env = env;
38 }
39 if (config.headers) {
40 const headers: Record<string, string> = {};
41 for (const [k, val] of Object.entries(config.headers)) {
42 headers[k] = typeof val === 'string' ? expand(val) : (val as any);
43 }
44 out.headers = headers;
45 }
46
47 if (missing.size > 0) {
48 logger.warn(
49 `MCP server '${serverName}': unset env var(s) ${[...missing].join(', ')} — ` +
50 `referenced in config but not exported. The server will likely fail to authenticate. ` +
51 `Export them (e.g. \`export ${[...missing][0]}=...\`) or re-add with --inline.`,
52 );
53 }
54 return out;
55}
56
57/**
58 * MCPManager: lifecycle owner for all configured MCP servers.

Callers 2

startOneMethod · 0.85

Calls 2

warnMethod · 0.80
expandFunction · 0.70

Tested by

no test coverage detected