Skip to main content
Glama
fborello

MCP Spotify Server

by fborello

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
NameRequiredDescriptionDefault
playlist_idYesID da playlist no Spotify
track_idsYesArray com os IDs das músicas para adicionar

Implementation Reference

  • 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)}`,
            },
          ],
        };
      }
    }
  • 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);

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/fborello/MCPSpotify'

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