Skip to content

Commit 5f49306

Browse files
Karanraj-6radugheo
authored andcommitted
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 2cc3817 commit 5f49306

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/uipath_mcp/_cli/_utils/_config.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
import os
4+
import re
45
from typing import Any
56

67
logger = logging.getLogger(__name__)
@@ -66,16 +67,29 @@ def exists(self) -> bool:
6667
"""Check if mcp.json exists"""
6768
return os.path.exists(self.config_path)
6869

70+
@staticmethod
71+
def validate_server_name(name: str) -> None:
72+
"""
73+
Validate the server name.
74+
75+
The server name must only contain letters (a-z, A-Z), numbers (0-9), and hyphens (-).
76+
Raises a ValueError if the name is invalid.
77+
"""
78+
if not re.match(r'^[a-zA-Z0-9-]+$', name):
79+
raise ValueError(f'Invalid server name "{name}": only letters, numbers, and hyphens are allowed.')
80+
81+
6982
def _load_config(self) -> None:
7083
"""Load and process MCP configuration."""
7184
try:
7285
with open(self.config_path, "r") as f:
7386
self._raw_config = json.load(f)
7487

7588
servers_config = self._raw_config.get("servers", {})
76-
self._servers = {
77-
name: McpServer(name, config) for name, config in servers_config.items()
78-
}
89+
self._servers = {}
90+
for name in servers_config.keys():
91+
self.validate_server_name(name)
92+
self._servers[name] = McpServer(name, servers_config[name])
7993

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

0 commit comments

Comments
 (0)