elicit_egui 0.11.1

Elicitation-enabled egui widget tools — dual-mode MCP tools with code emission
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
//! Context menu, popup, tooltip, modal, and notification tools.
//!
//! Each tool returns a JSON description of a menu/popup/tooltip/modal action.

use elicitation::elicit_tool;
use rmcp::ErrorData;
use rmcp::model::{CallToolResult, Content};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::instrument;

use crate::Vec2Json;

// ---------------------------------------------------------------------------
// JSON interchange types
// ---------------------------------------------------------------------------

/// Serializable menu/popup/tooltip/modal/notification action.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
pub enum MenuActionJson {
    /// A right-click context menu on a region.
    ContextMenu {
        /// Identifier for the region.
        region_id: String,
    },
    /// An item inside a context menu.
    ContextMenuItem {
        /// Display label for the item.
        label: String,
        /// Optional keyboard shortcut hint text.
        shortcut: Option<String>,
    },
    /// A separator line in a context menu.
    ContextMenuSeparator,
    /// A popup at a specific position.
    Popup {
        /// Popup identifier.
        id: String,
        /// Position to show the popup at.
        position: Vec2Json,
        /// Description of the popup content.
        content: String,
    },
    /// A popup anchored below a widget.
    PopupBelowWidget {
        /// Identifier of the anchor widget.
        anchor_id: String,
        /// Description of the popup content.
        content: String,
    },
    /// Close the current popup.
    ClosePopup,
    /// A hover tooltip for a widget.
    Tooltip {
        /// Identifier of the widget to attach the tooltip to.
        widget_id: String,
        /// Tooltip text.
        text: String,
    },
    /// A rich tooltip with custom UI content.
    TooltipRich {
        /// Identifier of the widget to attach the tooltip to.
        widget_id: String,
        /// Description of rich tooltip UI content.
        content: String,
    },
    /// A tooltip shown at the mouse pointer position.
    TooltipAtPointer {
        /// Tooltip text.
        text: String,
    },
    /// A modal dialog.
    Modal {
        /// Dialog title.
        title: String,
        /// Description of the dialog body content.
        content: String,
        /// Button labels (e.g. \["OK", "Cancel"\]).
        buttons: Vec<String>,
    },
    /// A yes/no confirmation dialog.
    ConfirmDialog {
        /// Dialog title.
        title: String,
        /// Message to display.
        message: String,
    },
    /// An alert/info dialog with a single OK button.
    AlertDialog {
        /// Dialog title.
        title: String,
        /// Message to display.
        message: String,
    },
    /// A temporary notification/toast message.
    Notification {
        /// Notification text.
        text: String,
        /// Duration to show in seconds.
        duration_secs: f32,
        /// Screen position for the notification.
        position: Option<Vec2Json>,
    },
}

// ---------------------------------------------------------------------------
// Helper
// ---------------------------------------------------------------------------

fn menu_result(action: &MenuActionJson) -> CallToolResult {
    match serde_json::to_string(action) {
        Ok(s) => CallToolResult::success(vec![Content::text(s)]),
        Err(e) => CallToolResult::error(vec![Content::text(format!("serialize error: {e}"))]),
    }
}

// ---------------------------------------------------------------------------
// Context menus
// ---------------------------------------------------------------------------

/// Parameters for `egui_context_menu`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ContextMenuParams {
    /// Identifier for the region to attach the context menu to.
    pub region_id: String,
}

/// Add a right-click context menu to a region.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_context_menu",
    description = "Add a right-click context menu to a region. Returns MenuActionJson::ContextMenu.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_context_menu(p: ContextMenuParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::ContextMenu {
        region_id: p.region_id,
    };
    Ok(menu_result(&a))
}

/// Parameters for `egui_context_menu_item`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ContextMenuItemParams {
    /// Display label for the menu item.
    pub label: String,
    /// Optional keyboard shortcut hint text (e.g. "Ctrl+C").
    pub shortcut: Option<String>,
}

/// Add an item to a context menu.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_context_menu_item",
    description = "Add an item to a context menu. Returns MenuActionJson::ContextMenuItem.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_context_menu_item(p: ContextMenuItemParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::ContextMenuItem {
        label: p.label,
        shortcut: p.shortcut,
    };
    Ok(menu_result(&a))
}

/// Empty params for tools that take no arguments.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct EmptyMenuParams {}

/// Add a separator line in a context menu.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_context_menu_separator",
    description = "Add a separator line in a context menu. Returns MenuActionJson::ContextMenuSeparator.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_context_menu_separator(p: EmptyMenuParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(menu_result(&MenuActionJson::ContextMenuSeparator))
}

// ---------------------------------------------------------------------------
// Popups
// ---------------------------------------------------------------------------

/// Parameters for `egui_popup`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct MenuPopupParams {
    /// Popup identifier.
    pub id: String,
    /// Position to show the popup at.
    pub position: Vec2Json,
    /// Description of the popup content.
    pub content: String,
}

/// Show a popup at a position.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_popup",
    description = "Show a popup at a position. Returns MenuActionJson::Popup.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_popup(p: MenuPopupParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::Popup {
        id: p.id,
        position: p.position,
        content: p.content,
    };
    Ok(menu_result(&a))
}

