|
| 1 | +from typing import Literal |
| 2 | +from itertools import count |
| 3 | +from datetime import datetime, timezone |
| 4 | +from fastmcp import FastMCP |
| 5 | + |
| 6 | +# In-memory storage for demo purposes |
| 7 | +TODOS: list[dict] = [] |
| 8 | +_id = count(start=1) |
| 9 | + |
| 10 | +mcp = FastMCP(name="Todo Manager") |
| 11 | + |
| 12 | +@mcp.tool |
| 13 | +def create_todo( |
| 14 | + title: str, |
| 15 | + description: str = "", |
| 16 | + priority: Literal["low", "medium", "high"] = "medium", |
| 17 | +) -> dict: |
| 18 | + """Create a todo (id, title, status, priority, timestamps).""" |
| 19 | + todo = { |
| 20 | + "id": next(_id), |
| 21 | + "title": title, |
| 22 | + "description": description, |
| 23 | + "priority": priority, |
| 24 | + "status": "open", |
| 25 | + "created_at": datetime.now(timezone.utc).isoformat(), |
| 26 | + "completed_at": None, |
| 27 | + } |
| 28 | + TODOS.append(todo) |
| 29 | + return todo |
| 30 | + |
| 31 | +@mcp.tool |
| 32 | +def list_todos(status: Literal["open", "done", "all"] = "open") -> dict: |
| 33 | + """List todos by status ('open' | 'done' | 'all').""" |
| 34 | + if status == "all": |
| 35 | + items = TODOS |
| 36 | + elif status == "open": |
| 37 | + items = [t for t in TODOS if t["status"] == "open"] |
| 38 | + else: |
| 39 | + items = [t for t in TODOS if t["status"] == "done"] |
| 40 | + return {"items": items} |
| 41 | + |
| 42 | +@mcp.tool |
| 43 | +def complete_todo(todo_id: int) -> dict: |
| 44 | + """Mark a todo as done.""" |
| 45 | + for t in TODOS: |
| 46 | + if t["id"] == todo_id: |
| 47 | + t["status"] = "done" |
| 48 | + t["completed_at"] = datetime.now(timezone.utc).isoformat() |
| 49 | + return t |
| 50 | + raise ValueError(f"Todo {todo_id} not found") |
| 51 | + |
| 52 | +@mcp.tool |
| 53 | +def search_todos(query: str) -> dict: |
| 54 | + """Case-insensitive search in title/description.""" |
| 55 | + q = query.lower().strip() |
| 56 | + items = [t for t in TODOS if q in t["title"].lower() or q in t["description"].lower()] |
| 57 | + return {"items": items} |
| 58 | + |
| 59 | +# Read-only resources |
| 60 | +@mcp.resource("stats://todos") |
| 61 | +def todo_stats() -> dict: |
| 62 | + """Aggregated stats: total, open, done.""" |
| 63 | + total = len(TODOS) |
| 64 | + open_count = sum(1 for t in TODOS if t["status"] == "open") |
| 65 | + done_count = total - open_count |
| 66 | + return {"total": total, "open": open_count, "done": done_count} |
| 67 | + |
| 68 | +@mcp.resource("todo://{id}") |
| 69 | +def get_todo(id: int) -> dict: |
| 70 | + """Fetch a single todo by id.""" |
| 71 | + for t in TODOS: |
| 72 | + if t["id"] == id: |
| 73 | + return t |
| 74 | + raise ValueError(f"Todo {id} not found") |
| 75 | + |
| 76 | +# A reusable prompt |
| 77 | +@mcp.prompt |
| 78 | +def suggest_next_action(pending: int, project: str | None = None) -> str: |
| 79 | + """Render a small instruction for an LLM to propose next action.""" |
| 80 | + base = f"You have {pending} pending TODOs. " |
| 81 | + if project: |
| 82 | + base += f"They relate to the project '{project}'. " |
| 83 | + base += "Suggest the most impactful next action in one short sentence." |
| 84 | + return base |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + # Default transport is stdio; you can also use transport="http", host=..., port=... |
| 88 | + mcp.run() |
0 commit comments