Skip to main content
Glama
igorgarbuz

Spotify MCP Node Server

by igorgarbuz

removeTracksFromPlaylist

Remove specific tracks from a Spotify playlist by providing playlist and track IDs. This tool helps users manage playlist content by deleting unwanted songs.

Instructions

Remove tracks from a Spotify playlist

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
playlistIdYesThe Spotify ID of the playlist
trackIdsYesArray of Spotify track IDs to remove

Implementation Reference

  • The async handler function that implements the tool logic: validates input, constructs track URIs, calls Spotify API to remove items from playlist, and returns success/error messages.
    handler: async (args, extra: SpotifyHandlerExtra) => {
      const { playlistId, trackIds } = args;
    
      if (trackIds.length === 0) {
        return {
          content: [
            {
              type: 'text',
              text: 'Error: No track IDs provided',
            },
          ],
        };
      }
    
      try {
        const tracks = trackIds.map((id) => ({
          uri: `spotify:track:${id}`,
        }));
    
        await handleSpotifyRequest(async (spotifyApi) => {
          await spotifyApi.playlists.removeItemsFromPlaylist(playlistId, {
            tracks,
          });
        });
    
        return {
          content: [
            {
              type: 'text',
              text: `Successfully removed ${trackIds.length} track${
                trackIds.length === 1 ? '' : 's'
              } from playlist (ID: ${playlistId})`,
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: 'text',
              text: `Error removing tracks from playlist: ${
                error instanceof Error ? error.message : String(error)
              }`,
            },
          ],
        };
      }
    },
  • Zod schema for input validation: requires playlistId (string) and trackIds (array of strings).
    schema: {
      playlistId: z.string().describe('The Spotify ID of the playlist'),
      trackIds: z
        .array(z.string())
        .describe('Array of Spotify track IDs to remove'),
    },
  • src/write.ts:238-243 (registration)
    Includes the removeTracksFromPlaylist tool in the exported writeTools array, which is imported and registered elsewhere.
    export const writeTools = [
      addToQueue,
      addTracksToPlaylist,
      createPlaylist,
      removeTracksFromPlaylist,
    ];
  • src/index.ts:12-14 (registration)
    Registers all tools from playTools, readTools, and writeTools arrays with the MCP server by calling server.tool() for each.
    [...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