Team Ai
Apppublic

programmingwithabdullah/todo-aipowered

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
add_task.py70 linesDownload Raw Back to tools
1"""2MCP Tool: add_task3Purpose: Create new task for the user4"""5 6from pydantic import Field7from typing import Dict, Any8from database import get_session_context9from sqlmodel import Session10 11 12async def add_task(13    user_id: str = Field(description="User's ID from JWT context"),14    title: str = Field(15        description="Task title (required, max 200 chars)", min_length=1, max_length=20016    ),17    description: str = Field(18        description="Optional task description (max 1000 chars)",19        max_length=1000,20        default="",21    ),22) -> Dict[str, Any]:23    """24    Create a new task for the user.25 26    Args:27        user_id: User's ID from JWT context28        title: Task title (required, max 200 chars)29        description: Optional task description (max 1000 chars)30 31    Returns:32        dict: {task_id, title, status, message}33    """34    try:35        # Validate inputs for empty title36        if not title or len(title.strip()) == 0:37            return {"error": "Title cannot be empty", "status": "error"}38 39        # Use the existing database session context40        with get_session_context() as db:41            # Import TaskService42            from services.task_service import TaskService43 44            task_service = TaskService()45 46            # Prepare task data47            from schemas.task_schemas import TaskCreate48 49            task_create = TaskCreate(50                title=title.strip(), description=description.strip()51            )52 53            # Call existing TaskService54            task = task_service.create_task(user_id=user_id, task=task_create, db=db)55 56            # Return MCP-compliant response57            return {58                "task_id": task.id,59                "title": task.title,60                "status": "created",61                "message": f"Task '{task.title}' created successfully",62            }63 64    except Exception as e:65        from sqlalchemy.exc import SQLAlchemyError66 67        if isinstance(e, SQLAlchemyError):68            return {"error": f"Database error: {str(e)}", "status": "error"}69        return {"error": f"Unexpected error: {str(e)}", "status": "error"}70