This directory houses the backend gateway service for the Spotify AI Assistant. It is implemented in Python 3.11+ using FastAPI, LangChain, and the MCP Python SDK.
The gateway acts as the orchestrator: it establishes a connection to the local spotify-mcp-server over a stdio subprocess, routes user requests from the Android app to a local LLM running on Ollama, and manages real-time status and playback streaming over WebSockets.
- WebSocket Agent Streaming: A reactive
/chatendpoint that streams token-by-token LLM textual responses and tool calling lifecycle states (e.g.Thinking...,Calling tool search_spotify...). - Dynamic Path Resolution: Automatically locates the adjacent
spotify-mcp-serverand virtual environment without hardcoding absolute paths. Can be overridden in.envif needed. - Asynchronous Playback Sync: Runs a background async task that polls the Spotify API every 2 seconds and broadcasts playback events (track, artist, album art, play status, volume, device) down to the client.
- Strict Type Safety: Fully type-annotated code validated by
mypy. - Clean Session State: Manages conversational history memory independently per active WebSocket session.
- Python 3.11+
- uv (Recommended Python package manager):
curl -LsSf https://astral.sh/uv/install.sh | sh - Ollama: Running locally with the
qwen2.5:3bmodel:ollama run qwen2.5:3b
Copy the example configuration file and fill in your developer credentials:
cp .env.example .env| Key | Description | Default / Example |
|---|---|---|
SPOTIFY_CLIENT_ID |
Your Spotify developer client ID | your_spotify_client_id_here |
SPOTIFY_CLIENT_SECRET |
Your Spotify developer client secret | your_spotify_client_secret_here |
SPOTIFY_REFRESH_TOKEN |
Generated Spotify refresh token | your_spotify_refresh_token_here |
OLLAMA_MODEL |
The Ollama model name to use | qwen2.5:3b |
SPOTIFY_MCP_SERVER_DIR |
(Optional) Path override for the MCP server folder | Adjacent directory |
SPOTIFY_MCP_PYTHON_PATH |
(Optional) Path override for the virtualenv python interpreter | Adjacent directory virtualenv |
SPOTIFY_REDIRECT_PORT |
Port to use for Spotify callback authorization | 8888 |
Sync project dependencies using uv:
uv syncStart the FastAPI server using uvicorn. Make sure to bind to 0.0.0.0 so that external devices (like your physical Android phone on the same WiFi network) can access the service:
uv run uvicorn main:app --host 0.0.0.0 --port 8000Run type diagnostics using mypy:
uv run mypy main.py mcp_client.pyWe provide a standalone python script to test WebSocket connections, status notifications, and agent tool execution. Run it with:
uv run python path/to/scratch/test_websocket.py