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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
"""
MCP Server for task management operations (Phase III).

This module implements a proper MCP server using the FastMCP SDK.
The server exposes task operations as MCP tools that can be called by AI agents.
All operations are performed via HTTP calls to the backend API, not direct database access.

MCP Tools provided:
- add_task: Create a new task for a user
- list_tasks: Retrieve tasks with optional filtering
- complete_task: Mark a task as complete
- delete_task: Remove a task from the database
- update_task: Modify task title or description
- set_priority: Update task priority level
- list_tasks_by_priority: Filter tasks by priority

Architecture:
- MCP Server runs as a separate process (not inside agent)
- Agent connects via MCPServerStdio transport
- Tools use @mcp.tool() decorator (not @function_tool)
- All operations use HTTP API calls to backend, not direct database access
"""

import os
import re
from typing import Literal, Optional
import httpx
from mcp.server.fastmcp import FastMCP

# Create MCP server instance
mcp = FastMCP("task-management-server")

# Get backend base URL from environment, default to local development
BACKEND_BASE_URL = os.getenv("BACKEND_BASE_URL", "http://127.0.0.1:8000")


def detect_priority_from_text(text: str) -> str:
    """
    Detect priority level from user input text using NLP patterns.

    Args:
        text: User input text (task title/description)

    Returns:
        str: Detected priority level ("low", "medium", "high") or "medium" if not detected

    Examples:
        >>> detect_priority_from_text("Create HIGH priority task to buy milk")
        "high"
        >>> detect_priority_from_text("Add a task")
        "medium"
        >>> detect_priority_from_text("This is URGENT")
        "high"
    """
    text_lower = text.lower()

    # High priority patterns
    high_priority_patterns = [
        r'\bhigh\s*priority\b',
        r'\burgent\b',
        r'\bcritical\b',
        r'\bimportant\b',
        r'\basap\b',
        r'\bhigh\b',
    ]

    # Low priority patterns
    low_priority_patterns = [
        r'\blow\s*priority\b',
        r'\bminor\b',
        r'\boptional\b',
        r'\bwhen\s*you\s*have\s*time\b',
        r'low',
    ]

    # Check for high priority first (more specific)
    for pattern in high_priority_patterns:
        if re.search(pattern, text_lower):
            return "high"

    # Check for low priority
    for pattern in low_priority_patterns:
        if re.search(pattern, text_lower):
            return "low"

    # Check for medium/normal priority patterns
    if re.search(r'\bmedium\b|\bnormal\b', text_lower):
        return "medium"

    # Default to medium if no pattern matches
    return "medium"


@mcp.tool()
async def add_task(
    user_id: str,
    title: str,
    description: Optional[str] = None,
    priority: Optional[str] = None,
) -> dict:
    """
    Create a new task for a user via HTTP API call.

    MCP Tool Contract:
    - Purpose: Add a task to user's todo list
    - Stateless: All state managed by backend API
    - User Isolation: Enforced via user_id parameter in API
    - Priority Detection: Extracts priority from title/description if not provided

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        title: Task title (required, max 200 characters)
        description: Task description (optional, max 1000 characters)
        priority: Task priority level (optional: "low", "medium", "high")
            - If not provided, automatically detects from title + description

    Returns:
        dict: Task creation result from backend API
            - task_id (int): Created task ID
            - status (str): "created"
            - title (str): Task title
            - priority (str): Assigned priority level

    Example:
        >>> add_task(user_id="user-123", title="Create HIGH priority task to buy milk")
        {"task_id": 42, "status": "created", "title": "...", "priority": "high"}
        >>> add_task(user_id="user-123", title="Buy groceries", priority="high")
        {"task_id": 43, "status": "created", "title": "...", "priority": "high"}
    """
    # Detect priority from title and description if not provided
    if priority is None:
        # Combine title and description for priority detection
        combined_text = f"{title} {description or ''}"
        priority = detect_priority_from_text(combined_text)
    else:
        # Validate priority value
        priority = priority.lower()
        if priority not in ["low", "medium", "high"]:
            priority = "medium"

    # Prepare the payload for the backend API
    payload = {
        "title": title,
        "description": description,
        "priority": priority,
        "completed": False,  # New tasks are not completed by default
    }

    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.post(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks",
                json=payload,
                headers={"Content-Type": "application/json"}
            )

            if response.status_code == 200:
                result = response.json()
                return {
                    "task_id": result.get("id"),
                    "status": "created",
                    "title": result.get("title"),
                    "priority": result.get("priority"),
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to create task: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to create task",
                "details": str(e)
            }


