create-playlist
Generate custom Spotify playlists by specifying a name and optional description. Automates playlist creation through the Spotify MCP Server, simplifying music organization and management.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | No | Description of the playlist | |
| name | Yes | Name of the playlist |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"description": {
"description": "Description of the playlist",
"type": "string"
},
"name": {
"description": "Name of the playlist",
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
Implementation Reference
- mcp/spotify-mcp.ts:309-375 (handler)The handler function for the 'create-playlist' tool. It retrieves a valid access token, fetches the current user's ID, and creates a new private playlist via the Spotify API with the provided name and optional description. Returns success message with playlist details or error content.async ({ name, description = "" }) => { try { const accessToken = await getValidAccessToken(); // Get user ID const userResponse = await fetch("https://api.spotify.com/v1/me", { headers: { Authorization: `Bearer ${accessToken}`, }, }); const userData = (await userResponse.json()) as any; const userId = userData.id; // Create playlist const response = await fetch( `https://api.spotify.com/v1/users/${userId}/playlists`, { method: "POST", headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, body: JSON.stringify({ name, description, public: false, }), } ); const data = (await response.json()) as any; if (!response.ok) { return { content: [ { type: "text", text: `Error creating playlist: ${JSON.stringify(data)}`, }, ], isError: true, }; } return { content: [ { type: "text", text: `Playlist created successfully!\nName: ${data.name}\nID: ${data.id}\nURL: ${data.external_urls.spotify}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Failed to create playlist: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }
- mcp/spotify-mcp.ts:305-308 (schema)Zod schema defining the input parameters for the create-playlist tool: required 'name' string and optional 'description' string.{ name: z.string().describe("Name of the playlist"), description: z.string().optional().describe("Description of the playlist"), },
- mcp/spotify-mcp.ts:303-376 (registration)Registration of the 'create-playlist' tool on the MCP server using server.tool(), including inline schema and handler.server.tool( "create-playlist", { name: z.string().describe("Name of the playlist"), description: z.string().optional().describe("Description of the playlist"), }, async ({ name, description = "" }) => { try { const accessToken = await getValidAccessToken(); // Get user ID const userResponse = await fetch("https://api.spotify.com/v1/me", { headers: { Authorization: `Bearer ${accessToken}`, }, }); const userData = (await userResponse.json()) as any; const userId = userData.id; // Create playlist const response = await fetch( `https://api.spotify.com/v1/users/${userId}/playlists`, { method: "POST", headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, body: JSON.stringify({ name, description, public: false, }), } ); const data = (await response.json()) as any; if (!response.ok) { return { content: [ { type: "text", text: `Error creating playlist: ${JSON.stringify(data)}`, }, ], isError: true, }; } return { content: [ { type: "text", text: `Playlist created successfully!\nName: ${data.name}\nID: ${data.id}\nURL: ${data.external_urls.spotify}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Failed to create playlist: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } } );
- mcp/spotify-mcp.ts:22-79 (helper)Helper function to get or refresh a valid Spotify access token using stored credentials and refresh token logic.async function getValidAccessToken() { if (!spotifyAuthInfo.accessToken || !spotifyAuthInfo.refreshToken) { throw new Error( "No access token available. Please set credentials first using the set-spotify-credentials tool." ); } try { // Try using current token const response = await fetch("https://api.spotify.com/v1/me", { headers: { Authorization: `Bearer ${spotifyAuthInfo.accessToken}`, }, }); // If token works, return it if (response.ok) { return spotifyAuthInfo.accessToken; } console.error("Access token expired, refreshing..."); // If token doesn't work, refresh it const refreshResponse = await fetch( "https://accounts.spotify.com/api/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: "Basic " + Buffer.from( spotifyAuthInfo.clientId + ":" + spotifyAuthInfo.clientSecret ).toString("base64"), }, body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: spotifyAuthInfo.refreshToken, }), } ); const data = (await refreshResponse.json()) as any; if (data.access_token) { console.error("Successfully refreshed access token"); spotifyAuthInfo.accessToken = data.access_token; return spotifyAuthInfo.accessToken; } throw new Error("Failed to refresh access token"); } catch (error) { throw new Error( "Error with access token: " + (error instanceof Error ? error.message : String(error)) ); } }