2026-04-16 13:03:29 +05:30
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
|
|
|
|
from api.db import db_client
|
2026-04-21 07:56:16 +05:30
|
|
|
from api.mcp_server.auth import authenticate_mcp_request
|
2026-05-23 14:45:50 +05:30
|
|
|
from api.mcp_server.tools._workflow_projection import project_workflow_to_sdk_view
|
2026-04-21 07:56:16 +05:30
|
|
|
from api.mcp_server.tracing import traced_tool
|
2026-05-23 14:45:50 +05:30
|
|
|
from api.mcp_server.ts_bridge import TsBridgeError
|
2026-04-16 13:03:29 +05:30
|
|
|
|
|
|
|
|
|
2026-04-21 07:56:16 +05:30
|
|
|
@traced_tool
|
|
|
|
|
async def list_workflows(status: str | None = "active") -> list[dict]:
|
2026-04-16 13:03:29 +05:30
|
|
|
"""List agents (workflows) in the caller's organization.
|
|
|
|
|
|
|
|
|
|
Returns id, name, status, and created_at for each agent. Use
|
2026-05-23 14:45:50 +05:30
|
|
|
`get_workflow` to fetch a single agent's current SDK view and
|
|
|
|
|
metadata. Defaults to active agents; pass `status="archived"` to
|
|
|
|
|
list archived agents, or `status=None` to list all.
|
2026-04-16 13:03:29 +05:30
|
|
|
"""
|
|
|
|
|
user = await authenticate_mcp_request()
|
|
|
|
|
workflows = await db_client.get_all_workflows_for_listing(
|
|
|
|
|
organization_id=user.selected_organization_id,
|
|
|
|
|
status=status,
|
|
|
|
|
)
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"id": w.id,
|
|
|
|
|
"name": w.name,
|
|
|
|
|
"status": w.status,
|
|
|
|
|
"created_at": w.created_at.isoformat() if w.created_at else None,
|
|
|
|
|
}
|
|
|
|
|
for w in workflows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-04-21 07:56:16 +05:30
|
|
|
@traced_tool
|
2026-04-16 13:03:29 +05:30
|
|
|
async def get_workflow(workflow_id: int) -> dict:
|
2026-05-23 14:45:50 +05:30
|
|
|
"""Fetch a single agent by id, projected into the SDK code view.
|
|
|
|
|
|
|
|
|
|
Output shape:
|
|
|
|
|
{"id": int, "name": str, "status": str, "version": "draft" | "published" | "legacy", "version_number": int | None, "code": "<TS source>"}
|
|
|
|
|
"""
|
2026-04-16 13:03:29 +05:30
|
|
|
user = await authenticate_mcp_request()
|
2026-04-17 12:01:57 +05:30
|
|
|
workflow = await db_client.get_workflow(
|
|
|
|
|
workflow_id, organization_id=user.selected_organization_id
|
|
|
|
|
)
|
|
|
|
|
if not workflow:
|
2026-04-16 13:03:29 +05:30
|
|
|
raise HTTPException(status_code=404, detail=f"Workflow {workflow_id} not found")
|
|
|
|
|
|
2026-05-23 14:45:50 +05:30
|
|
|
try:
|
|
|
|
|
view = await project_workflow_to_sdk_view(workflow)
|
|
|
|
|
except TsBridgeError as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"Failed to generate code: {e}")
|
|
|
|
|
|
2026-04-16 13:03:29 +05:30
|
|
|
return {
|
|
|
|
|
"id": workflow.id,
|
2026-05-23 14:45:50 +05:30
|
|
|
"name": view["name"],
|
2026-04-16 13:03:29 +05:30
|
|
|
"status": workflow.status,
|
2026-05-23 14:45:50 +05:30
|
|
|
"version": view["version"],
|
|
|
|
|
"version_number": view["version_number"],
|
|
|
|
|
"code": view["code"],
|
2026-04-16 13:03:29 +05:30
|
|
|
}
|