一个全面的微对话处理器(MCP)服务器,用于通过 yt-dlp 提取和分析 YouTube 数据。
yt-dlp(通过 uv 自动安装)⚠️ 重要提示:此项目需要 uv 才能正常运行。首先安装它:
# 安装 uv(macOS/Linux)
curl -LsSf https://astral.sh/uv/install.sh | sh
# 或通过 Homebrew(macOS)
brew install uv
# 或通过 pip
pip install uv
# 克隆仓库
git clone <repository-url>
cd youtube-mcp-server-enhanced
# 安装 yt-dlp 和所有依赖项
uv add yt-dlp
uv sync
# 验证安装
uv run yt-dlp --version
在项目根目录创建一个 .env 文件以配置服务器:
# 复制示例文件
cp .env.example .env
# 使用你喜欢的设置编辑
nano .env
示例 .env 配置:
# 速率限制(例如,“500K”表示每秒 500KB,“1M”表示每秒 1MB)
YOUTUBE_RATE_LIMIT=500K
# 重试配置
YOUTUBE_MAX_RETRIES=5
YOUTUBE_RETRY_DELAY=2.0
YOUTUBE_TIMEOUT=600
# 缓存
YOUTUBE_ENABLE_CACHE=true
YOUTUBE_CACHE_TTL=3600
# 日志级别
LOG_LEVEL=INFO
添加到你的 ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"youtube-mcp-server": {
"command": "uv",
"args": [
"run",
"--directory",
"/path/to/youtube-mcp-server-enhanced",
"python",
"-m",
"src.youtube_mcp_server.server"
],
"env": {
"YOUTUBE_RATE_LIMIT": "500K",
"YOUTUBE_MAX_RETRIES": "5",
"YOUTUBE_RETRY_DELAY": "2.0",
"YOUTUBE_TIMEOUT": "600",
"YOUTUBE_ENABLE_CACHE": "true",
"YOUTUBE_CACHE_TTL": "3600"
}
}
}
}
对于其他 MCP 客户端,配置服务器命令如下:
uv run --directory /path/to/youtube-mcp-server-enhanced python -m src.youtube_mcp_server.server
| 工具 | 描述 | 示例 |
|---|---|---|
get_video_info() | 提取全面的视频元数据 | get_video_info("https://youtube.com/watch?v=...") |
get_channel_info() | 提取频道信息和统计(支持多种 URL 格式) | get_channel_info("https://youtube.com/@channel") 或 get_channel_info("https://youtube.com/ChannelName") |
get_playlist_info() | 提取播放列表详情和视频列表 | get_playlist_info("https://youtube.com/playlist?list=...") |
get_video_comments() | 提取视频评论和回复 | get_video_comments("https://youtube.com/watch?v=...", 50) |
get_video_transcript() | 提取视频字幕 | get_video_transcript("https://youtube.com/watch?v=...") |
| 工具 | 描述 | 示例 |
|---|---|---|
search_youtube() | 搜索视频、频道或播放列表 | search_youtube("Python 教程", "video", 20) |
get_trending_videos() | 获取按地区的热门视频 | get_trending_videos("US", 15) |
| 工具 | 描述 | 示例 |
|---|---|---|
analyze_video_engagement() | 分析互动指标并进行基准比较 | analyze_video_engagement("https://youtube.com/watch?v=...") |
search_transcript() | 在视频字幕中搜索文本 | search_transcript("https://youtube.com/watch?v=...", "查询") |
| 工具 | 描述 | 示例 |
|---|---|---|
batch_extract_urls() | 并发处理多个 URL | batch_extract_urls(["url1", "url2"], "video") |
| 工具 | 描述 | 示例 |
|---|---|---|
get_extractor_health() | 监控提取器健康状况和状态 | get_extractor_health() |
get_extractor_config() | 查看当前配置 | get_extractor_config() |
clear_extractor_cache() | 清除所有缓存数据 | clear_extractor_cache() |
| 提示 | 描述 | 示例 |
|---|---|---|
analyze-video | 对视频进行全面分析,可选包括评论和字幕 | analyze-video(url, include_comments=true, include_transcript=true) |
compare-videos | 比较多个视频的互动指标 | compare-videos([url1, url2, url3]) |
{
"metadata": {
"id": "video_id",
"title": "视频标题",
"description": "视频描述...",
"uploader": "频道名称",
"uploader_id": "channel_id",
"upload_date": "20240101",
"tags": ["标签1", "标签2"],
"categories": ["娱乐"],
"thumbnail": "https://..."
},
"statistics": {
"view_count": 1000,
"like_count": 100,
"comment_count": 25,
"duration_seconds": 120,
"duration_string": "2:00"
},
"engagement": {
"like_to_view_ratio": 0.05,
"comment_to_view_ratio": 0.025,
"like_rate_percentage": "5.000%",
"comment_rate_percentage": "2.500%"
},
"technical": {
"age_limit": 0,
"availability": "公开",
"live_status": "非直播"
}
}
{
"id": "channel_id",
"name": "频道名称",
"url": "https://youtube.com/@channel",
"description": "频道描述...",
"avatar_url": "https://...",
"banner_url": "https://...",
"verified": true,
"country": "US",
"language": "en",
"tags": ["标签1", "标签2"],
"statistics": {
"subscriber_count": 10000,
"video_count": 150,
"view_count": 500000
}
}
{
"id": "playlist_id",
"title": "播放列表标题",
"description": "播放列表描述...",
"uploader": "频道名称",
"uploader_id": "channel_id",
"video_count": 25,
"total_duration_seconds": 7200,
"total_duration_formatted": "2h 0m",
"total_views": 50000,
"videos": [
{
"video_id": "video_id",
"title": "视频标题",
"uploader": "频道名称",
"duration": 300,
"view_count": 2000,
"playlist_index": 1
}
]
}
# 获取全面的视频信息
video_info = await get_video_info("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
# 提取视频评论
comments = await get_video_comments("https://www.youtube.com/watch?v=dQw4w9WgXcQ", max_comments=50)
# 获取视频字幕
transcript = await get_video_transcript("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
# 在字幕中搜索
results = await search_transcript("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "永远不会")
# 获取频道信息
channel_info = await get_channel_info("https://www.youtube.com/@RickAstleyYT")
# 获取播放列表详情
playlist_info = await get_playlist_info("https://www.youtube.com/playlist?list=...")
# 搜索视频
results = await search_youtube("Python 编程教程", "video", 10)
# 获取热门视频
trending = await get_trending_videos("US", 20)
# 分析视频互动并进行基准比较
engagement = await analyze_video_engagement("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
# 比较多个视频
comparison = await compare_videos([
"https://youtube.com/watch?v=video1",
"https://youtube.com/watch?v=video2"
])
# 并发处理多个 URL
results = await batch_extract_urls([
"https://youtube.com/watch?v=video1",
"https://youtube.com/watch?v=video2"
], "video")
health = await get_extractor_health()
# 返回:
{
"health": {
"status": "健康",
"yt_dlp_available": true,
"yt_dlp_version": "2025.6.30",
"cache": {"enabled": true, "size": 5, "ttl": 3600},
"config": {"rate_limit": "1M", "max_retries": 3, "timeout": 300}
},
"cache": {
"enabled": true,
"size": 5,
"ttl": 3600,
"keys": ["key1", "key2"],
"total_keys": 5
},
"server_version": "0.1.0",
"mcp_version": "1.0.0"
}
config = await get_extractor_config()
# 返回当前提取器设置和状态
如果你遇到速率限制问题:
.env 中增加睡眠间隔:YOUTUBE_RETRY_DELAY=3.0YOUTUBE_RATE_LIMIT=300Kuv --versionuv run yt-dlp --versionuv run yt-dlp⚠️ 始终使用 uv run 以确保正确的依赖管理:
# 启动 MCP 服务器(推荐)
uv run python -m src.youtube_mcp_server.server
# 或如果你有 run_server.py 文件
uv run python run_server.py
# 运行所有测试
uv run pytest tests/
# 运行特定测试文件
uv run pytest tests/test_basic.py
# 运行覆盖率测试
uv run pytest --cov=src tests/
本项目采用 MIT 许可证 - 详见 LICENSE 文件。
由杜安·莱特福特制作 ❤️
通过模型上下文协议(Model Context Protocol),赋能开发者从 YouTube 内容中提取有意义的见解。