get_top_artists
Retrieve a user's most listened-to Spotify artists within specified time periods, enabling analysis of listening habits and preferences.
Instructions
Get user's top artists from Spotify
Args:
limit: Number of artists (max 50)
time_range: One of 'short_term' (4 weeks), 'medium_term' (6 months), 'long_term' (all time)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | ||
| time_range | No | medium_term |
Implementation Reference
- main.py:112-120 (handler)MCP tool handler for get_top_artists. Decorated with @mcp.tool(), calls SpotifyClient.get_top_artists and returns result as string.@mcp.tool() async def get_top_artists(limit: int = 20, time_range: str = "medium_term") -> str: """ Get user's top artists from Spotify Args: limit: Number of artists (max 50) time_range: One of 'short_term' (4 weeks), 'medium_term' (6 months), 'long_term' (all time) """ return await client.get_top_artists(limit, time_range)
- spotify.py:205-220 (helper)Core implementation in SpotifyClient that queries Spotify API for user's top artists using spotipy.current_user_top_artists.async def get_top_artists( self, limit: int = 20, time_range: str = "medium_term" ) -> dict: """ Get the current user's top artists. - limit: Number of artists to return (default 20, max 50) - time_range: 'short_term' (last 4 weeks), 'medium_term' (last 6 months), or 'long_term' (all time) (default: medium_term) """ try: results = self.sp.current_user_top_artists( limit=limit, offset=0, time_range=time_range ) return results except Exception as e: return f"Error getting top artists: {str(e)}"