@miguelarios/cal-mcp 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +8 -0
- package/dist/main.d.ts +2 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +15 -17
- package/dist/main.js.map +1 -1
- package/dist/services/CalDavService.d.ts +73 -0
- package/dist/services/CalDavService.d.ts.map +1 -1
- package/dist/services/CalDavService.js +414 -9
- package/dist/services/CalDavService.js.map +1 -1
- package/dist/services/urlCache.d.ts +16 -0
- package/dist/services/urlCache.d.ts.map +1 -1
- package/dist/services/urlCache.js +32 -0
- package/dist/services/urlCache.js.map +1 -1
- package/dist/tools/calendarManagementTools.d.ts +11 -0
- package/dist/tools/calendarManagementTools.d.ts.map +1 -0
- package/dist/tools/calendarManagementTools.js +178 -0
- package/dist/tools/calendarManagementTools.js.map +1 -0
- package/dist/tools/calendarSchemas.d.ts +268 -0
- package/dist/tools/calendarSchemas.d.ts.map +1 -0
- package/dist/tools/calendarSchemas.js +96 -0
- package/dist/tools/calendarSchemas.js.map +1 -0
- package/dist/tools/calendarTools.d.ts +2 -9
- package/dist/tools/calendarTools.d.ts.map +1 -1
- package/dist/tools/calendarTools.js +525 -533
- package/dist/tools/calendarTools.js.map +1 -1
- package/package.json +4 -3
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { getLocalDateParts, getTimezone,
|
|
2
|
-
import { addExdateToIcs, combineIcsComponents, createExceptionComponent, generateEventIcs, removeExceptionFromIcs, splitIcsByUid, updateMasterEventIcs, } from "@miguelarios/pim-core/ics";
|
|
1
|
+
import { getLocalDateParts, getTimezone, zonedTimeToUtc } from "@miguelarios/pim-core";
|
|
2
|
+
import { addExdateToIcs, combineIcsComponents, createExceptionComponent, formatTriggerHuman, generateEventIcs, removeExceptionFromIcs, splitIcsByUid, updateMasterEventIcs, } from "@miguelarios/pim-core/ics";
|
|
3
|
+
import { confirmDestructive, fail, structured, toolError, } from "@miguelarios/pim-core/mcp";
|
|
3
4
|
import { drainDebugTimings, } from "../services/CalDavService.js";
|
|
5
|
+
import { batchCreateSchema, calendarListSchema, deleteResultSchema, eventListSchema, freeSlotsSchema, importResultSchema, singleEventSchema, } from "./calendarSchemas.js";
|
|
4
6
|
async function fetchEvents(service, calendar, start, end, detailLevel) {
|
|
5
7
|
const full = detailLevel === "full";
|
|
6
8
|
if (calendar) {
|
|
@@ -14,17 +16,135 @@ async function fetchEvents(service, calendar, start, end, detailLevel) {
|
|
|
14
16
|
: service.listEvents(cal.calendar_id, start, end)));
|
|
15
17
|
return results.flat();
|
|
16
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Timing diagnostics ride in the result's `_meta` rather than inside the
|
|
21
|
+
* payload, so they never have to be modelled in `outputSchema`.
|
|
22
|
+
*/
|
|
23
|
+
function debugMeta() {
|
|
24
|
+
if (process.env.CAL_MCP_DEBUG !== "1")
|
|
25
|
+
return undefined;
|
|
26
|
+
const timings = drainDebugTimings();
|
|
27
|
+
if (timings.length === 0)
|
|
28
|
+
return undefined;
|
|
29
|
+
return { _meta: { "com.miguelarios.cal-mcp/debug": { timings } } };
|
|
30
|
+
}
|
|
31
|
+
function ok(payload) {
|
|
32
|
+
return { ...structured(payload), ...debugMeta() };
|
|
33
|
+
}
|
|
34
|
+
function calFail(code, message, retryable = false) {
|
|
35
|
+
return { ...fail(code, message, retryable), ...debugMeta() };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Preserves this server's external error vocabulary (`not_found`,
|
|
39
|
+
* `validation_error`, `backend_error`) while letting the underlying
|
|
40
|
+
* `PimError` message through.
|
|
41
|
+
*/
|
|
42
|
+
function mapError(err) {
|
|
43
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
44
|
+
const code = err.code;
|
|
45
|
+
if (code === "CALENDAR_NOT_FOUND" || code === "EVENT_NOT_FOUND") {
|
|
46
|
+
return { ...toolError(err, () => "not_found"), ...debugMeta() };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { ...toolError(err, () => "backend_error"), ...debugMeta() };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Widens an attendee from the tool's input shape (`{ email }`) to the shape a
|
|
53
|
+
* read returns. `generateEventIcs` writes a bare `ATTENDEE:mailto:…` with no
|
|
54
|
+
* CN/PARTSTAT/ROLE/CUTYPE, so re-reading it yields nulls and the RFC 5545
|
|
55
|
+
* §3.2.3 default CUTYPE of INDIVIDUAL — which the parser maps to "person".
|
|
56
|
+
*/
|
|
57
|
+
function toResponseAttendee(attendee) {
|
|
58
|
+
return {
|
|
59
|
+
email: attendee.email,
|
|
60
|
+
name: null,
|
|
61
|
+
status: null,
|
|
62
|
+
role: null,
|
|
63
|
+
type: "person",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** Widens an alarm from the tool's input shape by rendering `trigger_human`. */
|
|
67
|
+
function toResponseAlarm(alarm) {
|
|
68
|
+
return {
|
|
69
|
+
...alarm,
|
|
70
|
+
trigger_human: alarm.type === "absolute"
|
|
71
|
+
? new Date(alarm.trigger).toISOString()
|
|
72
|
+
: formatTriggerHuman(Number(alarm.trigger)),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Runs a handler body, converting anything thrown into this server's error shape. */
|
|
76
|
+
async function run(body) {
|
|
77
|
+
try {
|
|
78
|
+
return await body();
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
return mapError(err);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const CALENDAR_PROP = {
|
|
85
|
+
type: "string",
|
|
86
|
+
description: "Provider-prefixed calendar ID",
|
|
87
|
+
};
|
|
88
|
+
const DETAIL_LEVEL_PROP = {
|
|
89
|
+
type: "string",
|
|
90
|
+
enum: ["summary", "full"],
|
|
91
|
+
description: "Response verbosity (default: summary)",
|
|
92
|
+
};
|
|
93
|
+
const ATTENDEES_PROP = (description) => ({
|
|
94
|
+
type: "array",
|
|
95
|
+
items: {
|
|
96
|
+
type: "object",
|
|
97
|
+
properties: {
|
|
98
|
+
email: {
|
|
99
|
+
type: "string",
|
|
100
|
+
description: "Attendee email address. Display name is resolved server-side from the invitee's address book.",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
required: ["email"],
|
|
104
|
+
},
|
|
105
|
+
description,
|
|
106
|
+
});
|
|
107
|
+
const ALARMS_PROP = {
|
|
108
|
+
type: "array",
|
|
109
|
+
items: {
|
|
110
|
+
type: "object",
|
|
111
|
+
properties: {
|
|
112
|
+
type: { type: "string", enum: ["relative", "absolute"], description: "Alarm type" },
|
|
113
|
+
trigger: {
|
|
114
|
+
type: ["string", "number"],
|
|
115
|
+
description: "Seconds offset (negative=before event) for relative, or ISO 8601 datetime for absolute",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
required: ["type", "trigger"],
|
|
119
|
+
},
|
|
120
|
+
description: "Event reminders/alarms",
|
|
121
|
+
};
|
|
122
|
+
const CATEGORIES_PROP = {
|
|
123
|
+
type: "array",
|
|
124
|
+
items: { type: "string", description: "Category / tag name" },
|
|
125
|
+
description: "Event categories/tags",
|
|
126
|
+
};
|
|
127
|
+
const READ_ONLY = {
|
|
128
|
+
readOnlyHint: true,
|
|
129
|
+
destructiveHint: false,
|
|
130
|
+
idempotentHint: true,
|
|
131
|
+
openWorldHint: true,
|
|
132
|
+
};
|
|
17
133
|
export const CALENDAR_TOOLS = [
|
|
18
134
|
{
|
|
19
135
|
name: "list_calendars",
|
|
136
|
+
title: "List Calendars",
|
|
20
137
|
description: "List all calendars across all configured CalDAV providers. Returns provider-prefixed IDs (e.g., mailbox/work).",
|
|
21
|
-
annotations:
|
|
138
|
+
annotations: READ_ONLY,
|
|
22
139
|
inputSchema: { type: "object", properties: {} },
|
|
140
|
+
outputSchema: calendarListSchema,
|
|
141
|
+
handler: (_args, service) => run(async () => ok({ calendars: await service.listCalendars() })),
|
|
23
142
|
},
|
|
24
143
|
{
|
|
25
144
|
name: "list_events",
|
|
145
|
+
title: "List Events",
|
|
26
146
|
description: "Query events in a date range. Recurring events are expanded into individual instances.",
|
|
27
|
-
annotations:
|
|
147
|
+
annotations: READ_ONLY,
|
|
28
148
|
inputSchema: {
|
|
29
149
|
type: "object",
|
|
30
150
|
properties: {
|
|
@@ -32,27 +152,22 @@ export const CALENDAR_TOOLS = [
|
|
|
32
152
|
type: "string",
|
|
33
153
|
description: "Provider-prefixed calendar ID (e.g., mailbox/Work). If omitted, queries all calendars.",
|
|
34
154
|
},
|
|
35
|
-
start: {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
end: {
|
|
40
|
-
type: "string",
|
|
41
|
-
description: "End of date range (ISO 8601)",
|
|
42
|
-
},
|
|
43
|
-
detail_level: {
|
|
44
|
-
type: "string",
|
|
45
|
-
enum: ["summary", "full"],
|
|
46
|
-
description: "Response verbosity (default: summary)",
|
|
47
|
-
},
|
|
155
|
+
start: { type: "string", description: "Start of date range (ISO 8601)" },
|
|
156
|
+
end: { type: "string", description: "End of date range (ISO 8601)" },
|
|
157
|
+
detail_level: DETAIL_LEVEL_PROP,
|
|
48
158
|
},
|
|
49
159
|
required: ["start", "end"],
|
|
50
160
|
},
|
|
161
|
+
outputSchema: eventListSchema,
|
|
162
|
+
handler: (args, service) => run(async () => ok({
|
|
163
|
+
events: await fetchEvents(service, args.calendar, args.start, args.end, args.detail_level ?? "summary"),
|
|
164
|
+
})),
|
|
51
165
|
},
|
|
52
166
|
{
|
|
53
167
|
name: "get_today_events",
|
|
168
|
+
title: "Get Today's Events",
|
|
54
169
|
description: "Get all events for today. Convenience wrapper over list_events.",
|
|
55
|
-
annotations:
|
|
170
|
+
annotations: READ_ONLY,
|
|
56
171
|
inputSchema: {
|
|
57
172
|
type: "object",
|
|
58
173
|
properties: {
|
|
@@ -60,25 +175,29 @@ export const CALENDAR_TOOLS = [
|
|
|
60
175
|
type: "string",
|
|
61
176
|
description: "Provider-prefixed calendar ID. If omitted, queries all calendars.",
|
|
62
177
|
},
|
|
63
|
-
detail_level:
|
|
64
|
-
type: "string",
|
|
65
|
-
enum: ["summary", "full"],
|
|
66
|
-
description: "Response verbosity (default: summary)",
|
|
67
|
-
},
|
|
178
|
+
detail_level: DETAIL_LEVEL_PROP,
|
|
68
179
|
},
|
|
69
180
|
},
|
|
181
|
+
outputSchema: eventListSchema,
|
|
182
|
+
handler: (args, service) => run(async () => {
|
|
183
|
+
const tz = getTimezone();
|
|
184
|
+
const { year, month, day } = getLocalDateParts(new Date(), tz);
|
|
185
|
+
const todayStart = zonedTimeToUtc(year, month, day, 0, 0, tz).toISOString();
|
|
186
|
+
const todayEnd = zonedTimeToUtc(year, month, day + 1, 0, 0, tz).toISOString();
|
|
187
|
+
return ok({
|
|
188
|
+
events: await fetchEvents(service, args.calendar, todayStart, todayEnd, args.detail_level ?? "summary"),
|
|
189
|
+
});
|
|
190
|
+
}),
|
|
70
191
|
},
|
|
71
192
|
{
|
|
72
193
|
name: "search_events",
|
|
194
|
+
title: "Search Events",
|
|
73
195
|
description: "Keyword search across event title, description, and location.",
|
|
74
|
-
annotations:
|
|
196
|
+
annotations: READ_ONLY,
|
|
75
197
|
inputSchema: {
|
|
76
198
|
type: "object",
|
|
77
199
|
properties: {
|
|
78
|
-
query: {
|
|
79
|
-
type: "string",
|
|
80
|
-
description: "Search term",
|
|
81
|
-
},
|
|
200
|
+
query: { type: "string", description: "Search term" },
|
|
82
201
|
calendar: {
|
|
83
202
|
type: "string",
|
|
84
203
|
description: "Provider-prefixed calendar ID. If omitted, searches all calendars.",
|
|
@@ -91,90 +210,66 @@ export const CALENDAR_TOOLS = [
|
|
|
91
210
|
type: "string",
|
|
92
211
|
description: "Range end (ISO 8601). Defaults to 90 days ahead.",
|
|
93
212
|
},
|
|
94
|
-
detail_level:
|
|
95
|
-
type: "string",
|
|
96
|
-
enum: ["summary", "full"],
|
|
97
|
-
description: "Response verbosity (default: summary)",
|
|
98
|
-
},
|
|
213
|
+
detail_level: DETAIL_LEVEL_PROP,
|
|
99
214
|
},
|
|
100
215
|
required: ["query"],
|
|
101
216
|
},
|
|
217
|
+
outputSchema: eventListSchema,
|
|
218
|
+
handler: (args, service) => run(async () => {
|
|
219
|
+
const query = args.query.toLowerCase();
|
|
220
|
+
const detailLevel = args.detail_level ?? "summary";
|
|
221
|
+
const now = new Date();
|
|
222
|
+
const start = args.start ?? new Date(now.getTime() - 90 * 86400000).toISOString();
|
|
223
|
+
const end = args.end ?? new Date(now.getTime() + 90 * 86400000).toISOString();
|
|
224
|
+
const events = await fetchEvents(service, args.calendar, start, end, detailLevel);
|
|
225
|
+
const matched = events.filter((e) => {
|
|
226
|
+
const title = e.title?.toLowerCase() ?? "";
|
|
227
|
+
const location = e.location?.toLowerCase() ?? "";
|
|
228
|
+
const description = detailLevel === "full" ? (e.description?.toLowerCase() ?? "") : "";
|
|
229
|
+
return title.includes(query) || location.includes(query) || description.includes(query);
|
|
230
|
+
});
|
|
231
|
+
return ok({ events: matched });
|
|
232
|
+
}),
|
|
102
233
|
},
|
|
103
234
|
{
|
|
104
235
|
name: "get_event",
|
|
236
|
+
title: "Get Event",
|
|
105
237
|
description: "Get full details of a single event by calendar and UID.",
|
|
106
|
-
annotations:
|
|
238
|
+
annotations: READ_ONLY,
|
|
107
239
|
inputSchema: {
|
|
108
240
|
type: "object",
|
|
109
241
|
properties: {
|
|
110
|
-
calendar:
|
|
111
|
-
type: "string",
|
|
112
|
-
description: "Provider-prefixed calendar ID",
|
|
113
|
-
},
|
|
242
|
+
calendar: CALENDAR_PROP,
|
|
114
243
|
uid: { type: "string", description: "Event UID" },
|
|
115
244
|
},
|
|
116
245
|
required: ["calendar", "uid"],
|
|
117
246
|
},
|
|
247
|
+
outputSchema: singleEventSchema,
|
|
248
|
+
handler: (args, service) => run(async () => ok({ event: await service.getEvent(args.calendar, args.uid) })),
|
|
118
249
|
},
|
|
119
250
|
{
|
|
120
251
|
name: "create_event",
|
|
252
|
+
title: "Create Event",
|
|
121
253
|
description: "Create a new calendar event.",
|
|
254
|
+
annotations: {
|
|
255
|
+
readOnlyHint: false,
|
|
256
|
+
destructiveHint: false,
|
|
257
|
+
idempotentHint: false,
|
|
258
|
+
openWorldHint: true,
|
|
259
|
+
},
|
|
122
260
|
inputSchema: {
|
|
123
261
|
type: "object",
|
|
124
262
|
properties: {
|
|
125
|
-
calendar:
|
|
126
|
-
type: "string",
|
|
127
|
-
description: "Provider-prefixed calendar ID",
|
|
128
|
-
},
|
|
263
|
+
calendar: CALENDAR_PROP,
|
|
129
264
|
title: { type: "string", description: "Event title" },
|
|
130
|
-
start: {
|
|
131
|
-
type: "string",
|
|
132
|
-
description: "Start time (ISO 8601)",
|
|
133
|
-
},
|
|
265
|
+
start: { type: "string", description: "Start time (ISO 8601)" },
|
|
134
266
|
end: { type: "string", description: "End time (ISO 8601)" },
|
|
135
|
-
all_day: {
|
|
136
|
-
type: "boolean",
|
|
137
|
-
description: "All-day event flag (default: false)",
|
|
138
|
-
},
|
|
267
|
+
all_day: { type: "boolean", description: "All-day event flag (default: false)" },
|
|
139
268
|
location: { type: "string", description: "Event location" },
|
|
140
|
-
description: {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
attendees: {
|
|
145
|
-
type: "array",
|
|
146
|
-
items: {
|
|
147
|
-
type: "object",
|
|
148
|
-
properties: {
|
|
149
|
-
email: {
|
|
150
|
-
type: "string",
|
|
151
|
-
description: "Attendee email address. Display name is resolved server-side from the invitee's address book.",
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
required: ["email"],
|
|
155
|
-
},
|
|
156
|
-
description: "List of attendee email addresses to invite.",
|
|
157
|
-
},
|
|
158
|
-
alarms: {
|
|
159
|
-
type: "array",
|
|
160
|
-
items: {
|
|
161
|
-
type: "object",
|
|
162
|
-
properties: {
|
|
163
|
-
type: { type: "string", enum: ["relative", "absolute"], description: "Alarm type" },
|
|
164
|
-
trigger: {
|
|
165
|
-
type: ["string", "number"],
|
|
166
|
-
description: "Seconds offset (negative=before event) for relative, or ISO 8601 datetime for absolute",
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
required: ["type", "trigger"],
|
|
170
|
-
},
|
|
171
|
-
description: "Event reminders/alarms",
|
|
172
|
-
},
|
|
173
|
-
categories: {
|
|
174
|
-
type: "array",
|
|
175
|
-
items: { type: "string", description: "Category / tag name" },
|
|
176
|
-
description: "Event categories/tags",
|
|
177
|
-
},
|
|
269
|
+
description: { type: "string", description: "Event description" },
|
|
270
|
+
attendees: ATTENDEES_PROP("List of attendee email addresses to invite."),
|
|
271
|
+
alarms: ALARMS_PROP,
|
|
272
|
+
categories: CATEGORIES_PROP,
|
|
178
273
|
recurrence_rule: {
|
|
179
274
|
type: "string",
|
|
180
275
|
description: "RFC 5545 RRULE string for a recurring event (e.g., 'FREQ=WEEKLY;BYDAY=MO,WE,FR' or 'FREQ=MONTHLY;BYDAY=+3FR;COUNT=12'). Accepted with or without the 'RRULE:' prefix. FREQ is required.",
|
|
@@ -187,73 +282,65 @@ export const CALENDAR_TOOLS = [
|
|
|
187
282
|
},
|
|
188
283
|
required: ["calendar", "title", "start", "end"],
|
|
189
284
|
},
|
|
285
|
+
outputSchema: singleEventSchema,
|
|
286
|
+
handler: (args, service) => run(async () => {
|
|
287
|
+
try {
|
|
288
|
+
// Populate ORGANIZER whenever attendees are present — CalDAV servers
|
|
289
|
+
// (SOGo/mailbox.org) reject ATTENDEE-without-ORGANIZER PUTs with 412.
|
|
290
|
+
const organizer = args.attendees && args.attendees.length > 0
|
|
291
|
+
? { email: service.getAccountEmail(args.calendar) }
|
|
292
|
+
: undefined;
|
|
293
|
+
const icsString = generateEventIcs({
|
|
294
|
+
title: args.title,
|
|
295
|
+
start: args.start,
|
|
296
|
+
end: args.end,
|
|
297
|
+
all_day: args.all_day ?? false,
|
|
298
|
+
location: args.location,
|
|
299
|
+
description: args.description,
|
|
300
|
+
attendees: args.attendees,
|
|
301
|
+
alarms: args.alarms,
|
|
302
|
+
categories: args.categories,
|
|
303
|
+
recurrence_rule: args.recurrence_rule,
|
|
304
|
+
organizer,
|
|
305
|
+
availability: args.availability,
|
|
306
|
+
timezone: getTimezone(),
|
|
307
|
+
});
|
|
308
|
+
const uidMatch = icsString.match(/UID:(.+)/);
|
|
309
|
+
const uid = uidMatch ? uidMatch[1].trim() : crypto.randomUUID();
|
|
310
|
+
return ok({ event: await service.createEvent(args.calendar, icsString, uid) });
|
|
311
|
+
}
|
|
312
|
+
catch (err) {
|
|
313
|
+
if (err instanceof Error && err.message.startsWith("Invalid recurrence_rule:")) {
|
|
314
|
+
return calFail("validation_error", err.message);
|
|
315
|
+
}
|
|
316
|
+
throw err;
|
|
317
|
+
}
|
|
318
|
+
}),
|
|
190
319
|
},
|
|
191
320
|
{
|
|
192
321
|
name: "update_event",
|
|
322
|
+
title: "Update Event",
|
|
193
323
|
description: "Update an existing event. Only provided fields are changed.",
|
|
324
|
+
annotations: {
|
|
325
|
+
readOnlyHint: false,
|
|
326
|
+
destructiveHint: false,
|
|
327
|
+
idempotentHint: true,
|
|
328
|
+
openWorldHint: true,
|
|
329
|
+
},
|
|
194
330
|
inputSchema: {
|
|
195
331
|
type: "object",
|
|
196
332
|
properties: {
|
|
197
|
-
calendar:
|
|
198
|
-
|
|
199
|
-
description: "Provider-prefixed calendar ID",
|
|
200
|
-
},
|
|
201
|
-
uid: {
|
|
202
|
-
type: "string",
|
|
203
|
-
description: "Event UID to update",
|
|
204
|
-
},
|
|
333
|
+
calendar: CALENDAR_PROP,
|
|
334
|
+
uid: { type: "string", description: "Event UID to update" },
|
|
205
335
|
title: { type: "string", description: "New event title" },
|
|
206
|
-
start: {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
},
|
|
210
|
-
end: {
|
|
211
|
-
type: "string",
|
|
212
|
-
description: "New end time (ISO 8601)",
|
|
213
|
-
},
|
|
214
|
-
all_day: {
|
|
215
|
-
type: "boolean",
|
|
216
|
-
description: "All-day event flag",
|
|
217
|
-
},
|
|
336
|
+
start: { type: "string", description: "New start time (ISO 8601)" },
|
|
337
|
+
end: { type: "string", description: "New end time (ISO 8601)" },
|
|
338
|
+
all_day: { type: "boolean", description: "All-day event flag" },
|
|
218
339
|
location: { type: "string", description: "New location" },
|
|
219
|
-
description: {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
attendees: {
|
|
224
|
-
type: "array",
|
|
225
|
-
items: {
|
|
226
|
-
type: "object",
|
|
227
|
-
properties: {
|
|
228
|
-
email: {
|
|
229
|
-
type: "string",
|
|
230
|
-
description: "Attendee email address. Display name is resolved server-side from the invitee's address book.",
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
required: ["email"],
|
|
234
|
-
},
|
|
235
|
-
description: "New attendee list (replaces existing).",
|
|
236
|
-
},
|
|
237
|
-
alarms: {
|
|
238
|
-
type: "array",
|
|
239
|
-
items: {
|
|
240
|
-
type: "object",
|
|
241
|
-
properties: {
|
|
242
|
-
type: { type: "string", enum: ["relative", "absolute"], description: "Alarm type" },
|
|
243
|
-
trigger: {
|
|
244
|
-
type: ["string", "number"],
|
|
245
|
-
description: "Seconds offset (negative=before event) for relative, or ISO 8601 datetime for absolute",
|
|
246
|
-
},
|
|
247
|
-
},
|
|
248
|
-
required: ["type", "trigger"],
|
|
249
|
-
},
|
|
250
|
-
description: "Event reminders/alarms",
|
|
251
|
-
},
|
|
252
|
-
categories: {
|
|
253
|
-
type: "array",
|
|
254
|
-
items: { type: "string", description: "Category / tag name" },
|
|
255
|
-
description: "Event categories/tags",
|
|
256
|
-
},
|
|
340
|
+
description: { type: "string", description: "New description" },
|
|
341
|
+
attendees: ATTENDEES_PROP("New attendee list (replaces existing)."),
|
|
342
|
+
alarms: ALARMS_PROP,
|
|
343
|
+
categories: CATEGORIES_PROP,
|
|
257
344
|
occurrence_date: {
|
|
258
345
|
type: "string",
|
|
259
346
|
description: "ISO 8601 date of the specific occurrence to modify. Required when span is 'this' on a recurring event. Get this value from list_events results.",
|
|
@@ -271,22 +358,158 @@ export const CALENDAR_TOOLS = [
|
|
|
271
358
|
},
|
|
272
359
|
required: ["calendar", "uid"],
|
|
273
360
|
},
|
|
361
|
+
outputSchema: singleEventSchema,
|
|
362
|
+
handler: (args, service) => run(async () => {
|
|
363
|
+
const span = args.span ?? "this";
|
|
364
|
+
const { event: existing } = await service.getEventWithMeta(args.calendar, args.uid);
|
|
365
|
+
// span="this" on a recurring event: create exception VEVENT
|
|
366
|
+
if (existing.is_recurring && span === "this") {
|
|
367
|
+
if (!args.occurrence_date) {
|
|
368
|
+
return calFail("validation_error", "occurrence_date is required when span is 'this' on a recurring event");
|
|
369
|
+
}
|
|
370
|
+
const occurrenceDate = args.occurrence_date;
|
|
371
|
+
const rawObj = await service.fetchRawCalendarObject(args.calendar, args.uid);
|
|
372
|
+
const overrides = {};
|
|
373
|
+
for (const field of [
|
|
374
|
+
"title",
|
|
375
|
+
"start",
|
|
376
|
+
"end",
|
|
377
|
+
"all_day",
|
|
378
|
+
"location",
|
|
379
|
+
"description",
|
|
380
|
+
"attendees",
|
|
381
|
+
"alarms",
|
|
382
|
+
"categories",
|
|
383
|
+
"availability",
|
|
384
|
+
]) {
|
|
385
|
+
if (args[field] !== undefined) {
|
|
386
|
+
// field-by-field copy across a union of value types
|
|
387
|
+
overrides[field] = args[field];
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// If the effective event will have attendees but no organizer yet,
|
|
391
|
+
// inject one so the CalDAV PUT satisfies server scheduling preconditions.
|
|
392
|
+
const effectiveAttendees = overrides.attendees ?? existing.attendees;
|
|
393
|
+
if (effectiveAttendees && effectiveAttendees.length > 0 && !existing.organizer) {
|
|
394
|
+
overrides.organizer = { email: service.getAccountEmail(args.calendar) };
|
|
395
|
+
}
|
|
396
|
+
const exceptionVevent = createExceptionComponent(rawObj.data, "vevent", occurrenceDate, overrides, existing.all_day);
|
|
397
|
+
const combinedIcs = combineIcsComponents(rawObj.data, exceptionVevent);
|
|
398
|
+
await service.updateEvent(args.calendar, args.uid, combinedIcs, {
|
|
399
|
+
url: rawObj.url,
|
|
400
|
+
etag: rawObj.etag,
|
|
401
|
+
});
|
|
402
|
+
// Build response from overrides + existing. Unlike every other write
|
|
403
|
+
// path, this one does not re-read the event afterwards, so anything
|
|
404
|
+
// taken from `overrides` is in the tool's *input* shape and has to be
|
|
405
|
+
// widened to the shape a read would return — otherwise the result
|
|
406
|
+
// fails validation against this tool's own outputSchema.
|
|
407
|
+
const occDuration = new Date(existing.end).getTime() - new Date(existing.start).getTime();
|
|
408
|
+
return ok({
|
|
409
|
+
event: {
|
|
410
|
+
...existing,
|
|
411
|
+
title: overrides.title ?? existing.title,
|
|
412
|
+
start: overrides.start ?? occurrenceDate,
|
|
413
|
+
end: overrides.end ??
|
|
414
|
+
new Date(new Date(occurrenceDate).getTime() + occDuration).toISOString(),
|
|
415
|
+
all_day: overrides.all_day ?? existing.all_day,
|
|
416
|
+
location: overrides.location ?? existing.location,
|
|
417
|
+
description: overrides.description ?? existing.description,
|
|
418
|
+
attendees: overrides.attendees
|
|
419
|
+
? overrides.attendees.map(toResponseAttendee)
|
|
420
|
+
: existing.attendees,
|
|
421
|
+
alarms: overrides.alarms ? overrides.alarms.map(toResponseAlarm) : existing.alarms,
|
|
422
|
+
organizer: overrides.organizer
|
|
423
|
+
? { name: overrides.organizer.name ?? null, email: overrides.organizer.email }
|
|
424
|
+
: existing.organizer,
|
|
425
|
+
categories: overrides.categories ?? existing.categories,
|
|
426
|
+
occurrence_date: occurrenceDate,
|
|
427
|
+
recurrence_rule: null,
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
// Non-exception path (span "all", or non-recurring event): mutate the
|
|
432
|
+
// existing object in place — preserves RRULE/EXDATE/RDATE, exception
|
|
433
|
+
// overrides, attendee participation state, STATUS, and unknown props.
|
|
434
|
+
const rawObj = await service.fetchRawCalendarObject(args.calendar, args.uid);
|
|
435
|
+
const updates = { timezone: getTimezone() };
|
|
436
|
+
for (const field of [
|
|
437
|
+
"title",
|
|
438
|
+
"start",
|
|
439
|
+
"end",
|
|
440
|
+
"all_day",
|
|
441
|
+
"location",
|
|
442
|
+
"description",
|
|
443
|
+
"attendees",
|
|
444
|
+
"alarms",
|
|
445
|
+
"categories",
|
|
446
|
+
"availability",
|
|
447
|
+
]) {
|
|
448
|
+
if (args[field] !== undefined) {
|
|
449
|
+
// field-by-field copy across a union of value types
|
|
450
|
+
updates[field] = args[field];
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// Inject ORGANIZER only when the event will have attendees but has none
|
|
454
|
+
// (CalDAV scheduling servers reject ATTENDEE-without-ORGANIZER, RFC 6638).
|
|
455
|
+
const effectiveAttendees = updates.attendees ?? existing.attendees;
|
|
456
|
+
if (effectiveAttendees && effectiveAttendees.length > 0 && !existing.organizer) {
|
|
457
|
+
updates.organizer = { email: service.getAccountEmail(args.calendar) };
|
|
458
|
+
}
|
|
459
|
+
const updatedIcs = updateMasterEventIcs(rawObj.data, updates);
|
|
460
|
+
return ok({
|
|
461
|
+
event: await service.updateEvent(args.calendar, args.uid, updatedIcs, {
|
|
462
|
+
url: rawObj.url,
|
|
463
|
+
etag: rawObj.etag,
|
|
464
|
+
}),
|
|
465
|
+
});
|
|
466
|
+
}),
|
|
274
467
|
},
|
|
275
468
|
{
|
|
276
|
-
name: "
|
|
277
|
-
|
|
278
|
-
|
|
469
|
+
name: "move_event",
|
|
470
|
+
title: "Move Event",
|
|
471
|
+
description: "Move an event to another calendar, equivalent to reassigning its calendar in a CalDAV client. Both calendars must belong to the same account.",
|
|
472
|
+
annotations: {
|
|
473
|
+
readOnlyHint: false,
|
|
474
|
+
destructiveHint: false,
|
|
475
|
+
idempotentHint: true,
|
|
476
|
+
openWorldHint: true,
|
|
477
|
+
},
|
|
279
478
|
inputSchema: {
|
|
280
479
|
type: "object",
|
|
281
480
|
properties: {
|
|
282
|
-
calendar:
|
|
481
|
+
calendar: CALENDAR_PROP,
|
|
482
|
+
uid: { type: "string", description: "Event UID to move" },
|
|
483
|
+
target_calendar: {
|
|
283
484
|
type: "string",
|
|
284
|
-
description: "
|
|
285
|
-
},
|
|
286
|
-
uid: {
|
|
287
|
-
type: "string",
|
|
288
|
-
description: "Event UID to delete",
|
|
485
|
+
description: "Destination provider-prefixed calendar ID, on the same account",
|
|
289
486
|
},
|
|
487
|
+
},
|
|
488
|
+
required: ["calendar", "uid", "target_calendar"],
|
|
489
|
+
},
|
|
490
|
+
outputSchema: singleEventSchema,
|
|
491
|
+
handler: (args, service) => run(async () => {
|
|
492
|
+
const { meta } = await service.getEventWithMeta(args.calendar, args.uid);
|
|
493
|
+
return ok({
|
|
494
|
+
event: await service.moveEvent(args.calendar, args.uid, args.target_calendar, meta),
|
|
495
|
+
});
|
|
496
|
+
}),
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
name: "delete_event",
|
|
500
|
+
title: "Delete Event",
|
|
501
|
+
description: "Delete a calendar event by UID. Asks the user to confirm first, unless it is excluding a single occurrence of a recurring event (span 'this'), which is recoverable.",
|
|
502
|
+
annotations: {
|
|
503
|
+
readOnlyHint: false,
|
|
504
|
+
destructiveHint: true,
|
|
505
|
+
idempotentHint: true,
|
|
506
|
+
openWorldHint: true,
|
|
507
|
+
},
|
|
508
|
+
inputSchema: {
|
|
509
|
+
type: "object",
|
|
510
|
+
properties: {
|
|
511
|
+
calendar: CALENDAR_PROP,
|
|
512
|
+
uid: { type: "string", description: "Event UID to delete" },
|
|
290
513
|
occurrence_date: {
|
|
291
514
|
type: "string",
|
|
292
515
|
description: "ISO 8601 date of the specific occurrence to delete. Required when span is 'this' on a recurring event. Get this value from list_events results.",
|
|
@@ -299,51 +522,81 @@ export const CALENDAR_TOOLS = [
|
|
|
299
522
|
},
|
|
300
523
|
required: ["calendar", "uid"],
|
|
301
524
|
},
|
|
525
|
+
outputSchema: deleteResultSchema,
|
|
526
|
+
handler: async (args, service, ctx) => {
|
|
527
|
+
const span = args.span ?? "all";
|
|
528
|
+
// Excluding one occurrence of a recurring event is recoverable — the
|
|
529
|
+
// occurrence can be re-added. EVERY other path removes the calendar
|
|
530
|
+
// object outright, including `span: "this"` on a NON-recurring event,
|
|
531
|
+
// which is the same irreversible delete as `span: "all"`. So recurrence
|
|
532
|
+
// has to be resolved before deciding whether to gate; keying the gate off
|
|
533
|
+
// `span` alone would let an agent delete a one-off event unconfirmed
|
|
534
|
+
// simply by passing the narrower-sounding span.
|
|
535
|
+
if (span === "this") {
|
|
536
|
+
return run(async () => {
|
|
537
|
+
const { event: existing, meta: eventMeta } = await service.getEventWithMeta(args.calendar, args.uid);
|
|
538
|
+
if (existing.is_recurring) {
|
|
539
|
+
if (!args.occurrence_date) {
|
|
540
|
+
return calFail("validation_error", "occurrence_date is required when span is 'this' on a recurring event");
|
|
541
|
+
}
|
|
542
|
+
const occurrenceDate = args.occurrence_date;
|
|
543
|
+
const rawObj = await service.fetchRawCalendarObject(args.calendar, args.uid);
|
|
544
|
+
let updatedIcs = addExdateToIcs(rawObj.data, occurrenceDate, existing.all_day);
|
|
545
|
+
// Remove any existing exception VEVENT/VTODO for this occurrence,
|
|
546
|
+
// matching by resolved instant (handles both UTC and TZID-form
|
|
547
|
+
// RECURRENCE-ID) rather than a literal-text regex.
|
|
548
|
+
updatedIcs = removeExceptionFromIcs(updatedIcs, occurrenceDate, existing.all_day);
|
|
549
|
+
await service.updateEvent(args.calendar, args.uid, updatedIcs, {
|
|
550
|
+
url: rawObj.url,
|
|
551
|
+
etag: rawObj.etag,
|
|
552
|
+
});
|
|
553
|
+
return ok({ deleted: true, uid: args.uid });
|
|
554
|
+
}
|
|
555
|
+
// Non-recurring: there is no occurrence to exclude, so this deletes
|
|
556
|
+
// the whole event. Gate it exactly like `span: "all"`.
|
|
557
|
+
const gate = confirmDestructive(ctx, "confirm_delete_event", `Delete event ${args.uid} from ${args.calendar}? This cannot be undone.`);
|
|
558
|
+
if (gate.status === "interrupt")
|
|
559
|
+
return gate.result;
|
|
560
|
+
await service.deleteEvent(args.calendar, args.uid, eventMeta);
|
|
561
|
+
return ok({ deleted: true, uid: args.uid });
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
// span="all" — removes the calendar object, series and all.
|
|
565
|
+
const gate = confirmDestructive(ctx, "confirm_delete_event", `Delete event ${args.uid} from ${args.calendar}? If it recurs, the entire series is removed. This cannot be undone.`);
|
|
566
|
+
if (gate.status === "interrupt")
|
|
567
|
+
return gate.result;
|
|
568
|
+
return run(async () => {
|
|
569
|
+
await service.deleteEvent(args.calendar, args.uid);
|
|
570
|
+
return ok({ deleted: true, uid: args.uid });
|
|
571
|
+
});
|
|
572
|
+
},
|
|
302
573
|
},
|
|
303
574
|
{
|
|
304
575
|
name: "create_events_batch",
|
|
576
|
+
title: "Create Events (Batch)",
|
|
305
577
|
description: "Create multiple events at once. Returns created event count.",
|
|
578
|
+
annotations: {
|
|
579
|
+
readOnlyHint: false,
|
|
580
|
+
destructiveHint: false,
|
|
581
|
+
idempotentHint: false,
|
|
582
|
+
openWorldHint: true,
|
|
583
|
+
},
|
|
306
584
|
inputSchema: {
|
|
307
585
|
type: "object",
|
|
308
586
|
properties: {
|
|
309
|
-
calendar:
|
|
310
|
-
type: "string",
|
|
311
|
-
description: "Provider-prefixed calendar ID",
|
|
312
|
-
},
|
|
587
|
+
calendar: CALENDAR_PROP,
|
|
313
588
|
events: {
|
|
314
589
|
type: "array",
|
|
315
590
|
items: {
|
|
316
591
|
type: "object",
|
|
317
592
|
properties: {
|
|
318
593
|
title: { type: "string", description: "Event title" },
|
|
319
|
-
start: {
|
|
320
|
-
type: "string",
|
|
321
|
-
description: "Start time (ISO 8601)",
|
|
322
|
-
},
|
|
594
|
+
start: { type: "string", description: "Start time (ISO 8601)" },
|
|
323
595
|
end: { type: "string", description: "End time (ISO 8601)" },
|
|
324
|
-
all_day: {
|
|
325
|
-
type: "boolean",
|
|
326
|
-
description: "All-day event flag (default: false)",
|
|
327
|
-
},
|
|
596
|
+
all_day: { type: "boolean", description: "All-day event flag (default: false)" },
|
|
328
597
|
location: { type: "string", description: "Event location" },
|
|
329
|
-
description: {
|
|
330
|
-
|
|
331
|
-
description: "Event description",
|
|
332
|
-
},
|
|
333
|
-
attendees: {
|
|
334
|
-
type: "array",
|
|
335
|
-
items: {
|
|
336
|
-
type: "object",
|
|
337
|
-
properties: {
|
|
338
|
-
email: {
|
|
339
|
-
type: "string",
|
|
340
|
-
description: "Attendee email address. Display name is resolved server-side from the invitee's address book.",
|
|
341
|
-
},
|
|
342
|
-
},
|
|
343
|
-
required: ["email"],
|
|
344
|
-
},
|
|
345
|
-
description: "List of attendee email addresses to invite.",
|
|
346
|
-
},
|
|
598
|
+
description: { type: "string", description: "Event description" },
|
|
599
|
+
attendees: ATTENDEES_PROP("List of attendee email addresses to invite."),
|
|
347
600
|
alarms: {
|
|
348
601
|
type: "array",
|
|
349
602
|
items: {
|
|
@@ -362,11 +615,7 @@ export const CALENDAR_TOOLS = [
|
|
|
362
615
|
},
|
|
363
616
|
description: "Event reminders/alarms",
|
|
364
617
|
},
|
|
365
|
-
categories:
|
|
366
|
-
type: "array",
|
|
367
|
-
items: { type: "string", description: "Category / tag name" },
|
|
368
|
-
description: "Event categories/tags",
|
|
369
|
-
},
|
|
618
|
+
categories: CATEGORIES_PROP,
|
|
370
619
|
recurrence_rule: {
|
|
371
620
|
type: "string",
|
|
372
621
|
description: "RFC 5545 RRULE string for a recurring event (e.g., 'FREQ=WEEKLY;BYDAY=MO'). FREQ is required.",
|
|
@@ -384,29 +633,87 @@ export const CALENDAR_TOOLS = [
|
|
|
384
633
|
},
|
|
385
634
|
required: ["calendar", "events"],
|
|
386
635
|
},
|
|
636
|
+
outputSchema: batchCreateSchema,
|
|
637
|
+
handler: (args, service) => run(async () => {
|
|
638
|
+
const accountEmail = service.getAccountEmail(args.calendar);
|
|
639
|
+
const createdEvents = [];
|
|
640
|
+
try {
|
|
641
|
+
for (const input of args.events) {
|
|
642
|
+
const organizer = input.attendees && input.attendees.length > 0 ? { email: accountEmail } : undefined;
|
|
643
|
+
const icsString = generateEventIcs({
|
|
644
|
+
...input,
|
|
645
|
+
organizer,
|
|
646
|
+
timezone: getTimezone(),
|
|
647
|
+
});
|
|
648
|
+
const uidMatch = icsString.match(/UID:(.+)/);
|
|
649
|
+
const uid = uidMatch ? uidMatch[1].trim() : crypto.randomUUID();
|
|
650
|
+
createdEvents.push(await service.createEvent(args.calendar, icsString, uid));
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
catch (err) {
|
|
654
|
+
if (err instanceof Error && err.message.startsWith("Invalid recurrence_rule:")) {
|
|
655
|
+
return calFail("validation_error", err.message);
|
|
656
|
+
}
|
|
657
|
+
throw err;
|
|
658
|
+
}
|
|
659
|
+
return ok({ created: createdEvents.length, events: createdEvents });
|
|
660
|
+
}),
|
|
387
661
|
},
|
|
388
662
|
{
|
|
389
663
|
name: "import_ics",
|
|
664
|
+
title: "Import iCalendar",
|
|
390
665
|
description: "Import events from iCalendar (.ics) content into a calendar.",
|
|
666
|
+
annotations: {
|
|
667
|
+
readOnlyHint: false,
|
|
668
|
+
destructiveHint: false,
|
|
669
|
+
idempotentHint: false,
|
|
670
|
+
openWorldHint: true,
|
|
671
|
+
},
|
|
391
672
|
inputSchema: {
|
|
392
673
|
type: "object",
|
|
393
674
|
properties: {
|
|
394
|
-
calendar:
|
|
395
|
-
|
|
396
|
-
description: "Provider-prefixed calendar ID",
|
|
397
|
-
},
|
|
398
|
-
ics_content: {
|
|
399
|
-
type: "string",
|
|
400
|
-
description: "Raw iCalendar content string",
|
|
401
|
-
},
|
|
675
|
+
calendar: CALENDAR_PROP,
|
|
676
|
+
ics_content: { type: "string", description: "Raw iCalendar content string" },
|
|
402
677
|
},
|
|
403
678
|
required: ["calendar", "ics_content"],
|
|
404
679
|
},
|
|
680
|
+
outputSchema: importResultSchema,
|
|
681
|
+
handler: (args, service) => run(async () => {
|
|
682
|
+
const groups = splitIcsByUid(args.ics_content);
|
|
683
|
+
if (groups.length === 0) {
|
|
684
|
+
return calFail("validation_error", "No events found in ICS content");
|
|
685
|
+
}
|
|
686
|
+
const importedEvents = [];
|
|
687
|
+
const failed = [];
|
|
688
|
+
for (const group of groups) {
|
|
689
|
+
try {
|
|
690
|
+
await service.createEvent(args.calendar, group.ics, group.uid);
|
|
691
|
+
try {
|
|
692
|
+
importedEvents.push(await service.getEvent(args.calendar, group.uid));
|
|
693
|
+
}
|
|
694
|
+
catch {
|
|
695
|
+
importedEvents.push({ uid: group.uid });
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
catch (err) {
|
|
699
|
+
failed.push({
|
|
700
|
+
uid: group.uid,
|
|
701
|
+
message: err instanceof Error ? err.message : String(err),
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
return ok({
|
|
706
|
+
imported: importedEvents.length,
|
|
707
|
+
...(failed.length > 0 ? { failed } : {}),
|
|
708
|
+
events: importedEvents,
|
|
709
|
+
});
|
|
710
|
+
}),
|
|
405
711
|
},
|
|
406
712
|
{
|
|
407
713
|
name: "find_free_slots",
|
|
714
|
+
title: "Find Free Slots",
|
|
408
715
|
description: "Find available time slots across specified calendars. Returns free windows matching the requested duration.",
|
|
409
|
-
annotations:
|
|
716
|
+
annotations: READ_ONLY,
|
|
410
717
|
inputSchema: {
|
|
411
718
|
type: "object",
|
|
412
719
|
properties: {
|
|
@@ -415,18 +722,9 @@ export const CALENDAR_TOOLS = [
|
|
|
415
722
|
items: { type: "string", description: "Provider-prefixed calendar ID" },
|
|
416
723
|
description: "Provider-prefixed calendar IDs to check availability against. If omitted, uses all calendars.",
|
|
417
724
|
},
|
|
418
|
-
start: {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
},
|
|
422
|
-
end: {
|
|
423
|
-
type: "string",
|
|
424
|
-
description: "End of search range (ISO 8601)",
|
|
425
|
-
},
|
|
426
|
-
duration: {
|
|
427
|
-
type: "number",
|
|
428
|
-
description: "Minimum slot duration in minutes",
|
|
429
|
-
},
|
|
725
|
+
start: { type: "string", description: "Start of search range (ISO 8601)" },
|
|
726
|
+
end: { type: "string", description: "End of search range (ISO 8601)" },
|
|
727
|
+
duration: { type: "number", description: "Minimum slot duration in minutes" },
|
|
430
728
|
preferred_start: {
|
|
431
729
|
type: "string",
|
|
432
730
|
description: "Preferred earliest time (HH:MM, e.g., 08:00)",
|
|
@@ -451,328 +749,22 @@ export const CALENDAR_TOOLS = [
|
|
|
451
749
|
},
|
|
452
750
|
required: ["start", "end", "duration"],
|
|
453
751
|
},
|
|
752
|
+
outputSchema: freeSlotsSchema,
|
|
753
|
+
handler: (args, service) => run(async () => {
|
|
754
|
+
let calendarIds = args.calendars;
|
|
755
|
+
if (!calendarIds || calendarIds.length === 0) {
|
|
756
|
+
const allCals = await service.listCalendars();
|
|
757
|
+
calendarIds = allCals.map((c) => c.calendar_id);
|
|
758
|
+
}
|
|
759
|
+
const slots = await service.findFreeSlots(calendarIds, args.start, args.end, args.duration, {
|
|
760
|
+
preferredStart: args.preferred_start,
|
|
761
|
+
preferredEnd: args.preferred_end,
|
|
762
|
+
ignoreTentative: args.ignore_tentative ?? false,
|
|
763
|
+
excludeCalendars: args.exclude_calendars,
|
|
764
|
+
includeAllDayAsBusy: args.include_all_day_as_busy ?? false,
|
|
765
|
+
});
|
|
766
|
+
return ok({ slots, count: slots.length });
|
|
767
|
+
}),
|
|
454
768
|
},
|
|
455
769
|
];
|
|
456
|
-
function withDebug(payload) {
|
|
457
|
-
if (process.env.CAL_MCP_DEBUG !== "1")
|
|
458
|
-
return payload;
|
|
459
|
-
const timings = drainDebugTimings();
|
|
460
|
-
if (timings.length === 0)
|
|
461
|
-
return payload;
|
|
462
|
-
return { ...payload, _debug: { timings } };
|
|
463
|
-
}
|
|
464
|
-
function ok(payload) {
|
|
465
|
-
return {
|
|
466
|
-
content: [{ type: "text", text: JSON.stringify(withDebug(payload), null, 2) }],
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
function error(code, message) {
|
|
470
|
-
return {
|
|
471
|
-
content: [
|
|
472
|
-
{
|
|
473
|
-
type: "text",
|
|
474
|
-
text: JSON.stringify(withDebug({ error: code, message })),
|
|
475
|
-
},
|
|
476
|
-
],
|
|
477
|
-
isError: true,
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
export async function handleCalendarTool(name, args, service) {
|
|
481
|
-
try {
|
|
482
|
-
switch (name) {
|
|
483
|
-
case "list_calendars": {
|
|
484
|
-
const calendars = await service.listCalendars();
|
|
485
|
-
return ok({ calendars });
|
|
486
|
-
}
|
|
487
|
-
case "list_events": {
|
|
488
|
-
const calendar = args.calendar;
|
|
489
|
-
const detailLevel = args.detail_level ?? "summary";
|
|
490
|
-
const events = await fetchEvents(service, calendar, args.start, args.end, detailLevel);
|
|
491
|
-
return ok({ events });
|
|
492
|
-
}
|
|
493
|
-
case "get_today_events": {
|
|
494
|
-
const calendar = args.calendar;
|
|
495
|
-
const detailLevel = args.detail_level ?? "summary";
|
|
496
|
-
const tz = getTimezone();
|
|
497
|
-
const now = new Date();
|
|
498
|
-
const { year, month, day } = getLocalDateParts(now, tz);
|
|
499
|
-
const todayStart = zonedTimeToUtc(year, month, day, 0, 0, tz).toISOString();
|
|
500
|
-
const todayEnd = zonedTimeToUtc(year, month, day + 1, 0, 0, tz).toISOString();
|
|
501
|
-
const events = await fetchEvents(service, calendar, todayStart, todayEnd, detailLevel);
|
|
502
|
-
return ok({ events });
|
|
503
|
-
}
|
|
504
|
-
case "search_events": {
|
|
505
|
-
const query = args.query.toLowerCase();
|
|
506
|
-
const calendar = args.calendar;
|
|
507
|
-
const detailLevel = args.detail_level ?? "summary";
|
|
508
|
-
const now = new Date();
|
|
509
|
-
const start = args.start ?? new Date(now.getTime() - 90 * 86400000).toISOString();
|
|
510
|
-
const end = args.end ?? new Date(now.getTime() + 90 * 86400000).toISOString();
|
|
511
|
-
const events = await fetchEvents(service, calendar, start, end, detailLevel);
|
|
512
|
-
const matched = events.filter((e) => {
|
|
513
|
-
const title = e.title?.toLowerCase() ?? "";
|
|
514
|
-
const location = e.location?.toLowerCase() ?? "";
|
|
515
|
-
const description = detailLevel === "full" ? (e.description?.toLowerCase() ?? "") : "";
|
|
516
|
-
return title.includes(query) || location.includes(query) || description.includes(query);
|
|
517
|
-
});
|
|
518
|
-
return ok({ events: matched });
|
|
519
|
-
}
|
|
520
|
-
case "get_event": {
|
|
521
|
-
const event = await service.getEvent(args.calendar, args.uid);
|
|
522
|
-
return ok({ event });
|
|
523
|
-
}
|
|
524
|
-
case "create_event": {
|
|
525
|
-
try {
|
|
526
|
-
const attendees = args.attendees;
|
|
527
|
-
const calendarId = args.calendar;
|
|
528
|
-
// Populate ORGANIZER whenever attendees are present — CalDAV servers
|
|
529
|
-
// (SOGo/mailbox.org) reject ATTENDEE-without-ORGANIZER PUTs with 412.
|
|
530
|
-
const organizer = attendees && attendees.length > 0
|
|
531
|
-
? { email: service.getAccountEmail(calendarId) }
|
|
532
|
-
: undefined;
|
|
533
|
-
const icsString = generateEventIcs({
|
|
534
|
-
title: args.title,
|
|
535
|
-
start: args.start,
|
|
536
|
-
end: args.end,
|
|
537
|
-
all_day: args.all_day ?? false,
|
|
538
|
-
location: args.location,
|
|
539
|
-
description: args.description,
|
|
540
|
-
attendees,
|
|
541
|
-
alarms: args.alarms,
|
|
542
|
-
categories: args.categories,
|
|
543
|
-
recurrence_rule: args.recurrence_rule,
|
|
544
|
-
organizer,
|
|
545
|
-
availability: args.availability,
|
|
546
|
-
timezone: getTimezone(),
|
|
547
|
-
});
|
|
548
|
-
const uidMatch = icsString.match(/UID:(.+)/);
|
|
549
|
-
const uid = uidMatch ? uidMatch[1].trim() : crypto.randomUUID();
|
|
550
|
-
const event = await service.createEvent(args.calendar, icsString, uid);
|
|
551
|
-
return ok({ event });
|
|
552
|
-
}
|
|
553
|
-
catch (err) {
|
|
554
|
-
if (err instanceof Error && err.message.startsWith("Invalid recurrence_rule:")) {
|
|
555
|
-
return error("validation_error", err.message);
|
|
556
|
-
}
|
|
557
|
-
throw err;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
case "update_event": {
|
|
561
|
-
const span = args.span ?? "this";
|
|
562
|
-
const { event: existing } = await service.getEventWithMeta(args.calendar, args.uid);
|
|
563
|
-
// span="this" on a recurring event: create exception VEVENT
|
|
564
|
-
if (existing.is_recurring && span === "this") {
|
|
565
|
-
const occurrenceDate = args.occurrence_date;
|
|
566
|
-
if (!occurrenceDate) {
|
|
567
|
-
return error("validation_error", "occurrence_date is required when span is 'this' on a recurring event");
|
|
568
|
-
}
|
|
569
|
-
const rawObj = await service.fetchRawCalendarObject(args.calendar, args.uid);
|
|
570
|
-
const overrides = {};
|
|
571
|
-
if (args.title !== undefined)
|
|
572
|
-
overrides.title = args.title;
|
|
573
|
-
if (args.start !== undefined)
|
|
574
|
-
overrides.start = args.start;
|
|
575
|
-
if (args.end !== undefined)
|
|
576
|
-
overrides.end = args.end;
|
|
577
|
-
if (args.all_day !== undefined)
|
|
578
|
-
overrides.all_day = args.all_day;
|
|
579
|
-
if (args.location !== undefined)
|
|
580
|
-
overrides.location = args.location;
|
|
581
|
-
if (args.description !== undefined)
|
|
582
|
-
overrides.description = args.description;
|
|
583
|
-
if (args.attendees !== undefined)
|
|
584
|
-
overrides.attendees = args.attendees;
|
|
585
|
-
if (args.alarms !== undefined)
|
|
586
|
-
overrides.alarms = args.alarms;
|
|
587
|
-
if (args.categories !== undefined)
|
|
588
|
-
overrides.categories = args.categories;
|
|
589
|
-
if (args.availability !== undefined)
|
|
590
|
-
overrides.availability = args.availability;
|
|
591
|
-
// If the effective event will have attendees but no organizer yet,
|
|
592
|
-
// inject one so the CalDAV PUT satisfies server scheduling preconditions.
|
|
593
|
-
const effectiveAttendees = overrides.attendees ?? existing.attendees;
|
|
594
|
-
if (effectiveAttendees && effectiveAttendees.length > 0 && !existing.organizer) {
|
|
595
|
-
overrides.organizer = { email: service.getAccountEmail(args.calendar) };
|
|
596
|
-
}
|
|
597
|
-
const exceptionVevent = createExceptionComponent(rawObj.data, "vevent", occurrenceDate, overrides, existing.all_day);
|
|
598
|
-
const combinedIcs = combineIcsComponents(rawObj.data, exceptionVevent);
|
|
599
|
-
await service.updateEvent(args.calendar, args.uid, combinedIcs, {
|
|
600
|
-
url: rawObj.url,
|
|
601
|
-
etag: rawObj.etag,
|
|
602
|
-
});
|
|
603
|
-
// Build response from overrides + existing
|
|
604
|
-
const occDuration = new Date(existing.end).getTime() - new Date(existing.start).getTime();
|
|
605
|
-
const responseEvent = {
|
|
606
|
-
...existing,
|
|
607
|
-
title: overrides.title ?? existing.title,
|
|
608
|
-
start: overrides.start ?? occurrenceDate,
|
|
609
|
-
end: overrides.end ??
|
|
610
|
-
new Date(new Date(occurrenceDate).getTime() + occDuration).toISOString(),
|
|
611
|
-
all_day: overrides.all_day ?? existing.all_day,
|
|
612
|
-
location: overrides.location ?? existing.location,
|
|
613
|
-
description: overrides.description ?? existing.description,
|
|
614
|
-
attendees: overrides.attendees ?? existing.attendees,
|
|
615
|
-
alarms: overrides.alarms ?? existing.alarms,
|
|
616
|
-
categories: overrides.categories ?? existing.categories,
|
|
617
|
-
occurrence_date: occurrenceDate,
|
|
618
|
-
recurrence_rule: null,
|
|
619
|
-
};
|
|
620
|
-
return ok({ event: responseEvent });
|
|
621
|
-
}
|
|
622
|
-
// Non-exception path (span "all", or non-recurring event): mutate the
|
|
623
|
-
// existing object in place — preserves RRULE/EXDATE/RDATE, exception
|
|
624
|
-
// overrides, attendee participation state, STATUS, and unknown props.
|
|
625
|
-
const rawObj = await service.fetchRawCalendarObject(args.calendar, args.uid);
|
|
626
|
-
const updates = { timezone: getTimezone() };
|
|
627
|
-
if (args.title !== undefined)
|
|
628
|
-
updates.title = args.title;
|
|
629
|
-
if (args.start !== undefined)
|
|
630
|
-
updates.start = args.start;
|
|
631
|
-
if (args.end !== undefined)
|
|
632
|
-
updates.end = args.end;
|
|
633
|
-
if (args.all_day !== undefined)
|
|
634
|
-
updates.all_day = args.all_day;
|
|
635
|
-
if (args.location !== undefined)
|
|
636
|
-
updates.location = args.location;
|
|
637
|
-
if (args.description !== undefined)
|
|
638
|
-
updates.description = args.description;
|
|
639
|
-
if (args.attendees !== undefined)
|
|
640
|
-
updates.attendees = args.attendees;
|
|
641
|
-
if (args.alarms !== undefined)
|
|
642
|
-
updates.alarms = args.alarms;
|
|
643
|
-
if (args.categories !== undefined)
|
|
644
|
-
updates.categories = args.categories;
|
|
645
|
-
if (args.availability !== undefined)
|
|
646
|
-
updates.availability = args.availability;
|
|
647
|
-
// Inject ORGANIZER only when the event will have attendees but has none
|
|
648
|
-
// (CalDAV scheduling servers reject ATTENDEE-without-ORGANIZER, RFC 6638).
|
|
649
|
-
const effectiveAttendees = updates.attendees ?? existing.attendees;
|
|
650
|
-
if (effectiveAttendees && effectiveAttendees.length > 0 && !existing.organizer) {
|
|
651
|
-
updates.organizer = { email: service.getAccountEmail(args.calendar) };
|
|
652
|
-
}
|
|
653
|
-
const updatedIcs = updateMasterEventIcs(rawObj.data, updates);
|
|
654
|
-
const event = await service.updateEvent(args.calendar, args.uid, updatedIcs, {
|
|
655
|
-
url: rawObj.url,
|
|
656
|
-
etag: rawObj.etag,
|
|
657
|
-
});
|
|
658
|
-
return ok({ event });
|
|
659
|
-
}
|
|
660
|
-
case "delete_event": {
|
|
661
|
-
const span = args.span ?? "all";
|
|
662
|
-
// span="this" on a recurring event: add EXDATE to exclude this occurrence
|
|
663
|
-
if (span === "this") {
|
|
664
|
-
const { event: existing, meta: eventMeta } = await service.getEventWithMeta(args.calendar, args.uid);
|
|
665
|
-
if (existing.is_recurring) {
|
|
666
|
-
const occurrenceDate = args.occurrence_date;
|
|
667
|
-
if (!occurrenceDate) {
|
|
668
|
-
return error("validation_error", "occurrence_date is required when span is 'this' on a recurring event");
|
|
669
|
-
}
|
|
670
|
-
const rawObj = await service.fetchRawCalendarObject(args.calendar, args.uid);
|
|
671
|
-
let updatedIcs = addExdateToIcs(rawObj.data, occurrenceDate, existing.all_day);
|
|
672
|
-
// Remove any existing exception VEVENT/VTODO for this occurrence,
|
|
673
|
-
// matching by resolved instant (handles both UTC and TZID-form
|
|
674
|
-
// RECURRENCE-ID) rather than a literal-text regex.
|
|
675
|
-
updatedIcs = removeExceptionFromIcs(updatedIcs, occurrenceDate, existing.all_day);
|
|
676
|
-
await service.updateEvent(args.calendar, args.uid, updatedIcs, {
|
|
677
|
-
url: rawObj.url,
|
|
678
|
-
etag: rawObj.etag,
|
|
679
|
-
});
|
|
680
|
-
return ok({ deleted: true, uid: args.uid });
|
|
681
|
-
}
|
|
682
|
-
// Non-recurring with span="this" — just delete normally
|
|
683
|
-
await service.deleteEvent(args.calendar, args.uid, eventMeta);
|
|
684
|
-
return ok({ deleted: true, uid: args.uid });
|
|
685
|
-
}
|
|
686
|
-
// span="all" — delete the entire calendar object
|
|
687
|
-
await service.deleteEvent(args.calendar, args.uid);
|
|
688
|
-
return ok({ deleted: true, uid: args.uid });
|
|
689
|
-
}
|
|
690
|
-
case "create_events_batch": {
|
|
691
|
-
const eventInputs = args.events;
|
|
692
|
-
const calendarId = args.calendar;
|
|
693
|
-
const accountEmail = service.getAccountEmail(calendarId);
|
|
694
|
-
const createdEvents = [];
|
|
695
|
-
try {
|
|
696
|
-
for (const input of eventInputs) {
|
|
697
|
-
const organizer = input.attendees && input.attendees.length > 0 ? { email: accountEmail } : undefined;
|
|
698
|
-
const icsString = generateEventIcs({
|
|
699
|
-
...input,
|
|
700
|
-
organizer,
|
|
701
|
-
timezone: getTimezone(),
|
|
702
|
-
});
|
|
703
|
-
const uidMatch = icsString.match(/UID:(.+)/);
|
|
704
|
-
const uid = uidMatch ? uidMatch[1].trim() : crypto.randomUUID();
|
|
705
|
-
const event = await service.createEvent(calendarId, icsString, uid);
|
|
706
|
-
createdEvents.push(event);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
catch (err) {
|
|
710
|
-
if (err instanceof Error && err.message.startsWith("Invalid recurrence_rule:")) {
|
|
711
|
-
return error("validation_error", err.message);
|
|
712
|
-
}
|
|
713
|
-
throw err;
|
|
714
|
-
}
|
|
715
|
-
return ok({ created: createdEvents.length, events: createdEvents });
|
|
716
|
-
}
|
|
717
|
-
case "import_ics": {
|
|
718
|
-
const icsContent = args.ics_content;
|
|
719
|
-
const groups = splitIcsByUid(icsContent);
|
|
720
|
-
if (groups.length === 0) {
|
|
721
|
-
return error("validation_error", "No events found in ICS content");
|
|
722
|
-
}
|
|
723
|
-
const importedEvents = [];
|
|
724
|
-
const failed = [];
|
|
725
|
-
for (const group of groups) {
|
|
726
|
-
try {
|
|
727
|
-
await service.createEvent(args.calendar, group.ics, group.uid);
|
|
728
|
-
try {
|
|
729
|
-
importedEvents.push(await service.getEvent(args.calendar, group.uid));
|
|
730
|
-
}
|
|
731
|
-
catch {
|
|
732
|
-
importedEvents.push({ uid: group.uid });
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
catch (err) {
|
|
736
|
-
failed.push({
|
|
737
|
-
uid: group.uid,
|
|
738
|
-
message: err instanceof Error ? err.message : String(err),
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
return ok({
|
|
743
|
-
imported: importedEvents.length,
|
|
744
|
-
...(failed.length > 0 ? { failed } : {}),
|
|
745
|
-
events: importedEvents,
|
|
746
|
-
});
|
|
747
|
-
}
|
|
748
|
-
case "find_free_slots": {
|
|
749
|
-
let calendarIds = args.calendars;
|
|
750
|
-
if (!calendarIds || calendarIds.length === 0) {
|
|
751
|
-
const allCals = await service.listCalendars();
|
|
752
|
-
calendarIds = allCals.map((c) => c.calendar_id);
|
|
753
|
-
}
|
|
754
|
-
const slots = await service.findFreeSlots(calendarIds, args.start, args.end, args.duration, {
|
|
755
|
-
preferredStart: args.preferred_start,
|
|
756
|
-
preferredEnd: args.preferred_end,
|
|
757
|
-
ignoreTentative: args.ignore_tentative ?? false,
|
|
758
|
-
excludeCalendars: args.exclude_calendars,
|
|
759
|
-
includeAllDayAsBusy: args.include_all_day_as_busy ?? false,
|
|
760
|
-
});
|
|
761
|
-
return ok({ slots, count: slots.length });
|
|
762
|
-
}
|
|
763
|
-
default:
|
|
764
|
-
return error("validation_error", `Unknown tool: ${name}`);
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
catch (err) {
|
|
768
|
-
if (err && typeof err === "object" && "code" in err) {
|
|
769
|
-
const calErr = err;
|
|
770
|
-
if (calErr.code === "CALENDAR_NOT_FOUND" || calErr.code === "EVENT_NOT_FOUND") {
|
|
771
|
-
return error("not_found", calErr.message);
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
const pimError = toPimError(err instanceof Error ? err : new Error(String(err)));
|
|
775
|
-
return error("backend_error", pimError.message);
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
770
|
//# sourceMappingURL=calendarTools.js.map
|