File size: 5,367 Bytes
7380407 |
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 |
import gradio as gr
import json
from datetime import datetime
from typing import List, Dict, Optional
# Simple in-memory task storage
tasks_db = []
task_id_counter = 1
# MCP Server Tool Functions
def create_task(title: str, description: str = "", priority: str = "medium") -> Dict:
"""Create a new task"""
global task_id_counter
task = {
"id": task_id_counter,
"title": title,
"description": description,
"priority": priority,
"status": "pending",
"created_at": datetime.now().isoformat()
}
tasks_db.append(task)
task_id_counter += 1
return {"success": True, "task": task, "message": "Task created successfully"}
def list_tasks(status: Optional[str] = None) -> Dict:
"""List all tasks or filter by status"""
if status:
filtered = [t for t in tasks_db if t["status"] == status]
return {"success": True, "tasks": filtered, "count": len(filtered)}
return {"success": True, "tasks": tasks_db, "count": len(tasks_db)}
def update_task(task_id: int, status: str) -> Dict:
"""Update task status"""
for task in tasks_db:
if task["id"] == task_id:
task["status"] = status
task["updated_at"] = datetime.now().isoformat()
return {"success": True, "task": task, "message": "Task updated"}
return {"success": False, "message": "Task not found"}
def delete_task(task_id: int) -> Dict:
"""Delete a task"""
global tasks_db
initial_len = len(tasks_db)
tasks_db = [t for t in tasks_db if t["id"] != task_id]
if len(tasks_db) < initial_len:
return {"success": True, "message": f"Task {task_id} deleted"}
return {"success": False, "message": "Task not found"}
def get_task_stats() -> Dict:
"""Get task statistics"""
stats = {
"total": len(tasks_db),
"pending": len([t for t in tasks_db if t["status"] == "pending"]),
"in_progress": len([t for t in tasks_db if t["status"] == "in_progress"]),
"completed": len([t for t in tasks_db if t["status"] == "completed"])
}
return {"success": True, "stats": stats}
# Gradio Interface Functions
def create_task_ui(title, desc, priority):
result = create_task(title, desc, priority)
return json.dumps(result, indent=2)
def list_tasks_ui(status_filter):
status = status_filter if status_filter != "all" else None
result = list_tasks(status)
return json.dumps(result, indent=2)
def update_task_ui(task_id, new_status):
result = update_task(int(task_id), new_status)
return json.dumps(result, indent=2)
def delete_task_ui(task_id):
result = delete_task(int(task_id))
return json.dumps(result, indent=2)
def stats_ui():
result = get_task_stats()
return json.dumps(result, indent=2)
# Create Gradio Interface
with gr.Blocks(title="Task Manager MCP Server") as demo:
gr.Markdown(
"""
# 📋 Task Manager MCP Server
### Track 1: Building MCP - MCP 1st Birthday Hackathon
This is a simple MCP server providing task management tools for AI agents.
**Available Tools:**
- Create tasks with title, description, and priority
- List all tasks or filter by status
- Update task status
- Delete tasks
- Get task statistics
**MCP Integration:** This server can be used by AI agents via the Model Context Protocol
to manage tasks programmatically.
"""
)
with gr.Tab("Create Task"):
title_input = gr.Textbox(label="Task Title", placeholder="Enter task title")
desc_input = gr.Textbox(label="Description", placeholder="Enter description", lines=3)
priority_input = gr.Radio(["low", "medium", "high"], label="Priority", value="medium")
create_btn = gr.Button("Create Task")
create_output = gr.Code(label="Result", language="json")
create_btn.click(create_task_ui, [title_input, desc_input, priority_input], create_output)
with gr.Tab("List Tasks"):
status_filter = gr.Radio(["all", "pending", "in_progress", "completed"],
label="Filter by Status", value="all")
list_btn = gr.Button("List Tasks")
list_output = gr.Code(label="Tasks", language="json")
list_btn.click(list_tasks_ui, [status_filter], list_output)
with gr.Tab("Update Task"):
update_id = gr.Number(label="Task ID", precision=0)
update_status = gr.Radio(["pending", "in_progress", "completed"],
label="New Status", value="in_progress")
update_btn = gr.Button("Update Task")
update_output = gr.Code(label="Result", language="json")
update_btn.click(update_task_ui, [update_id, update_status], update_output)
with gr.Tab("Delete Task"):
delete_id = gr.Number(label="Task ID to Delete", precision=0)
delete_btn = gr.Button("Delete Task", variant="stop")
delete_output = gr.Code(label="Result", language="json")
delete_btn.click(delete_task_ui, [delete_id], delete_output)
with gr.Tab("Statistics"):
stats_btn = gr.Button("Get Statistics")
stats_output = gr.Code(label="Statistics", language="json")
stats_btn.click(stats_ui, [], stats_output)
if __name__ == "__main__":
demo.launch() |