Skip to content

Commit bb37954

Browse files
weicaniehusseinmozannarcheng-tan
authored
feat(teammanager): merge mcp agent configs from config file and frontend settings (#320)
Co-authored-by: Hussein Mozannar <[email protected]> Co-authored-by: cheng-tan <[email protected]>
1 parent cef1620 commit bb37954

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/magentic_ui/backend/teammanager/teammanager.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def prepare_run_paths(
9797
internal_run_dir = internal_workspace_root / Path(run_suffix)
9898
external_run_dir = external_workspace_root / Path(run_suffix)
9999
# Can only make dir on internal, as it is what a potential docker container sees.
100-
# TO-ANSWER: why?
100+
# TO-ANSWER: why ?
101101
logger.info(f"Creating run dirs: {internal_run_dir} and {external_run_dir}")
102102
if self.inside_docker:
103103
internal_run_dir.mkdir(parents=True, exist_ok=True)
@@ -211,6 +211,69 @@ async def _create_team(
211211
]
212212
)
213213

214+
# Logic here: first, we see if the config file passed to magentic-ui has valid MCP agent configuration
215+
# If valid: the configuration file takes precedent over the UI settings to configure MCP agent for team
216+
# If invalid: we disregard in the configuration file and use the UI settings to configure MCP agent for team
217+
218+
# Get mcp_agent_configs from configuration file
219+
mcp_agent_config_from_config_file: List[Dict[str, Any]] = self.config.get(
220+
"mcp_agent_configs", []
221+
)
222+
# Get mcp_agent_configs from frontend settings
223+
settings_mcp_configs = settings_config.get("mcp_agent_configs", [])
224+
225+
# Verify the MCP agent configuration in the configuration file
226+
def validate_mcp_config(mcp_config: Dict[str, Any]) -> bool:
227+
"""
228+
Verify the MCP agent configuration is valid
229+
Check if the mcp_servers field is a list and has at least one element, and name and description are alpha-numeric
230+
"""
231+
mcp_servers: List[Dict[str, Any]] = mcp_config.get("mcp_servers", [])
232+
if not isinstance(mcp_servers, list) or len(mcp_servers) == 0:
233+
return False
234+
235+
agent_name: str | None = mcp_config.get("name")
236+
agent_description: str | None = mcp_config.get("description")
237+
if (
238+
not isinstance(agent_name, str)
239+
or not agent_name.isalnum()
240+
or not isinstance(agent_description, str)
241+
):
242+
return False
243+
244+
for server in mcp_servers:
245+
if not isinstance(server, dict):
246+
return False
247+
server_name = server.get("server_name")
248+
if not isinstance(server_name, str) or not server_name.isalnum():
249+
return False
250+
251+
return True
252+
253+
# If there are MCP configurations in config file, validate and use them
254+
if mcp_agent_config_from_config_file:
255+
# Verify the MCP agent configuration in the configuration file
256+
all_valid = True
257+
for config in mcp_agent_config_from_config_file:
258+
if validate_mcp_config(config):
259+
pass
260+
else:
261+
all_valid = False
262+
break
263+
264+
if not all_valid:
265+
logger.warning(
266+
"MCP agent configurations in config file are invalid, falling back to frontend settings."
267+
)
268+
settings_config["mcp_agent_configs"] = settings_mcp_configs
269+
else:
270+
settings_config["mcp_agent_configs"] = (
271+
mcp_agent_config_from_config_file
272+
)
273+
else:
274+
# If no MCP configurations in config file, use frontend settings
275+
settings_config["mcp_agent_configs"] = settings_mcp_configs
276+
214277
# Common configuration parameters
215278
config_params = {
216279
**settings_config, # type: ignore,

0 commit comments

Comments
 (0)