itunes_create_playlist
Create and populate a new Apple Music playlist by specifying a name and listing exact track names. Returns confirmation with the number of tracks added.
Instructions
Create a new playlist with the given name and add tracks to it. 'songs' should be a comma-separated list of exact track names. Returns a confirmation message including the number of tracks added.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| songs | Yes |
Implementation Reference
- mcp_applemusic.py:80-104 (handler)The main handler function for the 'itunes_create_playlist' tool. It processes a list of song names, constructs an AppleScript to create a new playlist in Music.app, adds matching tracks by duplicating them, and returns a confirmation message.@mcp.tool() def itunes_create_playlist(name: str, songs: str) -> str: """ Create a new playlist with the given name and add tracks to it. 'songs' should be a comma-separated list of exact track names. Returns a confirmation message including the number of tracks added. """ # Split the songs string into a list. song_list = [s.strip() for s in songs.split(",") if s.strip()] if not song_list: return "No songs provided." # Build a condition string that matches any one of the song names. # Example: 'name is "Song1" or name is "Song2"' conditions = " or ".join([f'name is "{s}"' for s in song_list]) script = f""" tell application "Music" set newPlaylist to make new user playlist with properties {{name:"{name}"}} set matchingTracks to every track of playlist "Library" whose ({conditions}) repeat with t in matchingTracks duplicate t to newPlaylist end repeat return "Playlist \\"{name}\\" created with " & (count of tracks of newPlaylist) & " tracks." end tell """ return run_applescript(script)
- mcp_applemusic.py:5-10 (helper)Utility function used by the tool to execute AppleScript commands against the Music application.def run_applescript(script: str) -> str: """Execute an AppleScript command via osascript and return its output.""" result = subprocess.run(["osascript", "-e", script], capture_output=True, text=True) if result.returncode != 0: return f"Error: {result.stderr.strip()}" return result.stdout.strip()
- mcp_applemusic.py:81-86 (schema)Input schema and documentation for the tool, specifying parameters 'name' (str) for playlist name and 'songs' (str) as comma-separated track names.def itunes_create_playlist(name: str, songs: str) -> str: """ Create a new playlist with the given name and add tracks to it. 'songs' should be a comma-separated list of exact track names. Returns a confirmation message including the number of tracks added. """
- mcp_applemusic.py:80-80 (registration)The @mcp.tool() decorator registers the itunes_create_playlist function as an MCP tool.@mcp.tool()