Skip to main content
Glama

addTracksToPlaylist

Add multiple songs to a specific Spotify playlist using track IDs, optionally specifying insertion position.

Instructions

Add tracks to a Spotify playlist

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
playlistIdYesThe Spotify ID of the playlist
trackIdsYesArray of Spotify track IDs to add
positionNoPosition to insert the tracks (0-based index)

Implementation Reference

  • Handler function that destructures args, validates trackIds array, converts IDs to URIs, calls Spotify API to add items to playlist at optional position, handles errors, and returns formatted content response.
    handler: async (args, _extra: SpotifyHandlerExtra) => { const { playlistId, trackIds, position } = args; if (trackIds.length === 0) { return { content: [ { type: 'text', text: 'Error: No track IDs provided', }, ], }; } try { const trackUris = trackIds.map((id) => `spotify:track:${id}`); await handleSpotifyRequest(async (spotifyApi) => { await spotifyApi.playlists.addItemsToPlaylist( playlistId, trackUris, position, ); }); return { content: [ { type: 'text', text: `Successfully added ${trackIds.length} track${ trackIds.length === 1 ? '' : 's' } to playlist (ID: ${playlistId})`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error adding tracks to playlist: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } },
  • Zod schema defining the input parameters: required playlistId (string), trackIds (array of strings), optional position (non-negative number).
    schema: { playlistId: z.string().describe('The Spotify ID of the playlist'), trackIds: z.array(z.string()).describe('Array of Spotify track IDs to add'), position: z .number() .nonnegative() .optional() .describe('Position to insert the tracks (0-based index)'), },
  • src/play.ts:362-371 (registration)
    addTracksToPlaylist tool is included in the exported playTools array.
    export const playTools = [ playMusic, pausePlayback, skipToNext, skipToPrevious, createPlaylist, addTracksToPlaylist, resumePlayback, addToQueue, ];
  • src/index.ts:12-14 (registration)
    playTools (containing addTracksToPlaylist) imported and registered to MCP server via server.tool() calls.
    [...readTools, ...playTools, ...albumTools].forEach((tool) => { server.tool(tool.name, tool.description, tool.schema, tool.handler); });

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/makesh-kumar/spotify-mcp-server'

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