add_to_playlist
Add tracks to a Spotify playlist by providing the playlist ID and track IDs. This tool enables playlist management through the Spotify MCP server.
Instructions
Add tracks to a playlist
Args:
playlist_id: Spotify playlist ID
track_ids: List of track IDs to add
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| playlist_id | Yes | ||
| track_ids | Yes |
Implementation Reference
- main.py:188-196 (handler)MCP tool handler and registration for add_to_playlist. This is the entrypoint function executed when the tool is called, delegating to the SpotifyClient.@mcp.tool() async def add_to_playlist(playlist_id: str, track_ids: list[str]) -> str: """ Add tracks to a playlist Args: playlist_id: Spotify playlist ID track_ids: List of track IDs to add """ return await client.add_to_playlist(playlist_id, track_ids)
- spotify.py:306-318 (helper)Core helper function in SpotifyClient that implements the logic: converts track IDs to Spotify URIs and adds items to the playlist using spotipy.Spotify.playlist_add_items.async def add_to_playlist(self, playlist_id: str, track_ids: list[str]) -> str: """ Add tracks to a playlist - playlist_id: Spotify playlist ID - track_ids: List of track IDs to add """ try: # Convert track IDs to full URIs track_uris = [f"spotify:track:{track_id}" for track_id in track_ids] self.sp.playlist_add_items(playlist_id, track_uris) return "Tracks added to playlist successfully" except Exception as e: return f"Error adding tracks to playlist: {str(e)}"
- main.py:5-5 (helper)Instantiation of the global SpotifyClient instance used by all tool handlers.client = SpotifyClient()
- main.py:4-4 (registration)Initialization of the FastMCP server instance where tools are registered.mcp = FastMCP("spotify")