
sonic-pi-mcp
io.github.ashishra0/sonic-pi-mcp
Run, change, stop, debug and record live Sonic Pi music from AI agents, with confirmed execution.
Documentation
sonic-pi-mcp
A reliable MCP server for Sonic Pi, written in Rust. It lets AI agents such as Claude Code, Codex, Cursor and VS Code compose, run, change, debug, stop and record live music in a locally running Sonic Pi.
- Independent lifecycle. The server starts, initialises and answers with Sonic Pi closed. It finds Sonic Pi when it launches and reconnects after restarts without replaying anything.
- Confirmed execution. Each run reports what Sonic Pi actually did:
completed,running,error(with class, message and your line number) orunconfirmed, which is never retried automatically. Sending a packet is not counted as success. - Stop that works when it matters.
sonic_pi_stopis sent even when runaway code makes Sonic Pi too busy to answer. - Long-form recording. Lossless takes of up to 4 hours, recorded by Sonic Pi's own audio engine, with a hard time limit, crash recovery and provenance.
- Actionable diagnostics.
doctorwalks through installation, process, credentials, handshake and audio engine health, and gives a fix for each failure. - Single native binary. No Python, Node.js or other runtime is needed. Only Sonic Pi itself is required.
mcp-name: io.github.ashishra0/sonic-pi-mcp
Requirements
- Sonic Pi 5.x, installed and (for anything that plays music) running.
- Verified on macOS (Apple Silicon) with Sonic Pi 5.0.0, including a 60-minute recording soak. Linux and Windows builds are tested in CI against a simulated Sonic Pi but have not yet been verified with the real app. Reports are very welcome (see CONTRIBUTING.md). Details: docs/COMPATIBILITY.md.
Install
cargo install sonic-pi-mcp --locked # from crates.io (Rust 1.89+)
sonic-pi-mcp doctor # check everything (start Sonic Pi first)
Prebuilt binaries for macOS (Apple Silicon, Intel), Linux (x86_64, aarch64) and Windows (x86_64) are
attached to each GitHub release, with SHA-256
checksums. From a checkout: cargo install --path . --locked.
Connect your MCP client
The server speaks MCP over stdio: the command is sonic-pi-mcp with the single argument serve.
If the client can't find it on its PATH, use the absolute path (e.g. ~/.cargo/bin/sonic-pi-mcp).
Claude Code
claude mcp add --scope user sonic-pi -- sonic-pi-mcp serve
Codex
codex mcp add sonic-pi -- sonic-pi-mcp serve
Claude Desktop (claude_desktop_config.json) and Cursor (~/.cursor/mcp.json)
{ "mcpServers": { "sonic-pi": { "command": "sonic-pi-mcp", "args": ["serve"] } } }
VS Code (.vscode/mcp.json)
{ "servers": { "sonic-pi": { "type": "stdio", "command": "sonic-pi-mcp", "args": ["serve"] } } }
Tools
30 tools. Each has a title and honest hints (read-only, destructive, idempotent; all closed-world),
and every input schema is plain JSON Schema without $ref or nullable type arrays, so it works in
strict clients too.
| Tool | What it does |
|---|---|
sonic_pi_status | Connection state (ready, degraded, unavailable, …), version, audio engine health, last error |
sonic_pi_discover | Where Sonic Pi's port and token were found (the token is never shown) |
sonic_pi_run | Run code and report completed / running / error / unconfirmed |
sonic_pi_stop | Stop all runs and live_loops. confirmed is true once Sonic Pi has answered after the stop |
sonic_pi_validate | Size limits, policy guard rail, Ruby syntax check using Sonic Pi's bundled Ruby, unknown names, loud-amp warnings |
sonic_pi_logs | Recent runtime errors, including errors raised later inside live_loops, filterable by request_id |
sonic_pi_reconnect | Force rediscovery and a new handshake |
sonic_pi_doctor | Step-by-step diagnosis with a fix for each failure |
Music knowledge. These tools read the installed Sonic Pi, so they always match its version. Sonic Pi must be installed but does not need to be running:
| Tool | What it does |
|---|---|
list_catalog | kind: synths, effects, samples (optionally one group: drum, bass, loop, ambi, …), scales or chords |
get_parameters | Every option of a synth or effect: default, valid range, slidable, description |
get_documentation | Official docs for a language function (live_loop, sync, …) or any synth, FX or sample |
get_examples | Sonic Pi's own bundled example pieces |
Unknown names come back as NOT_FOUND with "did you mean" suggestions. sonic_pi_validate also uses
this data: it flags unknown synth, FX, sample, scale and chord names, and built-in function names used
as variables (scale = …), which Sonic Pi itself refuses to run.
Compositions (stable, not being extended): the model is the source of truth; Sonic Pi code is derived from it.
| Tool | What it does |
|---|---|
composition_create / composition_get | New piece (tempo, key, bar length) / inspect it, optionally with the generated code |
track_add / track_remove | One live_loop per track; bodies may use root and mode (the key) |
track_update | Body, synth, volume, mute and solo in one undoable step; muted tracks keep running silently so they stay in time |
effect_apply | Add or remove an effect on one track |
tempo_set | Tempo (and optionally key) through Sonic Pi's shared state, with no loop redefinition |
section_transition | Arrangement: define sections as sets of audible tracks and switch between them |
composition_play / composition_stop | Start, resync, or stop only this piece |
composition_undo / composition_redo / composition_save / composition_load | Revision history and JSON persistence |
While a piece is playing, each edit sends only the loops that changed. Sonic Pi hot-swaps them at their next iteration and the other tracks keep playing. Verified on the real runtime: no gap in the kick across a swap, and the off-beat stays within 16 ms. If Sonic Pi rejects an edit, the edit is rolled back, so the model and the music never disagree.
Recording (long-form, up to 4 h):
| Tool | What it does |
|---|---|
recording_start | Starts capturing Sonic Pi's main output to a 24-bit/48 kHz WAV and returns immediately. Sonic Pi's audio engine writes the file directly. max_seconds is a hard limit (also a timer for previews). Refused if there isn't enough free disk (~1 GB/hour) |
recording_status | Seconds and bytes recorded so far, runtime errors and code sent since the start; or the final result |
recording_stop | Finalises the WAV. Returns duration, format, level checks (peak/RMS/clipping/silence) and provenance (Sonic Pi version, code hashes of what was sent while recording) |
audio_analyze | Detailed signal measurements of a finished recording (frozen feature: measurements, not a verdict on the music) |
A recording keeps going when playback stops (sonic_pi_stop does not end it). If Sonic Pi quits or
its audio engine crashes mid-take, the recording is marked interrupted, and the audio captured so far
is kept and the WAV header repaired. When the server exits it stops its recording; if it crashes, the next server process finds and
closes the orphaned recording.
Resources: sonic-pi://guide/workflow, sonic-pi://reference/{synths,effects,samples,scales-and-chords}
and sonic-pi://examples/<category>/<name>.
Every result has structured content. A failure comes back as an isError result with a stable
error.code, such as SONIC_PI_NOT_RUNNING, STALE_CREDENTIALS, RUBY_SYNTAX_ERROR or
POLICY_VIOLATION. See src/error.rs for the full list.
Example sonic_pi_run results
{"status":"completed","confirmed":true,"request_id":"sonic-pi-mcp-4399-3","elapsed_ms":481,
"message":"Sonic Pi evaluated the code. Any live_loops are now playing; ..."}
{"status":"error","confirmed":true,"request_id":"sonic-pi-mcp-5111-5","elapsed_ms":152,
"error":{"code":"RUBY_SYNTAX_ERROR","line":2,"message":"Syntax Error Sonic Pi couldn't read your code",
"details":["SyntaxError: unexpected local variable or method, expecting end-of-input",
"source: play 62 amp: 0.3 do"]}}
(Both are real outputs from Sonic Pi 5.0.)
Example prompts for Claude Code
- "Check that Sonic Pi is connected, then play a short C major arpeggio on the piano."
- "Start a 120 BPM deep house groove with separate live_loops for kick, hats, bass and chords."
- "Make the bass line darker and add a filter sweep, but don't stop the drums."
- "Compose 2 minutes of slowly evolving ambient focus music with a generative melody."
- "Something stopped playing. Check the Sonic Pi logs and fix the error."
- "Stop everything."
More in examples/.
CLI
sonic-pi-mcp serve # MCP over stdio (what clients launch)
sonic-pi-mcp doctor [--json] # exit code 1 if any check fails
sonic-pi-mcp status [--json]
sonic-pi-mcp discover [--json]
sonic-pi-mcp version [--json]
| Flag | Env | Default |
|---|---|---|
--sonic-pi-home | SONIC_PI_HOME | ~/.sonic-pi |
--sonic-pi-app | SONIC_PI_APP | /Applications/Sonic Pi.app (macOS) |
--spider-port + --token | SONIC_PI_SPIDER_PORT, SONIC_PI_TOKEN | auto-discovered |
--no-process-scan | off (Unix scans ps) | |
--ack-timeout-ms | SONIC_PI_MCP_ACK_TIMEOUT_MS | 3000 |
--max-code-bytes | SONIC_PI_MCP_MAX_CODE_BYTES | 262144 (max 4 MiB; code above ~55 KB is sent via a file) |
--allow-unrestricted-ruby | SONIC_PI_MCP_ALLOW_UNRESTRICTED_RUBY | off |
--data-dir | SONIC_PI_MCP_DATA_DIR | platform data dir (compositions, recordings) |
--log-level | SONIC_PI_MCP_LOG | warn (stderr only) |
Invalid configuration causes the server to exit with code 2 and an error that starts CONFIG_INVALID.
Security
Sonic Pi code is arbitrary Ruby running with your user's privileges, and Sonic Pi has no sandbox.
This server blocks obvious host-access APIs by default (system, backticks, File, require,
eval, sockets, …). This is a guard rail to catch an agent's mistakes. It is not a sandbox,
and a determined author can get around it. The real safety boundary is your MCP client's approval
of each tool call. Read docs/SECURITY.md before you enable auto-approve or
--allow-unrestricted-ruby.
Documentation
Research · Architecture · Decisions · Compatibility · Testing · Troubleshooting · Security · Reliability evidence
Contributing
See CONTRIBUTING.md. Security issues: docs/SECURITY.md.
License
Licensed under either of Apache License 2.0 or MIT license, at your option.
sonic-pi-mcpcargo install sonic-pi-mcp