add_to_playlist
Enable movie playlist management by adding a specific movie to an existing playlist using the Plex MCP Server API. Specify playlist and movie keys to execute.
Instructions
Add a movie to an existing playlist.
Parameters: playlist_key: The key of the playlist. movie_key: The key of the movie to add.
Returns: A success message if the movie is added, or an error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| movie_key | Yes | ||
| playlist_key | Yes |
Implementation Reference
- src/plex_mcp/plex_mcp.py:478-521 (handler)The @mcp.tool()-decorated handler function `add_to_playlist` that implements the core logic: connects to Plex server, locates the playlist and movie by their rating keys, adds the movie to the playlist using `playlist.addItems()`, and returns success or error messages. Handles exceptions for invalid keys, not found items, etc.@mcp.tool() async def add_to_playlist(playlist_key: str, movie_key: str) -> str: """ Add a movie to an existing playlist. Parameters: playlist_key: The key of the playlist. movie_key: The key of the movie to add. Returns: A success message if the movie is added, or an error message. """ try: plex = await get_plex_server() except Exception as e: return f"ERROR: Could not connect to Plex server. {str(e)}" try: p_key = int(playlist_key) m_key = int(movie_key) # Find the playlist all_playlists = await asyncio.to_thread(plex.playlists) playlist = next((p for p in all_playlists if p.ratingKey == p_key), None) if not playlist: return f"No playlist found with key {playlist_key}." # Perform a global search for the movie movies = await asyncio.to_thread(lambda: plex.library.search(libtype="movie", ratingKey=m_key)) if not movies: return f"No movie found with key {movie_key}." movie = movies[0] # Since the search is scoped to the ratingKey, there should be at most one result # Add the movie to the playlist await asyncio.to_thread(lambda p=playlist, m=movie: p.addItems([m])) logger.info("Added movie '%s' to playlist '%s'", movie.title, playlist.title) return f"Successfully added '{movie.title}' to playlist '{playlist.title}'." except ValueError: return "ERROR: Invalid playlist or movie key. Please provide valid numbers." except Exception as e: logger.exception("Failed to add movie to playlist") return f"ERROR: Failed to add movie to playlist. {str(e)}"