#!/usr/bin/env python3 """ Spotify MCP Server Launcher for OpenClaw. Ensures credentials.json is configured from SPOTIFY_REFRESH_TOKEN in a writable HOME, pins mcp<2 to avoid FastMCP renaming breakage in mcp 2.x, then executes uvx mcp-server-spotify. """ import json import os import sys from pathlib import Path def get_writable_home() -> Path: candidates = [ Path("/home/node/.openclaw/spotify-home"), Path("/tmp/spotify-home"), ] for p in candidates: try: p.mkdir(parents=True, exist_ok=True) test_file = p / ".write_test" test_file.write_text("ok") test_file.unlink() return p except Exception: continue return Path("/tmp") def main(): refresh_token = os.environ.get("SPOTIFY_REFRESH_TOKEN") spotify_home = get_writable_home() os.environ["HOME"] = str(spotify_home) creds_dir = spotify_home / ".spotify-mcp" creds_file = creds_dir / "credentials.json" if refresh_token: try: creds_dir.mkdir(parents=True, exist_ok=True) creds = { "access_token": "init", "refresh_token": refresh_token, "expires_at": 0, } creds_file.write_text(json.dumps(creds, indent=2)) try: creds_file.chmod(0o600) except Exception: pass except Exception as e: print(f"[run_spotify_mcp] Warning: Could not write credentials file: {e}", file=sys.stderr) # Launch mcp-server-spotify via uvx with pinned mcp<2 os.execvp("uvx", ["uvx", "--with", "mcp<2", "mcp-server-spotify"]) if __name__ == "__main__": main()