create_playlist
Create a new Spotify playlist with specified tracks, name, description, and privacy settings using the Spotify Model Context Protocol.
Instructions
Create a new playlist on Spotify and add tracks to it.
Args:
name: Name of the playlist
track_uris: List of Spotify track URIs to add to the playlist
description: Optional description for the playlist
public: Whether the playlist should be public (default: False)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| track_uris | Yes | ||
| description | No | ||
| public | No |
Implementation Reference
- spotify.py:34-34 (registration)Registration of the create_playlist tool using the @mcp.tool decorator@mcp.tool("create_playlist")
- spotify.py:35-60 (handler)Handler function that performs authentication/refresh and calls the SpotifyClient to create the playlist.def create_playlist(name: str, track_uris: List[str], description: str = "", public: bool = False) -> dict: """ Create a new playlist on Spotify and add tracks to it. Args: name: Name of the playlist track_uris: List of Spotify track URIs to add to the playlist description: Optional description for the playlist public: Whether the playlist should be public (default: False) """ user_id = get_current_user_id() tokens = get_user_tokens(user_id) if not tokens: raise Exception("No tokens found for user") if tokens["token_expiry"] < time.time(): tokens = spotify_client.refresh_access_token(tokens["refresh_token"]) update_access_token(user_id, tokens["access_token"], tokens.get("expires_in", 3600)) return spotify_client.create_playlist_with_tracks( access_token=tokens["access_token"], user_id=user_id, name=name, track_uris=track_uris, description=description )
- spotify_client.py:40-88 (helper)Core implementation that makes Spotify API calls to create a playlist and add tracks to it.def create_playlist_with_tracks(self, access_token: str, user_id: str, name: str, track_uris: List[str], description: str = "") -> dict: """ Creates a private playlist and adds the specified tracks to it. Args: access_token: Spotify access token user_id: Spotify user ID name: Name of the playlist track_uris: List of Spotify track URIs to add description: Optional playlist description Returns: The created playlist data if successful, None otherwise """ headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } # Create the playlist playlist_data = { "name": name, "description": description, "public": False } response = requests.post( f"{self.base_url}/users/{user_id}/playlists", headers=headers, json=playlist_data ) if response.status_code != 201: return None playlist = response.json() # Add tracks to the playlist tracks_data = { "uris": track_uris } response = requests.post( f"{self.base_url}/playlists/{playlist['id']}/tracks", headers=headers, json=tracks_data ) return playlist if response.status_code == 201 else None
- spotify.py:35-44 (schema)Input schema defined by function signature and docstring describing parameters.def create_playlist(name: str, track_uris: List[str], description: str = "", public: bool = False) -> dict: """ Create a new playlist on Spotify and add tracks to it. Args: name: Name of the playlist track_uris: List of Spotify track URIs to add to the playlist description: Optional description for the playlist public: Whether the playlist should be public (default: False) """