Spotify-MCP-Server
The Spotify MCP Server is a project designed to facilitate AI assistants' control over Spotify playback by using the Model-Context-Protocol (MCP). It provides a standardized interface for accessing key Spotify features through MCP tools, integrating seamlessly with AI models and bots.
๐ต Spotify MCP Server
Welcome to the Spotify MCP Server!
This project enables AI assistants and other clients to control Spotify playback via a standardized protocol using the Model-Context-Protocol (MCP) architecture.
๐ฆ What is MCP?
Model-Context-Protocol (MCP) is a framework that allows AI models and assistants to interact with external tools and services through a standardized interface.
This project exposes Spotify functionality as MCP tools, making it accessible to any MCP-compatible clientโincluding AI assistants, bots, and more.
โจ Features
- ๐ Search: Search for songs on Spotify
- โถ๏ธ Play: Play songs on your active Spotify device
- โธ๏ธ Pause: Pause currently playing music
- โญ๏ธ Next Track: Skip to the next track
- โฎ๏ธ Previous Track: Go back to the previous track
- ๐ Volume Control: Adjust the playback volume
- ๐ค User Info: Get current user information
- ๐ Playlists: View your Spotify playlists
- ๐ต Now Playing: Get information about the currently playing track
๐ ๏ธ Prerequisites
- Python 3.11 or higher
- Spotify Premium account
- Spotify Developer credentials
โก Setup
-
Clone this repository:
git clone https://github.com/KaanCL/Spotify-MCP-Server.git cd Spotify-MCP-Server
-
Set up a virtual environment:
uv venv
-
Install dependencies:
uv sync
If you need to install Spotipy separately:
pip install spotipy
Note: FastMCP and other dependencies are managed via
uv sync
.
๐ Configuration
- Create a Spotify application at Spotify Developer Dashboard
- Get your Client ID and Client Secret from your Spotify application
- Create a
.env
file in the project root with your credentials:CLIENT_ID=your-spotify-client-id CLIENT_SECRET=your-spotify-client-secret REDIRECT_URI=http://localhost:8888/callback
๐ Initialization & First Run
When you run the MCP server for the first time:
- A browser window will open asking you to log in to your Spotify account
- You'll need to authorize the application to access your Spotify account
- After authorization, you'll be redirected to the callback URL
- The MCP server will initialize and start listening for connections on port 8080
๐ Usage
Run the MCP server:
uv run mcp install main.py
The server will initialize and register the following MCP tools:
Tool Name | Description | Parameters |
---|---|---|
search | Find songs on Spotify | query: str |
start_playback | Play a specific song | track_name: str |
pause_playback | Pause the currently playing song | None |
resume_playback | Resume paused playback | None |
next_track | Skip to the next track | None |
previous_track | Go back to the previous track | None |
get_user_playlists | View your Spotify playlists | None |
set_player_volume | Adjust the volume | volume: int (0-100) |
current_playback | Get info about what's playing | None |
get_current_user | Get user profile information | None |
๐งฉ Tool Documentation
Each MCP tool is a Python function decorated with @mcp.tool()
and is exposed as an API endpoint.
All tools return Python dictionaries (or lists of dicts) for easy JSON serialization and integration.
-
search(query: str) โ dict
Search for tracks on Spotify by query string. Returns up to 5 matching tracks, each with name, artist, album, URI, and Spotify URL. -
start_playback(track_name: str) โ dict
Start playback for a given track name. Searches for the track and starts playback on the user's active device. -
pause_playback() โ dict
Pause the current playback on the active device. -
resume_playback() โ dict
Resume playback if paused. -
next_track() โ dict
Skip to the next track in the current playlist or queue. -
previous_track() โ dict
Return to the previous track. -
get_user_playlists() โ dict
Retrieve the user's playlists, including name, URL, ID, and track count. -
set_player_volume(volume: int) โ dict
Set the Spotify player's volume (0-100). -
current_playback() โ dict
Get info about the currently playing track, including playback state, track name, artist, album, progress, and duration. -
get_current_user() โ dict
Get the current Spotify user's profile information (display name, email, user ID).
๐ค MCP Client Example
Any MCP-compatible client can interact with this server, including AI assistants and programmatic clients:
from mcp.client import MCPClient
# Connect to the MCP server
client = MCPClient("http://localhost:8080")
# Search for a song
results = client.call("search", {"query": "Money Trees"})
print(results)
# Play a song
client.call("start_playback", {"track_name": "Money Trees"})
# Pause playback
client.call("pause_playback")
# Get current playback info
playback_info = client.call("current_playback")
print(playback_info)
โ ๏ธ Error Handling
All API responses are returned as JSON objects.
If an error occurs, the response will include an "error"
key with a descriptive message.
Examples:
{ "error": "No tracks found: No tracks found for your query." }
or
{ "error": "No active device found: Please open Spotify on a device and try again." }
Tip: Always check for the
"error"
key in responses before using the data.
Error Handling Internals
- Errors are standardized using a utility function (
format_error
) and an Enum (SpotifyError
) for clarity and maintainability. - Common error scenarios include: no active device, no tracks found, nothing playing, invalid volume, and authentication errors.
- All errors are returned in a consistent format for easy client-side handling.
๐๏ธ Architecture Diagram
graph TD
A[MCP Client] -- HTTP --> B[FastMCP Server]
B -- Python Call --> C[spotify.py]
C -- REST API --> D[Spotify Web API]
๐ง How It Works: MCP Architecture
- MCP Server: Registers tools and handles requests.
- FastMCP Implementation: Uses FastMCP for a lightweight, high-performance server.
- Spotify Integration: Connects to the Spotify API using Spotipy.
- Tool Registration: Each Spotify function is registered as an MCP tool with type hints and documentation.
The MCP server exposes these tools through a standardized protocol, allowing AI models and other clients to discover and call the tools without needing to understand the underlying Spotify API implementation details.
๐งฉ Code Quality & Maintainability
- Enums: Used for Spotify scopes and error types for clarity and type safety.
- Centralized Error Handling: All errors are formatted using a utility function for consistency.
- DRY Principle: Common checks (like active device) are factored into utility functions.
- Type Hints: All functions use Python type hints for better code analysis and editor support.
- Comprehensive Docstrings: Every function and class is documented for clarity.
- Extensible: Add new MCP tools by defining a function in
spotify.py
and registering it with@mcp.tool()
.
๐งฉ Extending the Server
To add new Spotify features:
- Implement a new function in
spotify.py
with proper error handling and docstrings. - Register the function as an MCP tool in
main.py
using the@mcp.tool()
decorator. - Document the new tool in this README.
Developer Note:
All MCP tools are designed to return Python dictionaries for easy JSON serialization and integration with web APIs or front-end clients.
To extend functionality, simply add new@mcp.tool()
functions following the same pattern as above.
For debugging or logging, you can print or log responses within each tool.
Type hints and detailed docstrings are provided for all tools to enhance code clarity, maintainability, and discoverability (especially in IDEs).
Ensure that thespotify
module implements the required API interactions.
For authentication and authorization, make sure the Spotify API credentials are properly configured in your environment.
๐ Security Notes
- Your Spotify credentials are stored locally in the
.env
file - The
.env
file is excluded from git via.gitignore
- The MCP server only accepts connections from localhost by default
- Never commit your credentials to version control!
๐งฉ Troubleshooting
- No active device found: Make sure Spotify is open and active on one of your devices.
- Authentication errors: Double-check your
.env
credentials and Spotify Developer Dashboard settings. - API rate limits: Spotify API has rate limits; avoid excessive requests.
- Unexpected errors: Check the
"error"
key in the response for details.
๐จโ๐ป Developer & Contribution Notes
- All functions and tools return dicts or lists for easy JSON serialization.
- Error handling is consistent; always check for
"error"
in responses. - Extend the MCP tools in
main.py
to add new Spotify features. - For debugging, add logging or print statements as needed.
- Pull requests and suggestions are welcome! Please open an issue first to discuss what you would like to change.