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
//! Dual-mode response-checking and interaction tools.
//!
//! These tools query the result of a widget interaction, providing
//! a declarative way to inspect [`egui::Response`] properties.

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

use crate::serde_types::{RectJson, Vec2Json};

// ---------------------------------------------------------------------------
// Response query JSON types
// ---------------------------------------------------------------------------

/// Serializable response query.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
pub enum ResponseQueryJson {
    /// Check if widget was clicked.
    Clicked,
    /// Check if widget was double-clicked.
    DoubleClicked,
    /// Check if widget was secondary-clicked (right-click).
    SecondaryClicked,
    /// Check if widget is being hovered.
    Hovered,
    /// Check if widget has keyboard focus.
    HasFocus,
    /// Check if widget gained focus.
    GainedFocus,
    /// Check if widget lost focus.
    LostFocus,
    /// Check if widget is being dragged.
    Dragged,
    /// Check if drag was released.
    DragReleased,
    /// Check if the value changed.
    Changed,
    /// Get the widget bounding rect.
    Rect,
    /// Get the drag delta (how much the pointer moved while dragging).
    DragDelta,
    /// Get the hover position.
    HoverPos,
    /// Request focus for the widget.
    RequestFocus,
    /// Surrender focus from the widget.
    SurrenderFocus,
    /// Show a tooltip on hover with given text.
    ShowTooltip {
        /// Tooltip text.
        text: String,
    },
    /// Set the enabled state.
    SetEnabled {
        /// Whether the widget is enabled.
        enabled: bool,
    },
    /// Highlight the widget (e.g. during a tutorial).
    Highlight,
    /// Scroll to make the widget visible.
    ScrollToMe,
    /// Check if widget was clicked N times.
    ClickedN {
        /// Click count.
        count: usize,
    },
    /// Context menu response.
    ContextMenu,
}

/// Serializable response info (what the runtime returns).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
pub enum ResponseInfoJson {
    /// Boolean result.
    Bool {
        /// The result.
        value: bool,
    },
    /// Rectangle result.
    Rect {
        /// The bounding rectangle.
        rect: RectJson,
    },
    /// Vector/delta result.
    Vec2 {
        /// The vector value.
        value: Vec2Json,
    },
    /// Optional position result.
    OptionalPos {
        /// The position, if any.
        pos: Option<Vec2Json>,
    },
    /// Action performed (no data returned).
    Action,
}

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

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

/// Empty params for no-argument response tools.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct EmptyResponseParams {}

// ---------------------------------------------------------------------------
// Click / hover checks
// ---------------------------------------------------------------------------

/// Check if a widget was clicked.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_clicked",
    description = "Check if the widget was clicked. Returns ResponseQueryJson::Clicked."
)]
#[instrument(skip_all)]
async fn response_clicked(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::Clicked))
}

/// Check if a widget was double-clicked.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_double_clicked",
    description = "Check if the widget was double-clicked. Returns ResponseQueryJson::DoubleClicked.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_double_clicked(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::DoubleClicked))
}

/// Check if a widget was right-clicked.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_secondary_clicked",
    description = "Check if the widget was right-clicked. Returns ResponseQueryJson::SecondaryClicked.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_secondary_clicked(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::SecondaryClicked))
}

/// Parameters for `response_clicked_n`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct ClickedNParams {
    /// Click count to check.
    pub count: usize,
}

/// Check if a widget was clicked N times.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_clicked_n",
    description = "Check if the widget was clicked N times. Returns ResponseQueryJson::ClickedN."
)]
#[instrument(skip_all)]
async fn response_clicked_n(p: ClickedNParams) -> Result<CallToolResult, ErrorData> {
    let q = ResponseQueryJson::ClickedN { count: p.count };
    Ok(query_result(&q))
}

/// Check if a widget is hovered.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_hovered",
    description = "Check if the widget is being hovered. Returns ResponseQueryJson::Hovered.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_hovered(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::Hovered))
}

// ---------------------------------------------------------------------------
// Focus
// ---------------------------------------------------------------------------

/// Check if a widget has keyboard focus.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_has_focus",
    description = "Check if the widget has keyboard focus. Returns ResponseQueryJson::HasFocus.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_has_focus(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::HasFocus))
}

