Skip to main content
Glama
igorgarbuz
by igorgarbuz
write.ts6.18 kB
import { handleSpotifyRequest } from './utils.js'; import { z } from 'zod'; import type { SpotifyHandlerExtra, tool } from './types.js'; // TODO: refactor repeated code into a utility function // TODO: optimize return messages to be less verbose as it fills model's context quickly const createPlaylist: tool<{ name: z.ZodString; description: z.ZodOptional<z.ZodString>; public: z.ZodOptional<z.ZodBoolean>; }> = { name: 'createPlaylist', description: 'Create a new playlist on Spotify', schema: { name: z.string().describe('The name of the playlist'), description: z .string() .optional() .describe('The description of the playlist'), public: z .boolean() .optional() .describe('Whether the playlist should be public'), }, handler: async (args, extra: SpotifyHandlerExtra) => { const { name, description, public: isPublic = false } = args; const result = await handleSpotifyRequest(async (spotifyApi) => { const me = await spotifyApi.currentUser.profile(); return await spotifyApi.playlists.createPlaylist(me.id, { name, description, public: isPublic, }); }); return { content: [ { type: 'text', text: `Successfully created playlist "${name}"\nPlaylist ID: ${result.id}`, }, ], }; }, }; const addTracksToPlaylist: tool<{ playlistId: z.ZodString; trackIds: z.ZodArray<z.ZodString>; position: z.ZodOptional<z.ZodNumber>; }> = { name: 'addTracksToPlaylist', description: 'Add tracks to a Spotify playlist', schema: { playlistId: z.string().describe('The Spotify ID of the playlist'), trackIds: z.array(z.string()).describe('Array of Spotify track IDs to add'), position: z .number() .nonnegative() .optional() .describe('Position to insert the tracks (0-based index)'), }, handler: async (args, extra: SpotifyHandlerExtra) => { const { playlistId, trackIds, position } = args; if (trackIds.length === 0) { return { content: [ { type: 'text', text: 'Error: No track IDs provided', }, ], }; } try { const trackUris = trackIds.map((id) => `spotify:track:${id}`); await handleSpotifyRequest(async (spotifyApi) => { await spotifyApi.playlists.addItemsToPlaylist( playlistId, trackUris, position, ); }); return { content: [ { type: 'text', text: `Successfully added ${trackIds.length} track${ trackIds.length === 1 ? '' : 's' } to playlist (ID: ${playlistId})`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error adding tracks to playlist: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }, }; const addToQueue: tool<{ uri: z.ZodOptional<z.ZodString>; type: z.ZodOptional<z.ZodEnum<['track', 'album', 'artist', 'playlist']>>; id: z.ZodOptional<z.ZodString>; deviceId: z.ZodOptional<z.ZodString>; }> = { name: 'addToQueue', description: 'Adds a track, album, artist or playlist to the playback queue', schema: { uri: z .string() .optional() .describe('The Spotify URI to play (overrides type and id)'), type: z .enum(['track', 'album', 'artist', 'playlist']) .optional() .describe('The type of item to play'), id: z.string().optional().describe('The Spotify ID of the item to play'), deviceId: z .string() .optional() .describe('The Spotify device ID to add the track to'), }, handler: async (args) => { const { uri, type, id, deviceId } = args; let spotifyUri = uri; if (!spotifyUri && type && id) { spotifyUri = `spotify:${type}:${id}`; } if (!spotifyUri) { return { content: [ { type: 'text', text: 'Error: Must provide either a URI or both a type and ID', isError: true, }, ], }; } await handleSpotifyRequest(async (spotifyApi) => { await spotifyApi.player.addItemToPlaybackQueue( spotifyUri, deviceId || '', ); }); return { content: [ { type: 'text', text: `Added item ${spotifyUri} to queue`, }, ], }; }, }; const removeTracksFromPlaylist: tool<{ playlistId: z.ZodString; trackIds: z.ZodArray<z.ZodString>; }> = { name: 'removeTracksFromPlaylist', description: 'Remove tracks from a Spotify playlist', schema: { playlistId: z.string().describe('The Spotify ID of the playlist'), trackIds: z .array(z.string()) .describe('Array of Spotify track IDs to remove'), }, 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) }`, }, ], }; } }, }; export const writeTools = [ addToQueue, addTracksToPlaylist, createPlaylist, removeTracksFromPlaylist, ];

Implementation Reference

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