sp-mc
v0.3.1
Published
MCP server for controlling Sonic Pi via OSC
Readme
sp-mc
This connects any MCP client with Sonic Pi enabling you to create music with English.
Tell Claude : "Use mcp to create an Acid House track with 120 bpm in the key of F"
Sonic Pi MCP Server
I wanted to use mcp-sonic-pi but had version issues on my system so ported it to Node
So far only tested on a Linux system with Claude Code.
Current status
- Live OSC tools (
initialize_sonic_pi,play_music, etc.) unchanged and working as before. - New
render_miditool can read Sonic Pi.rbfiles and export.midwith per-instrument tracks (drums split per instrument). It now honorswith_bpmblocks, simple.timesrepetition, commonlive_looppatterns, and maps popular drum samples to separate tracks.
Known limitations (planned improvements):
- Complex Ruby control flow/randomness is skipped with warnings.
- Timing is best-effort for straightforward
sleep/.times; no randomness/conditionals. - Limited chord vocabulary and drum sample map; falls back to grand piano or default drum pitch when unknown.
notes.choosedraws from the lastscaleassignment (random choice);one_in(n)blocks execute probabilistically.
Features
- Direct OSC communication with Sonic Pi (no psonic dependency)
- Supports both Sonic Pi v3.x and v4.x
- Automatic port detection from Sonic Pi log files
The server provides six tools:
initialize_sonic_pi- Initialize connection to Sonic Piplay_music- Execute Sonic Pi codestop_music- Stop all playbackget_beat_pattern- Get beat patterns (blues, rock, hiphop, electronic)make_acid- Generate an Acid House style trackrender_midi- Read a Sonic Pi.rbfile and render a.midfile with one track per instrument (drums split per instrument)
Sonic Pi → MIDI rendering
Use the render_midi MCP tool to export a .mid file from an existing Sonic Pi Ruby script:
{
"name": "render_midi",
"arguments": {
"path": "/path/to/song.rb",
"bars": 8,
"output": "/tmp/song.mid"
}
}Drums are split into separate tracks (kick, snare, hats, etc.). Use loop_names to render specific live_loops or drum_split: false to merge all percussion into one track.
Known limitations (planned improvements):
- Complex Ruby control flow/randomness is skipped with warnings.
- Timing is best-effort for straightforward
sleep/.times; no randomness/conditionals. - Limited chord vocabulary and drum sample map; falls back to grand piano or default drum pitch when unknown.
Prerequisites
- Sonic Pi
- Node
Setup
The easy way is :
claude mcp add sp-mc npx 'sp-mc@latest'or
codex mcp add sp-mc npx 'sp-mc@latest'Start Sonic Pi then start Claude/Codex.
Installation
git clone https://github.com/danja/sp-mc.git
cd sp-mc
npm installConfigure MCP Connection
eg. in ~/.claude.json:
"sonic-pi": {
"type": "stdio",
"command": "node",
"args": ["/path/to/sp-mc/mcp/server.js"],
"env": {}
}Running in VS Code
There is a VS Code extension vscode-sonic-pi which provides syntax highlighting for Sonic Pi code. With Claude Code running in a VS Code terminal you can build up songs in the editor and manage them on the file system.
Port Configuration Note
The MCP server automatically detects the correct port from Sonic Pi's log files. If Sonic Pi is running on a non-standard OSC port (e.g., 4560 instead of the default 4557), the server will find and use the correct port automatically by parsing ~/.sonic-pi/log/server-output.log.
Testing
This project includes comprehensive test coverage with vitest. Tests can run against either a mock Sonic Pi server or a real Sonic Pi installation.
Run tests (mock mode by default):
npm testRun tests in watch mode:
npm run test:watchRun tests with mock Sonic Pi server:
npm run test:mockRun tests with real Sonic Pi (requires Sonic Pi to be running):
npm run test:realTest Structure:
mcp/log-parser.test.js- Tests for Sonic Pi log file parsingmcp/sonic-pi-client.test.js- Tests for OSC communication with Sonic Pimcp/server.test.js- Tests for MCP server beat patterns and validationmcp/render/render.test.js- Tests for Sonic Pi → MIDI renderingmcp/render/render-roundtrip.test.js- Round-trip timing checks (e.g., with_bpm alignment)mcp/render/utils.test.js- Note/chord/sample mapping teststest/helpers/- Mock Sonic Pi server and test utilitiestest/fixtures/- Sample log files for testing
Note: If you have Sonic Pi installed, some tests may use your real Sonic Pi logs for integration testing. To run pure unit tests, temporarily move ~/.sonic-pi/log/ or use Docker/CI environments.
Documentation
See /mcp/README.md for detailed documentation.
