Skip to main content
Glama
playlists-tool.ts3.2 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; import { SpotifyClient } from "../../spotify-client.js"; import { getUserPlaylists, createPlaylist, addTracksToPlaylist, formatPlaylists, } from "../playlists.js"; export function registerPlaylistTools(server: McpServer, spotifyClient: SpotifyClient) { // Get User Playlists Tool server.tool( "get_user_playlists", "Get user's Spotify playlists", { limit: z .number() .optional() .default(20) .describe("Maximum number of playlists to return (default: 20)"), }, async ({ limit }) => { try { const playlists = await getUserPlaylists(spotifyClient, limit); const formatted = formatPlaylists(playlists); return { content: [{ type: "text", text: formatted }], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; return { content: [{ type: "text", text: `Error getting playlists: ${errorMessage}` }], isError: true, }; } } ); // Create Playlist Tool server.tool( "create_playlist", "Create a new Spotify playlist", { name: z.string().describe("Name of the playlist"), description: z.string().optional().describe("Description of the playlist"), public: z .boolean() .optional() .default(true) .describe("Whether the playlist is public (default: true)"), }, async ({ name, description, public: isPublic }) => { try { const playlist = await createPlaylist(spotifyClient, { name, description, public: isPublic, }); return { content: [ { type: "text", text: `Created playlist: ${playlist.name}\nURI: ${playlist.uri}\nLink: ${playlist.external_urls.spotify}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; return { content: [{ type: "text", text: `Error creating playlist: ${errorMessage}` }], isError: true, }; } } ); // Add Tracks to Playlist Tool server.tool( "add_tracks_to_playlist", "Add tracks to a Spotify playlist", { playlist_id: z.string().describe("Playlist ID"), track_uris: z .array(z.string()) .describe("Array of Spotify track URIs to add"), }, async ({ playlist_id, track_uris }) => { try { await addTracksToPlaylist(spotifyClient, { playlist_id, track_uris }); return { content: [ { type: "text", text: `Successfully added ${track_uris.length} track(s) to playlist`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; return { content: [{ type: "text", text: `Error adding tracks: ${errorMessage}` }], isError: true, }; } } ); }

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/nicklaustrup/mcp-spotify'

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