addTracksToPlaylist
Add tracks to a Spotify playlist using track IDs and specify the insertion position. Manage playlists efficiently with the Spotify MCP Server.
Instructions
Add tracks to a Spotify playlist
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| playlistId | Yes | The Spotify ID of the playlist | |
| position | No | Position to insert the tracks (0-based index) | |
| trackIds | Yes | Array of Spotify track IDs to add |
Implementation Reference
- src/play.ts:222-269 (handler)The handler function implements the logic to add tracks to a Spotify playlist by converting track IDs to URIs and calling spotifyApi.playlists.addItemsToPlaylist.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) }`, }, ], }; } },
- src/play.ts:213-221 (schema)Zod schema defining the input parameters: playlistId (string), trackIds (array of strings), position (optional nonnegative 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)The addTracksToPlaylist tool is included in the playTools array which collects play-related tools.export const playTools = [ playMusic, pausePlayback, skipToNext, skipToPrevious, createPlaylist, addTracksToPlaylist, resumePlayback, addToQueue, ];
- src/index.ts:12-14 (registration)All tools, including those from playTools containing addTracksToPlaylist, are registered to the MCP server via server.tool() in a loop.[...readTools, ...playTools, ...albumTools].forEach((tool) => { server.tool(tool.name, tool.description, tool.schema, tool.handler); });