getPlaylistTracks
Retrieve track listings from Spotify playlists by providing the playlist ID, with options to limit results and specify offset for pagination.
Instructions
Get a list of tracks in a Spotify playlist
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| playlistId | Yes | The Spotify ID of the playlist | |
| limit | No | Maximum number of tracks to return (1-50) | |
| offset | No | The index of the first item to return. Defaults to 0 |
Implementation Reference
- src/read.ts:271-318 (handler)The asynchronous handler function that retrieves tracks from a specified Spotify playlist using the Spotify API, handles pagination with limit and offset, filters and formats track information including artists and duration, and returns a markdown-formatted list.handler: async (args, extra: SpotifyHandlerExtra) => { const { playlistId, limit = 50, offset = 0 } = args; const playlistTracks = await handleSpotifyRequest(async (spotifyApi) => { return await spotifyApi.playlists.getPlaylistItems( playlistId, undefined, undefined, limit as MaxInt<50>, offset, ); }); if ((playlistTracks.items?.length ?? 0) === 0) { return { content: [ { type: 'text', text: "This playlist doesn't have any tracks", }, ], }; } const formattedTracks = playlistTracks.items .map((item, i) => { const { track } = item; if (!track) return `${i + 1}. [Removed track]`; if (isTrack(track)) { const artists = track.artists.map((a) => a.name).join(', '); const duration = formatDuration(track.duration_ms); return `${i + 1}. "${track.name}" by ${artists} (${duration}) - ID: ${track.id}`; } return `${i + 1}. Unknown item`; }) .join('\n'); return { content: [ { type: 'text', text: `# Tracks in Playlist\n\n${formattedTracks}`, }, ], }; },
- src/read.ts:256-270 (schema)Zod schema for input validation: requires playlistId (string), optional limit (1-50) and offset (0-1000) for pagination.schema: { playlistId: z.string().describe('The Spotify ID of the playlist'), limit: z .number() .min(1) .max(50) .optional() .describe('Maximum number of tracks to return (1-50)'), offset: z .number() .min(0) .max(1000) .optional() .describe('The index of the first item to return. Defaults to 0'), },
- src/read.ts:522-529 (registration)The getPlaylistTracks tool is included in the exported readTools array, registering it for use in the MCP toolset.searchSpotify, getNowPlaying, getUserPlaylists, getPlaylistTracks, getRecentlyPlayed, getFollowedArtists, getUserTopItems, ];
- src/read.ts:6-14 (helper)Helper function to type-guard Spotify items as tracks, used in the handler to safely access track properties.function isTrack(item: any): item is SpotifyTrack { return ( item && item.type === 'track' && Array.isArray(item.artists) && item.album && typeof item.album.name === 'string' ); }