add_tracks_to_playlist
Add tracks to a Spotify playlist using track URIs and specify playlist ID. Insert tracks at a specific position if needed.
Instructions
Add one or more tracks to a playlist
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | The Spotify ID or URI of the playlist | |
| uris | Yes | Array of Spotify track URIs to add | |
| position | No | Optional. The position to insert the tracks (zero-based) |
Implementation Reference
- src/handlers/playlists.ts:72-86 (handler)The core handler function implementing add_tracks_to_playlist by extracting playlist ID, preparing data with URIs and optional position, and making a POST request to Spotify API endpoint /playlists/{id}/tracks.async addTracksToPlaylist(args: AddTracksToPlaylistArgs) { const playlistId = this.extractPlaylistId(args.id); const { uris, position } = args; const data = { uris, ...(position !== undefined && { position }) }; return this.api.makeRequest( `/playlists/${playlistId}/tracks`, 'POST', data ); }
- src/types/playlists.ts:25-29 (schema)TypeScript interface defining the input parameters for the addTracksToPlaylist handler: playlist ID, array of track URIs, and optional insert position.export interface AddTracksToPlaylistArgs { id: string; uris: string[]; position?: number; }
- src/index.ts:557-580 (registration)MCP tool registration in listTools response, defining the name, description, and inputSchema matching the handler args.{ name: 'add_tracks_to_playlist', description: 'Add one or more tracks to a playlist', inputSchema: { type: 'object', properties: { id: { type: 'string', description: 'The Spotify ID or URI of the playlist' }, uris: { type: 'array', items: { type: 'string' }, description: 'Array of Spotify track URIs to add' }, position: { type: 'number', description: 'Optional. The position to insert the tracks (zero-based)', minimum: 0 } }, required: ['id', 'uris'] }, },
- src/index.ts:861-867 (registration)Dispatch logic in the main CallToolRequest handler switch statement: validates required args and delegates to playlistsHandler.addTracksToPlaylistcase 'add_tracks_to_playlist': { const args = this.validateArgs<AddTracksToPlaylistArgs>(request.params.arguments, ['id', 'uris']); const result = await this.playlistsHandler.addTracksToPlaylist(args); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], }; }
- src/handlers/playlists.ts:8-10 (helper)Helper method used by addTracksToPlaylist (and others) to normalize playlist ID from Spotify URI or plain ID.private extractPlaylistId(id: string): string { return id.startsWith('spotify:playlist:') ? id.split(':')[2] : id; }