File size: 7,805 Bytes
35cd1bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MCP (Model Context Protocol) Server Implementation for the Todo Backend API.

Implements the MCP server with 5 task management tools using the Official MCP SDK.
"""
from mcp.server import Server
from mcp.types import CallToolResult, Tool
from typing import Dict, Any, List
import json
from sqlmodel import create_engine, Session
from sqlmodel.ext.asyncio.session import AsyncSession
from contextlib import asynccontextmanager

from ..models.user import UserRead
from ..services.task_service import TaskService
from ..services.ai_service import ai_service


# Create the MCP server instance
server = Server("todo-mcp-server")




@server.list_tools()
async def list_tools() -> List[Tool]:
    """List all available tools in the MCP server."""
    return [
        Tool(
            name="add_task",
            description="Create a new task",
            input_schema={
                "type": "object",
                "properties": {
                    "user_id": {"type": "string", "description": "User identifier"},
                    "title": {"type": "string", "description": "Task title"},
                    "description": {"type": "string", "description": "Task description (optional)"}
                },
                "required": ["user_id", "title"]
            }
        ),
        Tool(
            name="list_tasks",
            description="Retrieve tasks from the list",
            input_schema={
                "type": "object",
                "properties": {
                    "user_id": {"type": "string", "description": "User identifier"},
                    "status": {"type": "string", "enum": ["all", "pending", "completed"], "description": "Filter by status"}
                },
                "required": ["user_id"]
            }
        ),
        Tool(
            name="complete_task",
            description="Mark a task as complete",
            input_schema={
                "type": "object",
                "properties": {
                    "user_id": {"type": "string", "description": "User identifier"},
                    "task_id": {"type": "integer", "description": "Task ID to complete"}
                },
                "required": ["user_id", "task_id"]
            }
        ),
        Tool(
            name="delete_task",
            description="Remove a task from the list",
            input_schema={
                "type": "object",
                "properties": {
                    "user_id": {"type": "string", "description": "User identifier"},
                    "task_id": {"type": "integer", "description": "Task ID to delete"}
                },
                "required": ["user_id", "task_id"]
            }
        ),
        Tool(
            name="update_task",
            description="Modify task title or description",
            input_schema={
                "type": "object",
                "properties": {
                    "user_id": {"type": "string", "description": "User identifier"},
                    "task_id": {"type": "integer", "description": "Task ID to update"},
                    "title": {"type": "string", "description": "New title (optional)"},
                    "description": {"type": "string", "description": "New description (optional)"}
                },
                "required": ["user_id", "task_id"]
            }
        )
    ]


@server.call_tool()
async def call_tool(name: str, arguments: Dict[str, Any]) -> CallToolResult:
    """
    Handle tool calls from the MCP server.

    Args:
        name: Name of the tool to call
        arguments: Arguments to pass to the tool

    Returns:
        Result of the tool call
    """
    try:
        # Extract user_id from arguments
        user_id = int(arguments.get("user_id"))

        # Create a mock user object for compatibility with existing services
        # In a real implementation, you would fetch the user from the database
        current_user = UserRead(
            id=user_id,
            email=f"user_{user_id}@example.com"  # Placeholder email
        )

        # Get the database session - in a real implementation, you would get this properly
        # For now, we'll use the AI service which handles the database operations
        if name == "add_task":
            title = arguments.get("title")
            description = arguments.get("description")

            # Using the AI service's method for consistency
            result = await ai_service.add_task_tool(
                title=title,
                description=description,
                user=current_user,
                session=None  # Will be handled internally
            )

            return CallToolResult(content=json.dumps(result))

        elif name == "list_tasks":
            status = arguments.get("status", "all")

            # Using the AI service's method for consistency
            result = await ai_service.list_tasks_tool(
                user=current_user,
                session=None  # Will be handled internally
            )

            # Filter results based on status if needed
            if status != "all" and "tasks" in result:
                if status == "pending":
                    result["tasks"] = [task for task in result["tasks"] if not task.get("completed")]
                elif status == "completed":
                    result["tasks"] = [task for task in result["tasks"] if task.get("completed")]

            return CallToolResult(content=json.dumps(result))

        elif name == "complete_task":
            task_id = arguments.get("task_id")

            # Using the AI service's method for consistency
            result = await ai_service.complete_task_tool(
                task_id=task_id,
                user=current_user,
                session=None  # Will be handled internally
            )

            return CallToolResult(content=json.dumps(result))

        elif name == "delete_task":
            task_id = arguments.get("task_id")

            # Using the AI service's method for consistency
            result = await ai_service.delete_task_tool(
                task_id=task_id,
                user=current_user,
                session=None  # Will be handled internally
            )

            return CallToolResult(content=json.dumps(result))

        elif name == "update_task":
            task_id = arguments.get("task_id")
            title = arguments.get("title")
            description = arguments.get("description")

            # Using the AI service's method for consistency
            result = await ai_service.update_task_tool(
                task_id=task_id,
                title=title,
                description=description,
                user=current_user,
                session=None  # Will be handled internally
            )

            return CallToolResult(content=json.dumps(result))

        else:
            return CallToolResult(
                content=json.dumps({
                    "success": False,
                    "error": f"Unknown tool: {name}",
                    "message": f"Tool '{name}' not found"
                })
            )

    except Exception as e:
        return CallToolResult(
            content=json.dumps({
                "success": False,
                "error": str(e),
                "message": f"Error executing tool {name}: {str(e)}"
            })
        )


# For standalone execution
if __name__ == "__main__":
    import asyncio
    import argparse

    parser = argparse.ArgumentParser(description="Run the Todo MCP Server")
    parser.add_argument("--port", type=int, default=3000, help="Port to run the server on")
    args = parser.parse_args()

    async def main():
        # Start the server
        await server.run(port=args.port)

    asyncio.run(main())