From 1050346915d6bc374202f66f94b3db095fe7486f Mon Sep 17 00:00:00 2001 From: Dustin Healy <54083382+dustinhealy@users.noreply.github.com> Date: Wed, 30 Jul 2025 11:37:56 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20Support=20for=20`cust?= =?UTF-8?q?omUserVar`=20Replacement=20in=20'args'=20Field=20(#8743)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/api/src/mcp/mcp.spec.ts | 41 ++++++++++++++++++++++++++++++++ packages/api/src/utils/env.ts | 8 +++++++ 2 files changed, 49 insertions(+) diff --git a/packages/api/src/mcp/mcp.spec.ts b/packages/api/src/mcp/mcp.spec.ts index f27e4ed9ab..5f3b2d9b6d 100644 --- a/packages/api/src/mcp/mcp.spec.ts +++ b/packages/api/src/mcp/mcp.spec.ts @@ -655,6 +655,47 @@ describe('Environment Variable Extraction (MCP)', () => { ); }); + it('should process customUserVars in args field', () => { + const user = createTestUser({ + id: 'user-123', + email: 'test@example.com', + }); + const customUserVars = { + MY_API_KEY: 'user-provided-api-key-12345', + PROFILE_NAME: 'production-profile', + }; + const obj: MCPOptions = { + command: 'npx', + args: [ + '-y', + '@smithery/cli@latest', + 'run', + '@upstash/context7-mcp', + '--key', + '{{MY_API_KEY}}', + '--profile', + '{{PROFILE_NAME}}', + '--user', + '{{LIBRECHAT_USER_EMAIL}}', + ], + }; + + const result = processMCPEnv(obj, user, customUserVars); + + expect('args' in result && result.args).toEqual([ + '-y', + '@smithery/cli@latest', + 'run', + '@upstash/context7-mcp', + '--key', + 'user-provided-api-key-12345', + '--profile', + 'production-profile', + '--user', + 'test@example.com', + ]); + }); + it('should prioritize customUserVars over user fields and system env vars if placeholders are the same (though not recommended)', () => { // This tests the order of operations: customUserVars -> userFields -> systemEnv // BUt it's generally not recommended to have overlapping placeholder names. diff --git a/packages/api/src/utils/env.ts b/packages/api/src/utils/env.ts index 83530b93e4..54169d9b93 100644 --- a/packages/api/src/utils/env.ts +++ b/packages/api/src/utils/env.ts @@ -124,6 +124,14 @@ export function processMCPEnv( newObj.env = processedEnv; } + if ('args' in newObj && newObj.args) { + const processedArgs: string[] = []; + for (const originalValue of newObj.args) { + processedArgs.push(processSingleValue({ originalValue, customUserVars, user })); + } + newObj.args = processedArgs; + } + // 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) {