Skip to content

Commit c163b11

Browse files
authored
✨ feat: Add Support for customUserVar Replacement in 'args' Field (danny-avila#8743)
1 parent fcf7971 commit c163b11

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/api/src/mcp/mcp.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,47 @@ describe('Environment Variable Extraction (MCP)', () => {
655655
);
656656
});
657657

658+
it('should process customUserVars in args field', () => {
659+
const user = createTestUser({
660+
id: 'user-123',
661+
662+
});
663+
const customUserVars = {
664+
MY_API_KEY: 'user-provided-api-key-12345',
665+
PROFILE_NAME: 'production-profile',
666+
};
667+
const obj: MCPOptions = {
668+
command: 'npx',
669+
args: [
670+
'-y',
671+
'@smithery/cli@latest',
672+
'run',
673+
'@upstash/context7-mcp',
674+
'--key',
675+
'{{MY_API_KEY}}',
676+
'--profile',
677+
'{{PROFILE_NAME}}',
678+
'--user',
679+
'{{LIBRECHAT_USER_EMAIL}}',
680+
],
681+
};
682+
683+
const result = processMCPEnv(obj, user, customUserVars);
684+
685+
expect('args' in result && result.args).toEqual([
686+
'-y',
687+
'@smithery/cli@latest',
688+
'run',
689+
'@upstash/context7-mcp',
690+
'--key',
691+
'user-provided-api-key-12345',
692+
'--profile',
693+
'production-profile',
694+
'--user',
695+
696+
]);
697+
});
698+
658699
it('should prioritize customUserVars over user fields and system env vars if placeholders are the same (though not recommended)', () => {
659700
// This tests the order of operations: customUserVars -> userFields -> systemEnv
660701
// BUt it's generally not recommended to have overlapping placeholder names.

packages/api/src/utils/env.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export function processMCPEnv(
124124
newObj.env = processedEnv;
125125
}
126126

127+
if ('args' in newObj && newObj.args) {
128+
const processedArgs: string[] = [];
129+
for (const originalValue of newObj.args) {
130+
processedArgs.push(processSingleValue({ originalValue, customUserVars, user }));
131+
}
132+
newObj.args = processedArgs;
133+
}
134+
127135
// Process headers if they exist (for WebSocket, SSE, StreamableHTTP types)
128136
// Note: `env` and `headers` are on different branches of the MCPOptions union type.
129137
if ('headers' in newObj && newObj.headers) {

0 commit comments

Comments
 (0)