Skip to content

Commit e4a3863

Browse files
committed
Add server name validation to MCP config loader
- Validate server names contain only letters, numbers, and hyphens - Raise ValueError for invalid server names - Initialize self._servers before populating
1 parent 1d85e3b commit e4a3863

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/uipath_mcp/_cli/_utils/_config.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
from typing import Any, Dict, List, Optional
5+
import re
56

67
logger = logging.getLogger(__name__)
78

@@ -50,16 +51,29 @@ def exists(self) -> bool:
5051
"""Check if mcp.json exists"""
5152
return os.path.exists(self.config_path)
5253

54+
@staticmethod
55+
def validate_server_name(name: str) -> None:
56+
"""
57+
Validate the server name.
58+
59+
The server name must only contain letters (a-z, A-Z), numbers (0-9), and hyphens (-).
60+
Raises a ValueError if the name is invalid.
61+
"""
62+
if not re.match(r'^[a-zA-Z0-9-]+$', name):
63+
raise ValueError(f'Invalid server name "{name}": only letters, numbers, and hyphens are allowed.')
64+
65+
5366
def _load_config(self) -> None:
5467
"""Load and process MCP configuration."""
5568
try:
5669
with open(self.config_path, "r") as f:
5770
self._raw_config = json.load(f)
5871

5972
servers_config = self._raw_config.get("servers", {})
60-
self._servers = {
61-
name: McpServer(name, config) for name, config in servers_config.items()
62-
}
73+
self._servers = {}
74+
for name in servers_config.keys():
75+
self.validate_server_name(name)
76+
self._servers[name] = McpServer(name, servers_config[name])
6377

6478
except json.JSONDecodeError as e:
6579
logger.error(f"Invalid JSON in {self.config_path}: {str(e)}")

0 commit comments

Comments
 (0)