/// Parameters for `egui_popup_below_widget`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct PopupBelowWidgetParams {
    /// Identifier of the anchor widget.
    pub anchor_id: String,
    /// Description of the popup content.
    pub content: String,
}

/// Show a popup below a specific widget.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_popup_below_widget",
    description = "Show popup below a specific widget. Returns MenuActionJson::PopupBelowWidget.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_popup_below_widget(p: PopupBelowWidgetParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::PopupBelowWidget {
        anchor_id: p.anchor_id,
        content: p.content,
    };
    Ok(menu_result(&a))
}

/// Close the current popup.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_close_popup",
    description = "Close the current popup. Returns MenuActionJson::ClosePopup.",
    emit = Auto,
    emit = None
)]
#[instrument(skip_all)]
async fn egui_close_popup(p: EmptyMenuParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(menu_result(&MenuActionJson::ClosePopup))
}

// ---------------------------------------------------------------------------
// Tooltips
// ---------------------------------------------------------------------------

/// Parameters for `egui_tooltip`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct MenuTooltipParams {
    /// Identifier of the widget to attach the tooltip to.
    pub widget_id: String,
    /// Tooltip text.
    pub text: String,
}

/// Show a tooltip on hover for a widget.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_tooltip",
    description = "Show a tooltip on hover for a widget. Returns MenuActionJson::Tooltip.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_tooltip(p: MenuTooltipParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::Tooltip {
        widget_id: p.widget_id,
        text: p.text,
    };
    Ok(menu_result(&a))
}

/// Parameters for `egui_tooltip_rich`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct TooltipRichParams {
    /// Identifier of the widget to attach the tooltip to.
    pub widget_id: String,
    /// Description of the rich tooltip UI content.
    pub content: String,
}

/// Show a rich tooltip with custom UI content.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_tooltip_rich",
    description = "Show a rich tooltip with custom UI content. Returns MenuActionJson::TooltipRich.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_tooltip_rich(p: TooltipRichParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::TooltipRich {
        widget_id: p.widget_id,
        content: p.content,
    };
    Ok(menu_result(&a))
}

/// Parameters for `egui_tooltip_at_pointer`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct TooltipAtPointerParams {
    /// Tooltip text.
    pub text: String,
}

/// Show a tooltip at the mouse pointer position.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_tooltip_at_pointer",
    description = "Show tooltip at the mouse pointer position. Returns MenuActionJson::TooltipAtPointer.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_tooltip_at_pointer(p: TooltipAtPointerParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::TooltipAtPointer { text: p.text };
    Ok(menu_result(&a))
}

// ---------------------------------------------------------------------------
// Modals / Dialogs
// ---------------------------------------------------------------------------

/// Parameters for `egui_modal`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ModalParams {
    /// Dialog title.
    pub title: String,
    /// Description of the dialog body content.
    pub content: String,
    /// Button labels (e.g. \["OK", "Cancel"\]).
    pub buttons: Vec<String>,
}

/// Show a modal dialog.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_modal",
    description = "Show a modal dialog with title, content, and buttons. Returns MenuActionJson::Modal.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_modal(p: ModalParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::Modal {
        title: p.title,
        content: p.content,
        buttons: p.buttons,
    };
    Ok(menu_result(&a))
}

/// Parameters for `egui_confirm_dialog`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ConfirmDialogParams {
    /// Dialog title.
    pub title: String,
    /// Message to display.
    pub message: String,
}

/// Show a yes/no confirmation dialog.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_confirm_dialog",
    description = "Show a yes/no confirmation dialog. Returns MenuActionJson::ConfirmDialog.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_confirm_dialog(p: ConfirmDialogParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::ConfirmDialog {
        title: p.title,
        message: p.message,
    };
    Ok(menu_result(&a))
}

/// Parameters for `egui_alert_dialog`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct AlertDialogParams {
    /// Dialog title.
    pub title: String,
    /// Message to display.
    pub message: String,
}

/// Show an alert/info dialog with a single OK button.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_alert_dialog",
    description = "Show an alert/info dialog with single OK button. Returns MenuActionJson::AlertDialog.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_alert_dialog(p: AlertDialogParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::AlertDialog {
        title: p.title,
        message: p.message,
    };
    Ok(menu_result(&a))
}

// ---------------------------------------------------------------------------
// Notification / Toast
// ---------------------------------------------------------------------------

/// Parameters for `egui_notification`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct NotificationParams {
    /// Notification text.
    pub text: String,
    /// Duration to show the notification in seconds.
    pub duration_secs: f32,
    /// Optional screen position for the notification.
    pub position: Option<Vec2Json>,
}

/// Show a temporary notification/toast message.
#[elicit_tool(
    plugin = "egui_menus",
    name = "egui_notification",
    description = "Show a temporary notification/toast message. Returns MenuActionJson::Notification.",
    emit = Auto
)]
#[instrument(skip_all)]
async fn egui_notification(p: NotificationParams) -> Result<CallToolResult, ErrorData> {
    let a = MenuActionJson::Notification {
        text: p.text,
        duration_secs: p.duration_secs,
        position: p.position,
    };
    Ok(menu_result(&a))
}