refactor(agent): improve MCP server configuration handling in agent creation and update
This commit is contained in:
parent
bd659ecaff
commit
8f1fef71a5
@ -192,70 +192,78 @@ 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:
|
||||||
processed_servers = []
|
if config["mcp_servers"] is not None:
|
||||||
for server in config["mcp_servers"]:
|
processed_servers = []
|
||||||
# Convert server id to UUID if it's a string
|
for server in config["mcp_servers"]:
|
||||||
server_id = server["id"]
|
# Convert server id to UUID if it's a string
|
||||||
if isinstance(server_id, str):
|
server_id = server["id"]
|
||||||
server_id = uuid.UUID(server_id)
|
if isinstance(server_id, str):
|
||||||
|
server_id = uuid.UUID(server_id)
|
||||||
|
|
||||||
# Search for MCP server in the database
|
# Search for MCP server in the database
|
||||||
mcp_server = get_mcp_server(db, server_id)
|
mcp_server = get_mcp_server(db, server_id)
|
||||||
if not mcp_server:
|
if not mcp_server:
|
||||||
raise HTTPException(
|
|
||||||
status_code=400,
|
|
||||||
detail=f"MCP server not found: {server['id']}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check if all required environment variables are provided
|
|
||||||
for env_key, env_value in mcp_server.environments.items():
|
|
||||||
if env_key not in server.get("envs", {}):
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"Environment variable '{env_key}' not provided for MCP server {mcp_server.name}",
|
detail=f"MCP server not found: {server['id']}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add the processed server with its tools
|
# Check if all required environment variables are provided
|
||||||
processed_servers.append(
|
for env_key, env_value in mcp_server.environments.items():
|
||||||
{
|
if env_key not in server.get("envs", {}):
|
||||||
"id": str(server["id"]),
|
raise HTTPException(
|
||||||
"envs": server["envs"],
|
status_code=400,
|
||||||
"tools": server["tools"],
|
detail=f"Environment variable '{env_key}' not provided for MCP server {mcp_server.name}",
|
||||||
}
|
)
|
||||||
)
|
|
||||||
|
|
||||||
config["mcp_servers"] = processed_servers
|
# Add the processed server with its tools
|
||||||
|
processed_servers.append(
|
||||||
|
{
|
||||||
|
"id": str(server["id"]),
|
||||||
|
"envs": server["envs"],
|
||||||
|
"tools": server["tools"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
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:
|
||||||
processed_custom_servers = []
|
if config["custom_mcp_servers"] is not None:
|
||||||
for server in config["custom_mcp_servers"]:
|
processed_custom_servers = []
|
||||||
# Validate URL format
|
for server in config["custom_mcp_servers"]:
|
||||||
if not server.get("url"):
|
# Validate URL format
|
||||||
raise HTTPException(
|
if not server.get("url"):
|
||||||
status_code=400,
|
raise HTTPException(
|
||||||
detail="URL is required for custom MCP servers",
|
status_code=400,
|
||||||
|
detail="URL is required for custom MCP servers",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add the custom server
|
||||||
|
processed_custom_servers.append(
|
||||||
|
{"url": server["url"], "headers": server.get("headers", {})}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add the custom server
|
config["custom_mcp_servers"] = processed_custom_servers
|
||||||
processed_custom_servers.append(
|
else:
|
||||||
{"url": server["url"], "headers": server.get("headers", {})}
|
config["custom_mcp_servers"] = []
|
||||||
)
|
|
||||||
|
|
||||||
config["custom_mcp_servers"] = processed_custom_servers
|
|
||||||
|
|
||||||
# Process sub-agents
|
# Process sub-agents
|
||||||
if "sub_agents" in config:
|
if "sub_agents" in config:
|
||||||
config["sub_agents"] = [
|
if config["sub_agents"] is not None:
|
||||||
str(agent_id) for agent_id in config["sub_agents"]
|
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:
|
||||||
config["tools"] = [
|
if config["tools"] is not None:
|
||||||
{"id": str(tool["id"]), "envs": tool["envs"]}
|
config["tools"] = [
|
||||||
for tool in config["tools"]
|
{"id": str(tool["id"]), "envs": tool["envs"]}
|
||||||
]
|
for tool in config["tools"]
|
||||||
|
]
|
||||||
|
|
||||||
agent.config = config
|
agent.config = config
|
||||||
|
|
||||||
@ -392,70 +400,78 @@ async def update_agent(
|
|||||||
|
|
||||||
# Process MCP servers
|
# Process MCP servers
|
||||||
if "mcp_servers" in config:
|
if "mcp_servers" in config:
|
||||||
processed_servers = []
|
if config["mcp_servers"] is not None:
|
||||||
for server in config["mcp_servers"]:
|
processed_servers = []
|
||||||
# Convert server id to UUID if it's a string
|
for server in config["mcp_servers"]:
|
||||||
server_id = server["id"]
|
# Convert server id to UUID if it's a string
|
||||||
if isinstance(server_id, str):
|
server_id = server["id"]
|
||||||
server_id = uuid.UUID(server_id)
|
if isinstance(server_id, str):
|
||||||
|
server_id = uuid.UUID(server_id)
|
||||||
|
|
||||||
# Search for MCP server in the database
|
# Search for MCP server in the database
|
||||||
mcp_server = get_mcp_server(db, server_id)
|
mcp_server = get_mcp_server(db, server_id)
|
||||||
if not mcp_server:
|
if not mcp_server:
|
||||||
raise HTTPException(
|
|
||||||
status_code=400,
|
|
||||||
detail=f"MCP server not found: {server['id']}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check if all required environment variables are provided
|
|
||||||
for env_key, env_value in mcp_server.environments.items():
|
|
||||||
if env_key not in server.get("envs", {}):
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"Environment variable '{env_key}' not provided for MCP server {mcp_server.name}",
|
detail=f"MCP server not found: {server['id']}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add the processed server
|
# Check if all required environment variables are provided
|
||||||
processed_servers.append(
|
for env_key, env_value in mcp_server.environments.items():
|
||||||
{
|
if env_key not in server.get("envs", {}):
|
||||||
"id": str(server["id"]),
|
raise HTTPException(
|
||||||
"envs": server["envs"],
|
status_code=400,
|
||||||
"tools": server["tools"],
|
detail=f"Environment variable '{env_key}' not provided for MCP server {mcp_server.name}",
|
||||||
}
|
)
|
||||||
)
|
|
||||||
|
|
||||||
config["mcp_servers"] = processed_servers
|
# Add the processed server
|
||||||
|
processed_servers.append(
|
||||||
|
{
|
||||||
|
"id": str(server["id"]),
|
||||||
|
"envs": server["envs"],
|
||||||
|
"tools": server["tools"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
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:
|
||||||
processed_custom_servers = []
|
if config["custom_mcp_servers"] is not None:
|
||||||
for server in config["custom_mcp_servers"]:
|
processed_custom_servers = []
|
||||||
# Validate URL format
|
for server in config["custom_mcp_servers"]:
|
||||||
if not server.get("url"):
|
# Validate URL format
|
||||||
raise HTTPException(
|
if not server.get("url"):
|
||||||
status_code=400,
|
raise HTTPException(
|
||||||
detail="URL is required for custom MCP servers",
|
status_code=400,
|
||||||
|
detail="URL is required for custom MCP servers",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add the custom server
|
||||||
|
processed_custom_servers.append(
|
||||||
|
{"url": server["url"], "headers": server.get("headers", {})}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add the custom server
|
config["custom_mcp_servers"] = processed_custom_servers
|
||||||
processed_custom_servers.append(
|
else:
|
||||||
{"url": server["url"], "headers": server.get("headers", {})}
|
config["custom_mcp_servers"] = []
|
||||||
)
|
|
||||||
|
|
||||||
config["custom_mcp_servers"] = processed_custom_servers
|
|
||||||
|
|
||||||
# Process sub-agents
|
# Process sub-agents
|
||||||
if "sub_agents" in config:
|
if "sub_agents" in config:
|
||||||
config["sub_agents"] = [
|
if config["sub_agents"] is not None:
|
||||||
str(agent_id) for agent_id in config["sub_agents"]
|
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:
|
||||||
config["tools"] = [
|
if config["tools"] is not None:
|
||||||
{"id": str(tool["id"]), "envs": tool["envs"]}
|
config["tools"] = [
|
||||||
for tool in config["tools"]
|
{"id": str(tool["id"]), "envs": tool["envs"]}
|
||||||
]
|
for tool in config["tools"]
|
||||||
|
]
|
||||||
|
|
||||||
agent_data["config"] = config
|
agent_data["config"] = config
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user