这是一个模型上下文协议(MCP)服务器,允许AI助手通过OSC消息与Sonic Pi进行交互。这使得像Claude和Cursor这样的AI工具能够创建音乐并编程控制Sonic Pi。
在使用MCP服务器之前,需要在Sonic Pi缓冲区中添加以下代码。这段代码处理由服务器发送的OSC消息:
# 必要的Sonic Pi配置
# 将此代码添加到Sonic Pi的一个缓冲区中并运行它
live_loop :code_runner do
use_real_time
code = sync "/osc*/run-code"
# 因为我们接收到的代码是一个字符串,我们可以使用eval来执行它
# 代码作为消息的第一个元素传递
begin
eval(code[0].to_s)
rescue Exception => e
puts "执行代码时出错:#{e.message}"
end
end
确保在使用MCP服务器之前,这段代码正在Sonic Pi中运行。
在~/.cursor/mcpServers.json中添加:
{
"mcpServers": {
"sonic_pi_mcp": {
"name": "Sonic Pi MCP",
"command": "bunx",
"args": ["sonic-pi-mcp"],
"transport": {
"type": "stdio"
}
}
}
}
在~/Library/Application Support/Claude/claude_desktop_config.json中添加:
{
"mcpServers": {
"sonic_pi_mcp": {
"command": "bunx",
"args": ["sonic-pi-mcp"],
}
}
}
播放带有可定制参数的单个音符。
参数:
note(必需):MIDI音符编号(0-127)synth(可选):使用的合成器(例如,:saw, :beep, :prophet)sustain(可选):音符持续时间(秒,默认值:1)cutoff(可选):滤波器截止频率(默认值:100)示例:
// 使用锯齿波合成器播放中音C
{
"name": "play_note",
"parameters": {
"note": 60,
"synth": ":saw",
"sustain": 0.5,
"cutoff": 80
}
}
执行任意的Sonic Pi代码。
参数:
code(必需):要执行的Sonic Pi代码示例:
{
"name": "run_code",
"parameters": {
"code": "use_synth :prophet\nplay_pattern_timed [60, 64, 67], [0.5]"
}
}
这里是一些使用MCP工具的示例交互:
// 播放C大调琶音
{
"code": `
use_synth :piano
play_pattern_timed [60, 64, 67, 72], [0.25], release: 0.1
`
}
// 创建节奏模式
{
"code": `
live_loop :rhythm do
use_synth :tb303
play choose(chord(:C3, :minor)), release: 0.2, cutoff: rrand(60, 120)
sleep 0.25
end
`
}
没有声音
连接错误
代码执行错误
# 克隆仓库
git clone https://github.com/abhishekjairath/sonic-pi-mcp.git
cd sonic-pi-mcp
# 如果尚未安装,请安装Bun
curl -fsSL https://bun.sh/install | bash
# 安装依赖项
bun install
# 启动Sonic Pi并运行OSC处理器代码(参见Sonic Pi配置部分)
# 在开发模式下启动服务器
bun run dev
npm install -g @modelcontextprotocol/inspector
mcp-inspector
打开浏览器并导航至http://localhost:3000
在MCP Inspector UI中配置连接:
bunrun src/server.ts/path/to/your/sonic-pi-mcp(使用实际项目路径)测试play_note工具:
{
"name": "play_note",
"parameters": {
"note": 60,
"synth": ":beep",
"sustain": 0.5
}
}
run_code工具:{
"name": "run_code",
"parameters": {
"code": "use_synth :prophet\nplay_pattern_timed scale(:c4, :major), [0.25]"
}
}
Bun安装问题
bun --version以验证安装MCP Inspector连接问题
bun run dev)OSC通信问题
git checkout -b feature/amazing-feature)git commit -m '添加一些惊人的功能')git push origin feature/amazing-feature)本项目采用MIT许可证 - 查看LICENSE文件以获取详细信息。