1- import { MCPInstallation , MCPServerConfig , TeamMCPConfig } from '../../types/mcp' ;
1+ import { MCPInstallation , MCPServerConfig , TeamMCPConfig , MCPUserConfiguration } from '../../types/mcp' ;
22
33/**
4- * Process MCP installations into server configurations for the Gateway
4+ * Process MCP installations into server configurations for the Gateway (Three-tier architecture)
55 * @param teamId Team ID
66 * @param teamName Team name
77 * @param installations Raw MCP installations from API
8+ * @param userConfigurations User configurations from API
89 * @returns Processed team MCP configuration
910 */
1011export function processMCPInstallations (
1112 teamId : string ,
1213 teamName : string ,
13- installations : MCPInstallation [ ]
14+ installations : MCPInstallation [ ] ,
15+ userConfigurations : MCPUserConfiguration [ ] = [ ]
1416) : TeamMCPConfig {
1517 const servers : MCPServerConfig [ ] = installations . map ( installation => {
16- // Process installation methods to extract command and args
18+ // Process installation methods to extract template command and args
1719 const installationMethods = installation . server . installation_methods || [ ] ;
18- let command = 'npx' ;
19- let args : string [ ] = [ ] ;
20+ let templateCommand = 'npx' ;
21+ let templateArgs : string [ ] = [ ] ;
2022
21-
22- // Find the claude-desktop installation method
23+ // Find the claude-desktop installation method for template args
2324 const claudeDesktopMethod = installationMethods . find (
2425 /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
2526 ( method : any ) => method . client === 'claude-desktop'
2627 ) ;
2728
2829 if ( claudeDesktopMethod ) {
2930 // Use the installation method data directly
30- command = claudeDesktopMethod . command || 'npx' ;
31- args = claudeDesktopMethod . args || [ ] ;
31+ templateCommand = claudeDesktopMethod . command || 'npx' ;
32+ templateArgs = claudeDesktopMethod . args || [ ] ;
3233 } else {
3334 // Fallback logic for servers without proper installation_methods
3435 const runtime = installation . server . runtime ;
3536
3637 if ( runtime === 'node' || runtime === 'nodejs' ) {
37- command = 'npx' ;
38- args = [ installation . server . name ] ;
38+ templateCommand = 'npx' ;
39+ templateArgs = [ installation . server . name ] ;
3940 } else if ( runtime === 'python' ) {
40- command = 'python' ;
41- args = [ '-m' , installation . server . name ] ;
41+ templateCommand = 'python' ;
42+ templateArgs = [ '-m' , installation . server . name ] ;
4243 } else if ( runtime === 'go' ) {
43- command = installation . server . name ;
44- args = [ ] ;
44+ templateCommand = installation . server . name ;
45+ templateArgs = [ ] ;
4546 } else {
4647 // Final fallback
47- command = 'npx' ;
48- args = [ installation . server . name ] ;
48+ templateCommand = 'npx' ;
49+ templateArgs = [ installation . server . name ] ;
4950 }
5051 }
5152
52- // Merge environment variables from server definition and user customization
53+ // Find user configuration for this installation (use first one for now)
54+ const userConfig = userConfigurations . find ( config => config . installation_id === installation . id ) ;
55+
56+ // Three-tier assembly: Template + Team + User
57+ const finalArgs = [
58+ ...templateArgs , // Template args (fixed)
59+ ...( installation . team_args || [ ] ) , // Team args (shared)
60+ ...( userConfig ?. user_args || [ ] ) // User args (personal)
61+ ] ;
62+
63+ // Three-tier environment assembly: Template + Team + User
5364 const serverEnvVars = installation . server . environment_variables || [ ] ;
5465 const env : Record < string , string > = { } ;
5566
56- // Add server-defined environment variables (if they have default values )
67+ // Add server-defined environment variables (template level )
5768 // eslint-disable-next-line @typescript-eslint/no-explicit-any
5869 serverEnvVars . forEach ( ( envVar : any ) => {
5970 if ( envVar . name && envVar . default_value ) {
6071 env [ envVar . name ] = envVar . default_value ;
6172 }
6273 } ) ;
6374
64- // Override with user-provided environment variables
65- Object . assign ( env , installation . user_environment_variables || { } ) ;
75+ // Add team-level environment variables
76+ Object . assign ( env , installation . team_env || { } ) ;
77+
78+ // Add user-level environment variables
79+ Object . assign ( env , userConfig ?. user_env || { } ) ;
6680
6781 return {
6882 id : installation . id ,
6983 name : installation . server . name ,
7084 installation_name : installation . installation_name ,
71- command,
72- args,
85+ command : templateCommand ,
86+ args : finalArgs ,
7387 env,
7488 runtime : installation . server . runtime ,
7589 installation_type : installation . installation_type ,
@@ -81,6 +95,7 @@ export function processMCPInstallations(
8195 team_id : teamId ,
8296 team_name : teamName ,
8397 installations,
98+ user_configurations : userConfigurations ,
8499 servers,
85100 last_updated : new Date ( ) . toISOString ( )
86101 } ;
0 commit comments