Spotify MCP Server
A robust MCP server that connects Spotify playback, search, and library management to LLM agents via tools, with dual authentication and reliable token handling.
README
Spotify Model Context Protocol (MCP) Server
<p align="center"> <img src="https://img.shields.io/badge/Python-3.10%2B-blue.svg?style=flat-square" alt="Python Version"> <img src="https://img.shields.io/badge/Framework-FastMCP-green.svg?style=flat-square" alt="FastMCP"> <img src="https://img.shields.io/badge/API-Spotify%20Web-1DB954?style=flat-square&logo=spotify" alt="Spotify API"> <img src="https://img.shields.io/badge/MCP-Compatible-orange.svg?style=flat-square" alt="MCP Compatible"> <img src="https://img.shields.io/badge/License-MIT-lightgrey.svg?style=flat-square" alt="License"> </p>
A robust, production-grade Model Context Protocol (MCP) server that connects Spotify playback, search, and library management directly to LLM Agents (such as Cursor, Claude Desktop, and other MCP clients).
Built in Python using FastMCP and Spotipy, this server includes connection pooling, log isolation, and a custom dual-authentication strategy to ensure long-term stability and compatibility with non-interactive agents.
Features
- Playback Control: Play tracks/albums/playlists, pause, skip, adjust volume, toggle shuffle/repeat, and transfer playback between devices.
- Search & Discovery: Search tracks, artists, albums, and playlists, and get tailored music recommendations based on genres/artists.
- Library Management: View and modify your "Liked Songs" library and create/manage playlists.
- Dual Authentication: Local web redirect or manual copy-paste fallback designed to run seamlessly through the agent interface.
- Robust Connection Handling: Connection pooling via
requests.Sessionand thread-safe automatic token refreshing to prevent memory leaks and credentials expiration crashes. - Log Safety: All server logs and debug outputs are isolated to
stderr, leavingstdoutpurely for MCP protocol messages.
Setup & Configuration
Prerequisites
- Python 3.10+
- A Spotify account (Spotify Premium is required for playback control features).
1. Register Spotify App
- Go to the Spotify Developer Dashboard and log in.
- Click Create App.
- Name your app (e.g., "My MCP Spotify Server") and add a description.
- In the Redirect URIs field, enter:
http://127.0.0.1:8080/callback - Check the box for "Web API" under the APIs to request.
- Click Save.
- In the settings page of your new app, retrieve the Client ID and Client Secret.
2. Configure Environment Variables
Create a file named .env in the root of the Spotify MCP folder:
SPOTIPY_CLIENT_ID=your_spotify_client_id
SPOTIPY_CLIENT_SECRET=your_spotify_client_secret
SPOTIPY_REDIRECT_URI=http://127.0.0.1:8080/callback
Installation & Running
Ensure you have created the virtual environment and installed dependencies:
cd "Spotify MCP"
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Run Server Locally
To run and inspect the server using the MCP CLI tool:
# Activate virtual environment
source .venv/bin/activate
# Start the dev inspector
mcp dev src/server.py
Client Integration
Add the server to your MCP client configuration (e.g., Cursor, Claude Desktop, or custom MCP client).
For example, in claude_desktop_config.json:
{
"mcpServers": {
"spotify": {
"command": "/root/Spotify MCP/.venv/bin/python",
"args": [
"-m",
"src.server"
],
"env": {
"SPOTIPY_CLIENT_ID": "your_spotify_client_id",
"SPOTIPY_CLIENT_SECRET": "your_spotify_client_secret",
"SPOTIPY_REDIRECT_URI": "http://127.0.0.1:8080/callback"
}
}
}
}
Authentication Flow
Because MCP servers run in background processes without terminal input, the server supports a smooth dual-authentication flow:
Flow 1: Automated Local Redirect (Default)
When starting up, if credentials are set but not authorized:
- The server can attempt to open a browser to log you in.
- Once authorized, the Spotify callback redirects to
http://127.0.0.1:8080/callback?code=...which is processed, and tokens are written to.spotify_token_cachein the project root.
Flow 2: Interactive LLM/Agent Authorization (Recommended fallback)
If Flow 1 is blocked or you are running the agent on a remote environment:
- The agent will call the
get_auth_urltool. - The agent will display the Spotify login URL to you in the chat.
- Open the link, log in, authorize, and copy the final URL you redirect to (e.g.,
http://127.0.0.1:8080/callback?code=...). - Paste this URL back to the agent.
- The agent calls the
complete_auth(callback_url)tool. - The server finishes the auth flow, caches the tokens, and immediately gains access!
Available Tools
The server exposes 25 tools categorized as follows:
| Category | Tool | Description |
|---|---|---|
| System | is_authenticated |
Verifies connection status and logged-in user profile. |
get_auth_url |
Generates the login URL to authorize Spotify. | |
complete_auth |
Validates callback redirect and stores tokens. | |
| Playback | get_current_playback |
Shows what's playing, active device, progress bar, volume, etc. |
play_track |
Plays a track by name search or exact URI. | |
play_context |
Plays an album, playlist, or artist by URI. | |
pause_playback |
Pauses Spotify. | |
next_track |
Skips to the next song in the queue. | |
previous_track |
Plays the previous song in history. | |
set_volume |
Sets volume level (0 to 100). | |
get_available_devices |
Lists connected Spotify clients with their status and IDs. | |
transfer_playback |
Switches active playback to another device ID. | |
toggle_shuffle |
Toggles shuffle state. | |
set_repeat_mode |
Sets repeat mode ('track', 'context', 'off'). | |
| Search | search_spotify |
Searches for tracks, albums, artists, or playlists. |
get_recommendations |
Recommends tracks based on seed genres, artists, or tracks. | |
get_featured_playlists |
Retrieves current Spotify editorial featured playlists. | |
get_lyrics |
Retrieves song lyrics from Genius.com via web scraping. | |
| Library | get_user_playlists |
Lists playlists in the user's library. |
create_playlist |
Creates a new playlist. | |
add_to_playlist |
Adds tracks (comma-separated URIs) to a playlist. | |
get_playlist_tracks |
Lists tracks in a specific playlist. | |
get_user_saved_tracks |
Lists Liked Songs. | |
save_tracks |
Adds tracks to Liked Songs. | |
remove_tracks |
Removes tracks from Liked Songs. |
Project created with Gemini Antigravity, with much love. ❤️
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。