adk-rust-mcp-avtool 0.5.0

MCP server for audio/video processing using FFmpeg
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
//! MCP Server implementation for the AVTool server.
//!
//! This module provides the MCP server handler that exposes FFmpeg-based
//! audio/video processing tools.

use crate::handler::{
    AVToolHandler, AdjustVolumeParams, CombineAvParams, ConcatenateParams,
    ConvertAudioParams, GetMediaInfoParams, LayerAudioParams,
    OverlayImageParams, VideoToGifParams,
};
use adk_rust_mcp_common::config::Config;
use adk_rust_mcp_common::error::Error;
use rmcp::{
    model::{
        CallToolResult, Content, ListResourcesResult, ReadResourceResult,
        ServerCapabilities, ServerInfo,
    },
    ErrorData as McpError, ServerHandler,
};
use schemars::JsonSchema;
use serde::Deserialize;
use std::borrow::Cow;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::info;

/// MCP Server for audio/video processing.
#[derive(Clone)]
pub struct AVToolServer {
    /// Handler for FFmpeg operations
    handler: Arc<RwLock<Option<AVToolHandler>>>,
    /// Server configuration
    config: Config,
}

impl AVToolServer {
    /// Create a new AVToolServer with the given configuration.
    pub fn new(config: Config) -> Self {
        Self {
            handler: Arc::new(RwLock::new(None)),
            config,
        }
    }

    /// Initialize the handler (called lazily on first use).
    async fn ensure_handler(&self) -> Result<(), Error> {
        let mut handler = self.handler.write().await;
        if handler.is_none() {
            *handler = Some(AVToolHandler::new(self.config.clone()).await?);
        }
        Ok(())
    }

    /// Get media file information.
    pub async fn get_media_info(&self, params: GetMediaInfoParams) -> Result<CallToolResult, McpError> {
        info!(input = %params.input, "Getting media info");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let info = handler.get_media_info(params).await.map_err(|e| {
            McpError::internal_error(format!("Failed to get media info: {}", e), None)
        })?;

        let json = serde_json::to_string_pretty(&info).map_err(|e| {
            McpError::internal_error(format!("Failed to serialize result: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(json)]))
    }

    /// Convert WAV to MP3.
    pub async fn convert_wav_to_mp3(&self, params: ConvertAudioParams) -> Result<CallToolResult, McpError> {
        info!(input = %params.input, output = %params.output, "Converting WAV to MP3");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.convert_wav_to_mp3(params).await.map_err(|e| {
            McpError::internal_error(format!("Conversion failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Converted to: {}", output))]))
    }

    /// Convert video to GIF.
    pub async fn video_to_gif(&self, params: VideoToGifParams) -> Result<CallToolResult, McpError> {
        info!(input = %params.input, output = %params.output, "Converting video to GIF");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.video_to_gif(params).await.map_err(|e| {
            McpError::internal_error(format!("Conversion failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Created GIF: {}", output))]))
    }

    /// Combine audio and video.
    pub async fn combine_audio_video(&self, params: CombineAvParams) -> Result<CallToolResult, McpError> {
        info!(video = %params.video_input, audio = %params.audio_input, "Combining audio and video");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.combine_audio_video(params).await.map_err(|e| {
            McpError::internal_error(format!("Combine failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Combined to: {}", output))]))
    }

    /// Overlay image on video.
    pub async fn overlay_image(&self, params: OverlayImageParams) -> Result<CallToolResult, McpError> {
        info!(video = %params.video_input, image = %params.image_input, "Overlaying image on video");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.overlay_image(params).await.map_err(|e| {
            McpError::internal_error(format!("Overlay failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Created: {}", output))]))
    }

