video-transcriber-mcp 0.7.0

High-performance video transcription MCP server using whisper.cpp for faster transcription
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
use anyhow::Result;
use serde_json::{json, Value};
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
use tracing::{error, info};

use super::types::{McpRequest, McpResponse, McpTool};
use crate::transcriber::{TranscriberEngine, TranscriptionOptions, WhisperModel};
use crate::utils::paths::get_default_output_dir;

pub struct McpServer {
    transcriber: TranscriberEngine,
}

impl McpServer {
    pub fn new() -> Self {
        Self {
            transcriber: TranscriberEngine::new(),
        }
    }

    pub async fn run(&self) -> Result<()> {
        info!("📡 MCP Server listening on stdio...");

        let stdin = io::stdin();
        let mut stdout = io::stdout();

        for line in stdin.lock().lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            match self.handle_request(&line).await {
                Ok(response) => {
                    let response_json = serde_json::to_string(&response)?;
                    writeln!(stdout, "{}", response_json)?;
                    stdout.flush()?;
                }
                Err(e) => {
                    error!("Error handling request: {}", e);
                    let error_response = McpResponse::error(
                        None,
                        -32603,
                        format!("Internal error: {}", e),
                    );
                    let response_json = serde_json::to_string(&error_response)?;
                    writeln!(stdout, "{}", response_json)?;
                    stdout.flush()?;
                }
            }
        }

