Spotify自然语言控制服务

通过Model Context Protocol (MCP)服务器实现使用自然语言控制Spotify播放功能,支持播放、暂停、切歌、搜索等操作。

  • by obre10off
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 obre10off/spotify-mcp

使用说明

Spotify MCP服务器

该项目实现了 模型上下文协议(MCP) 允许您通过MCP客户端使用自然语言控制Spotify播放的服务器,例如 光标 或 Claude桌面版 (仅限macOS和Windows)。

特性

此服务器公开以下工具:

  • play:播放曲目、专辑或播放列表,或继续播放。
  • pause:暂停播放。
  • next:跳到下一首曲目。
  • previous:跳至上一曲目。
  • get_current_track:获取当前播放曲目的信息。
  • search:搜索曲目、专辑、艺术家或播放列表。

先决条件

  • 包子 (1.0.0或更高版本)
  • Spotify Premium帐户。
  • Spotify开发者应用程序:
    • 创建一个 Spotify开发者仪表板.
    • 获取您的客户ID和客户密码。
    • 添加 http://localhost:8888/callback 转到应用程序设置中的重定向URI。
  • MCP客户端(例如,桌面版的Cursor或Claude)。

安装和设置

  1. 克隆存储库:

    git clone https://github.com/obre10off/spotify-mcp.git
    cd spotify-mcp
    
  2. 安装依赖项:

    bun install
    
  3. 创建一个 .env 文件:

    创建一个名为的文件 .env 在项目目录的根目录中。添加以下内容,用您的实际Spotify凭据替换占位符:

    SPOTIFY_CLIENT_ID=your_spotify_client_id
    SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
    SPOTIFY_REDIRECT_URI=http://localhost:8888/callback
    # These will be filled in after running the auth script:
    SPOTIFY_ACCESS_TOKEN=
    SPOTIFY_REFRESH_TOKEN=
    
  4. 运行授权脚本:

    此脚本将打开您的浏览器,提示您登录Spotify并授予权限,然后检索您的初始访问和刷新令牌。

    bun run auth
    

    脚本将打印 SPOTIFY_ACCESS_TOKEN 和 SPOTIFY_REFRESH_TOKEN 到控制台。将这些值复制到您的 .env 文件。

  5. 配置您的MCP客户端:

    • 光标:

      • 打开光标的设置(Cmd+或Ctrl+)。

      • 搜索“模型上下文协议”。

      • 点击“在settings.json中编辑”。

      • 将以下内容添加到 mcp.servers 阵列(替换 /absolute/path/to/your/spotify-mcp 随着 绝对的 通往你的路 spotify-mcp 目录):

        {
            "mcp.servers": [
                {
                    "spotify": {
                        "command": "bun",
                        "args": ["/absolute/path/to/your/spotify-mcp/src/index.ts"],
                        "env": {
                            "SPOTIFY_CLIENT_ID": "your_spotify_client_id",
                            "SPOTIFY_CLIENT_SECRET": "your_spotify_client_secret",
                            "SPOTIFY_REDIRECT_URI": "http://localhost:8888/callback",
                            "SPOTIFY_ACCESS_TOKEN": "your_spotify_access_token",
                            "SPOTIFY_REFRESH_TOKEN": "your_spotify_refresh_token"
                        }
                    }
                }
            ]
        }
        

        建议使用 .env 文件,只将与Spotify相关的环境变量放在那里,而不是将值添加到settings.json文件中。

    • Claude桌面版(macOS/Windows):

      • 打开Claude for Desktop配置文件:
        • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
        • 窗户: %APPDATA%\Claude\claude_desktop_config.json
      • 将以下内容添加到 mcpServers 对象(替换 /absolute/path/to/your/spotify-mcp 随着 绝对的 通往你的路 spotify-mcp 目录):
       {
         "mcpServers": {
           "spotify": {
             "command": "bun",
             "args": ["/absolute/path/to/your/spotify-mcp/src/index.ts"]
           }
         }
       }
       ```
       It is recommended to use a `.env` file and only put the environment variables related to Spotify there, instead of adding the values to the `claude_desktop_config.json` file.
      
      
    • 重要提示: 始终使用 绝对的 客户端配置中的路径。

  6. 重新启动MCP客户端

    确保重新启动MCP客户端(Cursor/Claude)以应用设置。

运行服务器

bun run start


This command starts the server with automatic reloading on file changes (thanks to Bun's --watch flag). Keep this terminal window open while you're using the server.


## Usage

Once the server is running and your MCP client is configured, you can start using natural language commands to control Spotify. Examples:

"Play Bohemian Rhapsody"

"Pause the music"

"What song is playing?"

"Search for Taylor Swift albums"

"Next track"

"Play spotify:track:4uLU6hMCjMI75M1A2tKUQC"```