|
| 1 | +""" |
| 2 | +Client configuration management for MCPM |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Any, Dict, List, Optional |
| 7 | + |
| 8 | +from mcpm.utils.config import ConfigManager |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class ClientConfigManager: |
| 14 | + """Manages client-specific configuration including active client and stashed servers""" |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + """Initialize the client config manager""" |
| 18 | + self.config_manager = ConfigManager() |
| 19 | + self._config = self.config_manager.get_config() |
| 20 | + |
| 21 | + def _refresh_config(self): |
| 22 | + """Refresh the local config cache from the config manager""" |
| 23 | + self._config = self.config_manager.get_config() |
| 24 | + |
| 25 | + def get_active_client(self) -> str: |
| 26 | + """Get the name of the currently active client or None if not set""" |
| 27 | + self._refresh_config() |
| 28 | + return self._config.get("active_client") |
| 29 | + |
| 30 | + def set_active_client(self, client_name: Optional[str]) -> bool: |
| 31 | + """Set the active client |
| 32 | +
|
| 33 | + Args: |
| 34 | + client_name: Name of client to set as active, or None to clear |
| 35 | +
|
| 36 | + Returns: |
| 37 | + bool: Success or failure |
| 38 | + """ |
| 39 | + # If None, remove the active client |
| 40 | + if client_name is None: |
| 41 | + result = self.config_manager.set_config("active_client", None) |
| 42 | + self._refresh_config() |
| 43 | + return result |
| 44 | + |
| 45 | + # Get supported clients |
| 46 | + from mcpm.utils.client_registry import ClientRegistry |
| 47 | + |
| 48 | + supported_clients = ClientRegistry.get_supported_clients() |
| 49 | + |
| 50 | + if client_name not in supported_clients: |
| 51 | + logger.error(f"Unknown client: {client_name}") |
| 52 | + return False |
| 53 | + |
| 54 | + # Set the active client |
| 55 | + result = self.config_manager.set_config("active_client", client_name) |
| 56 | + self._refresh_config() |
| 57 | + return result |
| 58 | + |
| 59 | + def get_supported_clients(self) -> List[str]: |
| 60 | + """Get a list of supported client names""" |
| 61 | + # Import here to avoid circular imports |
| 62 | + from mcpm.utils.client_registry import ClientRegistry |
| 63 | + |
| 64 | + return ClientRegistry.get_supported_clients() |
| 65 | + |
| 66 | + def get_client_manager(self, client_name: str): |
| 67 | + """Get the appropriate client manager for a client |
| 68 | +
|
| 69 | + Args: |
| 70 | + client_name: Name of the client |
| 71 | +
|
| 72 | + Returns: |
| 73 | + BaseClientManager or None if client not supported |
| 74 | + """ |
| 75 | + # Import here to avoid circular imports |
| 76 | + from mcpm.utils.client_registry import ClientRegistry |
| 77 | + |
| 78 | + return ClientRegistry.get_client_manager(client_name) |
| 79 | + |
| 80 | + def stash_server(self, client_name: str, server_name: str, server_config: Any) -> bool: |
| 81 | + """Store a disabled server configuration in the global config |
| 82 | +
|
| 83 | + Args: |
| 84 | + client_name: Name of the client the server belongs to |
| 85 | + server_name: Name of the server to stash |
| 86 | + server_config: Server configuration to stash (ServerConfig object or dict) |
| 87 | +
|
| 88 | + Returns: |
| 89 | + bool: Success or failure |
| 90 | + """ |
| 91 | + # Refresh config to ensure we have the latest state |
| 92 | + self._refresh_config() |
| 93 | + |
| 94 | + # Ensure stashed_servers section exists |
| 95 | + if "stashed_servers" not in self._config: |
| 96 | + self._config["stashed_servers"] = {} |
| 97 | + |
| 98 | + # Ensure client section exists |
| 99 | + if client_name not in self._config["stashed_servers"]: |
| 100 | + self._config["stashed_servers"][client_name] = {} |
| 101 | + |
| 102 | + # Convert ServerConfig to dict if needed |
| 103 | + try: |
| 104 | + # If it's a ServerConfig object with to_dict method |
| 105 | + if hasattr(server_config, "to_dict") and callable(server_config.to_dict): |
| 106 | + server_dict = server_config.to_dict() |
| 107 | + else: |
| 108 | + # Assume it's already a dict or JSON serializable |
| 109 | + server_dict = server_config |
| 110 | + |
| 111 | + # Add the server configuration |
| 112 | + stashed_servers = self._config.get("stashed_servers", {}) |
| 113 | + if client_name not in stashed_servers: |
| 114 | + stashed_servers[client_name] = {} |
| 115 | + stashed_servers[client_name][server_name] = server_dict |
| 116 | + |
| 117 | + # Use set_config to save the updated stashed_servers |
| 118 | + result = self.config_manager.set_config("stashed_servers", stashed_servers) |
| 119 | + self._refresh_config() |
| 120 | + return result |
| 121 | + except Exception as e: |
| 122 | + logger.error(f"Failed to save stashed server: {e}") |
| 123 | + return False |
| 124 | + |
| 125 | + def pop_server(self, client_name: str, server_name: str) -> Optional[Dict[str, Any]]: |
| 126 | + """Retrieve a stashed server configuration from the global config |
| 127 | +
|
| 128 | + Args: |
| 129 | + client_name: Name of the client the server belongs to |
| 130 | + server_name: Name of the server to retrieve |
| 131 | +
|
| 132 | + Returns: |
| 133 | + Dict: Server configuration or None if not found |
| 134 | + """ |
| 135 | + # Refresh config to ensure we have the latest state |
| 136 | + self._refresh_config() |
| 137 | + |
| 138 | + # Check if stashed_servers section exists |
| 139 | + stashed_servers = self._config.get("stashed_servers", {}) |
| 140 | + if not stashed_servers: |
| 141 | + return None |
| 142 | + |
| 143 | + # Check if client section exists |
| 144 | + if client_name not in stashed_servers: |
| 145 | + return None |
| 146 | + |
| 147 | + # Check if server exists |
| 148 | + if server_name not in stashed_servers[client_name]: |
| 149 | + return None |
| 150 | + |
| 151 | + # Get the server configuration |
| 152 | + server_config = stashed_servers[client_name][server_name] |
| 153 | + |
| 154 | + # Remove the server from stashed servers |
| 155 | + del stashed_servers[client_name][server_name] |
| 156 | + |
| 157 | + # Clean up empty client section if needed |
| 158 | + if not stashed_servers[client_name]: |
| 159 | + del stashed_servers[client_name] |
| 160 | + |
| 161 | + # Clean up empty stashed_servers section if needed |
| 162 | + if not stashed_servers: |
| 163 | + # Set to None to remove the key completely |
| 164 | + self.config_manager.set_config("stashed_servers", None) |
| 165 | + else: |
| 166 | + # Update with modified stashed_servers |
| 167 | + self.config_manager.set_config("stashed_servers", stashed_servers) |
| 168 | + |
| 169 | + # Refresh config after changes |
| 170 | + self._refresh_config() |
| 171 | + |
| 172 | + return server_config |
| 173 | + |
| 174 | + def is_server_stashed(self, client_name: str, server_name: str) -> bool: |
| 175 | + """Check if a server is stashed in the global config |
| 176 | +
|
| 177 | + Args: |
| 178 | + client_name: Name of the client the server belongs to |
| 179 | + server_name: Name of the server to check |
| 180 | +
|
| 181 | + Returns: |
| 182 | + bool: True if server is stashed, False otherwise |
| 183 | + """ |
| 184 | + # Refresh config to ensure we have the latest state |
| 185 | + self._refresh_config() |
| 186 | + |
| 187 | + # Check if stashed_servers section exists |
| 188 | + stashed_servers = self._config.get("stashed_servers", {}) |
| 189 | + if not stashed_servers: |
| 190 | + return False |
| 191 | + |
| 192 | + # Check if client section exists |
| 193 | + if client_name not in stashed_servers: |
| 194 | + return False |
| 195 | + |
| 196 | + # Check if server exists |
| 197 | + return server_name in stashed_servers[client_name] |
| 198 | + |
| 199 | + def get_stashed_servers(self, client_name: str) -> Dict[str, Dict[str, Any]]: |
| 200 | + """Get all stashed servers for a client |
| 201 | +
|
| 202 | + Args: |
| 203 | + client_name: Name of the client to get stashed servers for |
| 204 | +
|
| 205 | + Returns: |
| 206 | + Dict: Dictionary of server configurations by name |
| 207 | + """ |
| 208 | + # Refresh config to ensure we have the latest state |
| 209 | + self._refresh_config() |
| 210 | + |
| 211 | + # Check if stashed_servers section exists |
| 212 | + stashed_servers = self._config.get("stashed_servers", {}) |
| 213 | + if not stashed_servers: |
| 214 | + return {} |
| 215 | + |
| 216 | + # Check if client section exists |
| 217 | + if client_name not in stashed_servers: |
| 218 | + return {} |
| 219 | + |
| 220 | + return stashed_servers[client_name] |
0 commit comments