Skip to main content
Glama
igorgarbuz
by igorgarbuz

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
NameRequiredDescriptionDefault
playlistIdYesThe Spotify ID of the playlist
limitNoMaximum number of tracks to return (1-50)
offsetNoThe index of the first item to return. Defaults to 0

Implementation Reference

  • 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}`, }, ], }; }, };
  • 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); });
  • 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,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/igorgarbuz/spotify-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server