auth-spotify
Authenticate with Spotify to enable Claude Desktop integration, allowing for music playback control, playlist management, and recommendations via natural language commands.
Instructions
Authenticate with Spotify
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.ts:631-638 (registration)Registration of the 'auth-spotify' tool in the ListTools handler, including its schema with no input parameters.{ name: "auth-spotify", description: "Authenticate with Spotify", inputSchema: { type: "object", properties: {}, }, },
- index.ts:634-638 (schema)Input schema for 'auth-spotify' tool: empty object (no parameters required).inputSchema: { type: "object", properties: {}, }, },
- index.ts:838-995 (handler)Core handler logic for executing the 'auth-spotify' tool: manages OAuth2 flow with Spotify, token storage, and validation.if (name === "auth-spotify") { try { console.error(`Checking current authentication status...`); try { if (accessToken) { console.error(`We have an access token in memory, testing it...`); try { const testResponse = await axios({ method: 'GET', url: `${SPOTIFY_API_BASE}/me`, headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, }); console.error(`Current token is valid! Already authenticated as: ${testResponse.data.display_name}`); return { content: [ { type: "text", text: `Already authenticated with Spotify as ${testResponse.data.display_name}!`, }, ], }; } catch (testError) { console.error(`Current token is invalid, proceeding with authentication flow`); } } else { console.error(`No access token in memory, checking token file...`); try { if (fs.existsSync(TOKEN_PATH)) { console.error(`Token file exists, attempting to load...`); const fileContent = fs.readFileSync(TOKEN_PATH, 'utf8'); if (fileContent && fileContent.trim() !== '') { console.error(`Found token file with content, parsing...`); const tokenData = JSON.parse(fileContent); accessToken = tokenData.accessToken; refreshToken = tokenData.refreshToken; tokenExpirationTime = tokenData.tokenExpirationTime; try { console.error(`Testing loaded token...`); const testResponse = await axios({ method: 'GET', url: `${SPOTIFY_API_BASE}/me`, headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, }); console.error(`Loaded token is valid! Authenticated as: ${testResponse.data.display_name}`); return { content: [ { type: "text", text: `Already authenticated with Spotify as ${testResponse.data.display_name}!`, }, ], }; } catch (loadedTokenError) { console.error(`Loaded token is invalid, continuing with auth flow...`); } } } } catch (fileError) { console.error(`Error handling token file: ${fileError}`); } } } catch (testError) { console.error(`Error testing current authentication: ${testError}`); } console.error('Starting authentication process...'); await startAuthServer(); if (!accessToken || !refreshToken) { throw new Error("Authentication failed: No tokens received"); } console.error(`Authentication successful, received tokens`); try { console.error(`Testing new tokens...`); const testResponse = await axios({ method: 'GET', url: `${SPOTIFY_API_BASE}/me`, headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, }); console.error(`New tokens are valid! Authenticated as: ${testResponse.data.display_name}`); return { content: [ { type: "text", text: `Successfully authenticated with Spotify as ${testResponse.data.display_name}!`, }, ], }; } catch (newTokenError) { console.error(`New tokens failed verification: ${newTokenError}`); throw new Error("Authentication succeeded but tokens are invalid"); } } catch (error: any) { if (error instanceof ServerAlreadyRunningError) { console.error(`Server already running error: ${error.message}`); try { if (accessToken) { const testResponse = await axios({ method: 'GET', url: `${SPOTIFY_API_BASE}/me`, headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, }); return { content: [ { type: "text", text: `Successfully authenticated with Spotify as ${testResponse.data.display_name}!`, }, ], }; } } catch (testError) { } return { content: [ { type: "text", text: `Another instance is already running on port ${error.port}. If you're having authentication issues, please restart Claude or close any other applications using port ${error.port}.`, }, ], }; } console.error(`Authentication error: ${error.message}`); return { content: [ { type: "text", text: `Authentication failed: ${error.message}`, }, ], }; } }