Skip to main content
Glama
spotify-mcp

spotify-mcp

by codebyotto
README.md
# spotify-mcp

A [FastMCP](https://github.com/jlowin/fastmcp) server that lets an AI agent search Spotify, read your listening history, and create or extend your playlists. It runs over stdio, so it works with Claude Code and other MCP clients.

It targets the Spotify Web API **after the February 2026 Development Mode changes**:
- playlist items are at `/playlists/{id}/items`;
- playlists are created with `POST /me/playlists`;
- search returns at most 10 results per request;
- batch `GET /tracks` no longer exists.

## Tools

| Tool | What it does | Annotations |
|---|---|---|
| `search_tracks(query, limit=5, offset=0)` | Searches the catalog. Returns unique, ranked, compact tracks. `limit` can be up to 30; the tool fetches Spotify's 10-result pages itself and removes duplicates. | read-only, idempotent, open-world |
| `list_my_playlists(limit=20, offset=0)` | Lists your playlists with `id`, `item_count` and a computed `can_edit` (owner or collaborative). | read-only, idempotent, open-world |
| `get_recently_played(limit=50, before=null)` | Lists your recently played tracks (Spotify keeps about the last 50). | read-only, idempotent, open-world |
| `get_my_top_tracks(time_range="short_term", limit=50, offset=0)` | Lists your most played tracks for about 4 weeks, 6 months or a year. | read-only, idempotent, open-world |
| `create_playlist(name, description="", public=false)` | Creates an empty playlist. Reads it back and reports what Spotify actually stored, with `warnings` if that differs from the request. | write, not destructive, **not idempotent**, open-world |
| `add_tracks_to_playlist(playlist_id, track_uris, position=null, dry_run=true)` | Previews (the default) or performs adding 1–20 tracks. Both modes first check that the playlist exists and is editable and that every track exists. | write, not destructive, **not idempotent**, open-world |

Typical chain: `search_tracks` (or a history tool) → `list_my_playlists` or `create_playlist` → `add_tracks_to_playlist(dry_run=true)` → you confirm → the same call with `dry_run=false`.

Track `uri` values from the read tools go into `add_tracks_to_playlist.track_uris`. Playlist `id` values from `list_my_playlists` or `create_playlist` go into `add_tracks_to_playlist.playlist_id`.

## Requirements
- [uv](https://docs.astral.sh/uv/) and Python ≥ 3.12.
- A Spotify **Premium** account. Since February 2026 the owner of a Development Mode app must have Premium, and the app allows at most 5 users.

## Commands

| What | Command |
|---|---|
| Install deps | `uv sync` |
| One-time Spotify login | `uv run spotify-mcp-login` |
| Check the stored login | `uv run spotify-mcp-login --check` |
| Start the server (stdio) | `uv run spotify-mcp` |
| Run the tests | `uv run pytest` |

The tests are fully mocked with `respx`, so they need no network and no credentials.

## Spotify setup (one time)
1. Go to <https://developer.spotify.com/dashboard> and choose **Create app**.
   - Under *Which API/SDKs are you planning to use?*, tick **Web API**.
   - Add the redirect URI **`http://127.0.0.1:8888/callback`**. Spotify does not accept `localhost`; it must be the loopback IP.
2. In the app's **User Management** tab, add the email of your Spotify account.
3. `cp .env.example .env` and set `SPOTIFY_CLIENT_ID`. The client secret is **not** needed because the server uses PKCE.
4. Run `uv run spotify-mcp-login`. A browser opens; approve the requested scopes.
   - The refresh token is stored in `.spotify_token.json` with mode 0600. The file is git-ignored.
   - The server refreshes access tokens from it automatically.

Requested scopes:
- `playlist-read-private` and `playlist-read-collaborative`, to list your playlists.
- `playlist-modify-private` and `playlist-modify-public`, to create playlists and add tracks.
- `user-read-private`, for search and your profile.
- `user-read-recently-played` and `user-top-read`, for your listening history.

## Connect to Claude Code
1. Copy `.mcp.json.template` to `.mcp.json`, in the directory where you start Claude Code. Or add the same entry to your user-level MCP config.
2. Replace `<ABSOLUTE_PATH_TO_REPO>` with the path to this repository and `<your-spotify-client-id>` with your Client ID.
   - `.mcp.json` is git-ignored because it contains the Client ID.
   - If the `env` block is left out, the server falls back to this repository's `.env`.
3. Start `claude` and approve the `spotify` server.
4. `/mcp` should show `spotify · connected` with 6 tools.

Then you can ask things like: *"Find five songs like my top tracks from the last month and put them in a new private playlist called Discoveries. Show me what you'll add first."*

## Design notes
- **Auth.** OAuth Authorization Code with PKCE.
  - Only the public Client ID appears in config, so there is no client secret that could leak.
  - Login is a separate command, because a stdio server cannot run an interactive browser flow in the middle of a tool call.
  - The server refreshes the token before it expires, and on a `401` it refreshes once and retries. If Spotify returns a new refresh token, it is saved.
- **Errors.** Every failure becomes `CATEGORY (HTTP n): what failed. Retryable: yes/no [(after Ns)]. Next step: …`.
  - Categories: `INVALID_INPUT`, `AUTH_REQUIRED`, `CONFIG_ERROR`, `PERMISSION_DENIED`, `NOT_FOUND`, `RATE_LIMITED`, `QUOTA_EXCEEDED`, `UPSTREAM_UNAVAILABLE`.
  - A `429` is not retried silently: the agent gets `Retry-After` and decides.
- **Inputs.** Schema bounds and patterns reject wrong input before any API call: Spotify's real `limit` ranges, a 22-character playlist ID, `spotify:track:` URIs, and 1–20 tracks per call.
- **Outputs.** Compact pydantic models keep only names and IDs; images, hrefs and markets are dropped.
- **Safety.** Writes only add; nothing is removed or overwritten. `add_tracks_to_playlist` defaults to a dry run, and `create_playlist` reads the playlist back because Spotify can echo `public=false` for a playlist it stores as public.

## Layout
```
src/spotify_mcp/
  server.py   FastMCP instance + the 6 tools; main() serves stdio
  client.py   async httpx wrapper: auth header, 401 refresh-retry, error mapping
  auth.py     PKCE login CLI, token store (0600), refresh
  errors.py   error categories + agent-facing messages
  models.py   compact output models + mappers
tests/        pytest tests; tools are called through MCP (fastmcp.Client), Spotify is mocked with respx
```

## License
[MIT](LICENSE)

TDQS

A4.1/5.0

Scored across 6 tools

Disambiguation4/5

Each tool targets a distinct resource+action: catalog search, playlist listing, recently played, top tracks, playlist creation, and track addition. The only potential confusion is between get_recently_played and get_my_top_tracks, but the descriptions explicitly cross-reference each other to steer selection.

Naming Consistency4/5

All names use a clear, readable snake_case verb_noun pattern (search_tracks, create_playlist, add_tracks_to_playlist). The only deviation is the inconsistent inclusion of 'my' in list_my_playlists and get_my_top_tracks versus the plain forms elsewhere, but this is minor and never ambiguous.

Tool Count4/5

Six tools is a tight, well-scoped set for a read-heavy Spotify integration with a single write path, and each tool clearly earns its place. It sits at the low end of the ideal 3-15 range but is not thin enough to hurt usability.

Completeness3/5

The create/search/add flow is well covered, but the surface has notable gaps: there is no way to read the contents of an existing playlist, remove or reorder tracks, or undo the warned-about non-idempotent add. An agent can build playlists but cannot inspect or clean them up afterward, creating dead ends.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues