musicxml-mcp
by Proxy-alt
README.md
# musicxml-mcp
An MCP server that lets an LLM write fully notated instrumental music and export valid **MusicXML 4.0**.
Instead of asking a model to hand-write raw MusicXML (divisions, `<backup>`, beam states, accidental bookkeeping, transposition, strict element order), the server gives it a compact JSON music model and compiles that to MusicXML with [xmlbuilder2](https://github.com/oozcitak/xmlbuilder2). The output is checked against the official W3C XSD in the test suite.
```jsonc
// one measure of a clarinet part (written in concert pitch; transposed automatically)
{ "notes": [
{ "pitch": "C#5", "dur": "q", "dir": { "dynamics": "p" }, "slur": "start" },
"E5/8", "D5/8",
{ "tuplet": "3:2", "notes": ["C#5/8", "B4/8", "A4/8"] },
{ "pitch": "B4", "dur": "q", "slur": "stop", "articulations": ["tenuto"], "fermata": true }
]}
```
## What the server handles for you
- **Timing:** durations like `q.`, `16`, `h+8` (auto-tied), nested tuplets, grace/cue notes, pickups, whole-measure rests. It computes `divisions`, `<duration>`, and `<backup>`/`<forward>` between voices.
- **Spelling:** accidentals are computed from the key and earlier notes in the bar (per staff and octave, and ties across barlines don't repeat them). Courtesy, editorial, and microtonal accidentals are supported.
- **Beaming:** notes are beamed by beat, including compound and additive meters (`3+2/8`), with hooks for dotted rhythms. Manual beams can override this.
- **Transposition:** transposing instruments (Bb/A/Eb/F, octave instruments) get their written pitches and keys, with enharmonic respelling past 7 accidentals.
- **Numbering:** slur, hairpin, 8va, bracket, glissando, and trill-line numbers are assigned automatically. Stem directions for multiple voices are set, and voice/staff numbers are handled.
- **Checking:** the checker reports overfull or underfull voices, ties with no target, unclosed lines, and notes outside an instrument's range. Invalid input gets precise paths and "did you mean" hints.
## Notation coverage
The typed model covers the notation that MusicXML supports:
- **Notes:** chords, cross-staff notes, noteheads, stems, colors, and hidden notes.
- **Articulations and ornaments:** all articulations, all ornaments (trills with wavy lines, turns, mordents, tremolos), and fermata shapes.
- **Arpeggios and lines:** arpeggios, glissandi, and slides.
- **String, wind, and guitar technique:** bowings, harmonics, fingering, string/fret, hammer-on/pull-off, bends, and so on.
- **Directions:** dynamics, hairpins, tempo and metric modulation, text, rehearsal marks, segno/coda/D.S./Fine, pedals, octave shifts, dashes and brackets, harp pedals, string mutes, principal voice, percussion pictograms, accordion registration, scordatura, and playback `<sound>`.
- **Harmony:** chord symbols (parsed from `"F#m7b5/C"`), Roman numerals, figured bass, and fretboard diagrams (`"x32010"`).
- **Lyrics:** multiple verses, automatic syllabic hyphenation, melisma extenders, and elisions.
- **Structure:** time and key signatures (including additive, senza misura, and non-traditional keys), clefs, repeats, voltas, barlines, breaks, multi-measure rests, beat and rhythmic slash notation, and measure-repeat styles.
- **Percussion and tablature:** named drum maps (`"kick,hihat/8"`) and TAB with automatic string/fret choice.
- **Parts:** groups, brackets, and braces.
- **Escape hatch:** for anything else, raw MusicXML fragments (`rawXml`, `rawNotations`) are checked for well-formedness and inserted verbatim.
## Tools
| Tool | Purpose |
| --- | --- |
| `create_score` | Parts (instrument presets), time, key, tempo, pickup, groups |
| `set_structure` | Score-wide per-measure marks: meter/key changes, tempo, rehearsal, repeats, endings, jumps, breaks |
| `write_measures` | Write consecutive measures of one part and get problems back |
| `edit_measure` | Change single events (a pitch, a dynamic, an articulation) or measure fields without resending the measure |
| `get_score` | Summary, or stored JSON for a measure range |
| `insert_measures` / `delete_measures` / `copy_measures` | Structural editing |
| `add_part` / `update_part` / `remove_part` / `update_score` | Score setup |
| `validate_score` | Full diagnostics, plus the play order through repeats/endings/D.S./coda, performed length, and extreme-range notes |
| `render_preview` | Engraved PNG via Verovio (optionally concert pitch), so the model can see its work |
| `export_score` | `.musicxml`, `.mxl`, `.mid` (swing, dynamics, hairpins, accents and fermatas played), or project `.json` |
| `list_instruments`, `notation_reference` | 76 presets with drum maps and tunings, plus the full syntax reference (also exposed as MCP resources) |
| `list_scores`, `delete_score`, `import_project` | Project management |
## Setup
```bash
npm install
```
```bash
npm run build
```
Add it to Claude Code:
```bash
claude mcp add musicxml -- node /absolute/path/to/MusXMLMCP/dist/index.js
```
Or add it to any MCP client config:
```json
{ "mcpServers": { "musicxml": { "command": "node", "args": ["/absolute/path/to/MusXMLMCP/dist/index.js"] } } }
```
Scores are saved as JSON under `~/.musicxml-mcp/scores` (override with `MUSICXML_MCP_HOME`), so they survive restarts. Exports go to `~/.musicxml-mcp/exports` unless you pass a path.
## Development
```bash
npm test
```
The tests include XSD validation with the system `xmllint` against `test/schema/musicxml.xsd`, plus end-to-end MCP calls.
```bash
npx tsx examples/demo.ts
```
The demo composes a string quartet over stdio and writes `examples/out/`.
Project layout: `src/music` (pitch, duration, harmony, instruments), `src/model` (input schema, store), `src/compile` (resolve → IR → emit via xmlbuilder2, `.mxl` zip), `src/render` (Verovio + resvg), `src/tools.ts` (MCP tools), `src/docs/reference.ts` (LLM-facing reference).
Preview caveats: Verovio's MusicXML import hides empty staves, may draw a concert key on transposing staves after a key change, and doesn't draw some marks (dashes, harp pedals, bends). These affect only the preview; the exported MusicXML is unaffected.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues