Update MCP_instructions.md
Browse files- MCP_instructions.md +79 -68
MCP_instructions.md
CHANGED
|
@@ -8,126 +8,137 @@ sdk_version: 5.29.0
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Spotify MCP Server
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
##
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
- `pharmaia/Demo_MCP_Server_Spotify`
|
| 22 |
- https://huggingface.co/spaces/pharmaia/Demo_MCP_Server_Spotify
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space`
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
|
| 31 |
|
| 32 |
-
##
|
| 33 |
-
|
| 34 |
-
- `FastAPI`
|
| 35 |
-
- `FastMCP`
|
| 36 |
-
- `httpx`
|
| 37 |
-
- `spotify_tokens.json` (
|
| 38 |
-
|
| 39 |
-
##
|
| 40 |
-
|
| 41 |
-
1.
|
| 42 |
-
2.
|
| 43 |
-
3. Spotify
|
| 44 |
-
4.
|
| 45 |
-
5.
|
| 46 |
-
|
| 47 |
-
##
|
| 48 |
-
|
| 49 |
-
- `GET /`:
|
| 50 |
-
- `GET /health`:
|
| 51 |
-
- `GET /auth/status`:
|
| 52 |
-
- `GET /auth/reset`:
|
| 53 |
-
- `GET /auth/login?force=true`:
|
| 54 |
-
- `GET /auth/callback`:
|
| 55 |
-
- `SSE /gradio_api/mcp/sse`:
|
| 56 |
-
|
| 57 |
-
##
|
| 58 |
-
|
| 59 |
-
- `spotify_auth_status`:
|
| 60 |
-
- `spotify_get_auth_url`:
|
| 61 |
-
- `spotify_get_my_profile`:
|
| 62 |
-
- `spotify_search_tracks`:
|
| 63 |
-
- `spotify_list_saved_tracks`:
|
| 64 |
-
- `spotify_list_recently_played`:
|
| 65 |
-
- `spotify_get_top_tracks`: top tracks
|
| 66 |
-
- `spotify_check_saved_tracks`:
|
| 67 |
- `spotify_save_tracks` / `spotify_remove_saved_tracks` / `spotify_set_tracks_saved`.
|
| 68 |
-
- `spotify_list_my_playlists`:
|
| 69 |
-
- `spotify_create_playlist`:
|
| 70 |
-
- `spotify_add_items_to_playlist`:
|
| 71 |
-
- `spotify_update_playlist_details`:
|
| 72 |
-
- `spotify_replace_playlist_items`:
|
| 73 |
-
- `spotify_delete_playlist`:
|
| 74 |
|
| 75 |
-
## Variables
|
| 76 |
|
| 77 |
-
|
| 78 |
|
| 79 |
- `SPOTIFY_CLIENT_ID`
|
| 80 |
- `SPOTIFY_CLIENT_SECRET`
|
| 81 |
- `SPOTIFY_REDIRECT_URI` = `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/callback`
|
| 82 |
-
- `SPOTIFY_SCOPES` (
|
| 83 |
-
- `SPOTIFY_TOKEN_FILE` (
|
| 84 |
- `MCP_PUBLIC_SSE_URL` = `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
|
| 85 |
-
- `PORT` (
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/callback`
|
| 90 |
|
| 91 |
-
##
|
| 92 |
|
| 93 |
-
1.
|
| 94 |
|
| 95 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/login`
|
| 96 |
|
| 97 |
-
2.
|
| 98 |
-
3.
|
| 99 |
|
| 100 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/status`
|
| 101 |
|
| 102 |
-
4.
|
| 103 |
|
| 104 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
|
| 105 |
|
| 106 |
-
##
|
| 107 |
|
| 108 |
-
1.
|
| 109 |
|
| 110 |
```bash
|
| 111 |
pip install -r requirements.txt
|
| 112 |
```
|
| 113 |
|
| 114 |
-
2.
|
| 115 |
|
| 116 |
```bash
|
| 117 |
python app.py
|
| 118 |
```
|
| 119 |
|
| 120 |
-
3.
|
| 121 |
|
| 122 |
- `http://localhost:7860/auth/login`
|
| 123 |
|
| 124 |
-
4.
|
| 125 |
|
| 126 |
- `http://localhost:7860/gradio_api/mcp/sse`
|
| 127 |
|
| 128 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
- El servidor guarda estado OAuth en memoria con TTL para prevenir replay de callback.
|
| 131 |
-
- Se validan scopes para operaciones sensibles de playlists.
|
| 132 |
-
- IDs/URIs/URLs de Spotify se normalizan antes de invocar la API.
|
| 133 |
-
- Respuestas de Spotify se transforman a payloads compactos para consumo en tools MCP.
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
+
---
|
| 12 |
+
title: Demo MIT - MCP Server
|
| 13 |
+
emoji: 馃捇
|
| 14 |
+
colorFrom: green
|
| 15 |
+
colorTo: gray
|
| 16 |
+
sdk: gradio
|
| 17 |
+
sdk_version: 5.29.0
|
| 18 |
+
app_file: app.py
|
| 19 |
+
pinned: false
|
| 20 |
+
---
|
| 21 |
|
| 22 |
# Spotify MCP Server
|
| 23 |
|
| 24 |
+
Spotify MCP (Model Context Protocol) server, implemented with **FastAPI + FastMCP**.
|
| 25 |
+
It authenticates a Spotify account via OAuth2 and exposes MCP tools to query profile data, search tracks, read library/recent playback, and manage playlists.
|
| 26 |
|
| 27 |
+
## Target Deployment (Hugging Face)
|
| 28 |
|
| 29 |
+
This server runs in the Space:
|
| 30 |
|
| 31 |
- `pharmaia/Demo_MCP_Server_Spotify`
|
| 32 |
- https://huggingface.co/spaces/pharmaia/Demo_MCP_Server_Spotify
|
| 33 |
|
| 34 |
+
Public base URL:
|
| 35 |
|
| 36 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space`
|
| 37 |
|
| 38 |
+
Public MCP SSE endpoint:
|
| 39 |
|
| 40 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
|
| 41 |
|
| 42 |
+
## General Architecture
|
| 43 |
+
|
| 44 |
+
- `FastAPI` exposes HTTP endpoints for healthcheck and OAuth flow.
|
| 45 |
+
- `FastMCP` registers MCP tools and publishes them over SSE.
|
| 46 |
+
- `httpx` performs calls to Spotify Accounts API (tokens) and Spotify Web API (data/actions).
|
| 47 |
+
- `spotify_tokens.json` (or a configurable path) stores tokens and expiration metadata.
|
| 48 |
+
|
| 49 |
+
## Authentication Flow
|
| 50 |
+
|
| 51 |
+
1. The client opens `/auth/login`.
|
| 52 |
+
2. The server generates an anti-CSRF `state` and redirects to Spotify `/authorize`.
|
| 53 |
+
3. Spotify redirects to `/auth/callback` with `code` + `state`.
|
| 54 |
+
4. The server validates `state`, exchanges `code` for tokens, and saves them.
|
| 55 |
+
5. MCP tools use the stored token; if expired, the server attempts automatic refresh.
|
| 56 |
+
|
| 57 |
+
## HTTP Endpoints (Production)
|
| 58 |
+
|
| 59 |
+
- `GET /`: basic service metadata and useful routes.
|
| 60 |
+
- `GET /health`: simple status (`ok`).
|
| 61 |
+
- `GET /auth/status`: local token status.
|
| 62 |
+
- `GET /auth/reset`: removes local token (forces re-authentication).
|
| 63 |
+
- `GET /auth/login?force=true`: starts OAuth (with consent dialog).
|
| 64 |
+
- `GET /auth/callback`: Spotify OAuth callback.
|
| 65 |
+
- `SSE /gradio_api/mcp/sse`: MCP SSE transport (mounted by FastMCP).
|
| 66 |
+
|
| 67 |
+
## Main MCP Tools
|
| 68 |
+
|
| 69 |
+
- `spotify_auth_status`: authentication status.
|
| 70 |
+
- `spotify_get_auth_url`: direct OAuth authorization URL.
|
| 71 |
+
- `spotify_get_my_profile`: current user profile.
|
| 72 |
+
- `spotify_search_tracks`: track search.
|
| 73 |
+
- `spotify_list_saved_tracks`: saved tracks.
|
| 74 |
+
- `spotify_list_recently_played`: recently played tracks.
|
| 75 |
+
- `spotify_get_top_tracks`: top tracks by time range.
|
| 76 |
+
- `spotify_check_saved_tracks`: checks whether tracks are saved.
|
| 77 |
- `spotify_save_tracks` / `spotify_remove_saved_tracks` / `spotify_set_tracks_saved`.
|
| 78 |
+
- `spotify_list_my_playlists`: user playlists.
|
| 79 |
+
- `spotify_create_playlist`: create playlist.
|
| 80 |
+
- `spotify_add_items_to_playlist`: add items.
|
| 81 |
+
- `spotify_update_playlist_details`: update metadata.
|
| 82 |
+
- `spotify_replace_playlist_items`: replace content.
|
| 83 |
+
- `spotify_delete_playlist`: delete (unfollow) playlist.
|
| 84 |
|
| 85 |
+
## Environment Variables (Hugging Face Spaces)
|
| 86 |
|
| 87 |
+
Configure in **Space Settings > Variables and secrets**:
|
| 88 |
|
| 89 |
- `SPOTIFY_CLIENT_ID`
|
| 90 |
- `SPOTIFY_CLIENT_SECRET`
|
| 91 |
- `SPOTIFY_REDIRECT_URI` = `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/callback`
|
| 92 |
+
- `SPOTIFY_SCOPES` (optional; the server appends required scopes)
|
| 93 |
+
- `SPOTIFY_TOKEN_FILE` (optional; default `spotify_tokens.json`)
|
| 94 |
- `MCP_PUBLIC_SSE_URL` = `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
|
| 95 |
+
- `PORT` (optional; default `7860`)
|
| 96 |
|
| 97 |
+
Important: in Spotify Developer Dashboard, the Redirect URI must match exactly:
|
| 98 |
|
| 99 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/callback`
|
| 100 |
|
| 101 |
+
## Production Usage (HF Space)
|
| 102 |
|
| 103 |
+
1. Open OAuth login:
|
| 104 |
|
| 105 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/login`
|
| 106 |
|
| 107 |
+
2. Complete Spotify consent.
|
| 108 |
+
3. Check status:
|
| 109 |
|
| 110 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/status`
|
| 111 |
|
| 112 |
+
4. Connect MCP client to the public SSE endpoint:
|
| 113 |
|
| 114 |
- `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
|
| 115 |
|
| 116 |
+
## Local Run (Optional)
|
| 117 |
|
| 118 |
+
1. Install dependencies:
|
| 119 |
|
| 120 |
```bash
|
| 121 |
pip install -r requirements.txt
|
| 122 |
```
|
| 123 |
|
| 124 |
+
2. Start the server:
|
| 125 |
|
| 126 |
```bash
|
| 127 |
python app.py
|
| 128 |
```
|
| 129 |
|
| 130 |
+
3. Open in browser:
|
| 131 |
|
| 132 |
- `http://localhost:7860/auth/login`
|
| 133 |
|
| 134 |
+
4. Connect MCP client to local SSE endpoint:
|
| 135 |
|
| 136 |
- `http://localhost:7860/gradio_api/mcp/sse`
|
| 137 |
|
| 138 |
+
## Design Notes
|
| 139 |
+
|
| 140 |
+
- The server keeps OAuth state in memory with TTL to prevent callback replay.
|
| 141 |
+
- Scopes are validated for sensitive playlist operations.
|
| 142 |
+
- Spotify IDs/URIs/URLs are normalized before API calls.
|
| 143 |
+
- Spotify responses are transformed into compact payloads for MCP tool consumption.
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|