omr-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omr_mcp-0.1.0/.github/copilot-instructions.md +120 -0
- omr_mcp-0.1.0/.gitignore +38 -0
- omr_mcp-0.1.0/.python-version +1 -0
- omr_mcp-0.1.0/CLAUDE.md +113 -0
- omr_mcp-0.1.0/PKG-INFO +27 -0
- omr_mcp-0.1.0/README.md +103 -0
- omr_mcp-0.1.0/docs/architecture.md +239 -0
- omr_mcp-0.1.0/docs/requirements.md +201 -0
- omr_mcp-0.1.0/examples/claude_desktop_config.json +9 -0
- omr_mcp-0.1.0/pyproject.toml +50 -0
- omr_mcp-0.1.0/src/omr_mcp/__init__.py +3 -0
- omr_mcp-0.1.0/src/omr_mcp/omr_engine.py +248 -0
- omr_mcp-0.1.0/src/omr_mcp/server.py +205 -0
- omr_mcp-0.1.0/src/omr_mcp/utils.py +105 -0
- omr_mcp-0.1.0/tests/__init__.py +1 -0
- omr_mcp-0.1.0/tests/conftest.py +17 -0
- omr_mcp-0.1.0/tests/fixtures/README.md +17 -0
- omr_mcp-0.1.0/tests/test_integration.py +149 -0
- omr_mcp-0.1.0/tests/test_omr.py +138 -0
- omr_mcp-0.1.0/tests/test_server.py +131 -0
- omr_mcp-0.1.0/tests/test_utils.py +109 -0
- omr_mcp-0.1.0/uv.lock +1588 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# omr-mcp — Copilot Instructions
|
|
2
|
+
|
|
3
|
+
## What This Server Does
|
|
4
|
+
|
|
5
|
+
Converts sheet music images (PNG/JPEG) to MusicXML using the `oemer` deep-learning OCR engine.
|
|
6
|
+
This is the entry point of the choir music pipeline — all other servers consume MusicXML produced here.
|
|
7
|
+
|
|
8
|
+
**Status:** Phases 1–4 substantially complete. 36 unit tests pass.
|
|
9
|
+
Integration tests written but not yet verified against real oemer.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Before You Write Any Code
|
|
14
|
+
|
|
15
|
+
Read these documents in order:
|
|
16
|
+
1. **`docs/HANDOVER.md`** — current status, remaining work, pre-flight checks, known gotchas, definition of done
|
|
17
|
+
2. **`docs/PLAN.md`** — architecture decisions and technology rationale
|
|
18
|
+
3. **`docs/requirements.md`** — functional and non-functional requirements, error codes
|
|
19
|
+
4. **`docs/architecture.md`** — component diagram, data-flow, module responsibilities
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Project Structure
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
omr-mcp/
|
|
27
|
+
├── pyproject.toml
|
|
28
|
+
├── .python-version # "llm311"
|
|
29
|
+
├── src/omr_mcp/
|
|
30
|
+
│ ├── server.py # MCP tool definitions and async handlers
|
|
31
|
+
│ ├── omr_engine.py # oemer wrapper — NO mcp imports
|
|
32
|
+
│ └── utils.py # image validation, base64 helpers
|
|
33
|
+
├── tests/
|
|
34
|
+
│ ├── test_omr.py # unit + integration tests
|
|
35
|
+
│ └── fixtures/
|
|
36
|
+
├── test_samples/
|
|
37
|
+
│ └── pdmx_satb_samples/ # 10 SATB PNG + MXL fixtures — use these in tests
|
|
38
|
+
│ ├── png/
|
|
39
|
+
│ └── mxl/
|
|
40
|
+
└── docs/
|
|
41
|
+
├── requirements.md
|
|
42
|
+
└── architecture.md
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Implemented Tools
|
|
48
|
+
|
|
49
|
+
| Tool | Input | Output |
|
|
50
|
+
|------|-------|--------|
|
|
51
|
+
| `recognize_sheet` | Image path or base64 | MusicXML string + metadata |
|
|
52
|
+
| `recognize_sheet_to_file` | Image path | MusicXML file path + metadata |
|
|
53
|
+
| `recognize_sheets` | List of image paths/base64 | Single merged MusicXML (multi-page) |
|
|
54
|
+
| `list_capabilities` | — | Server capabilities |
|
|
55
|
+
| `list_supported_formats` | — | Deprecated alias for `list_capabilities` |
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Coding Standards
|
|
60
|
+
|
|
61
|
+
- **Package manager:** `uv` only — never `pip install`
|
|
62
|
+
- **`omr_engine.py` must not import from `mcp`** — keeps it independently unit-testable
|
|
63
|
+
- **All tool handlers must be `async def`**
|
|
64
|
+
- **All error responses:** `{"error": "human description", "error_code": "SCREAMING_SNAKE"}`
|
|
65
|
+
- **Standard error codes:** `FILE_NOT_FOUND`, `UNSUPPORTED_FORMAT`, `FILE_TOO_LARGE`, `INVALID_INPUT`, `PROCESSING_FAILED`, `INVALID_PARAMETER`
|
|
66
|
+
- **Integration tests** must be `@pytest.mark.integration` — not run in the default `pytest` invocation
|
|
67
|
+
- **Python 3.11+**; `asyncio_mode = "auto"` in pytest config
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Running Tests
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Unit tests (fast, default)
|
|
75
|
+
VIRTUAL_ENV= .venv/bin/pytest tests/ -v
|
|
76
|
+
|
|
77
|
+
# Integration tests (invokes real oemer — slow, first run downloads ~100 MB)
|
|
78
|
+
VIRTUAL_ENV= .venv/bin/pytest tests/ -v -m integration
|
|
79
|
+
|
|
80
|
+
# Install dependencies
|
|
81
|
+
uv sync
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## MCP Server Startup Pattern
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import asyncio
|
|
90
|
+
import mcp.server.stdio
|
|
91
|
+
|
|
92
|
+
async def _run():
|
|
93
|
+
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
|
|
94
|
+
await app.run(read_stream, write_stream, app.create_initialization_options())
|
|
95
|
+
|
|
96
|
+
asyncio.run(_run())
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`mcp.server.stdio.run_server()` does not exist — always use the context manager above.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Known Gotchas
|
|
104
|
+
|
|
105
|
+
- **oemer downloads ~100 MB on first run** — expect ~10 min. Uses cache after that.
|
|
106
|
+
- **oemer output varies slightly between runs** — compare structure, not byte-identical XML.
|
|
107
|
+
- **MusicXML from oemer may not pass strict schema validation** — test for parseable content with
|
|
108
|
+
parts and measures present.
|
|
109
|
+
- **Never load or iterate `test_samples/pdmx_full/`** (14 GB dataset) in tests — use
|
|
110
|
+
`pdmx_satb_samples/` only.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## When You Finish Work
|
|
115
|
+
|
|
116
|
+
Update these documents so the next session starts with accurate information:
|
|
117
|
+
- **`docs/HANDOVER.md`** — check off done items, add new gotchas, update status
|
|
118
|
+
- **`docs/PLAN.md`** — check off completed phase items, note changed decisions
|
|
119
|
+
- **`docs/requirements.md`** — update if behaviour or interfaces changed
|
|
120
|
+
- **`docs/architecture.md`** — update if implementation structure changed
|
omr_mcp-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Working development docs — tracked locally only (live in each server's docs/ folder)
|
|
2
|
+
HANDOVER.md
|
|
3
|
+
PLAN.md
|
|
4
|
+
|
|
5
|
+
# Python
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.pyc
|
|
8
|
+
*.pyo
|
|
9
|
+
*.pyd
|
|
10
|
+
*.egg-info/
|
|
11
|
+
dist/
|
|
12
|
+
build/
|
|
13
|
+
.eggs/
|
|
14
|
+
|
|
15
|
+
# Virtual environments
|
|
16
|
+
.venv/
|
|
17
|
+
venv/
|
|
18
|
+
|
|
19
|
+
# Test caches
|
|
20
|
+
.pytest_cache/
|
|
21
|
+
.mypy_cache/
|
|
22
|
+
.ruff_cache/
|
|
23
|
+
|
|
24
|
+
# Test samples (large data — not committed)
|
|
25
|
+
test_samples/
|
|
26
|
+
|
|
27
|
+
# Editor / OS
|
|
28
|
+
.vscode/
|
|
29
|
+
.idea/
|
|
30
|
+
.DS_Store
|
|
31
|
+
Thumbs.db
|
|
32
|
+
|
|
33
|
+
# Environment overrides
|
|
34
|
+
.env
|
|
35
|
+
.env.*
|
|
36
|
+
|
|
37
|
+
# Snyk Security Extension - AI Rules (auto-generated)
|
|
38
|
+
.github/instructions/snyk_rules.instructions.md
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
llm311
|
omr_mcp-0.1.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# omr-mcp — Claude Code Instructions
|
|
2
|
+
|
|
3
|
+
## What This Server Does
|
|
4
|
+
|
|
5
|
+
Converts sheet music images (PNG/JPEG) to MusicXML using the `oemer` deep-learning OCR engine.
|
|
6
|
+
Entry point of the choir pipeline — all other servers consume MusicXML produced here.
|
|
7
|
+
|
|
8
|
+
**Status:** Phases 1–4 substantially complete. 36 unit tests pass. Integration tests written but
|
|
9
|
+
not yet verified against real oemer (first run downloads ~100 MB of model checkpoints, ~10 min).
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Running Tests
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# From this directory
|
|
17
|
+
VIRTUAL_ENV= .venv/bin/pytest tests/ -v # 36 unit tests (fast)
|
|
18
|
+
VIRTUAL_ENV= .venv/bin/pytest tests/ -v -m integration # invokes real oemer (~3–5 min/page)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Install dependencies: `uv sync`
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Key Files
|
|
26
|
+
|
|
27
|
+
| File | Purpose |
|
|
28
|
+
|------|---------|
|
|
29
|
+
| `src/omr_mcp/server.py` | MCP tool definitions and handlers |
|
|
30
|
+
| `src/omr_mcp/omr_engine.py` | oemer wrapper (no MCP imports) |
|
|
31
|
+
| `src/omr_mcp/utils.py` | Image validation, base64 helpers |
|
|
32
|
+
| `tests/test_omr.py` | Unit + integration tests |
|
|
33
|
+
| `test_samples/pdmx_satb_samples/` | 10 SATB PNG + MXL ground-truth fixtures |
|
|
34
|
+
| `docs/HANDOVER.md` | Status, remaining work, definition of done |
|
|
35
|
+
| `docs/PLAN.md` | Architecture decisions and rationale |
|
|
36
|
+
| `docs/requirements.md` | Functional/non-functional requirements |
|
|
37
|
+
| `docs/architecture.md` | Component diagram and data-flow |
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Implemented Tools
|
|
42
|
+
|
|
43
|
+
| Tool | Input | Output |
|
|
44
|
+
|------|-------|--------|
|
|
45
|
+
| `recognize_sheet` | Image path or base64 | MusicXML string + metadata |
|
|
46
|
+
| `recognize_sheet_to_file` | Image path | MusicXML file path + metadata |
|
|
47
|
+
| `recognize_sheets` | List of image paths/base64 | Single merged MusicXML (multi-page) |
|
|
48
|
+
| `list_capabilities` | — | Server capabilities |
|
|
49
|
+
| `list_supported_formats` | — | Deprecated alias for `list_capabilities` |
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Hard Rules
|
|
54
|
+
|
|
55
|
+
- **Never `pip install`** — use `uv sync` or `uv add`
|
|
56
|
+
- **`omr_engine.py` must not import from `mcp`** — keeps it unit-testable
|
|
57
|
+
- **All tool handlers must be `async def`**
|
|
58
|
+
- **All errors** → `{"error": "...", "error_code": "..."}` (codes: `FILE_NOT_FOUND`, `UNSUPPORTED_FORMAT`, `FILE_TOO_LARGE`, `INVALID_INPUT`, `PROCESSING_FAILED`, `INVALID_PARAMETER`)
|
|
59
|
+
- **Integration tests** must be `@pytest.mark.integration` and skipped in the default run
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## MCP Server Startup Pattern
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import asyncio
|
|
67
|
+
import mcp.server.stdio
|
|
68
|
+
|
|
69
|
+
async def _run():
|
|
70
|
+
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
|
|
71
|
+
await app.run(read_stream, write_stream, app.create_initialization_options())
|
|
72
|
+
|
|
73
|
+
asyncio.run(_run())
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`run_server()` does not exist in the installed MCP SDK — use the context manager above.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Known Gotchas
|
|
81
|
+
|
|
82
|
+
- **oemer downloads ~100 MB on first run** and takes ~10 min. Subsequent runs use cache.
|
|
83
|
+
- **oemer output varies slightly between runs** — never assert byte-identical XML; parse and
|
|
84
|
+
compare structure instead.
|
|
85
|
+
- **MusicXML from oemer may not validate against strict schema** — check for parseable content
|
|
86
|
+
and presence of parts/measures.
|
|
87
|
+
- **The full PDMX dataset (14 GB) lives in `test_samples/pdmx_full/`** — never load or iterate
|
|
88
|
+
it in tests; use `pdmx_satb_samples/` only.
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Claude Desktop Config
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"mcpServers": {
|
|
97
|
+
"omr": {
|
|
98
|
+
"command": "uv",
|
|
99
|
+
"args": ["--directory", "/path/to/omr-mcp", "run", "omr-mcp"]
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Document Update Policy
|
|
108
|
+
|
|
109
|
+
When you finish work or reach a milestone, update:
|
|
110
|
+
1. `docs/HANDOVER.md` — check off done items, add new gotchas
|
|
111
|
+
2. `docs/PLAN.md` — check off phase items, note changed decisions
|
|
112
|
+
3. `docs/requirements.md` — update if behaviour changed
|
|
113
|
+
4. `docs/architecture.md` — update if implementation changed
|
omr_mcp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: omr-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP server for Optical Music Recognition — converts sheet music images to MusicXML
|
|
5
|
+
Project-URL: Homepage, https://github.com/raulkivi/music-assistant
|
|
6
|
+
Project-URL: Repository, https://github.com/raulkivi/music-assistant
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/raulkivi/music-assistant/issues
|
|
8
|
+
Author-email: Raul Kivi <raulkivi@users.noreply.github.com>
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: mcp,music,musicxml,omr,optical-music-recognition,sheet-music
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: defusedxml>=0.7.0
|
|
22
|
+
Requires-Dist: mcp>=1.0.0
|
|
23
|
+
Requires-Dist: oemer>=0.1.0
|
|
24
|
+
Requires-Dist: pillow>=10.0.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
omr_mcp-0.1.0/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# omr-mcp
|
|
2
|
+
|
|
3
|
+
MCP server that converts sheet music images to MusicXML using optical music recognition (OMR).
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Takes a photo or scan of printed sheet music and returns a MusicXML document. Handles single pages or multi-page scores. Feeds directly into the rest of the choir-music-assistant pipeline.
|
|
8
|
+
|
|
9
|
+
## Tools
|
|
10
|
+
|
|
11
|
+
| Tool | Description |
|
|
12
|
+
|------|-------------|
|
|
13
|
+
| `recognize_sheet` | Convert a single image (file path or base64) to MusicXML string |
|
|
14
|
+
| `recognize_sheet_to_file` | Convert a single image and write MusicXML to a file |
|
|
15
|
+
| `recognize_sheets` | Process multiple pages and merge them into one MusicXML document |
|
|
16
|
+
| `list_capabilities` | Return server metadata: backend version, input/output formats, available tools |
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
cd omr-mcp
|
|
22
|
+
uv sync
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
On first run, oemer downloads ~100 MB of model checkpoints. This happens once and is cached.
|
|
26
|
+
|
|
27
|
+
## Running
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
uv run omr-mcp
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
No environment variables required.
|
|
34
|
+
|
|
35
|
+
## Claude Desktop configuration
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"mcpServers": {
|
|
40
|
+
"omr": {
|
|
41
|
+
"command": "uv",
|
|
42
|
+
"args": ["--directory", "/absolute/path/to/omr-mcp", "run", "omr-mcp"]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage examples
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
// Recognize a single image file
|
|
52
|
+
{"tool": "recognize_sheet", "arguments": {"image_path": "/path/to/scan.png"}}
|
|
53
|
+
|
|
54
|
+
// Recognize from base64-encoded image
|
|
55
|
+
{"tool": "recognize_sheet", "arguments": {"image_base64": "<base64 data>", "mime_type": "image/jpeg"}}
|
|
56
|
+
|
|
57
|
+
// Process multiple pages into one score
|
|
58
|
+
{"tool": "recognize_sheets", "arguments": {"image_paths": ["/path/page1.png", "/path/page2.png"]}}
|
|
59
|
+
|
|
60
|
+
// Save result directly to file
|
|
61
|
+
{"tool": "recognize_sheet_to_file", "arguments": {"image_path": "/path/scan.png", "output_path": "/tmp/score.mxl"}}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Testing
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# Unit tests (fast, no model required)
|
|
68
|
+
VIRTUAL_ENV= .venv/bin/pytest tests/ -v
|
|
69
|
+
|
|
70
|
+
# Integration tests (requires model download, ~10 min per page)
|
|
71
|
+
VIRTUAL_ENV= .venv/bin/pytest tests/ -v -m integration
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Test samples
|
|
75
|
+
|
|
76
|
+
SATB a cappella samples are available in `test_samples/pdmx_satb_samples/`:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
pdmx_satb_samples/
|
|
80
|
+
├── mxl/ # MusicXML ground truth
|
|
81
|
+
├── pdf/ # PDF scores
|
|
82
|
+
└── png/ # PNG images (OMR input)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Source: [PDMX dataset](https://zenodo.org/records/14648209) — 250K+ public domain scores.
|
|
86
|
+
|
|
87
|
+
## Dependencies
|
|
88
|
+
|
|
89
|
+
- [oemer](https://github.com/BreezeWhite/oemer) — deep learning OMR engine (UNet + SVM, ONNX Runtime)
|
|
90
|
+
- [Pillow](https://python-pillow.org/) — image loading and validation
|
|
91
|
+
- [defusedxml](https://github.com/tiran/defusedxml) — safe XML parsing
|
|
92
|
+
- [mcp](https://github.com/modelcontextprotocol/python-sdk) — MCP protocol
|
|
93
|
+
|
|
94
|
+
## Performance notes
|
|
95
|
+
|
|
96
|
+
- Processing time: 3–5 minutes per page on CPU
|
|
97
|
+
- oemer output may vary slightly between runs — do not assert on exact XML equality
|
|
98
|
+
- The generated MusicXML is functionally correct but may not pass strict schema validation
|
|
99
|
+
|
|
100
|
+
## System requirements
|
|
101
|
+
|
|
102
|
+
- Python 3.11+
|
|
103
|
+
- No system libraries required (ONNX Runtime is bundled via pip)
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# omr-mcp — Technical Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
omr-mcp is a stateless MCP server that converts sheet music images to MusicXML using the oemer deep-learning OMR engine. Every tool call is independent; no state is retained between calls.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Component Diagram
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
┌───────────────────────────────────────────────────────────┐
|
|
13
|
+
│ MCP Client │
|
|
14
|
+
│ (Claude Desktop / web app) │
|
|
15
|
+
└──────────────────────────┬────────────────────────────────┘
|
|
16
|
+
│ stdio (JSON-RPC)
|
|
17
|
+
┌──────────────────────────▼────────────────────────────────┐
|
|
18
|
+
│ server.py │
|
|
19
|
+
│ │
|
|
20
|
+
│ list_tools() → Tool definitions (5 tools) │
|
|
21
|
+
│ call_tool(name, args) → dispatches to engine functions │
|
|
22
|
+
│ _detect_input_format → "path" | "base64" │
|
|
23
|
+
│ main() → stdio_server() entry point │
|
|
24
|
+
└────────────┬──────────────────────────┬───────────────────┘
|
|
25
|
+
│ │
|
|
26
|
+
┌────────────▼────────────┐ ┌──────────▼───────────────────┐
|
|
27
|
+
│ omr_engine.py │ │ utils.py │
|
|
28
|
+
│ │ │ │
|
|
29
|
+
│ recognize_image() │ │ validate_image_path() │
|
|
30
|
+
│ recognize_image_to_ │ │ decode_base64_image() │
|
|
31
|
+
│ file() │ │ get_image_info() │
|
|
32
|
+
│ recognize_images() │ │ format_file_size() │
|
|
33
|
+
│ _merge_musicxml_pages()│ └──────────────────────────────┘
|
|
34
|
+
│ _run_oemer() │
|
|
35
|
+
│ _extract_musicxml_ │
|
|
36
|
+
│ metadata() │
|
|
37
|
+
└────────────┬────────────┘
|
|
38
|
+
│
|
|
39
|
+
┌────────────▼────────────┐
|
|
40
|
+
│ oemer library │
|
|
41
|
+
│ oemer.generate(path) │
|
|
42
|
+
│ UNet + SVM models │
|
|
43
|
+
│ ONNX Runtime inference │
|
|
44
|
+
└─────────────────────────┘
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Module Responsibilities
|
|
50
|
+
|
|
51
|
+
### `server.py`
|
|
52
|
+
|
|
53
|
+
Entry point and MCP protocol layer. Contains no business logic.
|
|
54
|
+
|
|
55
|
+
- Registers five tools with their JSON schemas
|
|
56
|
+
- Dispatches `call_tool` to the appropriate engine function
|
|
57
|
+
- Handles format detection: determines whether an input is a file path or base64 string via `_detect_input_format`
|
|
58
|
+
- Decodes base64 input to a temporary file before passing to the engine
|
|
59
|
+
- Converts engine results and exceptions into `TextContent` JSON responses
|
|
60
|
+
- Starts the server with `mcp.server.stdio.stdio_server()`
|
|
61
|
+
|
|
62
|
+
**Format detection heuristic in `_detect_input_format`:**
|
|
63
|
+
- If the string starts with `data:` → `"base64"`
|
|
64
|
+
- If the string is longer than 500 chars and contains no `/` or `\` path separators → `"base64"`
|
|
65
|
+
- Otherwise → `"path"`
|
|
66
|
+
|
|
67
|
+
### `omr_engine.py`
|
|
68
|
+
|
|
69
|
+
Core processing logic. No MCP imports.
|
|
70
|
+
|
|
71
|
+
| Function | Description |
|
|
72
|
+
|----------|-------------|
|
|
73
|
+
| `recognize_image(image_path)` | Run oemer on a single image; return MusicXML string + metadata |
|
|
74
|
+
| `recognize_image_to_file(input_path, output_path)` | Same as above but write MusicXML to disk |
|
|
75
|
+
| `recognize_images(image_paths)` | Process multiple pages; merge into one MusicXML document |
|
|
76
|
+
| `_run_oemer(image_path)` | Call `oemer.generate(image_path)`; return path to output MusicXML file |
|
|
77
|
+
| `_merge_musicxml_pages(pages)` | Stitch per-page MusicXML documents into one |
|
|
78
|
+
| `_extract_musicxml_metadata(xml)` | Regex-based extraction of stave count and measure count |
|
|
79
|
+
|
|
80
|
+
### `utils.py`
|
|
81
|
+
|
|
82
|
+
Input validation and helper utilities. No engine logic.
|
|
83
|
+
|
|
84
|
+
| Function | Description |
|
|
85
|
+
|----------|-------------|
|
|
86
|
+
| `validate_image_path(path)` | Check file exists, extension is PNG/JPG/JPEG, size ≤ 50 MB |
|
|
87
|
+
| `decode_base64_image(data, output_path)` | Decode base64 (strips `data:` prefix), write to temp file, verify with Pillow |
|
|
88
|
+
| `get_image_info(path)` | Return width, height, format, mode, file size via Pillow |
|
|
89
|
+
| `format_file_size(bytes)` | Human-readable string (KB / MB) |
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## Data Flow
|
|
94
|
+
|
|
95
|
+
### Single image recognition
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
call_tool("recognize_sheet", {image_path: "/path/scan.png"})
|
|
99
|
+
│
|
|
100
|
+
├─ server.py: _detect_input_format → "path"
|
|
101
|
+
├─ utils.py: validate_image_path ──→ error if invalid
|
|
102
|
+
├─ omr_engine.py: recognize_image
|
|
103
|
+
│ ├─ _run_oemer(image_path) → oemer.generate → "/tmp/oemer_out/score.xml"
|
|
104
|
+
│ ├─ read MusicXML from output file
|
|
105
|
+
│ └─ _extract_musicxml_metadata → {staves_detected, measures}
|
|
106
|
+
└─ return {"musicxml": "...", "metadata": {...}}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Base64 image input
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
call_tool("recognize_sheet", {image_base64: "<data>", mime_type: "image/jpeg"})
|
|
113
|
+
│
|
|
114
|
+
├─ server.py: _detect_input_format → "base64"
|
|
115
|
+
├─ utils.py: decode_base64_image → /tmp/omr_XXXXX.jpg
|
|
116
|
+
└─ (same as above from validate_image_path onwards)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Multi-page recognition
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
call_tool("recognize_sheets", {image_paths: ["/p1.png", "/p2.png"]})
|
|
123
|
+
│
|
|
124
|
+
├─ omr_engine.py: recognize_images
|
|
125
|
+
│ ├─ recognize_image("/p1.png") → musicxml_1
|
|
126
|
+
│ ├─ recognize_image("/p2.png") → musicxml_2
|
|
127
|
+
│ └─ _merge_musicxml_pages([musicxml_1, musicxml_2])
|
|
128
|
+
│ ├─ Parse each page with defusedxml.ElementTree
|
|
129
|
+
│ ├─ For each subsequent page:
|
|
130
|
+
│ │ find max measure number in base document
|
|
131
|
+
│ │ append page's measures, renumbering from (max + 1)
|
|
132
|
+
│ └─ Return merged XML string
|
|
133
|
+
└─ return {"musicxml": "...", "page_count": 2, "metadata": {...}}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Multi-Page Merge Algorithm
|
|
139
|
+
|
|
140
|
+
Pages are merged by appending measures from each successive page to the corresponding part in the base document.
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
base_doc = parse(page_1_xml)
|
|
144
|
+
for page in pages[1:]:
|
|
145
|
+
page_doc = parse(page_xml)
|
|
146
|
+
for part_index, part in enumerate(page_doc.parts):
|
|
147
|
+
base_part = base_doc.parts[part_index] # matched by index
|
|
148
|
+
last_num = max(m.number for m in base_part.measures)
|
|
149
|
+
for measure in part.measures:
|
|
150
|
+
measure.number += last_num # renumber sequentially
|
|
151
|
+
base_part.append(measure)
|
|
152
|
+
return serialize(base_doc)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Parts are matched by index (not by name), so the number of parts must be consistent across pages.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## oemer Integration
|
|
160
|
+
|
|
161
|
+
`_run_oemer(image_path)` calls `oemer.generate(image_path)` directly as a library function (not a subprocess). oemer:
|
|
162
|
+
|
|
163
|
+
1. Runs staffline detection (UNet segmentation model)
|
|
164
|
+
2. Runs symbol segmentation (UNet model)
|
|
165
|
+
3. Classifies symbols (SVM)
|
|
166
|
+
4. Writes a MusicXML file to a subdirectory near the input image
|
|
167
|
+
5. Returns the path to that file
|
|
168
|
+
|
|
169
|
+
Model checkpoints are downloaded on first call (~100 MB) and cached in `~/.cache/oemer/` (or the oemer package directory). Subsequent calls use the cached models.
|
|
170
|
+
|
|
171
|
+
Metadata (stave count, measure count) is extracted by scanning the returned MusicXML with regexes rather than a full parse, for speed.
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## Error Handling
|
|
176
|
+
|
|
177
|
+
All errors are returned as JSON text content rather than raising MCP protocol errors.
|
|
178
|
+
|
|
179
|
+
```json
|
|
180
|
+
{"error": "File not found: /path/scan.png", "error_code": "FILE_NOT_FOUND"}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
| Scenario | Code |
|
|
184
|
+
|----------|------|
|
|
185
|
+
| File not found | `FILE_NOT_FOUND` |
|
|
186
|
+
| Unsupported image format | `INVALID_FORMAT` |
|
|
187
|
+
| Image too large (> 50 MB) | `INVALID_INPUT` |
|
|
188
|
+
| Base64 decode failure | `INVALID_INPUT` |
|
|
189
|
+
| Pillow cannot open image | `INVALID_INPUT` |
|
|
190
|
+
| oemer processing error | `PROCESSING_FAILED` |
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Key Dependencies
|
|
195
|
+
|
|
196
|
+
| Package | Role |
|
|
197
|
+
|---------|------|
|
|
198
|
+
| `oemer` | OMR engine — UNet + SVM, ONNX Runtime |
|
|
199
|
+
| `Pillow` | Image validation and info extraction |
|
|
200
|
+
| `defusedxml` | Safe XML parsing (prevents XXE attacks) |
|
|
201
|
+
| `mcp` | MCP protocol SDK |
|
|
202
|
+
|
|
203
|
+
No system libraries are required. ONNX Runtime is bundled with the oemer pip package.
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## Server Startup
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
# server.py main()
|
|
211
|
+
async def _run():
|
|
212
|
+
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
|
|
213
|
+
await app.run(read_stream, write_stream, app.create_initialization_options())
|
|
214
|
+
|
|
215
|
+
asyncio.run(_run())
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
The server process is single-threaded. Each tool call is handled sequentially. oemer processing blocks the event loop for the duration (3–5 min per page on CPU); this is acceptable for Phase 1 as MCP clients handle timeouts.
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## File Layout
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
omr-mcp/
|
|
226
|
+
├── pyproject.toml
|
|
227
|
+
├── src/
|
|
228
|
+
│ └── omr_mcp/
|
|
229
|
+
│ ├── __init__.py
|
|
230
|
+
│ ├── server.py ← MCP layer (tool registration, dispatch)
|
|
231
|
+
│ ├── omr_engine.py ← Business logic (oemer, merge)
|
|
232
|
+
│ └── utils.py ← Input validation, base64, image info
|
|
233
|
+
└── tests/
|
|
234
|
+
├── test_server.py
|
|
235
|
+
├── test_omr.py
|
|
236
|
+
├── test_utils.py
|
|
237
|
+
└── test_samples/
|
|
238
|
+
└── pdmx_satb_samples/ ← PNG + MXL fixture pairs
|
|
239
|
+
```
|