    /// Concatenate media files.
    pub async fn concatenate(&self, params: ConcatenateParams) -> Result<CallToolResult, McpError> {
        info!(count = params.inputs.len(), output = %params.output, "Concatenating media files");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.concatenate(params).await.map_err(|e| {
            McpError::internal_error(format!("Concatenation failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Concatenated to: {}", output))]))
    }

    /// Adjust audio volume.
    pub async fn adjust_volume(&self, params: AdjustVolumeParams) -> Result<CallToolResult, McpError> {
        info!(input = %params.input, volume = %params.volume, "Adjusting audio volume");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.adjust_volume(params).await.map_err(|e| {
            McpError::internal_error(format!("Volume adjustment failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Adjusted volume: {}", output))]))
    }

    /// Layer multiple audio files.
    pub async fn layer_audio(&self, params: LayerAudioParams) -> Result<CallToolResult, McpError> {
        info!(layers = params.inputs.len(), output = %params.output, "Layering audio files");

        self.ensure_handler().await.map_err(|e| {
            McpError::internal_error(format!("Failed to initialize handler: {}", e), None)
        })?;

        let handler_guard = self.handler.read().await;
        let handler = handler_guard.as_ref().ok_or_else(|| {
            McpError::internal_error("Handler not initialized", None)
        })?;

        let output = handler.layer_audio(params).await.map_err(|e| {
            McpError::internal_error(format!("Audio layering failed: {}", e), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!("Layered audio: {}", output))]))
    }
}