        Ok(())
    }

    async fn handle_request(&self, request_json: &str) -> Result<McpResponse> {
        let request: McpRequest = serde_json::from_str(request_json)?;

        match request.method.as_str() {
            "initialize" => self.handle_initialize(request.id),
            "tools/list" => self.handle_list_tools(request.id),
            "tools/call" => self.handle_call_tool(request.id, request.params).await,
            "resources/list" => self.handle_list_resources(request.id),
            "resources/read" => self.handle_read_resource(request.id, request.params),
            _ => Ok(McpResponse::error(
                request.id,
                -32601,
                format!("Method not found: {}", request.method),
            )),
        }
    }

    fn handle_initialize(&self, id: Option<Value>) -> Result<McpResponse> {
        let result = json!({
            "protocolVersion": "2024-11-05",
            "capabilities": {
                "tools": {},
                "resources": {}
            },
            "serverInfo": {
                "name": env!("CARGO_PKG_NAME"),
                "version": env!("CARGO_PKG_VERSION")
            }
        });

        Ok(McpResponse::success(id, result))
    }

    fn handle_list_tools(&self, id: Option<Value>) -> Result<McpResponse> {
        let tools = vec![
            McpTool {
                name: "transcribe_video".to_string(),
                description: "Transcribe videos from 1000+ platforms (YouTube, Vimeo, TikTok, Twitter, etc.) or local video files using whisper.cpp (4-10x faster than Python whisper!). Downloads/extracts audio and generates transcript in TXT, JSON, and Markdown formats.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "Video URL from any supported platform OR absolute/relative path to a local video file (mp4, avi, mov, mkv, etc.)"
                        },
                        "output_dir": {
                            "type": "string",
                            "description": format!("Optional output directory path. Defaults to {}", get_default_output_dir().display())
                        },
                        "model": {
                            "type": "string",
                            "enum": ["tiny", "base", "small", "medium", "large"],
                            "description": "Whisper model to use. Larger models are more accurate but slower. Default: 'base'"
                        },
                        "language": {
                            "type": "string",
                            "description": "Language code (ISO 639-1: en, es, fr, de, etc.) or 'auto' for automatic detection. Default: 'auto'"
                        }
                    },
                    "required": ["url"]
                }),
            },
            McpTool {
                name: "check_dependencies".to_string(),
                description: "Check if all required dependencies (yt-dlp, ffmpeg, whisper models) are installed".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {}
                }),
            },
            McpTool {
                name: "list_supported_sites".to_string(),
                description: "List all video platforms supported by yt-dlp (1000+ sites including YouTube, Vimeo, TikTok, Twitter, Facebook, Instagram, educational platforms, and more)".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {}
                }),
            },
            McpTool {
                name: "list_transcripts".to_string(),
                description: "List all available transcripts in the output directory".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "output_dir": {
                            "type": "string",
                            "description": format!("Optional output directory path. Defaults to {}", get_default_output_dir().display())
                        }
                    }
                }),
            },
        ];

        let result = json!({ "tools": tools });
        Ok(McpResponse::success(id, result))
    }

    async fn handle_call_tool(&self, id: Option<Value>, params: Option<Value>) -> Result<McpResponse> {
        let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?;
        let name = params["name"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing tool name"))?;

        match name {
            "transcribe_video" => self.handle_transcribe_video(id, &params["arguments"]).await,
            "check_dependencies" => self.handle_check_dependencies(id),
            "list_supported_sites" => self.handle_list_supported_sites(id),
            "list_transcripts" => self.handle_list_transcripts(id, &params["arguments"]),
            _ => Ok(McpResponse::error(
                id,
                -32602,
                format!("Unknown tool: {}", name),
            )),
        }
    }

    async fn handle_transcribe_video(&self, id: Option<Value>, args: &Value) -> Result<McpResponse> {
        let url = args["url"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing 'url' parameter"))?;

        let output_dir = args["output_dir"]
            .as_str()
            .map(|s| s.to_string())
            .unwrap_or_else(|| get_default_output_dir().to_string_lossy().to_string());

        let model = args["model"]
            .as_str()
            .and_then(|s| s.parse::<WhisperModel>().ok())
            .unwrap_or(WhisperModel::Base);

        let language = args["language"]
            .as_str()
            .map(|s| s.to_string());

        let options = TranscriptionOptions {
            url: url.to_string(),
            output_dir,
            model,
            language,
        };

        info!("🎬 Starting transcription for: {}", url);

        match self.transcriber.transcribe(options).await {
            Ok(result) => {
                let result_json = json!({
                    "content": [{
                        "type": "text",
                        "text": format!(
                            "✅ Video transcribed successfully!\n\n\
                            **Video Details:**\n\
                            - Title: {}\n\
                            - Platform: {}\n\
                            - Duration: {}s\n\n\
                            **Transcription Settings:**\n\
                            - Model: {:?}\n\
                            - Engine: whisper.cpp (Rust)\n\n\
                            **Output Files:**\n\
                            - Text: {}\n\
                            - JSON: {}\n\
                            - Markdown: {}\n\n\
                            **Transcript Preview:**\n\
                            {}\n\n\
                            **Full transcript has {} words.**",
                            result.metadata.title,
                            result.metadata.platform,
                            result.metadata.duration,
                            result.model_used,
                            result.files.txt,
                            result.files.json,
                            result.files.md,
                            result.transcript_preview,
                            result.word_count
                        )
                    }]
                });
                Ok(McpResponse::success(id, result_json))
            }
            Err(e) => {
                error!("Transcription failed: {}", e);
                Ok(McpResponse::error(
                    id,
                    -32000,
                    format!("Transcription failed: {}", e),
                ))
            }
        }
    }

    fn handle_check_dependencies(&self, id: Option<Value>) -> Result<McpResponse> {
        match self.transcriber.check_dependencies() {
            Ok(status) => {
                let result = json!({
                    "content": [{
                        "type": "text",
                        "text": format!("✅ Dependency Check:\n\n{}", status)
                    }]
                });
                Ok(McpResponse::success(id, result))
            }
            Err(e) => Ok(McpResponse::error(
                id,
                -32000,
                format!("Dependency check failed: {}", e),
            )),
        }
    }

    fn handle_list_resources(&self, id: Option<Value>) -> Result<McpResponse> {
        use std::fs;

        let output_dir = get_default_output_dir();

        if !output_dir.exists() {
            let result = json!({ "resources": [] });
            return Ok(McpResponse::success(id, result));
        }

        let mut resources = Vec::new();

        if let Ok(entries) = fs::read_dir(&output_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                let filename = path.file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("");

                if filename.ends_with(".txt") || filename.ends_with(".md") || filename.ends_with(".json") {
                    if let Ok(metadata) = fs::metadata(&path) {
                        let mime_type = if filename.ends_with(".json") {
                            "application/json"
                        } else if filename.ends_with(".md") {
                            "text/markdown"
                        } else {
                            "text/plain"
                        };

                        let size_kb = metadata.len() as f64 / 1024.0;
                        let modified = metadata.modified()
                            .ok()
                            .and_then(|t| {
                                use std::time::SystemTime;
                                let duration = t.duration_since(SystemTime::UNIX_EPOCH).ok()?;
                                Some(duration.as_secs())
                            })
                            .unwrap_or(0);

                        resources.push(json!({
                            "uri": format!("file://{}", path.display()),
                            "name": filename,
                            "description": format!("Transcript file ({:.2} KB, modified {})",
                                size_kb,
                                format_timestamp(modified)
                            ),
                            "mimeType": mime_type
                        }));
                    }
                }
            }
        }

        let result = json!({ "resources": resources });
        Ok(McpResponse::success(id, result))
    }

    fn handle_read_resource(&self, id: Option<Value>, params: Option<Value>) -> Result<McpResponse> {
        use std::fs;

        let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?;
        let uri = params["uri"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing uri parameter"))?;

        // Remove file:// prefix
        let file_path = uri.strip_prefix("file://").unwrap_or(uri);

        match fs::read_to_string(file_path) {
            Ok(content) => {
                let mime_type = if file_path.ends_with(".json") {
                    "application/json"
                } else if file_path.ends_with(".md") {
                    "text/markdown"
                } else {
                    "text/plain"
                };

                let result = json!({
                    "contents": [{
                        "uri": uri,
                        "mimeType": mime_type,
                        "text": content
                    }]
                });
                Ok(McpResponse::success(id, result))
            }
            Err(e) => Ok(McpResponse::error(
                id,
                -32000,
                format!("Failed to read file: {}", e),
            )),
        }
    }

    fn handle_list_supported_sites(&self, id: Option<Value>) -> Result<McpResponse> {
        let result = json!({
            "content": [{
                "type": "text",
                "text": "📺 Supported Video Platforms (1000+ total)\n\n\
                **Popular platforms include:**\n\
                - YouTube\n\
                - Vimeo\n\
                - TikTok\n\
                - Twitter/X\n\
                - Facebook\n\
                - Instagram\n\
                - Twitch\n\
                - Dailymotion\n\
                - Reddit\n\
                - LinkedIn\n\
                - Many educational and conference platforms\n\n\
                **Total: 1000+ supported extractors**\n\n\
                You can transcribe videos from any of these platforms!"
            }]
        });
        Ok(McpResponse::success(id, result))
    }

    fn handle_list_transcripts(&self, id: Option<Value>, args: &Value) -> Result<McpResponse> {
        use std::collections::HashMap;
        use std::fs;

        let output_dir = args["output_dir"]
            .as_str()
            .map(|s| PathBuf::from(s))
            .unwrap_or_else(|| get_default_output_dir());

        if !output_dir.exists() {
            let result = json!({
                "content": [{
                    "type": "text",
                    "text": format!("📂 No transcripts directory found at: {}\n\nTranscribe your first video to create it!", output_dir.display())
                }]
            });
            return Ok(McpResponse::success(id, result));
        }

        let mut video_groups: HashMap<String, Vec<String>> = HashMap::new();

        if let Ok(entries) = fs::read_dir(&output_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                let filename = path.file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("");

                if filename.ends_with(".txt") || filename.ends_with(".md") {
                    let video_id = filename.split('-').next().unwrap_or("unknown").to_string();
                    video_groups.entry(video_id).or_insert_with(Vec::new).push(filename.to_string());
                }
            }
        }

        if video_groups.is_empty() {
            let result = json!({
                "content": [{
                    "type": "text",
                    "text": format!("📂 No transcripts found in {}\n\nTranscribe a video to get started!", output_dir.display())
                }]
            });
            return Ok(McpResponse::success(id, result));
        }

        let mut list_items = Vec::new();
        for (i, (video_id, files)) in video_groups.iter().enumerate() {
            let main_file = files.iter()
                .find(|f| f.ends_with(".txt"))
                .unwrap_or(&files[0]);

            let full_path = output_dir.join(main_file);

            if let Ok(metadata) = fs::metadata(&full_path) {
                let size_kb = metadata.len() as f64 / 1024.0;
                let modified = metadata.modified()
                    .ok()
                    .and_then(|t| {
                        use std::time::SystemTime;
                        let duration = t.duration_since(SystemTime::UNIX_EPOCH).ok()?;
                        Some(duration.as_secs())
                    })
                    .unwrap_or(0);

                let title = main_file
                    .replace(&format!("{}-", video_id), "")
                    .replace(".txt", "")
                    .replace(".md", "")
                    .replace(".json", "")
                    .replace("-", " ");

                let extensions: Vec<&str> = files.iter()
                    .filter_map(|f| f.split('.').last())
                    .collect();

                list_items.push(format!(
                    "{}. **{}**\n   Video ID: {}\n   Files: {} ({})\n   Size: {:.2} KB\n   Modified: {}\n   Path: {}",
                    i + 1,
                    title,
                    video_id,
                    files.len(),
                    extensions.join(", "),
                    size_kb,
                    format_timestamp(modified),
                    full_path.display()
                ));
            }
        }

        let result = json!({
            "content": [{
                "type": "text",
                "text": format!("📚 Available transcripts ({} videos):\n\n{}\n\n💡 Tip: You can read any transcript by asking me to read the file path shown above.",
                    video_groups.len(),
                    list_items.join("\n\n"))
            }]
        });
        Ok(McpResponse::success(id, result))
    }
}

fn format_timestamp(timestamp: u64) -> String {
    use chrono::{DateTime, Utc, TimeZone};
    let dt: DateTime<Utc> = Utc.timestamp_opt(timestamp as i64, 0).unwrap();
    dt.format("%Y-%m-%d").to_string()
}