File size: 10,684 Bytes
67f8819 eaf8d86 67f8819 eaf8d86 67f8819 | 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 | """
MCP tool definitions for Todo task management.
Per @specs/001-chatbot-mcp/contracts/mcp-tools.json
All tools enforce user isolation by requiring user_id in every request.
"""
from sqlmodel import Session, select
from typing import Optional, TYPE_CHECKING
from datetime import datetime
from uuid import UUID
from models.task import TaskTable
from config import engine
if TYPE_CHECKING:
from mcp.server import SimpleMCPRegistry
def register_task_tools(mcp_server) -> None:
"""
Register all task management tools with the MCP server.
Args:
mcp_server: The SimpleMCPRegistry instance
This function is called during MCP server initialization to register
all 5 task tools: add_task, list_tasks, complete_task, delete_task, update_task.
Per @specs/001-chatbot-mcp/plan.md, all task operations go through MCP.
"""
@mcp_server.tool()
async def add_task(
user_id: str,
title: str,
description: Optional[str] = None
) -> dict:
"""
Create a new todo task for a user.
Use this when the user requests to create, add, or make a new task.
Args:
user_id: ID of the user who will own the task (UUID string)
title: Task title (short, descriptive name)
description: Optional detailed description
Returns:
Dict with task_id, title, description, created_at, success, error
"""
try:
with Session(engine) as session:
task = TaskTable(
user_id=UUID(user_id),
title=title,
description=description,
completed=False,
created_at=datetime.utcnow()
)
session.add(task)
session.commit()
session.refresh(task)
return {
"task_id": str(task.id),
"title": task.title,
"description": task.description,
"created_at": task.created_at.isoformat(),
"success": True,
"error": None
}
except Exception as e:
return {
"task_id": None,
"title": None,
"description": None,
"created_at": None,
"success": False,
"error": str(e)
}
@mcp_server.tool()
async def list_tasks(
user_id: str,
include_completed: bool = True
) -> dict:
"""
List all tasks for a user.
Use this when the user asks to see, show, or display their tasks.
Args:
user_id: ID of the user whose tasks to list (UUID string)
include_completed: Whether to include completed tasks (default: true)
Returns:
Dict with tasks list, count, success, error
"""
try:
with Session(engine) as session:
statement = select(TaskTable).where(TaskTable.user_id == UUID(user_id))
if not include_completed:
statement = statement.where(TaskTable.completed == False)
tasks = session.exec(statement).all()
return {
"tasks": [
{
"id": str(task.id),
"title": task.title,
"description": task.description,
"completed": task.completed,
"created_at": task.created_at.isoformat(),
"completed_at": task.completed_at.isoformat() if task.completed_at else None
}
for task in tasks
],
"count": len(tasks),
"success": True,
"error": None
}
except Exception as e:
return {
"tasks": [],
"count": 0,
"success": False,
"error": str(e)
}
@mcp_server.tool()
async def complete_task(
user_id: str,
task_id: str
) -> dict:
"""
Mark a task as completed.
Use this when the user asks to complete, finish, or check off a task.
Args:
user_id: ID of the user who owns the task (UUID string)
task_id: ID of the task to mark as complete (UUID string)
Returns:
Dict with task_id, title, completed, completed_at, success, error
"""
try:
with Session(engine) as session:
task = session.query(TaskTable).filter(
TaskTable.id == UUID(task_id),
TaskTable.user_id == UUID(user_id)
).first()
if not task:
return {
"task_id": task_id,
"title": None,
"completed": False,
"completed_at": None,
"success": False,
"error": "Task not found or access denied"
}
task.completed = True
task.completed_at = datetime.utcnow()
session.add(task)
session.commit()
session.refresh(task)
return {
"task_id": str(task.id),
"title": task.title,
"completed": task.completed,
"completed_at": task.completed_at.isoformat(),
"success": True,
"error": None
}
except Exception as e:
return {
"task_id": task_id,
"title": None,
"completed": False,
"completed_at": None,
"success": False,
"error": str(e)
}
@mcp_server.tool()
async def delete_task(
user_id: str,
task_id: str
) -> dict:
"""
Delete a task permanently.
CRITICAL: The task_id parameter must be a valid UUID from the user's existing tasks.
If the user provides a task number or title, you MUST map it to the correct task_id.
Example: If user says "delete task 1", find the task with index 0 in their task list and use its ID.
Always confirm which task you're deleting by showing the task title before proceeding.
Use this when the user asks to delete, remove, or get rid of a task.
Args:
user_id: ID of the user who owns the task (UUID string)
task_id: ID of the task to delete (UUID string) - must match an existing task ID for this user
Returns:
Dict with task_id, title, deleted, success, error
"""
try:
with Session(engine) as session:
task = session.query(TaskTable).filter(
TaskTable.id == UUID(task_id),
TaskTable.user_id == UUID(user_id)
).first()
if not task:
return {
"task_id": task_id,
"title": None,
"deleted": False,
"success": False,
"error": "Task not found or access denied"
}
title = task.title
session.delete(task)
session.commit()
return {
"task_id": task_id,
"title": title,
"deleted": True,
"success": True,
"error": None
}
except Exception as e:
return {
"task_id": task_id,
"title": None,
"deleted": False,
"success": False,
"error": str(e)
}
@mcp_server.tool()
async def update_task(
user_id: str,
task_id: str,
title: Optional[str] = None,
description: Optional[str] = None
) -> dict:
"""
Update a task's title or description.
CRITICAL: The task_id parameter must be a valid UUID from the user's existing tasks.
If the user provides a task number or title, you MUST map it to the correct task_id.
Example: If user says "update task 1 to buy milk", find the task with index 0 in their task list and use its ID.
Use this when the user asks to change, modify, or edit a task.
Args:
user_id: ID of the user who owns the task (UUID string)
task_id: ID of the task to update (UUID string) - must match an existing task ID for this user
title: New task title (optional)
description: New task description (optional, empty string to clear)
Returns:
Dict with task_id, title, description, updated_at, success, error
"""
try:
with Session(engine) as session:
task = session.query(TaskTable).filter(
TaskTable.id == UUID(task_id),
TaskTable.user_id == UUID(user_id)
).first()
if not task:
return {
"task_id": task_id,
"title": None,
"description": None,
"updated_at": None,
"success": False,
"error": "Task not found or access denied"
}
if title is not None:
task.title = title
if description is not None:
task.description = description if description else None
session.add(task)
session.commit()
session.refresh(task)
return {
"task_id": str(task.id),
"title": task.title,
"description": task.description,
"updated_at": task.created_at.isoformat(), # Using created_at as updated_at not in model
"success": True,
"error": None
}
except Exception as e:
return {
"task_id": task_id,
"title": None,
"description": None,
"updated_at": None,
"success": False,
"error": str(e)
}
|