Skip to main content
Glama
igorgarbuz
by igorgarbuz

removeTracksFromPlaylist

Delete specific songs from a Spotify playlist by providing the playlist ID and track IDs to remove unwanted tracks.

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 main handler function that executes 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 defining the input parameters: 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)
    The tool object is included in the writeTools array, which is exported and later imported into index.ts for MCP server registration.
    export const writeTools = [ addToQueue, addTracksToPlaylist, createPlaylist, removeTracksFromPlaylist, ];
  • src/index.ts:12-14 (registration)
    All tools from writeTools (including removeTracksFromPlaylist) are registered with the MCP server using server.tool().
    [...playTools, ...readTools, ...writeTools].forEach((tool) => { server.tool(tool.name, tool.description, tool.schema, tool.handler); });
  • Helper utility called within the handler to safely execute Spotify API calls, managing authentication, token refresh, and error handling.
    export async function handleSpotifyRequest<T>( action: (spotifyApi: SpotifyApi) => Promise<T>, ): Promise<T> { let config = loadSpotifyConfig(); let spotifyApi: SpotifyApi; try { // If token is expired, refresh first if ( config.accessTokenExpiresAt && config.accessTokenExpiresAt - Date.now() < 60 * 1000 ) { config = await refreshAccessToken(config); } spotifyApi = createSpotifyApi(); return await action(spotifyApi); } catch (error: any) { // If 401, try refresh once if (error?.status === 401 || /401|unauthorized/i.test(error?.message)) { config = await refreshAccessToken(config); cachedSpotifyApi = null; spotifyApi = createSpotifyApi(); return await action(spotifyApi); } // Skip JSON parsing errors as these are actually successful operations const errorMessage = error instanceof Error ? error.message : String(error); if ( errorMessage.includes('Unexpected token') || errorMessage.includes('Unexpected non-whitespace character') || errorMessage.includes('Exponent part is missing a number in JSON') ) { return undefined as T; } // Rethrow other errors throw error; } }

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