Manages MCP configuration loading and validation
| 162 | ) |
| 163 | |
| 164 | class MCPConfigManager: |
| 165 | """Manages MCP configuration loading and validation""" |
| 166 | |
| 167 | def __init__(self, config_path: Optional[str] = None): |
| 168 | """Initialize with optional custom config path""" |
| 169 | if config_path: |
| 170 | self.config_path = Path(config_path) |
| 171 | else: |
| 172 | self.config_path = Path.home() / ".optillm" / "mcp_config.json" |
| 173 | |
| 174 | # Default configuration |
| 175 | self.servers: Dict[str, ServerConfig] = {} |
| 176 | self.log_level: str = "INFO" |
| 177 | |
| 178 | def load_config(self) -> bool: |
| 179 | """Load configuration from file""" |
| 180 | try: |
| 181 | if not self.config_path.exists(): |
| 182 | logger.warning(f"MCP config file not found at {self.config_path}") |
| 183 | return False |
| 184 | |
| 185 | with open(self.config_path, 'r') as f: |
| 186 | config_data = f.read() |
| 187 | logger.debug(f"Raw config data: {config_data}") |
| 188 | config = json.loads(config_data) |
| 189 | |
| 190 | # Set log level |
| 191 | self.log_level = config.get("log_level", "INFO") |
| 192 | log_level = getattr(logging, self.log_level.upper(), logging.INFO) |
| 193 | logger.setLevel(log_level) |
| 194 | |
| 195 | # Load server configurations |
| 196 | servers_config = config.get("mcpServers", {}) |
| 197 | for server_name, server_config in servers_config.items(): |
| 198 | self.servers[server_name] = ServerConfig.from_dict(server_config) |
| 199 | logger.debug(f"Loaded server config for {server_name}: {server_config}") |
| 200 | |
| 201 | logger.info(f"Loaded configuration with {len(self.servers)} servers") |
| 202 | return True |
| 203 | |
| 204 | except Exception as e: |
| 205 | logger.error(f"Error loading MCP configuration: {e}") |
| 206 | logger.error(traceback.format_exc()) |
| 207 | return False |
| 208 | |
| 209 | def create_default_config(self) -> bool: |
| 210 | """Create a default configuration file if none exists""" |
| 211 | try: |
| 212 | if self.config_path.exists(): |
| 213 | return True |
| 214 | |
| 215 | default_config = { |
| 216 | "mcpServers": {}, |
| 217 | "log_level": "INFO" |
| 218 | } |
| 219 | |
| 220 | self.config_path.parent.mkdir(parents=True, exist_ok=True) |
| 221 | with open(self.config_path, 'w') as f: |
no outgoing calls