sonos-s1-mcp
by unrealJune
README.md
# sonos-s1-mcp
A self-hosted remote MCP connector for **Sonos S1** systems. The official Sonos connector works through the Sonos cloud, and that cloud only supports S2. This server talks to your speakers directly over the local UPnP API (via [SoCo](https://github.com/SoCo/SoCo)), so it has to run on the same LAN as your speakers.
Tool names follow the official connector's where possible (`get_households_and_groups_and_players`, `get_now_playing`, `adjust_group_volume`, `add_players_to_group`, …). There are 40 tools in total:
| Area | Tools |
|---|---|
| System | `get_households_and_groups_and_players`, `get_now_playing` |
| Playback | `play`, `pause`, `pause_all`, `skip_to_next_track`, `skip_to_previous_track`, `seek`, `get_play_modes`, `set_play_modes` |
| Volume | `get/set/adjust_player_volume`, `set_player_mute`, `get/set/adjust_group_volume`, `set_group_mute` |
| Grouping | `add_players_to_group`, `remove_players_from_group`, `group_all_players` |
| Library | `get_favorites`, `play_favorite`, `get_playlists`, `play_playlist`, `get_queue`, `play_from_queue`, `remove_from_queue`, `clear_queue` |
| Spotify | `search_spotify`, `play_spotify` |
| Home theater / sound | `switch_to_tv`, `switch_to_line_in`, `get/set_night_sound_and_speech_enhancement`, `get_eq`, `set_eq` |
| Misc | `get_sleep_timer`, `set_sleep_timer`, `list_alarms` |
Speakers are addressed by room name (case-insensitive; a partial match works if only one room matches). Group actions accept any room in the group.
## Deploy
### Image
Multi-arch images (amd64, arm64) are published to GHCR:
- `ghcr.io/unrealjune/sonos-s1-mcp:edge` is rebuilt from `main` on every push and nightly.
- `ghcr.io/unrealjune/sonos-s1-mcp:X.Y.Z`, `:X.Y`, and `:latest` are built on every automatic release (see Development).
### Helm (Kubernetes)
The chart is published as an OCI artifact on every release, with chart version = app version = image tag:
```bash
helm install sonos oci://ghcr.io/unrealjune/charts/sonos-s1-mcp --version 0.2.0 \
--set hostNetwork=true \
--set publicUrl=https://sonos.example.com \
--set oidc.issuer=https://auth.example.com/application/o/sonos-s1/ \
--set oidc.clientId=<client-id> \
--set 'oidc.allowedGroups={authentik Admins}' \
--set existingSecret=sonos-s1-mcp # holds OIDC_CLIENT_SECRET (+ MCP_AUTH_TOKEN, Spotify keys)
```
Discovery needs the pod on the speakers' network. Use `hostNetwork=true`, or set `sonos.hosts` to a speaker IP the pod can route to. See [`values.yaml`](charts/sonos-s1-mcp/values.yaml) for ingress and other options.
### Docker Compose
```bash
cp .env.example .env # fill it in (see below)
docker compose up -d --build
curl localhost:8080/healthz
```
Compose uses `network_mode: host` so that SSDP discovery can find the speakers. If you set `SONOS_HOSTS` to a speaker IP, you can drop host networking and publish port 8080 instead. Giving the speakers DHCP reservations is a good idea either way.
## Expose it over HTTPS
claude.ai calls custom connectors from Anthropic's servers, so the endpoint must be reachable from the public internet over TLS. Any of these work: a Cloudflare Tunnel, Tailscale Funnel, or your existing reverse proxy pointing `https://sonos.example.com` → `http://<host>:8080`.
**Don't put Authentik forward-auth or an outpost in front of `/mcp`.** The server does its own OAuth bearer validation. A login redirect in front of it breaks the MCP client.
## Auth: OAuth with login through Authentik
claude.ai connects with just the URL. To make that work, this server is its own OAuth 2.1 authorization server: it supports dynamic client registration, PKCE, and refresh-token rotation, and keeps its state in SQLite under `DATA_DIR`. Login is delegated to your OIDC provider.
```
claude.ai ──register/authorize──▶ sonos-s1-mcp ──login──▶ Authentik
│ group check + consent page
claude.ai ◀──────code, tokens──────────┘
```
Authentik sees a single fixed client, which is this server. MCP clients never talk to Authentik directly.
1. **In Authentik**, create an OAuth2/OpenID provider and an application:
- Client type: Confidential
- Redirect URI (strict): `https://sonos.example.com/oauth/callback`
- Scopes: `openid`, `profile`, `email`. The profile scope carries the `groups` claim.
- Bind a group policy to the application so only your household can log in.
2. **Configure the server**: set `PUBLIC_URL`, `OIDC_ISSUER`, `OIDC_CLIENT_ID`, `OIDC_CLIENT_SECRET`, and `OIDC_ALLOWED_GROUPS` (checked again here, as a second gate). Give it a persistent `DATA_DIR`.
3. **In claude.ai**, go to Settings → Connectors → Add custom connector and enter `https://sonos.example.com/mcp`. Nothing else is needed. Claude registers itself, sends you to Authentik, and you approve the connection on the consent page.
Safeguards:
- Registration only accepts redirect URIs on `ALLOWED_REDIRECT_HOSTS`. The default is claude.ai, claude.com, and localhost.
- The consent page shows where tokens will go and can't be framed.
- Codes are single-use. Refresh tokens rotate, and the old grant dies with them.
To sign everything out, delete `oauth.db`.
For **Claude Code** or other clients, `MCP_AUTH_TOKEN` also works as a static bearer token:
```bash
claude mcp add --transport http sonos-s1 https://sonos.example.com/mcp --header "Authorization: Bearer $MCP_AUTH_TOKEN"
```
If `OIDC_ISSUER` is unset, the server runs in static-token-only mode and accepts `Authorization: Bearer …` or `?token=…`.
## Spotify
Playback goes through the Spotify account that's **linked in your Sonos S1 app**. `play_spotify` accepts `spotify:` URIs and `open.spotify.com` links for tracks, albums, playlists, and artists (artists play their top tracks). It can replace the queue, play next, or add to the end. Spotify items saved as Sonos Favorites also work through `play_favorite`.
`search_spotify` uses the Spotify Web API through the client-credentials flow, so it needs only an app key and no Spotify login:
1. Sign in at <https://developer.spotify.com/dashboard> with any Spotify account and click **Create app**.
2. Enter any name and description. For the redirect URI, enter any valid URL such as `https://localhost/callback`; it's never used. Under APIs, tick **Web API**, accept the terms, and save.
3. Open the app's **Settings** and copy the **Client ID**. Then click **View client secret** and copy that too.
4. Set `SPOTIFY_CLIENT_ID` and `SPOTIFY_CLIENT_SECRET`. With the Helm chart, put them in a Secret referenced from `extraEnvFrom`.
Without those keys, you can still paste Spotify links into `play_spotify`.
## Development
- `scripts/smoke.sh <image>` checks health, 401s, and tool listing, then runs the whole OAuth flow (`tests/oauth_e2e.py`) against a fake OIDC provider (`tests/fake_oidc.py`). That covers registration, login, the group gate, consent, PKCE, refresh rotation, and revocation. CI runs it on every PR.
- Dependabot opens weekly PRs for pip, the Docker base image, and GitHub Actions. Every update (patch, minor, and major) gets auto-merge, and it merges only after the required `ci-ok` check passes. An hourly sweep re-arms any PR that GitHub dropped auto-merge on.
- Auto-merging GitHub Actions bumps needs an `AUTOMERGE_TOKEN` secret, because the built-in token can't merge workflow-file changes. Create a fine-grained PAT scoped to this repo with **Contents**, **Pull requests**, and **Workflows** set to read/write. Add it as both an Actions secret and a Dependabot secret.
- **Releases are automatic.** Every push to `main` that changes something shippable gets the next version from [`scripts/next-version.sh`](scripts/next-version.sh): `feat:` bumps the minor version, `type!:` or `BREAKING CHANGE` bumps the major version (the minor while on 0.x), and anything else (fixes, Dependabot bumps) bumps the patch. The run tags `vX.Y.Z`, creates a GitHub release, and publishes the image and chart. Changes only to CI, docs, or scripts don't release. A nightly run picks up merges that didn't trigger `push`.
## Caveats
- Verified against a live S1 household (stereo pair, sub, Play:3, Connect) and Authentik. The OAuth login itself has only been exercised up to the metadata and JWKS, not a full claude.ai login.
- Spotify share-link playback depends on SoCo matching your linked Spotify account. If adding to the queue fails with a UPnP error 800, check that Spotify is linked in the S1 app and try updating SoCo.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues