一个全面的 MCP(模型上下文协议)服务器,用于提取 YouTube 视频字幕,支持多种传输方式(标准 I/O、服务发送事件(SSE)、HTTP),Docker 部署和 npm 包分发。
npm install -g yt-transcript-dl-mcp
git clone <repository-url>
cd yt-transcript-dl-repo
npm install
npm run build
# 从 GitHub 容器注册表(推荐)
docker pull ghcr.io/jedarden/yt-transcript-dl-mcp:latest
docker run -p 3001:3001 -p 3002:3002 ghcr.io/jedarden/yt-transcript-dl-mcp:latest --multi-transport
# 从源码构建
docker build -t yt-transcript-dl-mcp .
docker run -p 3001:3001 -p 3002:3002 yt-transcript-dl-mcp --multi-transport
以不同模式启动 MCP 服务器:
# 标准 I/O 模式(默认)
yt-transcript-dl-mcp start
# SSE 模式
yt-transcript-dl-mcp start --transport sse --port 3000
# HTTP 模式
yt-transcript-dl-mcp start --transport http --port 3000
# 启用详细日志
yt-transcript-dl-mcp start --verbose
使用示例视频测试服务器:
# 使用 YouTube 视频测试
yt-transcript-dl-mcp test dQw4w9WgXcQ
# 使用不同语言测试
yt-transcript-dl-mcp test dQw4w9WgXcQ --language es
# 使用不同格式测试
yt-transcript-dl-mcp test dQw4w9WgXcQ --format srt
import { YouTubeTranscriptService } from 'yt-transcript-dl-mcp';
const service = new YouTubeTranscriptService();
// 提取单个视频字幕
const result = await service.getTranscript('dQw4w9WgXcQ', 'en', 'json');
console.log(result);
// 批量处理
const bulkResult = await service.getBulkTranscripts({
videoIds: ['dQw4w9WgXcQ', 'jNQXAC9IVRw'],
outputFormat: 'json',
language: 'en'
});
console.log(bulkResult);
服务器提供以下 MCP 工具:
get_transcript从单个 YouTube 视频中提取字幕。
参数:
videoId(必需):YouTube 视频 ID 或 URLlanguage(可选):语言代码(默认:'en')format(可选):输出格式 - 'text', 'json', 或 'srt'(默认:'json')get_bulk_transcripts从多个 YouTube 视频中提取字幕。
参数:
videoIds(必需):YouTube 视频 ID 或 URL 数组language(可选):语言代码(默认:'en')outputFormat(可选):输出格式 - 'text', 'json', 或 'srt'(默认:'json')includeMetadata(可选):在响应中包含元数据(默认:true)get_playlist_transcripts从 YouTube 播放列表中的所有视频中提取字幕。
参数:
playlistId(必需):YouTube 播放列表 ID 或 URLlanguage(可选):语言代码(默认:'en')outputFormat(可选):输出格式 - 'text', 'json', 或 'srt'(默认:'json')includeMetadata(可选):在响应中包含元数据(默认:true)format_transcript将现有字幕数据格式化为不同的格式。
参数:
transcript(必需):字幕数据数组format(必需):输出格式 - 'text', 'json', 或 'srt'get_cache_stats获取缓存统计信息和性能指标。
clear_cache清除字幕缓存。
# 服务器配置
PORT=3000
HOST=0.0.0.0
MCP_TRANSPORT=stdio
# CORS 设置
CORS_ENABLED=true
CORS_ORIGINS=*
# 速率限制
RATE_LIMIT_WINDOW=900000 # 15 分钟,单位为毫秒
RATE_LIMIT_MAX=100
# 缓存
CACHE_ENABLED=true
CACHE_TTL=3600 # 1 小时,单位为秒
CACHE_MAX_SIZE=1000
# 日志
LOG_LEVEL=info
LOG_FORMAT=simple
创建一个 config.json 文件:
{
"port": 3000,
"host": "0.0.0.0",
"cors": {
"enabled": true,
"origins": ["*"]
},
"rateLimit": {
"windowMs": 900000,
"max": 100
},
"cache": {
"enabled": true,
"ttl": 3600,
"maxSize": 1000
},
"logging": {
"level": "info",
"format": "simple"
}
}
version: '3.8'
services:
yt-transcript-mcp:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- PORT=3000
- LOG_LEVEL=info
restart: unless-stopped
healthcheck:
test: ["CMD", "node", "dist/health-check.js"]
interval: 30s
timeout: 10s
retries: 3
Docker 容器包括内置健康检查:
# 检查容器健康
docker ps
docker exec <container-id> node dist/health-check.js
git clone <repository-url>
cd yt-transcript-dl-repo
npm install
# 运行所有测试
npm test
# 运行带有覆盖率的测试
npm run test:coverage
# 运行特定测试套件
npm run test:unit
npm run test:integration
npm run test:e2e
# 监视模式
npm run test:watch
# 构建 TypeScript
npm run build
# 开发模式带监视
npm run dev
# 代码检查
npm run lint
npm run lint:fix
# 测试标准 I/O 传输
./scripts/test-stdio.sh
# 使用示例视频测试
npm run test:sample
所有字幕响应遵循此结构:
interface TranscriptResponse {
videoId: string;
title?: string;
language: string;
transcript: TranscriptItem[];
metadata?: {
extractedAt: string;
source: string;
duration?: number;
error?: string;
};
}
interface TranscriptItem {
text: string;
start: number;
duration: number;
}
服务器处理各种错误场景:
启用调试日志:
export LOG_LEVEL=debug
yt-transcript-dl-mcp start --verbose
检查 logs/ 目录下的日志:
tail -f logs/combined.log
tail -f logs/error.log
MIT 许可证 - 查看 LICENSE 文件了解详情。