itunes_play_song
Play a specific song in Apple Music by searching for its exact name. Use this tool to control playback on macOS through AppleScript commands.
Instructions
Play the first track whose name exactly matches the given song name. Returns a confirmation message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| song | Yes |
Implementation Reference
- mcp_applemusic.py:64-77 (handler)The handler function decorated with @mcp.tool(), implementing the logic to play a specific song from the Music (iTunes) library using AppleScript. It searches for the first track matching the song name exactly and plays it, returning a confirmation message with artist.@mcp.tool() def itunes_play_song(song: str) -> str: """ Play the first track whose name exactly matches the given song name. Returns a confirmation message. """ script = f""" tell application "Music" set theTrack to first track of playlist "Library" whose name is "{song}" play theTrack return "Now playing: " & (name of theTrack) & " by " & (artist of theTrack) end tell """ return run_applescript(script)
- mcp_applemusic.py:5-10 (helper)Helper function used by itunes_play_song and other tools to execute AppleScript commands via subprocess.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()