check-credentials-status
Verify the status of Spotify account credentials to ensure proper connectivity and authorization for managing music, playlists, and recommendations through the Spotify MCP Server.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {},
"type": "object"
}
Implementation Reference
- mcp/spotify-mcp.ts:108-170 (handler)Handler for the check-credentials-status tool: checks if credentials are set and validates them by fetching the Spotify user profile using a potentially refreshed access token.server.tool("check-credentials-status", {}, async () => { if ( !spotifyAuthInfo.accessToken || !spotifyAuthInfo.refreshToken || !spotifyAuthInfo.clientId || !spotifyAuthInfo.clientSecret ) { return { content: [ { type: "text", text: "Spotify credentials are not set. Please use the set-spotify-credentials tool.", }, ], }; } try { const accessToken = await getValidAccessToken(); const response = await fetch("https://api.spotify.com/v1/me", { headers: { Authorization: `Bearer ${accessToken}`, }, }); if (response.ok) { const userData = (await response.json()) as any; return { content: [ { type: "text", text: `Spotify credentials are valid.\nLogged in as: ${ userData.display_name } (${userData.email || "email not available"})`, }, ], }; } else { return { content: [ { type: "text", text: `Spotify credentials may be invalid. Status code: ${response.status}`, }, ], isError: true, }; } } catch (error) { return { content: [ { type: "text", text: `Error checking credentials: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } });
- mcp/spotify-mcp.ts:22-79 (helper)Helper function to obtain a valid Spotify access token, refreshing it if the current one is expired or invalid.async function getValidAccessToken() { if (!spotifyAuthInfo.accessToken || !spotifyAuthInfo.refreshToken) { throw new Error( "No access token available. Please set credentials first using the set-spotify-credentials tool." ); } try { // Try using current token const response = await fetch("https://api.spotify.com/v1/me", { headers: { Authorization: `Bearer ${spotifyAuthInfo.accessToken}`, }, }); // If token works, return it if (response.ok) { return spotifyAuthInfo.accessToken; } console.error("Access token expired, refreshing..."); // If token doesn't work, refresh it const refreshResponse = await fetch( "https://accounts.spotify.com/api/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: "Basic " + Buffer.from( spotifyAuthInfo.clientId + ":" + spotifyAuthInfo.clientSecret ).toString("base64"), }, body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: spotifyAuthInfo.refreshToken, }), } ); const data = (await refreshResponse.json()) as any; if (data.access_token) { console.error("Successfully refreshed access token"); spotifyAuthInfo.accessToken = data.access_token; return spotifyAuthInfo.accessToken; } throw new Error("Failed to refresh access token"); } catch (error) { throw new Error( "Error with access token: " + (error instanceof Error ? error.message : String(error)) ); } }