Skip to main content
Glama
ytmusic-mcp

ytmusic-mcp

by umsachde

ytmusic-mcp

一个 MCP 服务器,封装了 ytmusicapi,使 Claude(或任何 MCP 客户端)能够搜索 YouTube Music、管理你的播放列表以及读取你的收听历史。

工具

工具

描述

search_music(query, filter=None, limit=20)

搜索 YouTube Music。filter 可以是 songs、videos、albums、artists、playlists、community_playlists、featured_playlists、profiles、podcasts、episodes 之一。

get_playlists()

列出你的库中的播放列表。

get_playlist_tracks(playlist_id)

获取播放列表中的曲目。

create_playlist(name, description="")

创建一个新的私有播放列表,返回其 ID。

add_to_playlist(playlist_id, video_id)

向播放列表添加曲目。

get_history()

获取你最近的播放历史。

未包含(v1): 基于 BPM 的推荐。YouTube Music 不暴露节奏数据,因此需要第二个数据源(例如音频分析 API)——这是未来版本的一个延伸目标,不属于本次构建的一部分。

Related MCP server: mcp-server-youtube

设置

1. 安装依赖

python3 -m venv .venv
source .venv/bin/activate
pip install -e .

2. 身份验证

没有官方的 YouTube Music API,因此 ytmusicapi 通过重用你已登录浏览器会话中的标头来进行身份验证。

  1. 在 Firefox(推荐——其原始标头复制比 Chrome 更可靠)中打开 music.youtube.com 并登录。

  2. 打开开发者工具(Cmd+Option+I / F12)→ 网络 标签页 → 按 browse 过滤。

  3. 点击进入一个播放列表,或重新加载页面,以触发一个 browse POST 请求。

  4. 点击该请求 → 标头 标签页 → 切换 原始标头 → 选择并复制整个块。

  5. 将其粘贴到项目根目录下名为 raw_headers.txt 的新文件中并保存。

  6. 运行:

    python scripts/setup_auth_from_file.py

    这将写入 headers_auth.json 并删除 raw_headers.txt。

或者,python scripts/setup_auth.py 通过交互式终端提示(而不是文件)执行相同操作,如果你更愿意直接粘贴的话。

headers_auth.json 等同于你的已登录会话——切勿提交或共享它。 它已被添加到 gitignore。

在继续之前验证身份验证是否有效:

python scripts/test_search.py

这些标头会定期过期/轮换。如果工具开始因身份验证错误而失败,请重新执行此步骤。

3. 添加到 Claude Code

claude mcp add ytmusic -s user \
  -e YTMUSIC_AUTH_PATH="$(pwd)/headers_auth.json" \
  -- "$(pwd)/.venv/bin/python" "$(pwd)/server.py"

-s user 使其在任何 Claude Code 会话中可用,而不仅限于此目录。对 Python 解释器、server.py 和 YTMUSIC_AUTH_PATH 使用绝对路径,因为服务器可以从任何工作目录启动。

对于其他 MCP 客户端(Claude Desktop 等),使用它们各自的配置格式将其指向相同的命令和环境变量。

错误处理

工具调用将常见的失败模式转换为清晰的消息,而不是原始回溯:

  • 缺失/过期/格式错误的身份验证 → 提示你重新运行 scripts/setup_auth_from_file.py。

  • 速率限制(HTTP 429) → 提示你等待并重试。

  • 受限/受限制的内容 → 报告为不可用,而不是崩溃。

  • 网络错误 → 直接报告。

许可证

MIT — 参见 LICENSE。

Available Tools

6 tools
add_to_playlistC

Add a track to a playlist by video ID.

Inputs: video_id (required), playlist_id (required). Returns: result

create_playlistC

Create a new private playlist and return its playlist ID.

Inputs: name (required), description. Returns: result

get_historyB

Get recent play history.

Inputs: no named parameters. Returns: result

get_playlistsA

List your YouTube Music library playlists.

Inputs: no named parameters. Returns: result

get_playlist_tracksC

Get the tracks in a playlist.

Inputs: playlist_id (required). Returns: result

search_musicB

Search YouTube Music.

filter: one of songs, videos, albums, artists, playlists, community_playlists, featured_playlists, profiles, podcasts, episodes. Omit for a mixed search. Returns an empty list if there are no matches.

Inputs: query (required), limit, filter. Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    B3.2/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool targets a distinct action or resource: search, library playlists, playlist tracks, playlist creation, playlist modification, and history. No two tools have overlapping purposes, making selection unambiguous.

    Naming Consistency4/5

    Tool names follow a consistent verb_noun pattern (search_music, get_playlists, get_playlist_tracks, create_playlist, add_to_playlist, get_history). The only minor deviation is 'add_to_playlist' instead of 'add_track_to_playlist', but the pattern is still clear.

    Tool Count5/5

    With 6 tools, the number is well-scoped for a YouTube Music server that focuses on search, playlist management, and history. Each tool earns its place without being overwhelming or too sparse.

    Completeness3/5

    The set covers search, playlist CRUD (create, read tracks, add to), and history, but lacks tools for removing tracks from playlists, updating playlist metadata, or managing likes/ratings. This leaves some common music management workflows incomplete.

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables users to search the YouTube Music catalog and manage playlists using natural language. It provides tools for searching songs, albums, and artists, as well as performing playlist operations like creating, adding, and deleting tracks.
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to search videos, read channels, browse playlists, fetch comments, and get transcripts from YouTube using the YouTube Data API v3 and InnerTube API for captions.
      2
      GPL 3.0