/// Check if a widget gained focus.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_gained_focus",
    description = "Check if the widget just gained focus. Returns ResponseQueryJson::GainedFocus.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_gained_focus(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::GainedFocus))
}

/// Check if a widget lost focus.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_lost_focus",
    description = "Check if the widget just lost focus. Returns ResponseQueryJson::LostFocus.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_lost_focus(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::LostFocus))
}

/// Request keyboard focus for a widget.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_request_focus",
    description = "Request keyboard focus for the widget. Returns ResponseQueryJson::RequestFocus.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_request_focus(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::RequestFocus))
}

/// Surrender keyboard focus from a widget.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_surrender_focus",
    description = "Surrender keyboard focus from the widget. Returns ResponseQueryJson::SurrenderFocus.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_surrender_focus(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::SurrenderFocus))
}

// ---------------------------------------------------------------------------
// Drag
// ---------------------------------------------------------------------------

/// Check if a widget is being dragged.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_dragged",
    description = "Check if the widget is being dragged. Returns ResponseQueryJson::Dragged.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_dragged(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::Dragged))
}

/// Check if drag was released.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_drag_released",
    description = "Check if drag was released. Returns ResponseQueryJson::DragReleased.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_drag_released(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::DragReleased))
}

/// Get the drag delta vector.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_drag_delta",
    description = "Get the drag delta (pointer movement while dragging). Returns ResponseQueryJson::DragDelta.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_drag_delta(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::DragDelta))
}

// ---------------------------------------------------------------------------
// Value / state
// ---------------------------------------------------------------------------

/// Check if the widget value changed.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_changed",
    description = "Check if the widget value changed. Returns ResponseQueryJson::Changed.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_changed(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::Changed))
}

/// Get the widget bounding rectangle.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_rect",
    description = "Get the widget bounding rectangle. Returns ResponseQueryJson::Rect.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_rect(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::Rect))
}

/// Get the hover position.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_hover_pos",
    description = "Get the pointer position while hovering. Returns ResponseQueryJson::HoverPos.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_hover_pos(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::HoverPos))
}

// ---------------------------------------------------------------------------
// Actions
// ---------------------------------------------------------------------------

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

/// Show a tooltip on hover.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_show_tooltip",
    description = "Show a tooltip when the widget is hovered. Returns ResponseQueryJson::ShowTooltip."
)]
#[instrument(skip_all)]
async fn response_show_tooltip(p: ShowTooltipParams) -> Result<CallToolResult, ErrorData> {
    let q = ResponseQueryJson::ShowTooltip { text: p.text };
    Ok(query_result(&q))
}

/// Parameters for `response_set_enabled`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct SetEnabledParams {
    /// Whether the widget is enabled.
    pub enabled: bool,
}

/// Set the enabled state of a widget.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_set_enabled",
    description = "Set the widget enabled/disabled state. Returns ResponseQueryJson::SetEnabled."
)]
#[instrument(skip_all)]
async fn response_set_enabled(p: SetEnabledParams) -> Result<CallToolResult, ErrorData> {
    let q = ResponseQueryJson::SetEnabled { enabled: p.enabled };
    Ok(query_result(&q))
}

/// Highlight a widget (e.g. for tutorials).
#[elicit_tool(
    plugin = "egui_response",
    name = "response_highlight",
    description = "Highlight the widget for emphasis. Returns ResponseQueryJson::Highlight.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_highlight(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::Highlight))
}

/// Scroll to make the widget visible.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_scroll_to_me",
    description = "Scroll to make the widget visible. Returns ResponseQueryJson::ScrollToMe.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_scroll_to_me(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::ScrollToMe))
}

/// Show a context menu for the widget.
#[elicit_tool(
    plugin = "egui_response",
    name = "response_context_menu",
    description = "Show a context menu for the widget. Returns ResponseQueryJson::ContextMenu.",
    emit = None
)]
#[instrument(skip_all)]
async fn response_context_menu(p: EmptyResponseParams) -> Result<CallToolResult, ErrorData> {
    let _ = p;
    Ok(query_result(&ResponseQueryJson::ContextMenu))
}