getPlaylistTracks
Retrieve tracks from a Spotify playlist by specifying the playlist ID, with options to limit results and paginate through tracks for efficient data access.
Instructions
Get a list of tracks in a Spotify playlist
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of tracks to return (1-50) | |
| offset | No | Offset for pagination (0-based index) | |
| playlistId | Yes | The Spotify ID of the playlist |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"limit": {
"description": "Maximum number of tracks to return (1-50)",
"maximum": 50,
"minimum": 1,
"type": "number"
},
"offset": {
"description": "Offset for pagination (0-based index)",
"minimum": 0,
"type": "number"
},
"playlistId": {
"description": "The Spotify ID of the playlist",
"type": "string"
}
},
"required": [
"playlistId"
],
"type": "object"
}
Implementation Reference
- src/read.ts:236-305 (handler)Complete implementation of the getPlaylistTracks tool, including schema definition and the handler function that fetches playlist items from Spotify API, filters tracks, formats them with artist, duration, and ID, and returns a formatted text response.const getPlaylistTracks: tool<{ playlistId: z.ZodString; limit: z.ZodOptional<z.ZodNumber>; offset: z.ZodOptional<z.ZodNumber>; }> = { 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) .optional() .describe('Offset for pagination (0-based index)'), }, 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 `${offset + i + 1}. [Removed track]`; if (isTrack(track)) { const artists = track.artists.map((a) => a.name).join(', '); const duration = formatDuration(track.duration_ms); return `${offset + i + 1}. "${track.name}" by ${artists} (${duration}) - ID: ${track.id}`; } return `${offset + i + 1}. Unknown item`; }) .join('\n'); return { content: [ { type: 'text', text: `# Tracks in Playlist (${offset + 1}-${offset + playlistTracks.items.length} of ${playlistTracks.total})\n\n${formattedTracks}`, }, ], }; }, };
- src/read.ts:243-256 (schema)Input schema using Zod for validating playlistId (required string), limit (optional 1-50), and offset (optional >=0).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) .optional() .describe('Offset for pagination (0-based index)'), },
- src/read.ts:531-539 (registration)Registration of the getPlaylistTracks tool within the readTools export array, likely used to register all read-related tools with the MCP server.export const readTools = [ searchSpotify, getNowPlaying, getMyPlaylists, getPlaylistTracks, getRecentlyPlayed, getUsersSavedTracks, getQueue, ];