pharmaia commited on
Commit
18dc43e
verified
1 Parent(s): a372367

Update MCP_instructions.md

Browse files
Files changed (1) hide show
  1. 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
- Servidor MCP (Model Context Protocol) para Spotify, implementado con **FastAPI + FastMCP**.
15
- Permite autenticar una cuenta de Spotify v铆a OAuth2 y exponer tools MCP para consultar perfil, buscar tracks, leer biblioteca/reproducci贸n reciente y gestionar playlists.
16
 
17
- ## Despliegue objetivo (Hugging Face)
18
 
19
- Este servidor corre en el Space:
20
 
21
  - `pharmaia/Demo_MCP_Server_Spotify`
22
  - https://huggingface.co/spaces/pharmaia/Demo_MCP_Server_Spotify
23
 
24
- URL p煤blica base:
25
 
26
  - `https://pharmaia-demo-mcp-server-spotify.hf.space`
27
 
28
- Endpoint MCP SSE p煤blico:
29
 
30
  - `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
31
 
32
- ## Arquitectura general
33
-
34
- - `FastAPI` expone endpoints HTTP para healthcheck y flujo OAuth.
35
- - `FastMCP` registra tools MCP y las publica por SSE.
36
- - `httpx` ejecuta llamadas a Spotify Accounts API (tokens) y Spotify Web API (datos/acciones).
37
- - `spotify_tokens.json` (o ruta configurable) guarda tokens y metadatos de expiraci贸n.
38
-
39
- ## Flujo de autenticaci贸n
40
-
41
- 1. Cliente abre `/auth/login`.
42
- 2. El servidor genera `state` anti-CSRF y redirige a Spotify `/authorize`.
43
- 3. Spotify redirige a `/auth/callback` con `code` + `state`.
44
- 4. El servidor valida `state`, intercambia `code` por tokens y guarda el resultado.
45
- 5. Las tools MCP usan el token guardado; si expira, el servidor intenta refresh autom谩tico.
46
-
47
- ## Endpoints HTTP (en producci贸n)
48
-
49
- - `GET /`: metadata b谩sica del servicio y rutas 煤tiles.
50
- - `GET /health`: estado simple (`ok`).
51
- - `GET /auth/status`: estado del token local.
52
- - `GET /auth/reset`: borra token local (forzar nueva autenticaci贸n).
53
- - `GET /auth/login?force=true`: inicia OAuth (con di谩logo de consentimiento).
54
- - `GET /auth/callback`: callback OAuth de Spotify.
55
- - `SSE /gradio_api/mcp/sse`: transporte MCP SSE (montado por FastMCP).
56
-
57
- ## Tools MCP principales
58
-
59
- - `spotify_auth_status`: estado de autenticaci贸n.
60
- - `spotify_get_auth_url`: URL directa de autorizaci贸n OAuth.
61
- - `spotify_get_my_profile`: perfil del usuario actual.
62
- - `spotify_search_tracks`: b煤squeda de canciones.
63
- - `spotify_list_saved_tracks`: tracks guardados.
64
- - `spotify_list_recently_played`: reproducci贸n reciente.
65
- - `spotify_get_top_tracks`: top tracks por rango temporal.
66
- - `spotify_check_saved_tracks`: valida si tracks est谩n guardados.
67
  - `spotify_save_tracks` / `spotify_remove_saved_tracks` / `spotify_set_tracks_saved`.
68
- - `spotify_list_my_playlists`: listas del usuario.
69
- - `spotify_create_playlist`: crear playlist.
70
- - `spotify_add_items_to_playlist`: agregar 铆tems.
71
- - `spotify_update_playlist_details`: actualizar metadata.
72
- - `spotify_replace_playlist_items`: reemplazar contenido.
73
- - `spotify_delete_playlist`: eliminar (unfollow) playlist.
74
 
75
- ## Variables de entorno (Hugging Face Spaces)
76
 
77
- Configurar en **Settings > Variables and secrets** del Space:
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` (opcional; el servidor completa scopes requeridos)
83
- - `SPOTIFY_TOKEN_FILE` (opcional; default `spotify_tokens.json`)
84
  - `MCP_PUBLIC_SSE_URL` = `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
85
- - `PORT` (opcional; default `7860`)
86
 
87
- Importante: en Spotify Developer Dashboard, el Redirect URI debe coincidir exactamente con:
88
 
89
  - `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/callback`
90
 
91
- ## Uso en producci贸n (HF Space)
92
 
93
- 1. Abrir login OAuth:
94
 
95
  - `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/login`
96
 
97
- 2. Completar consentimiento de Spotify.
98
- 3. Verificar estado:
99
 
100
  - `https://pharmaia-demo-mcp-server-spotify.hf.space/auth/status`
101
 
102
- 4. Conectar cliente MCP al SSE p煤blico:
103
 
104
  - `https://pharmaia-demo-mcp-server-spotify.hf.space/gradio_api/mcp/sse`
105
 
106
- ## Ejecuci贸n local (opcional)
107
 
108
- 1. Instalar dependencias:
109
 
110
  ```bash
111
  pip install -r requirements.txt
112
  ```
113
 
114
- 2. Ejecutar servidor:
115
 
116
  ```bash
117
  python app.py
118
  ```
119
 
120
- 3. Abrir en navegador:
121
 
122
  - `http://localhost:7860/auth/login`
123
 
124
- 4. Conectar cliente MCP al SSE local:
125
 
126
  - `http://localhost:7860/gradio_api/mcp/sse`
127
 
128
- ## Notas de dise帽o
 
 
 
 
 
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