Upload MCP_instructions.md
Browse files- MCP_instructions.md +133 -0
MCP_instructions.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Demo MIT - MCP Server
|
| 3 |
+
emoji: 馃捇
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
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.
|