getPlaylistTracks
Retrieve track listings from Spotify playlists by specifying playlist ID, with options to limit results and set 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-319 (handler)Executes the tool logic: fetches playlist items from Spotify API using handleSpotifyRequest, validates tracks with isTrack, formats track info (name, artists, duration, ID), handles empty/removed/unknown items, returns formatted text response or no-tracks message.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:254-270 (schema)Input schema definition using Zod for validating playlistId (required string), optional limit (1-50), offset (0-1000).name: 'getPlaylistTracks', description: 'Get a list of tracks in a Spotify playlist', 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/index.ts:12-14 (registration)Registers the getPlaylistTracks tool (along with others) with the MCP server by calling server.tool() with its name, description, schema, and handler from the readTools array.[...playTools, ...readTools, ...writeTools].forEach((tool) => { server.tool(tool.name, tool.description, tool.schema, tool.handler); });
- src/read.ts:6-14 (helper)Type guard helper function used in getPlaylistTracks handler to check if a playlist item is a valid SpotifyTrack.function isTrack(item: any): item is SpotifyTrack { return ( item && item.type === 'track' && Array.isArray(item.artists) && item.album && typeof item.album.name === 'string' ); }
- src/read.ts:525-525 (registration)Includes getPlaylistTracks in the readTools export array, which is imported and registered in index.ts.getPlaylistTracks,