-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.go
More file actions
407 lines (347 loc) · 9.91 KB
/
Copy pathmcp_server.go
File metadata and controls
407 lines (347 loc) · 9.91 KB
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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// 任务管理
type DownloadTask struct {
ID string `json:"id"`
Status string `json:"status"` // pending, downloading, completed, failed
Percentage int `json:"percentage"`
Speed string `json:"speed,omitempty"`
ElapsedTime int `json:"elapsed_time"`
FilePath string `json:"file_path,omitempty"`
Error string `json:"error,omitempty"`
VideoURL string `json:"video_url"`
Quality string `json:"quality"`
StartTime time.Time `json:"-"`
}
type TranscribeTask struct {
ID string `json:"id"`
Status string `json:"status"` // extracting_audio, transcribing, completed, failed
Percentage int `json:"percentage"`
Stage string `json:"stage,omitempty"`
ElapsedTime int `json:"elapsed_time"`
MP3Path string `json:"mp3_path,omitempty"`
TXTPath string `json:"txt_path,omitempty"`
Error string `json:"error,omitempty"`
VideoPath string `json:"video_path"`
StartTime time.Time `json:"-"`
}
var (
downloadTasks = make(map[string]*DownloadTask)
transcribeTasks = make(map[string]*TranscribeTask)
mu = &sync.RWMutex{}
)
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
// CORS
router.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
})
// ============ MCP 服务 API ============
// 列出可用的工具/功能
router.GET("/mcp/tools", func(c *gin.Context) {
tools := []map[string]interface{}{
{
"name": "download_video",
"description": "下载知乎视频为 MP4 格式(默认最高清晰度)",
"inputSchema": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"url": map[string]interface{}{
"type": "string",
"description": "知乎视频 URL",
},
"output_path": map[string]interface{}{
"type": "string",
"description": "输出路径(默认 ~/Downloads)",
},
},
"required": []string{"url"},
},
},
{
"name": "transcribe_video",
"description": "将视频转录为文本(包括音频提取和 Whisper 转录)",
"inputSchema": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"video_path": map[string]interface{}{
"type": "string",
"description": "MP4 视频文件路径",
},
"language": map[string]interface{}{
"type": "string",
"description": "语言代码(默认 zh 中文)",
},
},
"required": []string{"video_path"},
},
},
{
"name": "get_progress",
"description": "获取下载或转录任务的进度",
"inputSchema": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"task_id": map[string]interface{}{
"type": "string",
"description": "任务 ID",
},
"task_type": map[string]interface{}{
"type": "string",
"enum": []string{"download", "transcribe"},
"description": "任务类型",
},
},
"required": []string{"task_id", "task_type"},
},
},
}
c.JSON(200, gin.H{"tools": tools})
})
// 调用工具
router.POST("/mcp/call_tool", func(c *gin.Context) {
var req struct {
Name string `json:"name"`
Input map[string]interface{} `json:"input"`
}
if err := c.BindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
var response interface{}
var err error
switch req.Name {
case "download_video":
response, err = handleDownloadVideo(req.Input)
case "transcribe_video":
response, err = handleTranscribeVideo(req.Input)
case "get_progress":
response, err = handleGetProgress(req.Input)
default:
c.JSON(404, gin.H{"error": "未知的工具"})
return
}
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"result": response})
})
// ============ 健康检查 ============
router.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok", "service": "zhihu-downloader-mcp"})
})
fmt.Println("✓ MCP 服务启动在 http://127.0.0.1:5125")
fmt.Println(" 可用端点:")
fmt.Println(" GET /mcp/tools - 列出所有工具")
fmt.Println(" POST /mcp/call_tool - 调用工具")
fmt.Println(" GET /health - 健康检查")
router.Run("127.0.0.1:5125")
}
// ============ 工具处理函数 ============
func handleDownloadVideo(input map[string]interface{}) (interface{}, error) {
url, ok := input["url"].(string)
if !ok || url == "" {
return nil, fmt.Errorf("URL 必填")
}
outputPath, _ := input["output_path"].(string)
if outputPath == "" {
outputPath = filepath.Join(os.Getenv("HOME"), "Downloads")
}
taskID := uuid.New().String()
task := &DownloadTask{
ID: taskID,
Status: "pending",
VideoURL: url,
Quality: "hd", // 默认最高清晰度
StartTime: time.Now(),
}
mu.Lock()
downloadTasks[taskID] = task
mu.Unlock()
// 在后台执行下载
go downloadVideoWorker(taskID, url, outputPath)
return gin.H{
"task_id": taskID,
"status": "已启动下载任务",
}, nil
}
func handleTranscribeVideo(input map[string]interface{}) (interface{}, error) {
videoPath, ok := input["video_path"].(string)
if !ok || videoPath == "" {
return nil, fmt.Errorf("video_path 必填")
}
language, _ := input["language"].(string)
if language == "" {
language = "zh"
}
if _, err := os.Stat(videoPath); err != nil {
return nil, fmt.Errorf("视频文件不存在: %v", err)
}
taskID := uuid.New().String()
task := &TranscribeTask{
ID: taskID,
Status: "extracting_audio",
VideoPath: videoPath,
StartTime: time.Now(),
}
mu.Lock()
transcribeTasks[taskID] = task
mu.Unlock()
// 在后台执行转录
go transcribeVideoWorker(taskID, videoPath, language)
return gin.H{
"task_id": taskID,
"status": "已启动转录任务",
}, nil
}
func handleGetProgress(input map[string]interface{}) (interface{}, error) {
taskID, ok := input["task_id"].(string)
if !ok || taskID == "" {
return nil, fmt.Errorf("task_id 必填")
}
taskType, ok := input["task_type"].(string)
if !ok || taskType == "" {
return nil, fmt.Errorf("task_type 必填 (download 或 transcribe)")
}
mu.RLock()
defer mu.RUnlock()
if taskType == "download" {
task, exists := downloadTasks[taskID]
if !exists {
return nil, fmt.Errorf("下载任务不存在")
}
return task, nil
} else if taskType == "transcribe" {
task, exists := transcribeTasks[taskID]
if !exists {
return nil, fmt.Errorf("转录任务不存在")
}
return task, nil
}
return nil, fmt.Errorf("未知的任务类型")
}
// ============ 工作函数 ============
func downloadVideoWorker(taskID, url, outputPath string) {
mu.Lock()
task := downloadTasks[taskID]
task.Status = "downloading"
task.Percentage = 0
mu.Unlock()
os.MkdirAll(outputPath, 0755)
outputFile := filepath.Join(outputPath, fmt.Sprintf("video_%s.mp4", taskID[:8]))
// 调用 ffmpeg 下载
cmd := exec.Command("ffmpeg", "-y", "-i", url, "-c", "copy", "-progress", "pipe:1", outputFile)
stdout, _ := cmd.StdoutPipe()
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "progress=") {
mu.Lock()
task.Percentage = min(99, task.Percentage+1)
task.ElapsedTime = int(time.Since(task.StartTime).Seconds())
mu.Unlock()
}
}
}()
err := cmd.Run()
mu.Lock()
if err != nil {
task.Status = "failed"
task.Error = err.Error()
} else {
if info, err := os.Stat(outputFile); err == nil && info.Size() > 0 {
task.Status = "completed"
task.Percentage = 100
task.FilePath = outputFile
fmt.Printf("[%s] 下载完成: %s\n", taskID, outputFile)
} else {
task.Status = "failed"
task.Error = "文件为空或不存在"
}
}
mu.Unlock()
}
func transcribeVideoWorker(taskID, videoPath, language string) {
mu.Lock()
task := transcribeTasks[taskID]
mu.Unlock()
// 步骤1: 提取音频
mu.Lock()
task.Status = "extracting_audio"
task.Stage = "正在提取音频..."
task.Percentage = 10
mu.Unlock()
mp3Path := strings.TrimSuffix(videoPath, filepath.Ext(videoPath)) + ".mp3"
cmd := exec.Command("ffmpeg", "-y", "-i", videoPath, "-q:a", "9", mp3Path)
output, err := cmd.CombinedOutput()
if err != nil {
mu.Lock()
task.Status = "failed"
task.Error = fmt.Sprintf("音频提取失败: %v", err)
mu.Unlock()
return
}
if _, err := os.Stat(mp3Path); err != nil {
mu.Lock()
task.Status = "failed"
task.Error = "MP3 文件未创建"
mu.Unlock()
return
}
fmt.Printf("[%s] 音频提取完成\n", taskID)
// 步骤2: 转录
mu.Lock()
task.Status = "transcribing"
task.Stage = "正在转录..."
task.Percentage = 50
mu.Unlock()
outputDir := filepath.Dir(videoPath)
whisperCmd := exec.Command("bash", "-c",
fmt.Sprintf("export PATH=/opt/homebrew/bin:$PATH && /opt/homebrew/bin/whisper %q --output_format txt --output_dir %q --language %s --model base 2>&1",
mp3Path, outputDir, language))
output, err = whisperCmd.CombinedOutput()
if err != nil {
mu.Lock()
task.Status = "failed"
task.Error = fmt.Sprintf("转录失败: %v\n%s", err, string(output))
mu.Unlock()
return
}
txtPath := strings.TrimSuffix(mp3Path, filepath.Ext(mp3Path)) + ".txt"
// 步骤3: 完成
mu.Lock()
task.Status = "completed"
task.Percentage = 100
task.MP3Path = mp3Path
task.TXTPath = txtPath
task.ElapsedTime = int(time.Since(task.StartTime).Seconds())
mu.Unlock()
fmt.Printf("[%s] 转录完成: %s\n", taskID, txtPath)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}