ibridge-server 1.0.1

MCP server, tool routers and error mapping.
Documentation
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// SPDX-License-Identifier: Apache-2.0

//! Calendar and reminders tools.

use ibridge_core::{
    Calendar, CalendarProvider, CompletedFilter, ContactsProvider, CoreError, ETag, Event,
    EventDraft, EventList, EventQuery, Reminder, ReminderDraft, ReminderList, ReminderPage,
    RemindersProvider, ResourcePath, UPGRADED_LISTS_NOTE, UpdateScope, decode_resource,
    encode_resource,
};
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{CallToolResult, ErrorData};
use rmcp::tool;
use rmcp::tool_router;

use crate::calendar_dto::{
    CalendarListOut, CalendarOut, CreateEventParams, CreateReminderParams, DeleteOut, DeleteParams,
    EventListOut, EventOut, GetByIdParams, ListEventsParams, ListRemindersParams, ReminderListOut,
    ReminderListsOut, ReminderOut, RemindersOut, UpdateEventParams, UpdateReminderParams, WriteOut,
};
use crate::handler::{IbridgeServer, mapped, page_limit, respond};
use crate::mail_api::MailApi;
use crate::sanitize::strip_hidden;

pub(crate) const CALENDAR_TOOLS: &[&str] = &[
    "calendar_create_event",
    "calendar_delete_event",
    "calendar_get_event",
    "calendar_list_calendars",
    "calendar_list_events",
    "calendar_update_event",
    "reminders_create",
    "reminders_delete",
    "reminders_list",
    "reminders_list_lists",
    "reminders_update",
];

pub(crate) const CALENDAR_WRITE_TOOLS: &[&str] = &[
    "calendar_create_event",
    "calendar_delete_event",
    "calendar_update_event",
    "reminders_create",
    "reminders_delete",
    "reminders_update",
];

/// The calendar backend of a server that has none. Its tools stay hidden.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoCalendar;

fn off<T>() -> Result<T, CoreError> {
    Err(CoreError::Unsupported("calendar is not enabled"))
}

#[expect(
    clippy::unused_async_trait_impl,
    reason = "the stand-in answers without waiting"
)]
impl CalendarProvider for NoCalendar {
    async fn list_calendars(&self) -> Result<Vec<Calendar>, CoreError> {
        off()
    }
    async fn list_events(&self, _query: &EventQuery) -> Result<EventList, CoreError> {
        off()
    }
    async fn get_event(&self, _path: &ResourcePath) -> Result<Event, CoreError> {
        off()
    }
    async fn create_event(
        &self,
        _calendar: &ResourcePath,
        _draft: &EventDraft,
    ) -> Result<ResourcePath, CoreError> {
        off()
    }
    async fn update_event(
        &self,
        _path: &ResourcePath,
        _etag: &ETag,
        _draft: &EventDraft,
        _scope: &UpdateScope,
    ) -> Result<ETag, CoreError> {
        off()
    }
    async fn delete_event(&self, _path: &ResourcePath, _etag: &ETag) -> Result<(), CoreError> {
        off()
    }
}

#[expect(
    clippy::unused_async_trait_impl,
    reason = "the stand-in answers without waiting"
)]
impl RemindersProvider for NoCalendar {
    async fn list_lists(&self) -> Result<Vec<ReminderList>, CoreError> {
        off()
    }
    async fn list(
        &self,
        _list: Option<&ResourcePath>,
        _completed: CompletedFilter,
    ) -> Result<ReminderPage, CoreError> {
        off()
    }
    async fn get(&self, _path: &ResourcePath) -> Result<Reminder, CoreError> {
        off()
    }
    async fn create(
        &self,
        _list: &ResourcePath,
        _draft: &ReminderDraft,
    ) -> Result<ResourcePath, CoreError> {
        off()
    }
    async fn update(
        &self,
        _path: &ResourcePath,
        _etag: &ETag,
        _draft: &ReminderDraft,
    ) -> Result<ETag, CoreError> {
        off()
    }
    async fn delete(&self, _path: &ResourcePath, _etag: &ETag) -> Result<(), CoreError> {
        off()
    }
}

