refactor(agent): improve MCP server configuration handling in agent creation and update

This commit is contained in:
Davidson Gomes 2025-05-05 18:03:16 -03:00
parent bd659ecaff
commit 8f1fef71a5

View File

@ -192,6 +192,7 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent:
if isinstance(config, dict): if isinstance(config, dict):
# Process MCP servers # Process MCP servers
if "mcp_servers" in config: if "mcp_servers" in config:
if config["mcp_servers"] is not None:
processed_servers = [] processed_servers = []
for server in config["mcp_servers"]: for server in config["mcp_servers"]:
# Convert server id to UUID if it's a string # Convert server id to UUID if it's a string
@ -225,9 +226,12 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent:
) )
config["mcp_servers"] = processed_servers config["mcp_servers"] = processed_servers
else:
config["mcp_servers"] = []
# Process custom MCP servers # Process custom MCP servers
if "custom_mcp_servers" in config: if "custom_mcp_servers" in config:
if config["custom_mcp_servers"] is not None:
processed_custom_servers = [] processed_custom_servers = []
for server in config["custom_mcp_servers"]: for server in config["custom_mcp_servers"]:
# Validate URL format # Validate URL format
@ -243,15 +247,19 @@ async def create_agent(db: Session, agent: AgentCreate) -> Agent:
) )
config["custom_mcp_servers"] = processed_custom_servers config["custom_mcp_servers"] = processed_custom_servers
else:
config["custom_mcp_servers"] = []
# Process sub-agents # Process sub-agents
if "sub_agents" in config: if "sub_agents" in config:
if config["sub_agents"] is not None:
config["sub_agents"] = [ config["sub_agents"] = [
str(agent_id) for agent_id in config["sub_agents"] str(agent_id) for agent_id in config["sub_agents"]
] ]
# Process tools # Process tools
if "tools" in config: if "tools" in config:
if config["tools"] is not None:
config["tools"] = [ config["tools"] = [
{"id": str(tool["id"]), "envs": tool["envs"]} {"id": str(tool["id"]), "envs": tool["envs"]}
for tool in config["tools"] for tool in config["tools"]
@ -392,6 +400,7 @@ async def update_agent(
# Process MCP servers # Process MCP servers
if "mcp_servers" in config: if "mcp_servers" in config:
if config["mcp_servers"] is not None:
processed_servers = [] processed_servers = []
for server in config["mcp_servers"]: for server in config["mcp_servers"]:
# Convert server id to UUID if it's a string # Convert server id to UUID if it's a string
@ -425,9 +434,12 @@ async def update_agent(
) )
config["mcp_servers"] = processed_servers config["mcp_servers"] = processed_servers
else:
config["mcp_servers"] = []
# Process custom MCP servers # Process custom MCP servers
if "custom_mcp_servers" in config: if "custom_mcp_servers" in config:
if config["custom_mcp_servers"] is not None:
processed_custom_servers = [] processed_custom_servers = []
for server in config["custom_mcp_servers"]: for server in config["custom_mcp_servers"]:
# Validate URL format # Validate URL format
@ -443,15 +455,19 @@ async def update_agent(
) )
config["custom_mcp_servers"] = processed_custom_servers config["custom_mcp_servers"] = processed_custom_servers
else:
config["custom_mcp_servers"] = []
# Process sub-agents # Process sub-agents
if "sub_agents" in config: if "sub_agents" in config:
if config["sub_agents"] is not None:
config["sub_agents"] = [ config["sub_agents"] = [
str(agent_id) for agent_id in config["sub_agents"] str(agent_id) for agent_id in config["sub_agents"]
] ]
# Process tools # Process tools
if "tools" in config: if "tools" in config:
if config["tools"] is not None:
config["tools"] = [ config["tools"] = [
{"id": str(tool["id"]), "envs": tool["envs"]} {"id": str(tool["id"]), "envs": tool["envs"]}
for tool in config["tools"] for tool in config["tools"]