feat(mcp): event CRUD tools
Five new tools (events weren't in fable-mcp before). Split start_date + start_time inputs combine into a naive datetime that services/events.py interprets in the user's local timezone. Sentinels for update: - empty strings → leave unchanged - duration_minutes=-1 → leave unchanged - duration_minutes=0 → set to point event (NULL duration) - start_date/start_time must BOTH be set to move the event Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@ to a FastMCP instance. `register_all(mcp)` is the single entry point called
|
||||
from `mcp.server.build_mcp_server`.
|
||||
"""
|
||||
from fabledassistant.mcp.tools import (
|
||||
milestones, notes, projects, search, tasks,
|
||||
events, milestones, notes, projects, search, tasks,
|
||||
)
|
||||
|
||||
|
||||
@@ -16,3 +16,4 @@ def register_all(mcp) -> None:
|
||||
tasks.register(mcp)
|
||||
projects.register(mcp)
|
||||
milestones.register(mcp)
|
||||
events.register(mcp)
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
"""Calendar event MCP tools — new in Phase 3.
|
||||
|
||||
Events were previously only an internal-LLM tool; the MCP surface didn't have
|
||||
them. Wraps services/events.py.
|
||||
|
||||
Date/time inputs are split: start_date (YYYY-MM-DD) + start_time (HH:MM) get
|
||||
combined into a naive datetime; the service layer interprets it in the user's
|
||||
local timezone. duration_minutes=0 ⇒ point event (NULL duration). The
|
||||
LLM-era expected_weekday verification check is intentionally not replicated —
|
||||
Claude doesn't need it.
|
||||
|
||||
For update, sentinels:
|
||||
- title="" / location="" / description="" → leave unchanged
|
||||
- start_date="" / start_time="" → leave unchanged (both must be provided to
|
||||
move the event)
|
||||
- duration_minutes=-1 → leave unchanged; 0 means "set to point event"
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from fabledassistant.mcp._context import current_user_id
|
||||
from fabledassistant.services import events as events_svc
|
||||
|
||||
|
||||
def _combine(start_date: str, start_time: str) -> datetime:
|
||||
"""Combine YYYY-MM-DD + HH:MM into a naive datetime."""
|
||||
t = start_time or "00:00"
|
||||
return datetime.fromisoformat(f"{start_date}T{t}:00")
|
||||
|
||||
|
||||
def _event_dict(event) -> dict:
|
||||
"""Render an Event model to a dict, handling list_events (already dicts)."""
|
||||
return event if isinstance(event, dict) else event.to_dict()
|
||||
|
||||
|
||||
async def fable_list_events(date_from: str, date_to: str) -> dict:
|
||||
"""List events between date_from and date_to (YYYY-MM-DD, both inclusive at
|
||||
the day level — `date_to` is interpreted as end-of-that-day).
|
||||
|
||||
Recurring events are expanded into individual occurrences within the range.
|
||||
"""
|
||||
uid = current_user_id()
|
||||
start = datetime.fromisoformat(date_from)
|
||||
end = datetime.fromisoformat(date_to)
|
||||
rows = await events_svc.list_events(uid, start, end)
|
||||
return {"events": [_event_dict(e) for e in rows], "total": len(rows)}
|
||||
|
||||
|
||||
async def fable_create_event(
|
||||
title: str,
|
||||
start_date: str,
|
||||
start_time: str = "00:00",
|
||||
duration_minutes: int = 0,
|
||||
all_day: bool = False,
|
||||
location: str = "",
|
||||
description: str = "",
|
||||
) -> dict:
|
||||
"""Create a calendar event.
|
||||
|
||||
Args:
|
||||
title: Event title (required).
|
||||
start_date: YYYY-MM-DD.
|
||||
start_time: HH:MM (24-hour). Ignored when all_day=True.
|
||||
duration_minutes: 0 for a point event (no duration); otherwise minutes.
|
||||
all_day: True to make this an all-day event.
|
||||
location: Optional location string.
|
||||
description: Optional longer description.
|
||||
"""
|
||||
uid = current_user_id()
|
||||
event = await events_svc.create_event(
|
||||
uid,
|
||||
title=title,
|
||||
start_dt=_combine(start_date, start_time),
|
||||
duration_minutes=duration_minutes or None,
|
||||
all_day=all_day,
|
||||
location=location,
|
||||
description=description,
|
||||
)
|
||||
return event.to_dict()
|
||||
|
||||
|
||||
async def fable_get_event(event_id: int) -> dict:
|
||||
"""Fetch a single event by ID."""
|
||||
uid = current_user_id()
|
||||
event = await events_svc.get_event(uid, event_id)
|
||||
if event is None:
|
||||
raise ValueError(f"event {event_id} not found")
|
||||
return event.to_dict()
|
||||
|
||||
|
||||
async def fable_update_event(
|
||||
event_id: int,
|
||||
title: str = "",
|
||||
start_date: str = "",
|
||||
start_time: str = "",
|
||||
duration_minutes: int = -1,
|
||||
location: str = "",
|
||||
description: str = "",
|
||||
) -> dict:
|
||||
"""Update an event. Only explicitly provided fields are changed.
|
||||
|
||||
Args:
|
||||
event_id: ID of the event to update.
|
||||
title: New title; omit to leave unchanged.
|
||||
start_date / start_time: BOTH must be set to move the event. Omit either
|
||||
to leave the start_dt unchanged.
|
||||
duration_minutes: -1 leaves unchanged; 0 sets to point event; any
|
||||
positive value sets the duration.
|
||||
location / description: omit to leave unchanged.
|
||||
"""
|
||||
uid = current_user_id()
|
||||
fields: dict = {}
|
||||
if title:
|
||||
fields["title"] = title
|
||||
if location:
|
||||
fields["location"] = location
|
||||
if description:
|
||||
fields["description"] = description
|
||||
if start_date and start_time:
|
||||
fields["start_dt"] = _combine(start_date, start_time)
|
||||
if duration_minutes >= 0:
|
||||
# 0 means point event (NULL); positive sets a real duration.
|
||||
fields["duration_minutes"] = duration_minutes or None
|
||||
event = await events_svc.update_event(uid, event_id, **fields)
|
||||
if event is None:
|
||||
raise ValueError(f"event {event_id} not found")
|
||||
return event.to_dict()
|
||||
|
||||
|
||||
async def fable_delete_event(event_id: int) -> str:
|
||||
"""Permanently delete a calendar event by ID. Cannot be undone."""
|
||||
uid = current_user_id()
|
||||
existing = await events_svc.get_event(uid, event_id)
|
||||
if existing is None:
|
||||
raise ValueError(f"event {event_id} not found")
|
||||
await events_svc.delete_event(uid, event_id)
|
||||
return f"Event {event_id} deleted."
|
||||
|
||||
|
||||
def register(mcp) -> None:
|
||||
for fn in (
|
||||
fable_list_events,
|
||||
fable_create_event,
|
||||
fable_get_event,
|
||||
fable_update_event,
|
||||
fable_delete_event,
|
||||
):
|
||||
mcp.tool(name=fn.__name__)(fn)
|
||||
Reference in New Issue
Block a user