previous
Skip to the previous track on a Sonos device. Specify the device name or use the current one. Returns updated device state, including track details, volume, and playback status.
Instructions
Skip to the previous track on a Sonos device.
Args: name: The name of the device to skip the track on. If None, uses the current device.
Returns: Dict[str, Any]: The device's state after skipping to the previous track, including name, volume, state, and track info.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "previous"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | No |
Implementation Reference
- server.py:199-211 (handler)The 'previous' tool handler function, decorated with @mcp.tool() for registration. It retrieves the specified Sonos device, calls device.previous() to skip to the previous track, and returns the updated device state using get_info_from.
@mcp.tool() def previous(name: Optional[str] = None) -> Dict[str, Any]: """Skip to the previous track on a Sonos device. Args: name: The name of the device to skip the track on. If None, uses the current device. Returns: Dict[str, Any]: The device's state after skipping to the previous track, including name, volume, state, and track info. """ device = get_device(name) device.previous() return get_info_from(device) - server.py:64-87 (helper)Helper function used by the 'previous' tool to format and return the device's state information after executing the previous track skip.
def get_info_from(device: soco.SoCo) -> Dict[str, Any]: """Retrieve detailed information from a Sonos device. Args: device: The Sonos device to retrieve information from. Returns: Dict[str, Any]: A dictionary containing the device's name, volume, state, and current track information. """ track_info = device.get_current_track_info() return { "name": device.player_name, "volume": device.volume, "state": device.get_current_transport_info()["current_transport_state"], "track": { "title": track_info.get("title"), "artist": track_info.get("artist"), "album": track_info.get("album"), "position": track_info.get("position"), "duration": track_info.get("duration"), "playlist_position": track_info.get("playlist_position"), "album_art": track_info.get("album_art") } } - server.py:31-62 (helper)Helper function used by the 'previous' tool to retrieve the specified Sonos device object.
def get_device(name: Optional[str] = None) -> soco.SoCo: """Retrieve a Sonos device by name or return the current device. Args: name: The name of the device to retrieve. If None, returns the current device. Returns: soco.SoCo: The Sonos device object. Raises: ValueError: If the specified device name is not found. """ global device if not name and device: return device devices = get_devices() if not name: device = devices[list(devices.keys())[0]] return device if name in devices: device = devices[name] return device for key in devices: if key.lower() == name.lower(): device = devices[key] return device raise ValueError(f"Device {name} not found")