litra 3.3.0

Control Logitech Litra lights from the command line, Model Context Protocol (MCP) clients and Rust applications
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
use std::str::FromStr;

use litra::DeviceType;
use rmcp::{
    handler::server::{
        tool::ToolRouter,
        wrapper::{Json, Parameters},
    },
    model::*,
    schemars, tool, tool_handler, tool_router,
    transport::stdio,
    ErrorData as McpError, ServerHandler, ServiceExt,
};

use crate::{
    get_connected_devices, handle_back_brightness_command, handle_back_brightness_down_command,
    handle_back_brightness_up_command, handle_back_color_command, handle_back_off_command,
    handle_back_on_command, handle_back_toggle_command, handle_brightness_command,
    handle_brightness_down_command, handle_brightness_up_command, handle_off_command,
    handle_on_command, handle_temperature_command, handle_temperature_down_command,
    handle_temperature_up_command, handle_toggle_command, CliError, CliResult, DeviceInfo,
};

/// Wrapper struct for device list to satisfy MCP's requirement for object root type
#[derive(serde::Serialize, schemars::JsonSchema)]
pub struct DeviceListResponse {
    /// List of connected Litra devices
    pub devices: Vec<DeviceInfo>,
}

// Helper function to convert string to DeviceType
fn parse_device_type(device_type_str: Option<&String>) -> Option<DeviceType> {
    device_type_str.and_then(|s| DeviceType::from_str(s).ok())
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct LitraToolParams {
    /// The serial number of the device to target (optional - if not specified, all devices are targeted)
    pub serial_number: Option<String>,
    /// The device path to target (optional - useful for devices that don't show a serial number)
    pub device_path: Option<String>,
    /// The device type to target: "litra_glow", "litra_beam", or "litra_beam_lx" (optional)
    pub device_type: Option<String>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct LitraBrightnessParams {
    /// The serial number of the device to target (optional - if not specified, all devices are targeted)
    pub serial_number: Option<String>,
    /// The device path to target (optional - useful for devices that don't show a serial number)
    pub device_path: Option<String>,
    /// The device type to target: "litra_glow", "litra_beam", or "litra_beam_lx" (optional)
    pub device_type: Option<String>,
    /// The brightness value to set in lumens (use either this or percentage, not both)
    pub value: Option<u16>,
    /// The brightness as a percentage of maximum brightness (use either this or value, not both)
    pub percentage: Option<u8>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct LitraTemperatureParams {
    /// The serial number of the device to target (optional - if not specified, all devices are targeted)
    pub serial_number: Option<String>,
    /// The device path to target (optional - useful for devices that don't show a serial number)
    pub device_path: Option<String>,
    /// The device type to target: "litra_glow", "litra_beam", or "litra_beam_lx" (optional)
    pub device_type: Option<String>,
    /// The temperature value in Kelvin (must be a multiple of 100 between 2700K and 6500K)
    pub value: u16,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct LitraBackToolParams {
    /// The serial number of the device to target (optional - if not specified, all devices are targeted)
    pub serial_number: Option<String>,
    /// The device path to target (optional - useful for devices that don't show a serial number)
    pub device_path: Option<String>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct LitraBackBrightnessParams {
    /// The serial number of the device to target (optional - if not specified, all devices are targeted)
    pub serial_number: Option<String>,
    /// The device path to target (optional - useful for devices that don't show a serial number)
    pub device_path: Option<String>,
    /// The brightness as a percentage (1-100)
    pub percentage: u8,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct LitraBackColorParams {
    /// The serial number of the device to target (optional - if not specified, all devices are targeted)
    pub serial_number: Option<String>,
    /// The device path to target (optional - useful for devices that don't show a serial number)
    pub device_path: Option<String>,
    /// The color in hex format (e.g., "#FF0000" for red)
    pub hex: String,
    /// The zone ID to target (1-7, optional - if not specified, all zones are targeted)
    pub zone_id: Option<u8>,
}

#[derive(Clone)]
pub struct LitraMcpServer {
    tool_router: ToolRouter<LitraMcpServer>,
}

#[tool_router]
impl LitraMcpServer {
    pub fn new() -> Self {
        Self {
            tool_router: Self::tool_router(),
        }
    }

    #[tool(
        description = "Turn Logitech Litra device(s) on. By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_on(
        &self,
        Parameters(params): Parameters<LitraToolParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_on_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Device(s) turned on successfully",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Turn Logitech Litra device(s) off. By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_off(
        &self,
        Parameters(params): Parameters<LitraToolParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_off_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Device(s) turned off successfully",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Toggle Logitech Litra device(s) on or off. By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_toggle(
        &self,
        Parameters(params): Parameters<LitraToolParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_toggle_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Device(s) toggled successfully",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Set the brightness of Logitech Litra device(s). By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_brightness(
        &self,
        Parameters(params): Parameters<LitraBrightnessParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_brightness_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
            params.value,
            params.percentage,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Brightness set successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Increase the brightness of Logitech Litra device(s). By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_brightness_up(
        &self,
        Parameters(params): Parameters<LitraBrightnessParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_brightness_up_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
            params.value,
            params.percentage,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Brightness increased successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Decrease the brightness of Logitech Litra device(s). By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_brightness_down(
        &self,
        Parameters(params): Parameters<LitraBrightnessParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_brightness_down_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
            params.value,
            params.percentage,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Brightness decreased successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Set the temperature of Logitech Litra device(s). By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_temperature(
        &self,
        Parameters(params): Parameters<LitraTemperatureParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_temperature_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
            params.value,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Temperature set successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Increase the temperature of Logitech Litra device(s). By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_temperature_up(
        &self,
        Parameters(params): Parameters<LitraTemperatureParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_temperature_up_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
            params.value,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Temperature increased successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Decrease the temperature of Logitech Litra device(s). By default, all devices will be targeted, but you can optionally specify a serial number, device path, or device type.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_temperature_down(
        &self,
        Parameters(params): Parameters<LitraTemperatureParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_temperature_down_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            parse_device_type(params.device_type.as_ref()).as_ref(),
            params.value,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Temperature decreased successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Turn the back light of Logitech Litra Beam LX device(s) on. By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_back_on(
        &self,
        Parameters(params): Parameters<LitraBackToolParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_on_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light turned on successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Turn the back light of Logitech Litra Beam LX device(s) off. By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_back_off(
        &self,
        Parameters(params): Parameters<LitraBackToolParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_off_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light turned off successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Toggle the back light of Logitech Litra Beam LX device(s) on or off. By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_back_toggle(
        &self,
        Parameters(params): Parameters<LitraBackToolParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_toggle_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light toggled successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Set the brightness of the back light on Logitech Litra Beam LX device(s). By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_back_brightness(
        &self,
        Parameters(params): Parameters<LitraBackBrightnessParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_brightness_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            params.percentage,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light brightness set successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Increase the brightness of the back light on Logitech Litra Beam LX device(s). By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_back_brightness_up(
        &self,
        Parameters(params): Parameters<LitraBackBrightnessParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_brightness_up_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            params.percentage,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light brightness increased successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Decrease the brightness of the back light on Logitech Litra Beam LX device(s). By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path.",
        annotations(
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = false
        )
    )]
    async fn litra_back_brightness_down(
        &self,
        Parameters(params): Parameters<LitraBackBrightnessParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_brightness_down_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            params.percentage,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light brightness decreased successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "Set the color of the back light on Logitech Litra Beam LX device(s). By default, all Litra Beam LX devices will be targeted, but you can optionally specify a serial number or device path. Can target a specific zone (1-7) or all zones.",
        annotations(
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn litra_back_color(
        &self,
        Parameters(params): Parameters<LitraBackColorParams>,
    ) -> Result<CallToolResult, McpError> {
        match handle_back_color_command(
            params.serial_number.as_deref(),
            params.device_path.as_deref(),
            &params.hex,
            params.zone_id,
        ) {
            Ok(()) => Ok(CallToolResult::success(vec![Content::text(
                "Back light color set successfully for device(s)",
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e.to_string())])),
        }
    }

    #[tool(
        description = "List Logitech Litra devices connected to computer",
        annotations(read_only_hint = true, open_world_hint = false)
    )]
    async fn litra_devices(&self) -> Result<Json<DeviceListResponse>, McpError> {
        let litra_devices =
            get_connected_devices().map_err(|e| McpError::internal_error(e.to_string(), None))?;

        Ok(Json(DeviceListResponse {
            devices: litra_devices,
        }))
    }
}

#[tool_handler]
impl ServerHandler for LitraMcpServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            protocol_version: ProtocolVersion::V_2025_03_26,
            capabilities: ServerCapabilities::builder().enable_tools().build(),
            server_info: Implementation {
                name: env!("CARGO_PKG_NAME").to_owned(),
                title: Some("Litra".to_owned()),
                version: env!("CARGO_PKG_VERSION").to_owned(),
                icons: None,
                website_url: Some("https://github.com/timrogers/litra-rs".to_owned()),
            },
            instructions: None,
        }
    }
}

pub fn handle_mcp_command() -> CliResult {
    // Set up tracing for the MCP server
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive(tracing::Level::DEBUG.into()),
        )
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .init();

    // Create the MCP server
    let rt = tokio::runtime::Runtime::new().map_err(|_| CliError::DeviceNotFound)?;
    rt.block_on(async {
        tracing::info!("Starting Litra MCP server");

        let service = LitraMcpServer::new()
            .serve(stdio())
            .await
            .map_err(|e| CliError::MCPError(format!("{e}")))?;
        service
            .waiting()
            .await
            .map_err(|e| CliError::MCPError(format!("{e}")))?;
        Ok(())
    })
}