Skip to main content
Glama
igorgarbuz
by igorgarbuz

addTracksToPlaylist

Add multiple songs to a Spotify playlist by specifying track IDs and playlist ID. Insert tracks at specific positions to organize your music collection.

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

  • The async handler function that implements the core logic for adding tracks to a Spotify playlist. It handles input validation, converts track IDs to Spotify URIs, invokes the Spotify API via handleSpotifyRequest, and returns formatted success or error responses.
    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) }`, }, ], }; } }, };
  • Input schema definition using Zod validators for playlistId (string), trackIds (array of strings), and optional position (non-negative number). Includes Zod describe annotations for documentation.
    name: 'addTracksToPlaylist', description: 'Add tracks to a Spotify playlist', 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/write.ts:238-243 (registration)
    Local registration of the addTracksToPlaylist tool within the writeTools export array, grouping write-related Spotify tools.
    export const writeTools = [ addToQueue, addTracksToPlaylist, createPlaylist, removeTracksFromPlaylist, ];
  • src/index.ts:12-14 (registration)
    Global MCP server registration: all tools from playTools, readTools, and writeTools (including addTracksToPlaylist) are dynamically registered using server.tool() in a loop.
    [...playTools, ...readTools, ...writeTools].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/igorgarbuz/spotify-mcp'

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