File size: 13,155 Bytes
5be081e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# backend/src/mcp/tools/task_tools.py
"""
MCP Tools for AI Chatbot Task Management

Owner: MCP Engineer
Purpose: Tool definitions for AI agent task operations
Architecture: FastMCP tools with async database operations

Constitutional Constraints:
- REQUIRED: All tools enforce user_id isolation (Principle IV)
- REQUIRED: Tools are the ONLY way AI accesses data (Principle VII)
- REQUIRED: Ownership double-check before modifications (mcp-ownership-verification)

Note: Core functions are defined separately from MCP decorators to allow
direct calling from the chat endpoint while also exposing them via MCP.
"""

import logging
from typing import Annotated, Optional
from uuid import UUID
from fastmcp import FastMCP
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, text

from src.db.connection import get_session_factory
from src.db.models import Todo

logger = logging.getLogger("mcp.tools.task_tools")

# Initialize FastMCP server
mcp = FastMCP("task-tools")


async def find_task_by_short_id(
    short_id: str,
    user_id: str,
    session: AsyncSession
) -> Todo | None:
    """
    Find a task by short ID (first 8 chars of UUID).

    Uses raw SQL since SQLAlchemy UUID cast doesn't work well.
    """
    # Use raw SQL for the LIKE query on UUID
    query = text("""
        SELECT id, user_id, title, description, is_completed, created_at, updated_at
        FROM todo
        WHERE user_id = :user_id AND CAST(id AS TEXT) LIKE :pattern
        LIMIT 1
    """)
    result = await session.execute(query, {"user_id": user_id, "pattern": f"{short_id}%"})
    row = result.fetchone()

    if row:
        # Fetch the actual ORM object
        statement = select(Todo).where(Todo.id == row[0], Todo.user_id == user_id)
        orm_result = await session.execute(statement)
        return orm_result.scalar_one_or_none()
    return None


async def verify_task_ownership(
    task_id: str,
    user_id: str,
    session: AsyncSession
) -> Todo | None:
    """
    Double-check ownership before modification.

    Per mcp-ownership-verification skill:
    - Always verify task.user_id matches the authenticated user_id
    - Return None if task doesn't exist OR user doesn't own it
    - Error message must not reveal whether task exists for other users

    Args:
        task_id: UUID string of the task
        user_id: Authenticated user's ID from JWT sub claim
        session: Active database session

    Returns:
        Todo if found and owned by user, None otherwise
    """
    try:
        uuid_id = UUID(task_id)
    except ValueError:
        logger.warning(f"Invalid UUID format for task_id: {task_id}")
        return None

    statement = select(Todo).where(
        Todo.id == uuid_id,
        Todo.user_id == user_id
    )
    result = await session.execute(statement)
    return result.scalar_one_or_none()


# ============================================================================
# Core tool functions (directly callable)
# ============================================================================

async def add_task_impl(
    user_id: str,
    title: str,
    description: Optional[str] = None
) -> str:
    """Create a new task for the user."""
    # Validate title
    if not title or not title.strip():
        return "Error: Title is required and cannot be empty."

    if len(title) > 200:
        return "Error: Title must be 200 characters or less."

    session_factory = get_session_factory()
    async with session_factory() as session:
        try:
            task = Todo(
                user_id=user_id,
                title=title.strip(),
                description=description,
                is_completed=False
            )
            session.add(task)
            await session.commit()
            await session.refresh(task)

            logger.info(f"Task created: {task.id} for user {user_id}")
            return f"Task '{task.title}' added with ID {task.id}."
        except Exception as e:
            await session.rollback()
            logger.error(f"Failed to create task: {e}")
            return "Error: Failed to create task. Please try again."


async def list_tasks_impl(
    user_id: str,
    status: str = "all"
) -> str:
    """List the user's tasks with optional status filter."""
    # Validate status
    valid_statuses = ["all", "pending", "completed"]
    if status not in valid_statuses:
        return f"Error: Invalid status '{status}'. Use 'all', 'pending', or 'completed'."

    session_factory = get_session_factory()
    async with session_factory() as session:
        try:
            statement = select(Todo).where(Todo.user_id == user_id)

            if status == "pending":
                statement = statement.where(Todo.is_completed == False)
            elif status == "completed":
                statement = statement.where(Todo.is_completed == True)

            statement = statement.order_by(Todo.created_at.desc())
            result = await session.execute(statement)
            tasks = result.scalars().all()

            if not tasks:
                return "You have no tasks."

            status_label = f"{status} " if status != "all" else ""
            lines = [f"Your {status_label}tasks:"]
            for task in tasks:
                icon = "[x]" if task.is_completed else "[ ]"
                # Show full UUID for clarity
                lines.append(f"- ID: {task.id} {icon} {task.title}")

            logger.info(f"Listed {len(tasks)} tasks for user {user_id}")
            return "\n".join(lines)
        except Exception as e:
            logger.error(f"Failed to list tasks: {e}")
            return "Error: Failed to retrieve tasks. Please try again."


async def complete_task_impl(
    user_id: str,
    task_id: str
) -> str:
    """Mark a task as complete. Verifies ownership before modification."""
    # Validate task_id is provided
    if not task_id or not task_id.strip():
        return "Error: Task ID is required. Please use 'show my tasks' to see your tasks with their IDs."

    task_id = task_id.strip()

    session_factory = get_session_factory()
    async with session_factory() as session:
        try:
            # Handle short ID (first 8 chars) or full UUID
            if len(task_id) == 8:
                logger.info(f"Using short ID search for task_id: {task_id}")
                task = await find_task_by_short_id(task_id, user_id, session)
            else:
                logger.info(f"Using full UUID search for task_id: {task_id}")
                task = await verify_task_ownership(task_id, user_id, session)

            if not task:
                return f"Error: Task with ID '{task_id}' not found. Please check the ID and try again."

            if task.is_completed:
                return f"Task '{task.title}' is already complete."

            task.is_completed = True
            await session.commit()

            logger.info(f"Task completed: {task.id} for user {user_id}")
            return f"Task '{task.title}' marked as complete."
        except Exception as e:
            await session.rollback()
            logger.error(f"Failed to complete task: {e}")
            return "Error: Failed to complete task. Please try again."


