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
| Name | Required | Description | Default |
|---|---|---|---|
| playlistId | Yes | The Spotify ID of the playlist | |
| trackIds | Yes | Array of Spotify track IDs to remove |
Implementation Reference
- src/write.ts:188-236 (handler)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) }`, }, ], }; } }, };
- src/write.ts:182-187 (schema)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); });
- src/utils.ts:354-390 (helper)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; } }