impl ServerHandler for AVToolServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            instructions: Some(
                "Audio/video processing server using FFmpeg. \
                 Provides tools for media conversion, combining, and manipulation."
                    .to_string(),
            ),
            capabilities: ServerCapabilities::builder()
                .enable_tools()
                .build(),
            ..Default::default()
        }
    }

    fn list_tools(
        &self,
        _params: Option<rmcp::model::PaginatedRequestParams>,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<rmcp::model::ListToolsResult, McpError>> + Send + '_ {
        async move {
            use rmcp::model::ListToolsResult;

            let tools = vec![
                create_tool::<GetMediaInfoParams>(
                    "ffmpeg_get_media_info",
                    "Get information about a media file (duration, format, streams, codecs).",
                ),
                create_tool::<ConvertAudioParams>(
                    "ffmpeg_convert_audio_wav_to_mp3",
                    "Convert a WAV audio file to MP3 format with configurable bitrate.",
                ),
                create_tool::<VideoToGifParams>(
                    "ffmpeg_video_to_gif",
                    "Convert a video file to animated GIF with configurable FPS, width, and duration.",
                ),
                create_tool::<CombineAvParams>(
                    "ffmpeg_combine_audio_and_video",
                    "Combine separate audio and video files into a single file.",
                ),
                create_tool::<OverlayImageParams>(
                    "ffmpeg_overlay_image_on_video",
                    "Overlay an image on a video at a specified position with optional timing.",
                ),
                create_tool::<ConcatenateParams>(
                    "ffmpeg_concatenate_media_files",
                    "Concatenate multiple media files into a single file.",
                ),
                create_tool::<AdjustVolumeParams>(
                    "ffmpeg_adjust_volume",
                    "Adjust the volume of an audio file using multiplier or dB notation.",
                ),
                create_tool::<LayerAudioParams>(
                    "ffmpeg_layer_audio_files",
                    "Layer/mix multiple audio files with optional offset and volume control.",
                ),
            ];

            Ok(ListToolsResult {
                tools,
                next_cursor: None,
                meta: None,
            })
        }
    }

    fn call_tool(
        &self,
        params: rmcp::model::CallToolRequestParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<CallToolResult, McpError>> + Send + '_ {
        async move {
            match params.name.as_ref() {
                "ffmpeg_get_media_info" => {
                    let tool_params: GetMediaInfoParams = parse_params(params.arguments)?;
                    self.get_media_info(tool_params).await
                }
                "ffmpeg_convert_audio_wav_to_mp3" => {
                    let tool_params: ConvertAudioParams = parse_params(params.arguments)?;
                    self.convert_wav_to_mp3(tool_params).await
                }
                "ffmpeg_video_to_gif" => {
                    let tool_params: VideoToGifParams = parse_params(params.arguments)?;
                    self.video_to_gif(tool_params).await
                }
                "ffmpeg_combine_audio_and_video" => {
                    let tool_params: CombineAvParams = parse_params(params.arguments)?;
                    self.combine_audio_video(tool_params).await
                }
                "ffmpeg_overlay_image_on_video" => {
                    let tool_params: OverlayImageParams = parse_params(params.arguments)?;
                    self.overlay_image(tool_params).await
                }
                "ffmpeg_concatenate_media_files" => {
                    let tool_params: ConcatenateParams = parse_params(params.arguments)?;
                    self.concatenate(tool_params).await
                }
                "ffmpeg_adjust_volume" => {
                    let tool_params: AdjustVolumeParams = parse_params(params.arguments)?;
                    self.adjust_volume(tool_params).await
                }
                "ffmpeg_layer_audio_files" => {
                    let tool_params: LayerAudioParams = parse_params(params.arguments)?;
                    self.layer_audio(tool_params).await
                }
                _ => Err(McpError::invalid_params(format!("Unknown tool: {}", params.name), None)),
            }
        }
    }

    fn list_resources(
        &self,
        _params: Option<rmcp::model::PaginatedRequestParams>,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListResourcesResult, McpError>> + Send + '_ {
        async move {
            // AVTool server doesn't expose any resources
            Ok(ListResourcesResult {
                resources: vec![],
                next_cursor: None,
                meta: None,
            })
        }
    }

    fn read_resource(
        &self,
        params: rmcp::model::ReadResourceRequestParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<ReadResourceResult, McpError>> + Send + '_ {
        async move {
            Err(McpError::resource_not_found(
                format!("Unknown resource: {}", params.uri),
                None,
            ))
        }
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Create a tool definition from a parameter type.
fn create_tool<T: JsonSchema>(name: &'static str, description: &'static str) -> rmcp::model::Tool {
    use schemars::schema_for;

    let schema = schema_for!(T);
    let schema_value = serde_json::to_value(&schema).unwrap_or_default();
    
    let input_schema = match schema_value {
        serde_json::Value::Object(map) => Arc::new(map),
        _ => Arc::new(serde_json::Map::new()),
    };

    rmcp::model::Tool {
        name: Cow::Borrowed(name),
        description: Some(Cow::Borrowed(description)),
        input_schema,
        annotations: None,
        icons: None,
        meta: None,
        output_schema: None,
        title: None,
    }
}

/// Parse tool parameters from JSON arguments.
fn parse_params<T: for<'de> Deserialize<'de>>(
    arguments: Option<serde_json::Map<String, serde_json::Value>>,
) -> Result<T, McpError> {
    arguments
        .map(|args| serde_json::from_value(serde_json::Value::Object(args)))
        .transpose()
        .map_err(|e| McpError::invalid_params(format!("Invalid parameters: {}", e), None))?
        .ok_or_else(|| McpError::invalid_params("Missing parameters", None))
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn test_config() -> Config {
        Config {
            project_id: "test-project".to_string(),
            location: "us-central1".to_string(),
            gcs_bucket: None,
            port: 8080,
        ..Default::default()
        }
    }

    #[test]
    fn test_server_info() {
        let server = AVToolServer::new(test_config());
        let info = server.get_info();
        assert!(info.instructions.is_some());
        assert!(info.instructions.unwrap().contains("FFmpeg"));
    }

    #[test]
    fn test_create_tool() {
        let tool = create_tool::<GetMediaInfoParams>(
            "ffmpeg_get_media_info",
            "Get media info",
        );
        assert_eq!(tool.name.as_ref(), "ffmpeg_get_media_info");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_parse_params_valid() {
        let mut args = serde_json::Map::new();
        args.insert("input".to_string(), serde_json::Value::String("test.mp4".to_string()));
        
        let result: Result<GetMediaInfoParams, _> = parse_params(Some(args));
        assert!(result.is_ok());
        assert_eq!(result.unwrap().input, "test.mp4");
    }

    #[test]
    fn test_parse_params_missing() {
        let result: Result<GetMediaInfoParams, _> = parse_params(None);
        assert!(result.is_err());
    }
}