diff --git a/src/uipath_mcp/_cli/_utils/_config.py b/src/uipath_mcp/_cli/_utils/_config.py index 597930e..8c5bcd5 100644 --- a/src/uipath_mcp/_cli/_utils/_config.py +++ b/src/uipath_mcp/_cli/_utils/_config.py @@ -2,6 +2,7 @@ import logging import os from typing import Any, Dict, List, Optional +import re logger = logging.getLogger(__name__) @@ -50,6 +51,18 @@ def exists(self) -> bool: """Check if mcp.json exists""" return os.path.exists(self.config_path) + @staticmethod + def validate_server_name(name: str) -> None: + """ + Validate the server name. + + The server name must only contain letters (a-z, A-Z), numbers (0-9), and hyphens (-). + Raises a ValueError if the name is invalid. + """ + if not re.match(r'^[a-zA-Z0-9-]+$', name): + raise ValueError(f'Invalid server name "{name}": only letters, numbers, and hyphens are allowed.') + + def _load_config(self) -> None: """Load and process MCP configuration.""" try: @@ -57,9 +70,10 @@ def _load_config(self) -> None: self._raw_config = json.load(f) servers_config = self._raw_config.get("servers", {}) - self._servers = { - name: McpServer(name, config) for name, config in servers_config.items() - } + self._servers = {} + for name in servers_config.keys(): + self.validate_server_name(name) + self._servers[name] = McpServer(name, servers_config[name]) except json.JSONDecodeError as e: logger.error(f"Invalid JSON in {self.config_path}: {str(e)}")