Skip to main content
Glama

Spotify Model Context Protocol

by belljustin
main.py2.04 kB
import random import string import os from flask import Flask, redirect, request, session import requests from dotenv import load_dotenv from spotify_client import SpotifyClient from db import init_db, save_user_tokens load_dotenv() # Initialize the database when the app starts init_db() def generateRandomString(length: int) -> str: """ Generate a random string of a given length """ return ''.join(random.choices(string.ascii_letters + string.digits, k=length)) app = Flask(__name__) app.secret_key = os.getenv("FLASK_SECRET_KEY", generateRandomString(16)) CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID") CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET") REDIRECT_URI = "http://127.0.0.1:5000/callback" spotify_client = SpotifyClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI) @app.route("/login") def login(): state: str = generateRandomString(16) session["state"] = state auth_url = spotify_client.build_auth_url(state) return redirect(auth_url) @app.route("/callback") def callback(): code: str = request.args.get("code") state: str = request.args.get("state") if state is None or state != session["state"]: return redirect("/") token_data = spotify_client.get_access_token(code) if token_data: access_token = token_data["access_token"] refresh_token = token_data["refresh_token"] user_profile = get_user_profile(access_token) # Store tokens in database save_user_tokens( spotify_id=user_profile['id'], email=user_profile['email'], access_token=access_token, refresh_token=refresh_token ) return f"Success: {user_profile['email']}" else: return "Error: Failed to get access token", 400 def get_user_profile(access_token: str) -> dict: response = requests.get("https://api.spotify.com/v1/me", headers={"Authorization": f"Bearer {access_token}"}) return response.json() if __name__ == "__main__": app.run(debug=True)

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/belljustin/spotify-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server