get_top_artists
Retrieve your most listened-to Spotify artists within customizable timeframes to analyze listening patterns and discover music preferences.
Instructions
Get your top artists
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of artists to return (1-50, default: 10) | |
| time_range | No | Time range (short_term: 4 weeks, medium_term: 6 months, long_term: all time) | medium_term |
Implementation Reference
- src/index.ts:540-558 (handler)Handler for get_top_artists tool: extracts limit and time_range parameters, makes Spotify API request to /me/top/artists endpoint, formats response with artist name, genres, and followers count
case "get_top_artists": { const limit = (args.limit as number) || 10; const timeRange = (args.time_range as string) || "medium_term"; const data = await spotifyRequest( `/me/top/artists?limit=${limit}&time_range=${timeRange}` ); let resultText = `👤 Your Top ${limit} Artists:\n\n`; data.items.forEach((artist: any, index: number) => { resultText += `${index + 1}. ${artist.name}\n`; resultText += ` 🎭 Genres: ${artist.genres.slice(0, 3).join(", ")}\n`; resultText += ` 👥 Followers: ${artist.followers.total.toLocaleString()}\n\n`; }); return { content: [{ type: "text", text: resultText }], }; } - src/index.ts:261-279 (schema)Tool registration with input schema defining two optional parameters: limit (number, default 10) and time_range (enum: short_term/medium_term/long_term, default medium_term)
name: "get_top_artists", description: "Get your top artists", inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of artists to return (1-50, default: 10)", default: 10, }, time_range: { type: "string", enum: ["short_term", "medium_term", "long_term"], description: "Time range (short_term: 4 weeks, medium_term: 6 months, long_term: all time)", default: "medium_term", }, }, }, }, - src/index.ts:54-74 (helper)spotifyRequest helper function that handles authentication and makes HTTP requests to the Spotify API, used by get_top_artists handler
async function spotifyRequest(endpoint: string, method = "GET", data?: any) { const token = await getAccessToken(); try { const response = await axios({ method, url: `https://api.spotify.com/v1${endpoint}`, headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, data, }); return response.data; } catch (error: any) { if (error.response) { throw new Error(`Spotify API Error: ${error.response.data.error?.message || error.response.statusText}`); } throw new Error(`Network Error: ${error.message}`); } }