"""Task-related MCP tools.""" from sqlalchemy.orm import Session from sqlalchemy import desc from app.database import SessionLocal from app.models import Task, TaskStatus, Project, LogEntry def create_task(project_id: str, title: str, description: str = None, assigned_to: str = None) -> dict: """ Create a new task in a project. Args: project_id: ID of the project title: Task title description: Optional task description assigned_to: Optional user ID to assign the task to Returns: Created task data """ db: Session = SessionLocal() try: # Verify project exists project = db.query(Project).filter(Project.id == project_id).first() if not project: return {"error": f"Project {project_id} not found"} # Create task task = Task( project_id=project_id, title=title, description=description, assigned_to=assigned_to, status=TaskStatus.todo ) db.add(task) db.commit() return { "id": task.id, "project_id": task.project_id, "title": task.title, "description": task.description, "status": task.status.value, "assigned_to": task.assigned_to, "working_by": task.working_by, "created_at": task.created_at.isoformat() } finally: db.close() def list_tasks(project_id: str, status: str = None) -> dict: """ List all tasks for a project, optionally filtered by status. Args: project_id: ID of the project status: Optional status filter ("todo", "in_progress", "done") Returns: List of tasks """ db: Session = SessionLocal() try: # Build query query = db.query(Task).filter(Task.project_id == project_id) # Apply status filter if provided if status: try: status_enum = TaskStatus(status) query = query.filter(Task.status == status_enum) except ValueError: return {"error": f"Invalid status: {status}. Must be one of: todo, in_progress, done"} # Order by created_at descending tasks = query.order_by(desc(Task.created_at)).all() return { "tasks": [ { "id": task.id, "project_id": task.project_id, "title": task.title, "description": task.description, "status": task.status.value, "assigned_to": task.assigned_to, "working_by": task.working_by, "created_at": task.created_at.isoformat(), "completed_at": task.completed_at.isoformat() if task.completed_at else None } for task in tasks ] } finally: db.close() def list_activity(project_id: str, limit: int = 20) -> dict: """ Get recent activity (log entries) for a project. Args: project_id: ID of the project limit: Maximum number of entries to return (default 20) Returns: List of recent log entries """ db: Session = SessionLocal() try: # Query log entries ordered by most recent entries = ( db.query(LogEntry) .filter(LogEntry.project_id == project_id) .order_by(desc(LogEntry.created_at)) .limit(limit) .all() ) return { "activity": [ { "id": entry.id, "project_id": entry.project_id, "task_id": entry.task_id, "user_id": entry.user_id, "actor_type": entry.actor_type.value, "action_type": entry.action_type.value, "raw_input": entry.raw_input, "generated_doc": entry.generated_doc, "tags": entry.tags, "created_at": entry.created_at.isoformat() } for entry in entries ] } finally: db.close()