@mcp.tool()
async def list_tasks(
    user_id: str,
    status: Literal["all", "pending", "completed"] = "all",
) -> dict:
    """
    Retrieve tasks from user's todo list via HTTP API call.

    MCP Tool Contract:
    - Purpose: List tasks with optional status filtering
    - Stateless: Backend handles database queries
    - User Isolation: Enforced via user_id parameter in API

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        status: Filter by completion status (default: "all")
            - "all": All tasks
            - "pending": Incomplete tasks only
            - "completed": Completed tasks only

    Returns:
        dict: Task list result from backend API
            - tasks (list): Array of task objects
                - id (int): Task ID
                - title (str): Task title
                - description (str|None): Task description
                - completed (bool): Completion status
                - priority (str): Priority level
                - created_at (str): ISO 8601 timestamp
            - count (int): Total number of tasks returned

    Example:
        >>> list_tasks(user_id="user-123", status="pending")
        {
            "tasks": [
                {"id": 1, "title": "Buy groceries", "completed": False, ...},
                {"id": 2, "title": "Call dentist", "completed": False, ...}
            ],
            "count": 2
        }
    """
    # Build query parameters
    params = {"status": status} if status != "all" else {}

    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks",
                params=params
            )

            if response.status_code == 200:
                result = response.json()
                tasks = result.get("tasks", [])

                # Convert tasks to expected format
                task_list = [
                    {
                        "id": task.get("id"),
                        "title": task.get("title"),
                        "description": task.get("description"),
                        "completed": task.get("completed", False),
                        "priority": task.get("priority"),
                        "created_at": task.get("created_at"),
                    }
                    for task in tasks
                ]

                return {
                    "tasks": task_list,
                    "count": len(task_list),
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to list tasks: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to list tasks",
                "details": str(e)
            }


@mcp.tool()
async def complete_task(
    user_id: str,
    task_id: int,
) -> dict:
    """
    Mark a task as complete via HTTP API call.

    MCP Tool Contract:
    - Purpose: Toggle task completion status to completed
    - Stateless: Updates managed by backend API
    - User Isolation: Enforced via user_id parameter in API

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        task_id: Task ID to mark as complete

    Returns:
        dict: Task completion result from backend API
            - task_id (int): Updated task ID
            - status (str): "completed"
            - title (str): Task title

    Example:
        >>> complete_task(user_id="user-123", task_id=3)
        {"task_id": 3, "status": "completed", "title": "Call dentist"}
    """
    # Prepare the payload for toggling completion
    payload = {
        "completed": True
    }

    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.put(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks/{task_id}",
                json=payload,
                headers={"Content-Type": "application/json"}
            )

            if response.status_code == 200:
                result = response.json()
                return {
                    "task_id": result.get("id"),
                    "status": "completed",
                    "title": result.get("title"),
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to complete task: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to complete task",
                "details": str(e)
            }


@mcp.tool()
async def delete_task(
    user_id: str,
    task_id: int,
) -> dict:
    """
    Remove a task from the todo list via HTTP API call.

    MCP Tool Contract:
    - Purpose: Permanently delete task via backend API
    - Stateless: Deletion handled by backend
    - User Isolation: Enforced via user_id parameter in API

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        task_id: Task ID to delete

    Returns:
        dict: Task deletion result from backend API
            - task_id (int): Deleted task ID
            - status (str): "deleted"
            - title (str): Task title (from pre-deletion state)

    Example:
        >>> delete_task(user_id="user-123", task_id=2)
        {"task_id": 2, "status": "deleted", "title": "Old reminder"}
    """
    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.delete(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks/{task_id}"
            )

            if response.status_code == 200:
                result = response.json()
                return {
                    "task_id": task_id,
                    "status": "deleted",
                    "title": result.get("title", f"Task {task_id}"),
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to delete task: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to delete task",
                "details": str(e)
            }


@mcp.tool()
async def update_task(
    user_id: str,
    task_id: int,
    title: Optional[str] = None,
    description: Optional[str] = None,
    priority: Optional[str] = None,
) -> dict:
    """
    Modify task details via HTTP API call.

    MCP Tool Contract:
    - Purpose: Update task details
    - Stateless: Updates handled by backend API
    - User Isolation: Enforced via user_id parameter in API
    - Partial Updates: At least one field must be provided

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        task_id: Task ID to update
        title: New task title (optional, max 200 characters)
        description: New task description (optional, max 1000 characters)
        priority: New task priority (optional: "low", "medium", "high")

    Returns:
        dict: Task update result from backend API
            - task_id (int): Updated task ID
            - status (str): "updated"
            - title (str): Updated task title
            - priority (str): Updated priority level

    Example:
        >>> update_task(user_id="user-123", task_id=1, title="Buy groceries and fruits", priority="high")
        {"task_id": 1, "status": "updated", "title": "...", "priority": "high"}
    """
    # Validate: at least one field must be provided
    if title is None and description is None and priority is None:
        return {
            "error": "At least one of 'title', 'description', or 'priority' must be provided"
        }

    # Prepare the payload for the update
    payload = {}
    if title is not None:
        payload["title"] = title
    if description is not None:
        payload["description"] = description
    if priority is not None:
        payload["priority"] = priority

    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.put(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks/{task_id}",
                json=payload,
                headers={"Content-Type": "application/json"}
            )

            if response.status_code == 200:
                result = response.json()
                return {
                    "task_id": result.get("id"),
                    "status": "updated",
                    "title": result.get("title"),
                    "priority": result.get("priority"),
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to update task: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to update task",
                "details": str(e)
            }


