Skip to main content

MCP server for controlling a local Sonic Pi runtime

Project description

sonic-pi-mcp

sonic-pi-mcp is a Python MCP server that lets an MCP client control a local Sonic Pi runtime over the standard stdio transport.

It starts Sonic Pi's Ruby daemon, sends OSC messages to the Spider runtime, runs Sonic Pi code, stops jobs, reads runtime events/logs, and searches the local Sonic Pi docs/samples/synthdefs.

Requirements

  • Python 3.11+
  • A local Sonic Pi installation or checkout.
  • The Sonic Pi root directory must contain:
    • app/server/ruby/bin/daemon.rb
    • app/server/ruby/bin/spider-server.rb
    • usually etc/doc, etc/samples, and etc/synthdefs

No machine-specific path is baked into this package. Set SONIC_PI_ROOT in the MCP client environment unless you pass root_path to sonic_start.

Install

From PyPI, after the package is published:

pip install sonic-pi-mcp

From a local checkout:

pip install .

Build a wheel/sdist:

python -m build

Then install the wheel on another machine:

pip install dist/sonic_pi_mcp-0.1.0-py3-none-any.whl

The wheel only contains the Python package under src/sonic_pi_mcp. Generated runtime files, exported audio, examples, tests, and local scripts are excluded from distribution.

Publish to PyPI

This repository is prepared for PyPI Trusted Publishing through GitHub Actions. No PyPI API token should be stored in the repository.

Configure a PyPI Trusted Publisher for the package:

PyPI project name: sonic-pi-mcp
GitHub owner: WEEZZ-admin
GitHub repository: sonic-pi-mcp
Workflow name: publish.yml
Environment name: pypi

Then publish a GitHub Release. The workflow in .github/workflows/publish.yml will build the wheel/sdist and upload them to PyPI.

Before each release, update version in pyproject.toml. PyPI does not allow re-uploading the same version.

Recommended local preflight:

python -m pip install --upgrade build twine
python -m build
python -m twine check dist/*

Configuration

Required in most deployments:

SONIC_PI_ROOT=<path to the Sonic Pi root directory>

Useful optional variables:

SONIC_PI_MCP_RUNTIME_DIR=<writable directory for temporary run_file buffers>
SONIC_PI_MCP_STARTUP_TIMEOUT=60
SONIC_PI_MCP_KEEPALIVE_INTERVAL=4
SONIC_PI_MCP_EVENT_BUFFER_SIZE=5000
SONIC_PI_MCP_DEFAULT_COLLECT_MS=1500
SONIC_PI_HOME=<custom Sonic Pi user-home root for logs, if needed>

SONIC_PI_MCP_RUNTIME_DIR is used when code is too large for Sonic Pi's OSC packet size and must be submitted with run_file. If it is not set, the server uses a per-user cache directory such as %LOCALAPPDATA%\sonic-pi-mcp on Windows, ~/Library/Caches/sonic-pi-mcp on macOS, or ${XDG_CACHE_HOME:-~/.cache}/sonic-pi-mcp on Linux.

PowerShell example without hard-coding a drive:

$env:SONIC_PI_ROOT = Join-Path $env:ProgramFiles 'Sonic Pi'
$env:SONIC_PI_MCP_RUNTIME_DIR = Join-Path $env:LOCALAPPDATA 'sonic-pi-mcp'
sonic-pi-mcp

POSIX shell example:

export SONIC_PI_ROOT="$HOME/apps/sonic-pi"
export SONIC_PI_MCP_RUNTIME_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/sonic-pi-mcp"
sonic-pi-mcp

MCP Client Setup

This package is a stdio MCP server. Configure clients to run the installed console command sonic-pi-mcp.

Generic MCP JSON shape:

{
  "mcpServers": {
    "sonic-pi": {
      "command": "sonic-pi-mcp",
      "args": [],
      "env": {
        "SONIC_PI_ROOT": "<path to Sonic Pi root>",
        "SONIC_PI_MCP_RUNTIME_DIR": "<writable runtime directory>"
      }
    }
  }
}

If your client does not inherit shell environment variables, put the variables in the client config. Avoid relying on the terminal profile of the user who installed the package.

Run Manually

python -m sonic_pi_mcp

or:

sonic-pi-mcp

Both commands run the same stdio MCP server. They do not open an HTTP port.

MCP Tools

  • sonic_start(root_path?, no_inputs?)
  • sonic_status()
  • sonic_run_code(code, buffer_name?, collect_ms?)
  • sonic_stop(collect_ms?)
  • sonic_shutdown()
  • sonic_read_events(since?, limit?)
  • sonic_get_logs(source?, tail?)
  • sonic_send_cue(path, args?)
  • sonic_search_docs(query, limit?, root_path?)
  • sonic_list_samples(limit?, root_path?)
  • sonic_list_synths(limit?, root_path?)
  • sonic_list_fx(limit?, root_path?)

Suggested Agent Workflow

  1. Call sonic_start(no_inputs=true) unless the user needs audio input.
  2. Call sonic_status() and confirm state is ready.
  3. Use sonic_search_docs, sonic_list_samples, sonic_list_synths, and sonic_list_fx to stay within the user's installed Sonic Pi version.
  4. Send music with sonic_run_code(code, buffer_name, collect_ms).
  5. Inspect returned events for syntax_error, runtime_error, or missing Defining fn :live_loop_... messages.
  6. Call sonic_stop() before replacing a long-running composition.
  7. Call sonic_shutdown() when the session is no longer needed.

Security

sonic_run_code executes local Sonic Pi code through the same token-protected Spider API used by the Sonic Pi GUI. Treat access to this MCP server as local code execution and local audio-device control.

License

MIT License. You may use, copy, modify, publish, distribute, sublicense, and sell copies of this package, provided the license text is included.

Packaging Notes

The package is intentionally path-neutral:

  • No repository-local absolute path is embedded.
  • SONIC_PI_ROOT or the root_path tool argument identifies Sonic Pi.
  • SONIC_PI_MCP_RUNTIME_DIR controls where temporary large-buffer files are written.
  • Build configuration excludes generated files such as .runtime/, exports/, examples, tests, and local playback/export scripts.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sonic_pi_mcp-0.1.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sonic_pi_mcp-0.1.0-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file sonic_pi_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: sonic_pi_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sonic_pi_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b301b14c30ad3c7d015e4e4180a4605422bbf92fafe13c6dc1791b22a53e4d7f
MD5 852724edf85aa528177af04f4d36cf62
BLAKE2b-256 f976d5f6daaaed35f8d71b86e332202a9094f260d14d6f3b76346e7b1c03b8e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sonic_pi_mcp-0.1.0.tar.gz:

Publisher: publish.yml on WEEZZ-admin/sonic-pi-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sonic_pi_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sonic_pi_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sonic_pi_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2b9a0ef72b709c0a7960322971723f34c9a3c51f173f1b62792a36defefa708
MD5 c938e558ece09fa9281186f70f9d226c
BLAKE2b-256 b0ddc597cca877b540fd2be3d718affca94d5f5053fadf2263ed4fda5e3b61c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sonic_pi_mcp-0.1.0-py3-none-any.whl:

Publisher: publish.yml on WEEZZ-admin/sonic-pi-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page