Music21
FreeNot checkedExposes 16 music21 MIDI analysis and editing tools to AI agents, enabling them to read, edit, generate, and analyze MIDI files through natural language.
About
Exposes 16 music21 MIDI analysis and editing tools to AI agents, enabling them to read, edit, generate, and analyze MIDI files through natural language.
README
Symbolic music analysis and editing tools exposed through MCP, built on music21. The server works with user-supplied MIDI and supports analysis, transformation, and basic accompaniment generation.
The current implementation registers 16 MCP tools and has 120 automated tests.
What it does
Give an AI agent a MIDI file and it can:
- Read — parse MIDI, detect key, analyze chord progressions
- Edit — transpose, change velocity, filter notes, replace chords, merge files, quantize timings
- Generate — reharmonize progressions, harmonize melodies
- Analyze — detect modulations, extract melody, identify song form, search for patterns
Tools
The MCP-facing names currently use an mcp_ prefix, such as mcp_analyze_key and mcp_transpose_midi. The underlying Python functions use the unprefixed names shown below.
| Phase | Tool | What it does |
|---|---|---|
| 1a | parse_midi_file |
Load a .mid file into a music21 stream |
| 1a | analyze_key |
Detect key, mode, and confidence |
| 1a | export_midi |
Write a stream to a .mid file |
| 1c | transpose_midi |
Transpose by semitones or to a target key |
| 1c | change_velocity |
Scale, set, or offset note velocities (clamped 1–127) |
| 1c | modify_notes |
Remove/select notes by pitch range, shift octaves |
| 1c | replace_chord_at |
Replace a chord at a specific bar/beat |
| 1c | merge_midi |
Overlay two streams (flat or multi-part score) |
| 1c | quantize_midi |
Snap note timings to quarter/eighth/sixteenth grid |
| 2 | reharmonize |
Replace chords with diatonic or jazz substitutions |
| 2 | harmonize_melody |
Generate block chord accompaniment from a melody |
| 3 | analyze_chord_progression |
Identify chords + Roman numerals in a progression |
| 3 | extract_melody |
Isolate the melody line (highest pitch per offset) |
| 3 | detect_modulations |
Find key changes via sliding window analysis |
| 3 | analyze_form |
Identify song sections (A, B, A') by register changes |
| 3 | search_pattern |
Find melodic patterns by exact pitch or interval sequence |
Getting started
Prerequisites
- Python 3.11+
- A virtual environment (recommended)
Install
cd music21-mcp
# Create and activate venv
python3 -m venv venv
source venv/bin/activate
# Install the package and test dependencies in editable mode
pip install -e ".[dev]"
Run tests
# All 120 tests
pytest -v
# Just one tool's tests
pytest tests/test_transpose_midi.py -v
Use as MCP server
The server runs over stdio and is intended for MCP clients that support local stdio servers.
# Start the server
python -m music21_mcp.server
To configure in an MCP client, add to your client's MCP config:
{
"mcpServers": {
"music21-mcp": {
"command": "python",
"args": ["-m", "music21_mcp.server"],
"cwd": "/path/to/music21-mcp"
}
}
}
Or if using the project venv:
{
"mcpServers": {
"music21-mcp": {
"command": "/path/to/music21-mcp/venv/bin/python",
"args": ["-m", "music21_mcp.server"]
}
}
}
Use directly in Python
Every tool is a pure function you can call without the MCP layer:
from music21_mcp.tools.parse_midi import parse_midi_file
from music21_mcp.tools.analyze_key import analyze_key
from music21_mcp.tools.transpose_midi import transpose_midi
from music21_mcp.tools.export_midi import export_midi
# Load a MIDI file
stream = parse_midi_file("song.mid")
# Detect the key
key_info = analyze_key(stream)
print(key_info)
# {'key': 'C major', 'mode': 'major', 'confidence': 0.797}
# Transpose to F major
transposed = transpose_midi(stream, target_key="F")
# Save the result
export_midi(transposed, "song_in_f.mid")
Example: the full pipeline
from music21_mcp.tools import (
parse_midi_file, analyze_key, transpose_midi,
change_velocity, quantize_midi, export_midi,
)
# Load
s = parse_midi_file("input.mid")
# Detect key
print(analyze_key(s))
# Transpose up 2 semitones, make 20% louder, quantize to 16th notes
s = transpose_midi(s, semitones=2)
s = change_velocity(s, scale=1.2)
s = quantize_midi(s, grid="sixteenth")
# Save
export_midi(s, "output.mid")
Example prompts for testing
Single-tool prompts to verify each tool works in isolation:
Parse only — "Load
song.midwith parse_midi_file and tell me how many parts and measures it has."Key detection — "Use parse_midi_file to load
song.mid, then analyze_key to tell me the key, mode, and confidence."Export — "Load
song.midwith parse_midi_file and export it tocopy.midwith export_midi. Report the output path."Transpose — "Parse
song.midand transpose it down 3 semitones. Export tosong_down.mid."Transpose to key — "Load
song.mid, detect its key, then transpose it to E minor. Export tosong_em.mid."Velocity — "Parse
song.mid, set all velocities to a fixed value of 90. Export tosong_vel90.mid."Velocity boost — "Load
song.midand scale all velocities by 1.3 (clamped to 127). Export tosong_louder.mid."Filter notes — "Parse
song.midand remove all notes below C3. Export tosong_no_bass.mid."Octave shift — "Load
song.midand shift everything up 1 octave. Export tosong_oct_up.mid."Replace chord — "Parse
song.midand replace the chord at bar 2, beat 1 with a C major triad. Export tosong_newchord.mid."Merge — "Load
drums.midandbass.mid, merge them into a single file. Export todrum_and_bass.mid."Quantize — "Parse
song.midand quantize all timings to a sixteenth-note grid. Export tosong_quantized.mid."Reharmonize — "Load
song.midand reharmonize the chord progression using jazz substitutions. Export tosong_jazz.mid."Harmonize — "Parse
melody.midand generate block chord accompaniment from the melody line. Export tomelody_with_chords.mid."Chord analysis — "Load
song.midand analyze the chord progression. Give me the chords and Roman numerals."Extract melody — "Parse
song.midand extract just the melody line. Export tomelody_only.mid."Detect modulations — "Load
song.midand find all key changes. Tell me where each modulation happens."Analyze form — "Parse
song.midand identify the song structure (A, B, A' sections)."Search pattern — "Load
song.midand search for the pitch sequence C4-E4-G4. Tell me where it occurs."
Multi-tool chains to test tool composition:
Full analysis — "Load
song.mid. Detect the key, analyze the chord progression, extract the melody, and analyze the song form. Give me a complete analysis report."Transpose and harmonize — "Parse
song.mid, transpose up 5 semitones, then reharmonize the result with jazz substitutions. Export tosong_jazz_up.mid."Clean and quantize — "Load
song.mid, remove all notes below C2, quantize to an eighth-note grid, and scale velocities by 0.8. Export tosong_cleaned.mid."Melody extraction pipeline — "Parse
song.mid, extract the melody, harmonize it with block chords, and export tomelody_harmonized.mid."Key-aware reharm — "Load
song.mid, detect the key, then reharmonize using diatonic substitutions appropriate for that key. Export tosong_diatonic.mid."Modulation-aware transpose — "Parse
song.mid, detect all modulations, then transpose the whole piece so the first key becomes A minor. Export tosong_am.mid."Pattern search — "Load
song.midand search for the interval sequence [2, 2] (two whole steps up). Report every matching offset."Form and melody analysis — "Parse
song.mid, analyze its form, then extract the full melody line and export it tomelody.mid."Multi-file merge and clean — "Merge
drums.midandbass.mid, then merge that result withkeys.mid. Quantize the combined file to a sixteenth grid, set velocities to 100, and export tofull_band.mid."End-to-end remix — "Load
song.mid. Detect key, transpose down 2 semitones, quantize to eighth notes, reharmonize with jazz substitutions, boost velocity by 1.4, and export tosong_remix.mid."
Architecture
src/music21_mcp/
__init__.py # Package entry
server.py # FastMCP server — 16 tool wrappers returning JSON
tools/
parse_midi.py # Phase 1a
analyze_key.py
export_midi.py
transpose_midi.py # Phase 1c
change_velocity.py
modify_notes.py
replace_chord_at.py
merge_midi.py
quantize_midi.py
reharmonize.py # Phase 2
harmonize_melody.py
analyze_chord_progression.py # Phase 3
extract_melody.py
detect_modulations.py
analyze_form.py
search_pattern.py
tests/
test_*.py # One test file per tool, plus server + e2e tests
Most editing and analysis functions operate on music21 streams. Parsing and export perform file I/O, while the MCP wrappers accept file paths and return JSON strings. This keeps the domain logic independently testable.
Tech
- music21 — MIT's music theory and analysis toolkit
- MCP — Model Context Protocol for AI agent tool calling
- Python 3.11+, pytest, strict TDD
License
MIT
Installing Music21
This server has no published package — it is built from source. Open the repository and follow its README.
▸ github.com/cclawton/music21-mcpFAQ
Is Music21 MCP free?
Yes, Music21 MCP is free — one-click install via Unyly at no cost.
Does Music21 need an API key?
No, Music21 runs without API keys or environment variables.
Is Music21 hosted or self-hosted?
Self-hosted: the server runs locally on your machine via the install command above.
How do I install Music21 in Claude Desktop, Claude Code or Cursor?
Open Music21 on unyly.org, pick your client tab (Claude Desktop, Claude Code, Cursor) and press Install — the config is generated automatically, no JSON editing.
Related MCPs
Fetch
Web content fetching and conversion for efficient LLM usage.
AWS KB Retrieval
Retrieval from AWS Knowledge Base using Bedrock Agent Runtime.
by modelcontextprotocolSpring AI MCP Server
Provides auto-configuration for setting up an MCP server in Spring Boot applications.
llm-analysis-assistant
A very streamlined mcp client that supports calling and monitoring stdio/sse/streamableHttp, and can also view request responses through the /logs page. It also
by xuzexin-hzMCP-Agent
A simple, composable framework to build agents using Model Context Protocol by [LastMile AI](https://www.lastmileai.dev)
by lastmile-aiSpring AI MCP Client
Provides auto-configuration for MCP client functionality in Spring Boot applications.
mcp.natoma.ai
A Hosted MCP Platform to discover, install, manage and deploy MCP servers by [Natoma Labs](https://www.natoma.ai)
MCPHub
Website to list high quality MCP servers and reviews by real users. Also provide online chatbot for popular LLM models with MCP server support.
MCP Servers Rating and User Reviews
Website to rate MCP servers, write authentic user reviews, and [search engine for agent & mcp](http://www.deepnlp.org/search/agent)
mkinf
An Open Source registry of hosted MCP Servers to accelerate AI agent workflows.
Compare Music21 with
Not sure what to pick?
Find your stack in 60 seconds
Author?
Embed badge for your README
Browse similar
All ai MCPs
