get_access_token
Obtain a Spotify API access token to authenticate and authorize requests for interacting with Spotify's music catalog and services.
Instructions
Get a valid Spotify access token for API requests
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/utils/auth.ts:15-50 (handler)Core handler function that implements the logic to retrieve or refresh the Spotify access token using client credentials grant type, with caching based on expiration.async getAccessToken(): Promise<string> { // Check if we have a valid token if (this.tokenInfo && Date.now() < this.tokenInfo.expiresAt) { return this.tokenInfo.accessToken; } // Get new token try { const response = await axios.post('https://accounts.spotify.com/api/token', new URLSearchParams({ grant_type: 'client_credentials' }).toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + Buffer.from(SPOTIFY_CLIENT_ID + ':' + SPOTIFY_CLIENT_SECRET).toString('base64') } } ); this.tokenInfo = { accessToken: response.data.access_token, expiresAt: Date.now() + (response.data.expires_in * 1000) }; return this.tokenInfo.accessToken; } catch (error) { if (axios.isAxiosError(error)) { throw new McpError( ErrorCode.InternalError, `Failed to get Spotify access token: ${error.response?.data?.error ?? error.message}` ); } throw error; } }
- src/index.ts:695-700 (handler)MCP tool dispatch handler case for 'get_access_token' that invokes AuthManager.getAccessToken() and returns the token as text content.case 'get_access_token': { const token = await this.authManager.getAccessToken(); return { content: [{ type: 'text', text: token }], }; }
- src/index.ts:106-113 (registration)Registration of the 'get_access_token' tool in the MCP server's list of available tools, including name, description, and schema.{ name: 'get_access_token', description: 'Get a valid Spotify access token for API requests', inputSchema: { type: 'object', properties: {}, required: [] },
- src/index.ts:109-112 (schema)Input schema definition for the 'get_access_token' tool, which requires no parameters (empty object).inputSchema: { type: 'object', properties: {}, required: []
- src/utils/api.ts:21-21 (helper)Usage of getAccessToken in SpotifyApi.makeRequest as a supporting utility.const token = await this.authManager.getAccessToken();