spotify_add_tracks_to_playlist
Add tracks to an existing Spotify playlist by providing the playlist ID and track IDs. This tool enables playlist management through the MCP Spotify Server.
Instructions
Adiciona músicas a uma playlist existente
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| playlist_id | Yes | ID da playlist no Spotify | |
| track_ids | Yes | Array com os IDs das músicas para adicionar |
Implementation Reference
- src/tools/spotify-tools.ts:564-606 (handler)The handler function that executes the tool logic: adds specified tracks to a Spotify playlist using a direct POST request to the Spotify API.async addTracksToPlaylist(playlistId: string, trackIds: string[]) { try { await this.spotifyAuth.ensureValidToken(); const accessToken = await this.spotifyAuth.ensureValidToken(); // Adicionar músicas à playlist const response = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`, { method: 'POST', headers: { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ uris: trackIds.map(id => `spotify:track:${id}`) }) }); if (!response.ok) { const errorData = await response.json(); throw new Error(`Erro ao adicionar músicas: ${response.status} - ${errorData.error?.message || 'Erro desconhecido'}`); } const result = await response.json(); return { content: [ { type: 'text', text: `✅ ${trackIds.length} música(s) adicionada(s) à playlist com sucesso!\n\n**Detalhes:**\n- Playlist ID: ${playlistId}\n- Músicas adicionadas: ${trackIds.length}\n- Snapshot ID: ${result.snapshot_id}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `❌ Erro ao adicionar músicas à playlist: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:230-250 (schema)Input schema definition for the tool, specifying playlist_id (string) and track_ids (array of strings) as required parameters.{ name: 'spotify_add_tracks_to_playlist', description: 'Adiciona músicas a uma playlist existente', inputSchema: { type: 'object', properties: { playlist_id: { type: 'string', description: 'ID da playlist no Spotify', }, track_ids: { type: 'array', items: { type: 'string', }, description: 'Array com os IDs das músicas para adicionar', }, }, required: ['playlist_id', 'track_ids'], }, },
- src/index.ts:301-303 (registration)Registers the tool handler in the CallToolRequest switch statement, mapping the tool name to the SpotifyTools.addTracksToPlaylist method.case 'spotify_add_tracks_to_playlist': return await spotifyTools.addTracksToPlaylist(args.playlist_id, args.track_ids);