itunes_search
Search Apple Music library to find tracks matching a specified query. Retrieve results as a list formatted as "Track Name - Artist" for easy identification and selection.
Instructions
Search the Music library for tracks whose names contain the given query. Returns a list of tracks formatted as "Track Name - Artist".
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Implementation Reference
- mcp_applemusic.py:45-61 (handler)The handler function for the 'itunes_search' tool. It uses AppleScript executed via run_applescript to search the Music library for tracks matching the query and returns a formatted list.@mcp.tool() def itunes_search(query: str) -> str: """ Search the Music library for tracks whose names contain the given query. Returns a list of tracks formatted as "Track Name - Artist". """ script = f""" tell application "Music" set trackList to every track of playlist "Library" whose name contains "{query}" set output to "" repeat with t in trackList set output to output & (name of t) & " - " & (artist of t) & linefeed end repeat return output end tell """ return run_applescript(script)
- mcp_applemusic.py:5-10 (helper)Supporting helper function used by itunes_search and other tools to execute AppleScript commands.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:45-45 (registration)The @mcp.tool() decorator registers the itunes_search function as an MCP tool.@mcp.tool()