@mcp.tool()
async def set_priority(
    user_id: str,
    task_id: int,
    priority: str,
) -> dict:
    """
    Set or update a task's priority level via HTTP API call.

    MCP Tool Contract:
    - Purpose: Update task priority level
    - Stateless: Updates handled by backend API
    - User Isolation: Enforced via user_id parameter in API

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        task_id: Task ID to update
        priority: New priority level ("low", "medium", "high")

    Returns:
        dict: Priority update result from backend API
            - task_id (int): Updated task ID
            - status (str): "updated"
            - priority (str): New priority level
            - title (str): Task title

    Example:
        >>> set_priority(user_id="user-123", task_id=3, priority="high")
        {"task_id": 3, "status": "updated", "priority": "high", "title": "Call dentist"}
    """
    # Validate priority value
    priority = priority.lower()
    if priority not in ["low", "medium", "high"]:
        return {
            "error": "Priority must be one of: 'low', 'medium', 'high'"
        }

    # Prepare the payload for the update
    payload = {
        "priority": priority
    }

    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.put(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks/{task_id}",
                json=payload,
                headers={"Content-Type": "application/json"}
            )

            if response.status_code == 200:
                result = response.json()
                return {
                    "task_id": result.get("id"),
                    "status": "updated",
                    "priority": result.get("priority"),
                    "title": result.get("title"),
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to update priority: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to update priority",
                "details": str(e)
            }


@mcp.tool()
async def list_tasks_by_priority(
    user_id: str,
    priority: str,
    status: Literal["all", "pending", "completed"] = "all",
) -> dict:
    """
    Retrieve tasks filtered by priority level via HTTP API call.

    MCP Tool Contract:
    - Purpose: List tasks filtered by priority and optional completion status
    - Stateless: Backend handles database queries
    - User Isolation: Enforced via user_id parameter in API

    Args:
        user_id: User's unique identifier (string UUID from Better Auth)
        priority: Priority level to filter ("low", "medium", "high")
        status: Additional filter by completion status (default: "all")
            - "all": All tasks at this priority
            - "pending": Incomplete tasks only
            - "completed": Completed tasks only

    Returns:
        dict: Filtered task list result from backend API
            - tasks (list): Array of task objects matching priority
                - id (int): Task ID
                - title (str): Task title
                - priority (str): Priority level
                - completed (bool): Completion status
                - description (str|None): Task description
                - created_at (str): ISO 8601 timestamp
            - count (int): Total number of tasks returned
            - priority (str): Filter priority level
            - status (str): Filter status

    Example:
        >>> list_tasks_by_priority(user_id="user-123", priority="high", status="pending")
        {
            "tasks": [
                {"id": 1, "title": "Call dentist", "priority": "high", "completed": False, ...},
                {"id": 3, "title": "Fix bug", "priority": "high", "completed": False, ...}
            ],
            "count": 2,
            "priority": "high",
            "status": "pending"
        }
    """
    # Validate priority value
    priority = priority.lower()
    if priority not in ["low", "medium", "high"]:
        return {
            "error": "Priority must be one of: 'low', 'medium', 'high'"
        }

    # Build query parameters
    params = {"priority": priority}
    if status != "all":
        params["status"] = status

    # Make HTTP request to backend API
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(
                f"{BACKEND_BASE_URL}/api/{user_id}/tasks",
                params=params
            )

            if response.status_code == 200:
                result = response.json()
                tasks = result.get("tasks", [])

                # Convert tasks to expected format
                task_list = [
                    {
                        "id": task.get("id"),
                        "title": task.get("title"),
                        "priority": task.get("priority"),
                        "completed": task.get("completed", False),
                        "description": task.get("description"),
                        "created_at": task.get("created_at"),
                    }
                    for task in tasks
                ]

                return {
                    "tasks": task_list,
                    "count": len(task_list),
                    "priority": priority,
                    "status": status,
                }
            else:
                # Return error response
                return {
                    "error": f"Failed to list tasks by priority: {response.status_code}",
                    "details": response.text
                }
        except Exception as e:
            return {
                "error": "Failed to list tasks by priority",
                "details": str(e)
            }


async def run_mcp_server():
    """
    Entry point to run the MCP server.
    """
    async with mcp:
        await mcp.run()


if __name__ == "__main__":
    import asyncio
    asyncio.run(run_mcp_server())