Skip to main content
Glama
MCP MIDI Server

MCP MIDI Server

by robig
README.md
# MCP MIDI Server

A Model Context Protocol (MCP) server for sending MIDI messages. This server allows you to send MIDI Control Change (CC), Program Change (PC), and Note On/Off messages to MIDI devices.

## Features

- **Control Change (CC)**: Send CC messages to control parameters on MIDI devices
- **Program Change (PC)**: Change presets/programs on MIDI devices
- **Note On/Off**: Send MIDI notes with velocity control
- **Flexible Targeting**: Send to all devices or specific devices, all channels or specific channels
- **Port Discovery**: List all available MIDI output ports

## Installation

1. Install using uv:
```bash
cd mcp_server_midi
uv venv
source .venv/bin/activate  # On macOS/Linux
# or
.venv\Scripts\activate  # On Windows
uv pip install -e .
```

2. Install MIDI backend (required by mido):
```bash
# macOS
brew install portmidi

# Linux
sudo apt-get install libasound2-dev  # For ALSA
# or
sudo apt-get install libjack-dev     # For JACK

# Windows - no additional installation needed (uses built-in winmm)
```

## Usage with Claude Desktop

Add to your `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "midi": {
      "command": "mcp-server-midi"
    }
  }
}
```

Or if using uv directly:
```json
{
  "mcpServers": {
    "midi": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/mcp_server_midi",
        "run",
        "mcp-server-midi"
      ]
    }
  }
}
```

## Available Tools

### list_midi_outputs
List all available MIDI output ports on your system.

**Example:**
```
List available MIDI ports
```

### send_midi_cc
Send a MIDI Control Change message.

**Parameters:**
- `cc_number` (required): Control Change number (0-127)
- `cc_value` (required): Control Change value (0-127)
- `channel` (optional): MIDI channel (1-16), omit to send to all channels
- `port_name` (optional): Specific port name, omit to send to all ports

**Examples:**
```
Send CC#22 with value 64 to all devices on all channels
Send CC#7 (volume) with value 100 to channel 1
Send CC#1 (modulation) with value 127 to "IAC Driver Bus 1" on channel 3
```

### send_midi_pc
Send a MIDI Program Change message.

**Parameters:**
- `program_number` (required): Program number (0-127)
- `channel` (optional): MIDI channel (1-16), omit to send to all channels
- `port_name` (optional): Specific port name, omit to send to all ports

**Examples:**
```
Send Program Change 5 to all devices
Change to preset 10 on channel 2
```

### send_midi_note_on
Send a MIDI Note On message.

**Parameters:**
- `note` (required): MIDI note number (0-127, where 60 = Middle C)
- `velocity` (optional): Note velocity (1-127, default 64)
- `channel` (optional): MIDI channel (1-16), omit to send to all channels
- `port_name` (optional): Specific port name, omit to send to all ports

**Examples:**
```
Play Middle C (note 60) with default velocity
Play note 72 with velocity 100 on channel 1
```

### send_midi_note_off
Send a MIDI Note Off message.

**Parameters:**
- `note` (required): MIDI note number (0-127, where 60 = Middle C)
- `velocity` (optional): Release velocity (0-127, default 64)
- `channel` (optional): MIDI channel (1-16), omit to send to all channels
- `port_name` (optional): Specific port name, omit to send to all ports

**Examples:**
```
Stop note 60 (Middle C)
Stop note 72 on channel 1
```

### send_musical_sequence
Send a musical sequence with tempo and note durations. This allows you to play basic melodies and musical ideas by specifying notes with their durations in beats.

**Parameters:**
- `notes` (required): Array of note objects, each containing:
  - `note` (required): MIDI note number (0-127, where 60 = Middle C)
  - `velocity` (optional): Note velocity (1-127, default 64)
  - `duration` (required): Note duration in beats (1.0 = quarter note, 0.5 = eighth note, 2.0 = half note, etc.)
- `bpm` (optional): Tempo in beats per minute (20-300, default 120)
- `channel` (required): MIDI channel (1-16) - must be specified for sequences
- `port_name` (optional): Specific port name, omit to send to all ports

**Duration Reference:**
- `4.0` = Whole note
- `2.0` = Half note
- `1.0` = Quarter note
- `0.5` = Eighth note
- `0.25` = Sixteenth note
- `1.5` = Dotted quarter note
- `0.75` = Dotted eighth note

**Examples:**
```json
// Play a simple C major scale at 120 BPM on channel 1
{
  "notes": [
    {"note": 60, "velocity": 80, "duration": 1.0},
    {"note": 62, "velocity": 80, "duration": 1.0},
    {"note": 64, "velocity": 80, "duration": 1.0},
    {"note": 65, "velocity": 80, "duration": 1.0},
    {"note": 67, "velocity": 80, "duration": 1.0},
    {"note": 69, "velocity": 80, "duration": 1.0},
    {"note": 71, "velocity": 80, "duration": 1.0},
    {"note": 72, "velocity": 80, "duration": 2.0}
  ],
  "bpm": 120,
  "channel": 1
}

// Play "Mary Had a Little Lamb" at 100 BPM
{
  "notes": [
    {"note": 64, "duration": 1.0},
    {"note": 62, "duration": 1.0},
    {"note": 60, "duration": 1.0},
    {"note": 62, "duration": 1.0},
    {"note": 64, "duration": 1.0},
    {"note": 64, "duration": 1.0},
    {"note": 64, "duration": 2.0}
  ],
  "bpm": 100,
  "channel": 1
}

// Play a rhythm pattern with varying velocities
{
  "notes": [
    {"note": 60, "velocity": 100, "duration": 0.5},
    {"note": 60, "velocity": 60, "duration": 0.5},
    {"note": 60, "velocity": 80, "duration": 0.5},
    {"note": 60, "velocity": 60, "duration": 0.5}
  ],
  "bpm": 140,
  "channel": 10
}
```

**Use Cases:**
- Test musical ideas and melodies
- Send simple musical phrases to MIDI devices
- Create rhythmic patterns with specific timing
- Prototype musical sequences before implementing in DAW

## MIDI Note Reference

Common MIDI note numbers:
- 60 = Middle C (C4)
- 61 = C#4
- 62 = D4
- 63 = D#4
- 64 = E4
- 65 = F4
- 66 = F#4
- 67 = G4
- 68 = G#4
- 69 = A4 (440 Hz)
- 70 = A#4
- 71 = B4
- 72 = C5

## Common MIDI CC Numbers

- CC#1: Modulation Wheel
- CC#7: Volume
- CC#10: Pan
- CC#11: Expression
- CC#64: Sustain Pedal (0-63 = off, 64-127 = on)
- CC#71: Resonance/Timbre
- CC#74: Brightness/Cutoff

## Troubleshooting

### No MIDI devices found
- Ensure you have MIDI devices connected or virtual MIDI ports set up
- On macOS, you can create virtual MIDI ports using Audio MIDI Setup
- On Linux, use `aconnect -l` to list ALSA MIDI ports
- On Windows, check Device Manager for MIDI devices

### mido import error
- Make sure mido is installed: `uv pip install mido`
- Install the appropriate MIDI backend for your OS (see Installation section)

### Permission errors
- On Linux, you may need to add your user to the `audio` group:
  ```bash
  sudo usermod -a -G audio $USER
  ```
  Then log out and back in.

## Development

Run the server directly for testing:
```bash
python -m mcp_server_midi.server
```

## License

MIT License - See LICENSE file for details