Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- src/mcp/server.py +138 -0
- src/mcp/tools.py +241 -0
src/mcp/server.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MCP server registration and tool definitions.
|
| 3 |
+
[Task]: T-005
|
| 4 |
+
[From]: specs/003-phase-iii-chatbot/spec.md §6, plan.md §2.1.2
|
| 5 |
+
|
| 6 |
+
Provides OpenAI function calling format tool definitions for the agent.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from typing import List, Dict, Any
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_mcp_tools() -> List[Dict[str, Any]]:
|
| 13 |
+
"""
|
| 14 |
+
Return OpenAI function calling format tool definitions.
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
List of tool definitions compatible with OpenAI Chat Completions API
|
| 18 |
+
"""
|
| 19 |
+
return [
|
| 20 |
+
{
|
| 21 |
+
"type": "function",
|
| 22 |
+
"function": {
|
| 23 |
+
"name": "add_task",
|
| 24 |
+
"description": "Create a new task for the user with title and optional description",
|
| 25 |
+
"parameters": {
|
| 26 |
+
"type": "object",
|
| 27 |
+
"properties": {
|
| 28 |
+
"user_id": {
|
| 29 |
+
"type": "string",
|
| 30 |
+
"description": "User UUID from JWT token"
|
| 31 |
+
},
|
| 32 |
+
"title": {
|
| 33 |
+
"type": "string",
|
| 34 |
+
"description": "Task title (1-200 characters)"
|
| 35 |
+
},
|
| 36 |
+
"description": {
|
| 37 |
+
"type": "string",
|
| 38 |
+
"description": "Optional task description"
|
| 39 |
+
}
|
| 40 |
+
},
|
| 41 |
+
"required": ["user_id", "title"]
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"type": "function",
|
| 47 |
+
"function": {
|
| 48 |
+
"name": "list_tasks",
|
| 49 |
+
"description": "Get all tasks for the user, optionally filtered by completion status",
|
| 50 |
+
"parameters": {
|
| 51 |
+
"type": "object",
|
| 52 |
+
"properties": {
|
| 53 |
+
"user_id": {
|
| 54 |
+
"type": "string",
|
| 55 |
+
"description": "User UUID from JWT token"
|
| 56 |
+
},
|
| 57 |
+
"status": {
|
| 58 |
+
"type": "string",
|
| 59 |
+
"enum": ["all", "pending", "completed"],
|
| 60 |
+
"description": "Filter tasks by completion status (default: all)"
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
"required": ["user_id"]
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"type": "function",
|
| 69 |
+
"function": {
|
| 70 |
+
"name": "complete_task",
|
| 71 |
+
"description": "Mark a specific task as completed",
|
| 72 |
+
"parameters": {
|
| 73 |
+
"type": "object",
|
| 74 |
+
"properties": {
|
| 75 |
+
"user_id": {
|
| 76 |
+
"type": "string",
|
| 77 |
+
"description": "User UUID from JWT token"
|
| 78 |
+
},
|
| 79 |
+
"task_id": {
|
| 80 |
+
"type": "integer",
|
| 81 |
+
"description": "ID of the task to mark as complete"
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
"required": ["user_id", "task_id"]
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"type": "function",
|
| 90 |
+
"function": {
|
| 91 |
+
"name": "update_task",
|
| 92 |
+
"description": "Update task title and/or description",
|
| 93 |
+
"parameters": {
|
| 94 |
+
"type": "object",
|
| 95 |
+
"properties": {
|
| 96 |
+
"user_id": {
|
| 97 |
+
"type": "string",
|
| 98 |
+
"description": "User UUID from JWT token"
|
| 99 |
+
},
|
| 100 |
+
"task_id": {
|
| 101 |
+
"type": "integer",
|
| 102 |
+
"description": "ID of the task to update"
|
| 103 |
+
},
|
| 104 |
+
"title": {
|
| 105 |
+
"type": "string",
|
| 106 |
+
"description": "New task title (optional)"
|
| 107 |
+
},
|
| 108 |
+
"description": {
|
| 109 |
+
"type": "string",
|
| 110 |
+
"description": "New task description (optional)"
|
| 111 |
+
}
|
| 112 |
+
},
|
| 113 |
+
"required": ["user_id", "task_id"]
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"type": "function",
|
| 119 |
+
"function": {
|
| 120 |
+
"name": "delete_task",
|
| 121 |
+
"description": "Permanently delete a task",
|
| 122 |
+
"parameters": {
|
| 123 |
+
"type": "object",
|
| 124 |
+
"properties": {
|
| 125 |
+
"user_id": {
|
| 126 |
+
"type": "string",
|
| 127 |
+
"description": "User UUID from JWT token"
|
| 128 |
+
},
|
| 129 |
+
"task_id": {
|
| 130 |
+
"type": "integer",
|
| 131 |
+
"description": "ID of the task to delete"
|
| 132 |
+
}
|
| 133 |
+
},
|
| 134 |
+
"required": ["user_id", "task_id"]
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
]
|
src/mcp/tools.py
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MCP tool implementations for task operations.
|
| 3 |
+
[Task]: T-004
|
| 4 |
+
[From]: specs/003-phase-iii-chatbot/spec.md §6, plan.md §2.1.2
|
| 5 |
+
|
| 6 |
+
These tools provide stateless, database-backed operations for task management.
|
| 7 |
+
All tools enforce user isolation through user_id filtering.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from sqlmodel import Session, select, col
|
| 11 |
+
from src.models.task import Task
|
| 12 |
+
from uuid import UUID
|
| 13 |
+
from typing import Dict, Any, Optional
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def add_task(
|
| 17 |
+
session: Session,
|
| 18 |
+
user_id: UUID,
|
| 19 |
+
title: str,
|
| 20 |
+
description: Optional[str] = None
|
| 21 |
+
) -> Dict[str, Any]:
|
| 22 |
+
"""
|
| 23 |
+
Create a new task for the user.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
session: Database session
|
| 27 |
+
user_id: User UUID from JWT token
|
| 28 |
+
title: Task title (1-200 characters)
|
| 29 |
+
description: Optional task description
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
{"task_id": int, "status": "created", "title": str, "description": str}
|
| 33 |
+
"""
|
| 34 |
+
try:
|
| 35 |
+
task = Task(
|
| 36 |
+
user_id=user_id,
|
| 37 |
+
title=title,
|
| 38 |
+
description=description,
|
| 39 |
+
completed=False
|
| 40 |
+
)
|
| 41 |
+
session.add(task)
|
| 42 |
+
session.commit()
|
| 43 |
+
session.refresh(task)
|
| 44 |
+
|
| 45 |
+
return {
|
| 46 |
+
"task_id": task.id,
|
| 47 |
+
"status": "created",
|
| 48 |
+
"title": task.title,
|
| 49 |
+
"description": task.description
|
| 50 |
+
}
|
| 51 |
+
except Exception as e:
|
| 52 |
+
session.rollback()
|
| 53 |
+
return {"error": f"Failed to create task: {str(e)}"}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def list_tasks(
|
| 57 |
+
session: Session,
|
| 58 |
+
user_id: UUID,
|
| 59 |
+
status: str = "all"
|
| 60 |
+
) -> Dict[str, Any]:
|
| 61 |
+
"""
|
| 62 |
+
List all tasks for the user, optionally filtered by completion status.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
session: Database session
|
| 66 |
+
user_id: User UUID from JWT token
|
| 67 |
+
status: Filter by status ('all', 'pending', 'completed')
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
{"tasks": [...], "count": int}
|
| 71 |
+
"""
|
| 72 |
+
try:
|
| 73 |
+
query = select(Task).where(Task.user_id == user_id)
|
| 74 |
+
|
| 75 |
+
if status == "pending":
|
| 76 |
+
query = query.where(Task.completed == False)
|
| 77 |
+
elif status == "completed":
|
| 78 |
+
query = query.where(Task.completed == True)
|
| 79 |
+
|
| 80 |
+
tasks = session.exec(query.order_by(col(Task.created_at).desc())).all()
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
"tasks": [
|
| 84 |
+
{
|
| 85 |
+
"id": task.id,
|
| 86 |
+
"title": task.title,
|
| 87 |
+
"description": task.description,
|
| 88 |
+
"completed": task.completed,
|
| 89 |
+
"created_at": task.created_at.isoformat() if task.created_at else None
|
| 90 |
+
}
|
| 91 |
+
for task in tasks
|
| 92 |
+
],
|
| 93 |
+
"count": len(tasks)
|
| 94 |
+
}
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return {"error": f"Failed to list tasks: {str(e)}"}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def complete_task(
|
| 100 |
+
session: Session,
|
| 101 |
+
user_id: UUID,
|
| 102 |
+
task_id: int
|
| 103 |
+
) -> Dict[str, Any]:
|
| 104 |
+
"""
|
| 105 |
+
Mark a task as completed.
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
session: Database session
|
| 109 |
+
user_id: User UUID from JWT token
|
| 110 |
+
task_id: ID of the task to complete
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
{"task_id": int, "status": "completed", "title": str}
|
| 114 |
+
"""
|
| 115 |
+
try:
|
| 116 |
+
# Ensure user_id is UUID type
|
| 117 |
+
if isinstance(user_id, str):
|
| 118 |
+
user_id = UUID(user_id)
|
| 119 |
+
|
| 120 |
+
task = session.exec(
|
| 121 |
+
select(Task).where(
|
| 122 |
+
Task.id == task_id,
|
| 123 |
+
Task.user_id == user_id
|
| 124 |
+
)
|
| 125 |
+
).first()
|
| 126 |
+
|
| 127 |
+
if not task:
|
| 128 |
+
return {"error": "Task not found or access denied"}
|
| 129 |
+
|
| 130 |
+
task.completed = True
|
| 131 |
+
session.add(task)
|
| 132 |
+
session.commit()
|
| 133 |
+
session.refresh(task)
|
| 134 |
+
|
| 135 |
+
return {
|
| 136 |
+
"task_id": task.id,
|
| 137 |
+
"status": "completed",
|
| 138 |
+
"title": task.title
|
| 139 |
+
}
|
| 140 |
+
except Exception as e:
|
| 141 |
+
session.rollback()
|
| 142 |
+
return {"error": f"Failed to complete task: {str(e)}"}
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def update_task(
|
| 146 |
+
session: Session,
|
| 147 |
+
user_id: UUID,
|
| 148 |
+
task_id: int,
|
| 149 |
+
title: Optional[str] = None,
|
| 150 |
+
description: Optional[str] = None
|
| 151 |
+
) -> Dict[str, Any]:
|
| 152 |
+
"""
|
| 153 |
+
Update task title and/or description.
|
| 154 |
+
|
| 155 |
+
Args:
|
| 156 |
+
session: Database session
|
| 157 |
+
user_id: User UUID from JWT token
|
| 158 |
+
task_id: ID of the task to update
|
| 159 |
+
title: New task title (optional)
|
| 160 |
+
description: New task description (optional)
|
| 161 |
+
|
| 162 |
+
Returns:
|
| 163 |
+
{"task_id": int, "status": "updated", "title": str}
|
| 164 |
+
"""
|
| 165 |
+
try:
|
| 166 |
+
# Ensure user_id is UUID type
|
| 167 |
+
if isinstance(user_id, str):
|
| 168 |
+
user_id = UUID(user_id)
|
| 169 |
+
|
| 170 |
+
task = session.exec(
|
| 171 |
+
select(Task).where(
|
| 172 |
+
Task.id == task_id,
|
| 173 |
+
Task.user_id == user_id
|
| 174 |
+
)
|
| 175 |
+
).first()
|
| 176 |
+
|
| 177 |
+
if not task:
|
| 178 |
+
return {"error": "Task not found or access denied"}
|
| 179 |
+
|
| 180 |
+
if title:
|
| 181 |
+
task.title = title
|
| 182 |
+
if description is not None:
|
| 183 |
+
task.description = description
|
| 184 |
+
|
| 185 |
+
session.add(task)
|
| 186 |
+
session.commit()
|
| 187 |
+
session.refresh(task)
|
| 188 |
+
|
| 189 |
+
return {
|
| 190 |
+
"task_id": task.id,
|
| 191 |
+
"status": "updated",
|
| 192 |
+
"title": task.title
|
| 193 |
+
}
|
| 194 |
+
except Exception as e:
|
| 195 |
+
session.rollback()
|
| 196 |
+
return {"error": f"Failed to update task: {str(e)}"}
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def delete_task(
|
| 200 |
+
session: Session,
|
| 201 |
+
user_id: UUID,
|
| 202 |
+
task_id: int
|
| 203 |
+
) -> Dict[str, Any]:
|
| 204 |
+
"""
|
| 205 |
+
Permanently delete a task.
|
| 206 |
+
|
| 207 |
+
Args:
|
| 208 |
+
session: Database session
|
| 209 |
+
user_id: User UUID from JWT token
|
| 210 |
+
task_id: ID of the task to delete
|
| 211 |
+
|
| 212 |
+
Returns:
|
| 213 |
+
{"task_id": int, "status": "deleted", "title": str}
|
| 214 |
+
"""
|
| 215 |
+
try:
|
| 216 |
+
# Ensure user_id is UUID type
|
| 217 |
+
if isinstance(user_id, str):
|
| 218 |
+
user_id = UUID(user_id)
|
| 219 |
+
|
| 220 |
+
task = session.exec(
|
| 221 |
+
select(Task).where(
|
| 222 |
+
Task.id == task_id,
|
| 223 |
+
Task.user_id == user_id
|
| 224 |
+
)
|
| 225 |
+
).first()
|
| 226 |
+
|
| 227 |
+
if not task:
|
| 228 |
+
return {"error": "Task not found or access denied"}
|
| 229 |
+
|
| 230 |
+
title = task.title
|
| 231 |
+
session.delete(task)
|
| 232 |
+
session.commit()
|
| 233 |
+
|
| 234 |
+
return {
|
| 235 |
+
"task_id": task_id,
|
| 236 |
+
"status": "deleted",
|
| 237 |
+
"title": title
|
| 238 |
+
}
|
| 239 |
+
except Exception as e:
|
| 240 |
+
session.rollback()
|
| 241 |
+
return {"error": f"Failed to delete task: {str(e)}"}
|