A Model Context Protocol server that allows AI assistants like Claude and Cursor to create music and control Sonic Pi programmatically through OSC messages.
A Model Context Protocol (MCP) server that allows AI assistants to interact with Sonic Pi through OSC messages. This enables AI tools like Claude and Cursor to create music and control Sonic Pi programmatically.
Before using the MCP server, you need to add the following code to your Sonic Pi buffer. This code handles the OSC messages sent by the server:
# Required Sonic Pi configuration # Add this to a buffer in Sonic Pi and run it live_loop :code_runner do use_real_time code = sync "/osc*/run-code" # Since we receive the code as a string, we can use eval to execute it # The code comes as the first element of the message begin eval(code[0].to_s) rescue Exception => e puts "Error executing code: #{e.message}" end end
Make sure this code is running in Sonic Pi before using the MCP server.
Add to ~/.cursor/mcpServers.json
:
{ "mcpServers": { "sonic_pi_mcp": { "name": "Sonic Pi MCP", "command": "bunx", "args": ["sonic-pi-mcp"], "transport": { "type": "stdio" } } } }
Add to ~/Library/Application Support/Claude/claude_desktop_config.json
:
{ "mcpServers": { "sonic_pi_mcp": { "command": "bunx", "args": ["sonic-pi-mcp"], } } }
Plays a single note with customizable parameters.
Parameters:
note
(required): MIDI note number (0-127)synth
(optional): Synth to use (e.g., ":saw", ":beep", ":prophet")sustain
(optional): Note duration in seconds (default: 1)cutoff
(optional): Filter cutoff frequency (default: 100)Example:
// Play middle C with saw wave synth { "name": "play_note", "parameters": { "note": 60, "synth": ":saw", "sustain": 0.5, "cutoff": 80 } }
Executes arbitrary Sonic Pi code.
Parameters:
code
(required): Sonic Pi code to executeExample:
{ "name": "run_code", "parameters": { "code": "use_synth :prophet play_pattern_timed [60, 64, 67], [0.5]" } }
Here are some example interactions using the MCP tools:
// Play a C major arpeggio { "code": ` use_synth :piano play_pattern_timed [60, 64, 67, 72], [0.25], release: 0.1 ` }
// Create a rhythmic pattern { "code": ` live_loop :rhythm do use_synth :tb303 play choose(chord(:C3, :minor)), release: 0.2, cutoff: rrand(60, 120) sleep 0.25 end ` }
No Sound
Connection Errors
Code Execution Errors
# Clone the repository git clone https://github.com/abhishekjairath/sonic-pi-mcp.git cd sonic-pi-mcp # Install Bun if you haven't already curl -fsSL https://bun.sh/install | bash # Install dependencies bun install # Start Sonic Pi and run the OSC handler code (see Sonic Pi Configuration section) # Start the server in development mode bun run dev
npm install -g @modelcontextprotocol/inspector mcp-inspector
Open your browser and navigate to http://localhost:3000
In the MCP Inspector UI, configure the connection:
bun
run src/server.ts
/path/to/your/sonic-pi-mcp
(use your actual project path)Test the play_note
tool:
{ "name": "play_note", "parameters": { "note": 60, "synth": ":beep", "sustain": 0.5 } }
run_code
tool:{ "name": "run_code", "parameters": { "code": "use_synth :prophet play_pattern_timed scale(:c4, :major), [0.25]" } }
Bun Installation Issues
bun --version
to verify the installationMCP Inspector Connection Issues
bun run dev
)OSC Communication Issues
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the MIT License - see the LICENSE file for details.
Discover shared experiences
Shared threads will appear here, showcasing real-world applications and insights from the community. Check back soon for updates!