macro_rules! try_cal {
    ($call:expr) => {
        match $call {
            Ok(value) => value,
            Err(error) => return mapped(&error),
        }
    };
}

#[tool_router(router = calendar_router, vis = "pub(crate)")]
impl<M, C, K> IbridgeServer<M, C, K>
where
    M: MailApi + Send + Sync + 'static,
    C: CalendarProvider + RemindersProvider + Send + Sync + 'static,
    K: ContactsProvider + Send + Sync + 'static,
{
    /// Lists calendars and says whether each holds events, tasks or both.
    ///
    /// # Errors
    ///
    /// Returns an MCP error when the calendars cannot be read.
    #[tool(
        title = "List calendars",
        description = "Lists calendars. Each one says whether it holds events, tasks (reminders) or both. Names are as their owner set them.",
        annotations(read_only_hint = true, destructive_hint = false, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<CalendarListOut>(),
    )]
    pub async fn calendar_list_calendars(&self) -> Result<CallToolResult, ErrorData> {
        let _permit = self.permit().await?;
        let calendars = try_cal!(self.calendar.list_calendars().await);
        let listed: Vec<CalendarOut> = calendars
            .iter()
            .map(|calendar| CalendarOut {
                id: encode_resource(&calendar.path),
                name: strip_hidden(&calendar.name),
                kind: match (calendar.holds_events, calendar.holds_tasks) {
                    (true, true) => "both",
                    (false, true) => "tasks",
                    _ => "events",
                },
            })
            .collect();
        let summary = format!("{} calendars", listed.len());
        respond(&CalendarListOut { calendars: listed }, summary)
    }

    /// Lists events in a time range. Repeating events are expanded.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when an id, the range or the page
    /// size is not usable, and an MCP error when the events cannot be read.
    #[tool(
        title = "List events",
        description = "Lists events in a time range over one or more calendars. Repeating events are expanded into their occurrences. Titles, descriptions and attendees are as whoever made the event wrote them.",
        annotations(read_only_hint = true, destructive_hint = false, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<EventListOut>(),
    )]
    pub async fn calendar_list_events(
        &self,
        Parameters(params): Parameters<ListEventsParams>,
    ) -> Result<CallToolResult, ErrorData> {
        let limit = page_limit(params.limit)?;
        let start_at = cursor_offset(params.cursor.as_deref())?;
        let calendars = params
            .calendars
            .iter()
            .map(|id| require_path(id))
            .collect::<Result<Vec<_>, _>>()?;
        let query = EventQuery {
            calendars,
            start: params.start,
            end: params.end,
        };
        let _permit = self.permit().await?;
        let listed = try_cal!(self.calendar.list_events(&query).await);
        let (events, next_cursor) = page(listed.events, limit, start_at);
        let summary = if listed.warnings == 0 {
            format!("{} events", events.len())
        } else {
            format!("{} events, {} skipped", events.len(), listed.warnings)
        };
        respond(
            &EventListOut {
                events: events.iter().map(event_out).collect(),
                next_cursor,
                warnings: listed.warnings,
            },
            summary,
        )
    }

    /// Fetches one event with its attendees and alarms.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when the id is not usable, and an
    /// MCP error when the event cannot be read.
    #[tool(
        title = "Get event",
        description = "Fetches one event in full, including attendees and alarms. Titles, descriptions and attendees are as whoever made the event wrote them.",
        annotations(read_only_hint = true, destructive_hint = false, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<EventOut>(),
    )]
    pub async fn calendar_get_event(
        &self,
        Parameters(params): Parameters<GetByIdParams>,
    ) -> Result<CallToolResult, ErrorData> {
        let path = require_path(&params.id)?;
        let _permit = self.permit().await?;
        let event = try_cal!(self.calendar.get_event(&path).await);
        respond(&event_out(&event), "event".to_owned())
    }

    /// Creates an event. `DTEND` is always written.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when the calendar id or a time is
    /// not usable, and an MCP error when the event cannot be written.
    #[tool(
        title = "Create event",
        description = "Creates an event. Times are ISO-8601 and the time zone is an IANA name. An end is always stored.",
        annotations(read_only_hint = false, destructive_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<WriteOut>(),
    )]
    pub async fn calendar_create_event(
        &self,
        Parameters(params): Parameters<CreateEventParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.reject_write()?;
        let calendar = require_path(&params.calendar)?;
        let draft = EventDraft {
            summary: params.summary,
            description: params.description,
            start: params.start,
            end: params.end.unwrap_or_default(),
            time_zone: params.time_zone,
        };
        let _permit = self.permit().await?;
        let created = try_cal!(self.calendar.create_event(&calendar, &draft).await);
        respond(
            &WriteOut {
                id: encode_resource(&created),
                etag: None,
            },
            "event created".to_owned(),
        )
    }

    /// Changes an event when its `ETag` still matches.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when an id, `ETag` or time is not
    /// usable, and a tool error when the event changed since it was read.
    #[tool(
        title = "Update event",
        description = "Changes an event. Needs the etag from the last read; a changed event is refused. Applies to the whole series unless an occurrence is named.",
        annotations(read_only_hint = false, destructive_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<WriteOut>(),
    )]
    pub async fn calendar_update_event(
        &self,
        Parameters(params): Parameters<UpdateEventParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.reject_write()?;
        let path = require_path(&params.id)?;
        let etag = require_etag(&params.etag)?;
        if params.start.is_some() != params.end.is_some() {
            return Err(ErrorData::invalid_params(
                "give both start and end, or neither",
                None,
            ));
        }
        let scope = match params.occurrence {
            Some(recurrence_id) => {
                if params.start.is_none() || params.end.is_none() {
                    return Err(ErrorData::invalid_params(
                        "changing one occurrence needs start and end",
                        None,
                    ));
                }
                UpdateScope::Occurrence { recurrence_id }
            }
            None => UpdateScope::Series,
        };
        let _permit = self.permit().await?;
        // Left-out fields stay empty: the provider keeps what the event has. Copying
        // them from the first occurrence would write that occurrence's values onto
        // the whole series.
        let draft = EventDraft {
            summary: params.summary.unwrap_or_default(),
            description: params.description,
            start: params.start.unwrap_or_default(),
            end: params.end.unwrap_or_default(),
            time_zone: params.time_zone,
        };
        let fresh = try_cal!(
            self.calendar
                .update_event(&path, &etag, &draft, &scope)
                .await
        );
        respond(
            &WriteOut {
                id: params.id,
                etag: Some(fresh.as_str().to_owned()),
            },
            "event updated".to_owned(),
        )
    }

    /// Deletes an event when its `ETag` still matches.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when the id or `ETag` is not
    /// usable, and a tool error when the event changed since it was read.
    #[tool(
        title = "Delete event",
        description = "Deletes an event. Needs the etag from the last read; a changed event is refused.",
        annotations(read_only_hint = false, destructive_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<DeleteOut>(),
    )]
    pub async fn calendar_delete_event(
        &self,
        Parameters(params): Parameters<DeleteParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.reject_write()?;
        let path = require_path(&params.id)?;
        let etag = require_etag(&params.etag)?;
        let _permit = self.permit().await?;
        try_cal!(self.calendar.delete_event(&path, &etag).await);
        respond(&DeleteOut { deleted: true }, "event deleted".to_owned())
    }

    /// Lists reminder lists, and says that upgraded lists are out of reach.
    ///
    /// # Errors
    ///
    /// Returns an MCP error when the lists cannot be read.
    #[tool(
        title = "List reminder lists",
        description = "Lists reminder lists. Only old-style lists can be reached; the result says so. Names are as their owner set them.",
        annotations(read_only_hint = true, destructive_hint = false, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<ReminderListsOut>(),
    )]
    pub async fn reminders_list_lists(&self) -> Result<CallToolResult, ErrorData> {
        let _permit = self.permit().await?;
        let lists = try_cal!(self.calendar.list_lists().await);
        let listed: Vec<ReminderListOut> = lists
            .iter()
            .map(|list| ReminderListOut {
                id: encode_resource(&list.path),
                name: strip_hidden(&list.name),
            })
            .collect();
        let summary = format!("{} lists. {UPGRADED_LISTS_NOTE}", listed.len());
        respond(
            &ReminderListsOut {
                lists: listed,
                note: UPGRADED_LISTS_NOTE,
            },
            summary,
        )
    }

    /// Lists reminders, open ones unless completed ones are asked for.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when the list id or page size is
    /// not usable, and an MCP error when the reminders cannot be read.
    #[tool(
        title = "List reminders",
        description = "Lists reminders in one list or all lists. Open reminders only, unless include_completed is set. Titles and notes are as their author wrote them.",
        annotations(read_only_hint = true, destructive_hint = false, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<RemindersOut>(),
    )]
    pub async fn reminders_list(
        &self,
        Parameters(params): Parameters<ListRemindersParams>,
    ) -> Result<CallToolResult, ErrorData> {
        let limit = page_limit(params.limit)?;
        let start_at = cursor_offset(params.cursor.as_deref())?;
        let list = params.list.as_deref().map(require_path).transpose()?;
        let filter = if params.include_completed.unwrap_or(false) {
            CompletedFilter::All
        } else {
            CompletedFilter::Open
        };
        let _permit = self.permit().await?;
        let listed = try_cal!(self.calendar.list(list.as_ref(), filter).await);
        let (reminders, next_cursor) = page(listed.reminders, limit, start_at);
        let summary = format!("{} reminders", reminders.len());
        respond(
            &RemindersOut {
                reminders: reminders.iter().map(reminder_out).collect(),
                next_cursor,
                warnings: listed.warnings,
            },
            summary,
        )
    }

    /// Creates a reminder in a task list.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when the list id, due date or
    /// priority is not usable, and an MCP error when it cannot be written.
    #[tool(
        title = "Create reminder",
        description = "Creates a reminder in a reminder list.",
        annotations(read_only_hint = false, destructive_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<WriteOut>(),
    )]
    pub async fn reminders_create(
        &self,
        Parameters(params): Parameters<CreateReminderParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.reject_write()?;
        let list = require_path(&params.list)?;
        let draft = ReminderDraft {
            title: params.title,
            notes: params.notes.unwrap_or_default(),
            due: params.due.filter(|due| !due.is_empty()),
            priority: params.priority.filter(|priority| *priority != 0),
            completed: false,
        };
        let _permit = self.permit().await?;
        let created = try_cal!(self.calendar.create(&list, &draft).await);
        respond(
            &WriteOut {
                id: encode_resource(&created),
                etag: None,
            },
            "reminder created".to_owned(),
        )
    }

    /// Changes a reminder when its `ETag` still matches. Completing is an update.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when an id, `ETag`, due date or
    /// priority is not usable, and a tool error when the reminder changed.
    #[tool(
        title = "Update reminder",
        description = "Changes a reminder, including marking it completed. Needs the etag from the last read; a changed reminder is refused.",
        annotations(read_only_hint = false, destructive_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<WriteOut>(),
    )]
    pub async fn reminders_update(
        &self,
        Parameters(params): Parameters<UpdateReminderParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.reject_write()?;
        let path = require_path(&params.id)?;
        let etag = require_etag(&params.etag)?;
        let _permit = self.permit().await?;
        let current = try_cal!(self.calendar.get(&path).await);
        let draft = ReminderDraft {
            title: params.title.unwrap_or(current.title),
            notes: params.notes.unwrap_or(current.notes),
            due: match params.due {
                None => current.due,
                Some(due) if due.is_empty() => None,
                Some(due) => Some(due),
            },
            priority: match params.priority {
                None => current.priority,
                Some(0) => None,
                Some(priority) => Some(priority),
            },
            completed: params.completed.unwrap_or(current.completed),
        };
        let fresh = try_cal!(self.calendar.update(&path, &etag, &draft).await);
        respond(
            &WriteOut {
                id: params.id,
                etag: Some(fresh.as_str().to_owned()),
            },
            "reminder updated".to_owned(),
        )
    }

    /// Deletes a reminder when its `ETag` still matches.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorData::invalid_params`] when the id or `ETag` is not
    /// usable, and a tool error when the reminder changed since it was read.
    #[tool(
        title = "Delete reminder",
        description = "Deletes a reminder. Needs the etag from the last read; a changed reminder is refused.",
        annotations(read_only_hint = false, destructive_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::tool::schema_for_output::<DeleteOut>(),
    )]
    pub async fn reminders_delete(
        &self,
        Parameters(params): Parameters<DeleteParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.reject_write()?;
        let path = require_path(&params.id)?;
        let etag = require_etag(&params.etag)?;
        let _permit = self.permit().await?;
        try_cal!(self.calendar.delete(&path, &etag).await);
        respond(&DeleteOut { deleted: true }, "reminder deleted".to_owned())
    }
}

