11"""Config command for MCPM - Manage MCPM configuration"""
22
33import os
4+ import sys
45
56from rich .console import Console
67from rich .prompt import Prompt
78
89from mcpm .utils .config import NODE_EXECUTABLES , ConfigManager
10+ from mcpm .utils .non_interactive import is_non_interactive , should_force_operation
911from mcpm .utils .repository import RepositoryManager
1012from mcpm .utils .rich_click_config import click
1113
@@ -24,23 +26,102 @@ def config():
2426
2527
2628@config .command ()
29+ @click .option ("--key" , help = "Configuration key to set" )
30+ @click .option ("--value" , help = "Configuration value to set" )
31+ @click .option ("--force" , is_flag = True , help = "Skip confirmation prompts" )
2732@click .help_option ("-h" , "--help" )
28- def set ():
33+ def set (key , value , force ):
2934 """Set MCPM configuration.
3035
31- Example:
36+ Interactive by default, or use CLI parameters for automation.
37+ Use --key and --value to set configuration non-interactively.
38+
39+ Examples:
3240
3341 \b
34- mcpm config set
42+ mcpm config set # Interactive mode
43+ mcpm config set --key node_executable --value npx # Non-interactive mode
3544 """
36- set_key = Prompt .ask ("Configuration key to set" , choices = ["node_executable" ], default = "node_executable" )
37- node_executable = Prompt .ask (
38- "Select default node executable, it will be automatically applied when adding npx server with mcpm add" ,
39- choices = NODE_EXECUTABLES ,
40- )
4145 config_manager = ConfigManager ()
42- config_manager .set_config (set_key , node_executable )
43- console .print (f"[green]Default node executable set to:[/] { node_executable } " )
46+
47+ # Check if we have CLI parameters for non-interactive mode
48+ has_cli_params = key is not None and value is not None
49+ force_non_interactive = is_non_interactive () or should_force_operation () or force
50+
51+ if has_cli_params or force_non_interactive :
52+ exit_code = _set_config_non_interactive (
53+ config_manager = config_manager ,
54+ key = key ,
55+ value = value ,
56+ force = force
57+ )
58+ sys .exit (exit_code )
59+
60+ # Interactive mode
61+ set_key = Prompt .ask ("Configuration key to set" , choices = ["node_executable" ], default = "node_executable" )
62+
63+ if set_key == "node_executable" :
64+ node_executable = Prompt .ask (
65+ "Select default node executable, it will be automatically applied when adding npx server with mcpm add" ,
66+ choices = NODE_EXECUTABLES ,
67+ )
68+ config_manager .set_config (set_key , node_executable )
69+ console .print (f"[green]Default node executable set to:[/] { node_executable } " )
70+ else :
71+ console .print (f"[red]Error: Unknown configuration key '{ set_key } '[/]" )
72+
73+
74+ def _set_config_non_interactive (config_manager , key = None , value = None , force = False ):
75+ """Set configuration non-interactively."""
76+ try :
77+ # Define supported configuration keys and their valid values
78+ SUPPORTED_KEYS = {
79+ "node_executable" : {
80+ "valid_values" : NODE_EXECUTABLES ,
81+ "description" : "Default node executable for npx servers"
82+ }
83+ }
84+
85+ # Validate that both key and value are provided in non-interactive mode
86+ if not key or not value :
87+ console .print ("[red]Error: Both --key and --value are required in non-interactive mode[/]" )
88+ console .print ("[dim]Use 'mcpm config set' for interactive mode[/]" )
89+ return 1
90+
91+ # Validate the configuration key
92+ if key not in SUPPORTED_KEYS :
93+ console .print (f"[red]Error: Unknown configuration key '{ key } '[/]" )
94+ console .print ("[yellow]Supported keys:[/]" )
95+ for supported_key , info in SUPPORTED_KEYS .items ():
96+ console .print (f" • [cyan]{ supported_key } [/] - { info ['description' ]} " )
97+ return 1
98+
99+ # Validate the value for the specific key
100+ key_info = SUPPORTED_KEYS [key ]
101+ if "valid_values" in key_info and value not in key_info ["valid_values" ]:
102+ console .print (f"[red]Error: Invalid value '{ value } ' for key '{ key } '[/]" )
103+ console .print (f"[yellow]Valid values for '{ key } ':[/]" )
104+ for valid_value in key_info ["valid_values" ]:
105+ console .print (f" • [cyan]{ valid_value } [/]" )
106+ return 1
107+
108+ # Display what will be set
109+ console .print ("[bold green]Setting configuration:[/]" )
110+ console .print (f"Key: [cyan]{ key } [/]" )
111+ console .print (f"Value: [cyan]{ value } [/]" )
112+
113+ # Set the configuration
114+ success = config_manager .set_config (key , value )
115+ if success :
116+ console .print (f"[green]✅ Configuration '{ key } ' set to '{ value } '[/]" )
117+ return 0
118+ else :
119+ console .print (f"[red]Error: Failed to set configuration '{ key } '[/]" )
120+ return 1
121+
122+ except Exception as e :
123+ console .print (f"[red]Error setting configuration: { e } [/]" )
124+ return 1
44125
45126
46127@config .command ()
0 commit comments