@@ -3,6 +3,8 @@ import { requireAuthenticationAny, requireOAuthScope } from '../../middleware/oa
33import { getDb } from '../../db' ;
44import { eq , and , inArray } from 'drizzle-orm' ;
55import { mcpServers , mcpServerInstallations , mcpUserConfigurations , teamMemberships , devices } from '../../db/schema.sqlite' ;
6+ import { McpArgsStorage } from '../../utils/mcpArgsStorage' ;
7+ import { McpEnvStorage } from '../../utils/mcpEnvStorage' ;
68
79// Response schemas
810const GATEWAY_MCP_SERVER_SCHEMA = {
@@ -253,20 +255,27 @@ export default async function gatewayMeMcpConfigurationsRoute(server: FastifyIns
253255 let finalArgs = [ ...( claudeDesktopMethod . args || [ ] ) ] ;
254256 let finalEnv = { ...( claudeDesktopMethod . env || { } ) } ;
255257
256- // Apply team configuration
258+ // Apply team configuration with proper decryption
257259 if ( installation . team_args ) {
258260 try {
259- const teamArgs = JSON . parse ( installation . team_args ) ;
260- if ( Array . isArray ( teamArgs ) ) {
261+ const teamArgsSchema = JSON . parse ( server . team_args_schema || '[]' ) ;
262+ const decryptedTeamArgs = await McpArgsStorage . retrieveTeamArgs (
263+ installation . team_args ,
264+ teamArgsSchema ,
265+ { maskSecrets : false } , // Decrypt secrets for gateway
266+ request . log
267+ ) ;
268+
269+ // Apply decrypted team arguments
270+ if ( Array . isArray ( decryptedTeamArgs ) && decryptedTeamArgs . length > 0 ) {
261271 // Replace args based on team_args_schema
262- const teamArgsSchema = JSON . parse ( server . team_args_schema || '[]' ) ;
263- // eslint-disable-next-line @typescript-eslint/no-explicit-any
272+ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
264273 teamArgsSchema . forEach ( ( schema : any , index : number ) => {
265- if ( teamArgs [ index ] !== undefined ) {
274+ if ( decryptedTeamArgs [ index ] !== undefined ) {
266275 // Find the argument in finalArgs and replace it
267276 const argIndex = finalArgs . findIndex ( arg => arg === schema . name ) ;
268277 if ( argIndex !== - 1 ) {
269- finalArgs [ argIndex + 1 ] = teamArgs [ index ] ; // Replace the value after the argument name
278+ finalArgs [ argIndex + 1 ] = decryptedTeamArgs [ index ] ; // Replace the value after the argument name
270279 }
271280 }
272281 } ) ;
@@ -275,23 +284,28 @@ export default async function gatewayMeMcpConfigurationsRoute(server: FastifyIns
275284 request . log . warn ( {
276285 serverId : server . id ,
277286 error : error instanceof Error ? error . message : String ( error )
278- } , 'Failed to parse team_args' ) ;
287+ } , 'Failed to decrypt and parse team_args' ) ;
279288 }
280289 }
281290
282- // Apply team environment variables (encrypted)
291+ // Apply team environment variables with proper decryption
283292 if ( installation . team_env ) {
284293 try {
285- // Note: team_env is encrypted, would need decryption service here
286- // For now, we'll skip team env variables
287- request . log . debug ( {
288- serverId : server . id
289- } , 'Team environment variables are encrypted, skipping for now' ) ;
294+ const teamEnvSchema = JSON . parse ( server . team_env_schema || '[]' ) ;
295+ const decryptedTeamEnv = await McpEnvStorage . retrieveTeamEnv (
296+ installation . team_env ,
297+ teamEnvSchema ,
298+ { maskSecrets : false } , // Decrypt secrets for gateway
299+ request . log
300+ ) ;
301+
302+ // Merge decrypted team environment variables
303+ finalEnv = { ...finalEnv , ...decryptedTeamEnv } ;
290304 } catch ( error ) {
291305 request . log . warn ( {
292306 serverId : server . id ,
293307 error : error instanceof Error ? error . message : String ( error )
294- } , 'Failed to process team_env' ) ;
308+ } , 'Failed to decrypt and process team_env' ) ;
295309 }
296310 }
297311
@@ -317,22 +331,27 @@ export default async function gatewayMeMcpConfigurationsRoute(server: FastifyIns
317331 let configStatus : 'ready' | 'invalid' = 'ready' ;
318332
319333 if ( userConfig ) {
320- // Apply user args
334+ // Apply user args with proper decryption
321335 if ( userConfig . user_args ) {
322336 try {
323- const userArgs = JSON . parse ( userConfig . user_args ) ;
324337 const userArgsSchema = JSON . parse ( server . user_args_schema || '[]' ) ;
338+ const decryptedUserArgs = await McpArgsStorage . retrieveUserArgs (
339+ userConfig . user_args ,
340+ userArgsSchema ,
341+ { maskSecrets : false } , // Decrypt secrets for gateway
342+ request . log
343+ ) ;
325344
326345 // Replace user-specific arguments
327- // eslint-disable-next-line @typescript-eslint/no-explicit-any
346+ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
328347 userArgsSchema . forEach ( ( schema : any ) => {
329- if ( userArgs [ schema . name ] !== undefined ) {
348+ if ( decryptedUserArgs [ schema . name ] !== undefined ) {
330349 const argIndex = finalArgs . findIndex ( arg => arg === schema . name ) ;
331350 if ( argIndex !== - 1 ) {
332- finalArgs [ argIndex ] = userArgs [ schema . name ] ;
351+ finalArgs [ argIndex ] = decryptedUserArgs [ schema . name ] ;
333352 } else {
334353 // Add new argument if not found
335- finalArgs . push ( userArgs [ schema . name ] ) ;
354+ finalArgs . push ( decryptedUserArgs [ schema . name ] ) ;
336355 }
337356 } else if ( schema . required ) {
338357 configStatus = 'invalid' ;
@@ -346,21 +365,27 @@ export default async function gatewayMeMcpConfigurationsRoute(server: FastifyIns
346365 request . log . warn ( {
347366 serverId : server . id ,
348367 error : error instanceof Error ? error . message : String ( error )
349- } , 'Failed to parse user_args' ) ;
368+ } , 'Failed to decrypt and parse user_args' ) ;
350369 configStatus = 'invalid' ;
351370 }
352371 }
353372
354- // Apply user environment variables
373+ // Apply user environment variables with proper decryption
355374 if ( userConfig . user_env ) {
356375 try {
357- const userEnv = JSON . parse ( userConfig . user_env ) ;
358- finalEnv = { ...finalEnv , ...userEnv } ;
376+ const userEnvSchema = JSON . parse ( server . user_env_schema || '[]' ) ;
377+ const decryptedUserEnv = await McpEnvStorage . retrieveUserEnv (
378+ userConfig . user_env ,
379+ userEnvSchema ,
380+ { maskSecrets : false } , // Decrypt secrets for gateway
381+ request . log
382+ ) ;
383+ finalEnv = { ...finalEnv , ...decryptedUserEnv } ;
359384 } catch ( error ) {
360385 request . log . warn ( {
361386 serverId : server . id ,
362387 error : error instanceof Error ? error . message : String ( error )
363- } , 'Failed to parse user_env' ) ;
388+ } , 'Failed to decrypt and parse user_env' ) ;
364389 configStatus = 'invalid' ;
365390 }
366391 }
0 commit comments