async def update_task_impl(
    user_id: str,
    task_id: str,
    title: Optional[str] = None,
    description: Optional[str] = None
) -> str:
    """Update a task's title or description. Verifies ownership before modification."""
    logger.info(f"update_task_impl called: user_id={user_id}, task_id={task_id}, title={title}, description={description}")

    # Validate task_id is provided
    if not task_id or not task_id.strip():
        return "Error: Task ID is required. Please use 'show my tasks' to see your tasks with their IDs."

    task_id = task_id.strip()

    if not title and description is None:
        return "Error: Provide at least a new title or description to update."

    session_factory = get_session_factory()
    async with session_factory() as session:
        try:
            # Handle short ID (first 8 chars) or full UUID
            if len(task_id) == 8:
                logger.info(f"Using short ID search for task_id: {task_id}")
                task = await find_task_by_short_id(task_id, user_id, session)
            else:
                logger.info(f"Using full UUID search for task_id: {task_id}")
                task = await verify_task_ownership(task_id, user_id, session)

            if not task:
                logger.warning(f"Task not found or unauthorized: task_id={task_id}, user_id={user_id}")
                return f"Error: Task with ID '{task_id}' not found. Please check the ID and try again."

            old_title = task.title
            if title:
                if len(title) > 200:
                    return "Error: Title must be 200 characters or less."
                task.title = title.strip()
            if description is not None:
                task.description = description

            await session.commit()

            logger.info(f"Task updated: {task.id} for user {user_id}")
            return f"Task updated: '{old_title}' -> '{task.title}'"
        except Exception as e:
            await session.rollback()
            logger.error(f"Failed to update task: {e}")
            return "Error: Failed to update task. Please try again."


async def delete_task_impl(
    user_id: str,
    task_id: str
) -> str:
    """Delete a task. Verifies ownership before deletion."""
    # Validate task_id is provided
    if not task_id or not task_id.strip():
        return "Error: Task ID is required. Please use 'show my tasks' to see your tasks with their IDs."

    task_id = task_id.strip()

    session_factory = get_session_factory()
    async with session_factory() as session:
        try:
            # Handle short ID (first 8 chars) or full UUID
            if len(task_id) == 8:
                logger.info(f"Using short ID search for task_id: {task_id}")
                task = await find_task_by_short_id(task_id, user_id, session)
            else:
                logger.info(f"Using full UUID search for task_id: {task_id}")
                task = await verify_task_ownership(task_id, user_id, session)

            if not task:
                return f"Error: Task with ID '{task_id}' not found. Please check the ID and try again."

            task_title = task.title
            await session.delete(task)
            await session.commit()

            logger.info(f"Task deleted: {task_id} for user {user_id}")
            return f"Task '{task_title}' has been deleted."
        except Exception as e:
            await session.rollback()
            logger.error(f"Failed to delete task: {e}")
            return "Error: Failed to delete task. Please try again."


# ============================================================================
# MCP-decorated wrappers (for MCP server exposure)
# ============================================================================

@mcp.tool()
async def add_task(
    user_id: Annotated[str, "The authenticated user's ID from JWT sub claim"],
    title: Annotated[str, "Title of the task (max 200 characters)"],
    description: Annotated[Optional[str], "Optional description of the task"] = None
) -> str:
    """Create a new task for the user."""
    return await add_task_impl(user_id, title, description)


@mcp.tool()
async def list_tasks(
    user_id: Annotated[str, "The authenticated user's ID from JWT sub claim"],
    status: Annotated[str, "Filter by status: 'all', 'pending', or 'completed'"] = "all"
) -> str:
    """List the user's tasks with optional status filter."""
    return await list_tasks_impl(user_id, status)


@mcp.tool()
async def complete_task(
    user_id: Annotated[str, "The authenticated user's ID from JWT sub claim"],
    task_id: Annotated[str, "The ID (or first 8 characters) of the task to mark as complete"]
) -> str:
    """Mark a task as complete. Verifies ownership before modification."""
    return await complete_task_impl(user_id, task_id)


@mcp.tool()
async def update_task(
    user_id: Annotated[str, "The authenticated user's ID from JWT sub claim"],
    task_id: Annotated[str, "The ID (or first 8 characters) of the task to update"],
    title: Annotated[Optional[str], "New title for the task"] = None,
    description: Annotated[Optional[str], "New description for the task"] = None
) -> str:
    """Update a task's title or description. Verifies ownership before modification."""
    return await update_task_impl(user_id, task_id, title, description)


@mcp.tool()
async def delete_task(
    user_id: Annotated[str, "The authenticated user's ID from JWT sub claim"],
    task_id: Annotated[str, "The ID (or first 8 characters) of the task to delete"]
) -> str:
    """Delete a task. Verifies ownership before deletion."""
    return await delete_task_impl(user_id, task_id)


__all__ = [
    "mcp",
    "add_task", "list_tasks", "complete_task", "update_task", "delete_task",
    "add_task_impl", "list_tasks_impl", "complete_task_impl", "update_task_impl", "delete_task_impl",
    "verify_task_ownership"
]