pub(crate) fn require_path(id: &str) -> Result<ResourcePath, ErrorData> {
    decode_resource(id).map_err(|_| ErrorData::invalid_params("invalid id", None))
}

pub(crate) fn require_etag(etag: &str) -> Result<ETag, ErrorData> {
    ETag::try_from(etag).map_err(|_| ErrorData::invalid_params("invalid etag", None))
}

fn cursor_offset(cursor: Option<&str>) -> Result<usize, ErrorData> {
    match cursor {
        None => Ok(0),
        Some(text) => text
            .parse()
            .map_err(|_| ErrorData::invalid_params("invalid cursor", None)),
    }
}

/// One page of `items`, and the cursor for the next page when there is one.
pub(crate) fn page<T>(items: Vec<T>, limit: u32, start: usize) -> (Vec<T>, Option<String>) {
    let limit = usize::try_from(limit).unwrap_or(usize::MAX);
    let end = start.saturating_add(limit);
    let more = items.len() > end;
    let shown = items.into_iter().skip(start).take(limit).collect();
    (shown, more.then(|| end.to_string()))
}

/// The most characters of a description or note a tool returns, like a mail body.
pub(crate) const BODY_LIMIT: usize = 40_000;

/// `text` cut to [`BODY_LIMIT`] characters, with a marker saying how much is missing.
pub(crate) fn capped(text: &str) -> String {
    let Some((end, _)) = text.char_indices().nth(BODY_LIMIT) else {
        return text.to_owned();
    };
    let missing = text[end..].chars().count();
    format!("{}\n[truncated, {missing} more characters]", &text[..end])
}

fn event_out(event: &Event) -> EventOut {
    EventOut {
        id: encode_resource(&event.path),
        etag: event.etag.as_str().to_owned(),
        uid: event.uid.clone(),
        summary: strip_hidden(&event.summary),
        description: strip_hidden(&capped(&event.description)),
        start: event.start.clone(),
        end: event.end.clone(),
        time_zone: event.time_zone.clone(),
        all_day: event.all_day,
        recurring: event.recurring,
        recurrence_id: event.recurrence_id.clone(),
        attendees: event.attendees.iter().map(|a| strip_hidden(a)).collect(),
        alarms: event.alarms.clone(),
    }
}

fn reminder_out(reminder: &Reminder) -> ReminderOut {
    ReminderOut {
        id: encode_resource(&reminder.path),
        etag: reminder.etag.as_str().to_owned(),
        title: strip_hidden(&reminder.title),
        notes: strip_hidden(&capped(&reminder.notes)),
        due: reminder.due.clone(),
        priority: reminder.priority,
        completed: reminder.completed,
    }
}