music21-mcp-server

Bright-L01/music21-mcp-server

3.3

If you are the rightful owner of music21-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

The Music21 MCP Server is a robust and production-ready server that leverages the Music21 library to provide advanced music analysis capabilities through a simple API.

Music21 MCP Server

Python 3.10+ License: MIT Code style: black MCP

A production-ready Model Context Protocol (MCP) server that provides comprehensive music analysis and generation capabilities through the music21 library.

๐ŸŽต Features

Core Analysis Tools

  • Import & Export: Support for MusicXML, MIDI, ABC, Lilypond, and music21 corpus
  • Key Analysis: Multiple algorithms (Krumhansl, Aarden, Bellman-Budge)
  • Harmony Analysis: Chord progressions, Roman numerals, cadence detection
  • Voice Leading: Parallel detection, voice crossing analysis, smoothness metrics
  • Pattern Recognition: Melodic, rhythmic, and harmonic pattern identification

Advanced Capabilities

  • Harmonization: Bach chorale and jazz style harmonization
  • Counterpoint Generation: Species counterpoint (1-5) following strict rules
  • Style Imitation: Learn and generate music in the style of input pieces
  • Score Manipulation: Transposition, time stretching, orchestration

Production Features

  • Circuit Breakers: Automatic failure recovery within 60 seconds
  • Rate Limiting: Token bucket algorithm for resource protection
  • Memory Management: Automatic garbage collection and score caching
  • Health Monitoring: Real-time metrics and health checks
  • Graceful Shutdown: Clean resource cleanup on termination

๐Ÿš€ Quick Start

Installation

# Clone the repository
git clone https://github.com/yourusername/music21-mcp-server.git
cd music21-mcp-server

# Install with pip
pip install -e .

# Or with Poetry
poetry install

Basic Usage

# Start the server
python -m music21_mcp.server

# Or use the CLI
music21-mcp serve

Docker

# Build image
docker build -t music21-mcp-server .

# Run container
docker run -p 8000:8000 music21-mcp-server

๐Ÿ“– API Reference

Import Score

Import a musical score from various sources.

{
  "tool": "import_score",
  "arguments": {
    "score_id": "bach_invention_1",
    "source": "bach/inventions/invent1",
    "source_type": "corpus"
  }
}

Analyze Key

Determine the key of a musical piece.

{
  "tool": "analyze_key",
  "arguments": {
    "score_id": "bach_invention_1",
    "algorithm": "krumhansl"
  }
}

Generate Counterpoint

Create counterpoint following classical rules.

{
  "tool": "generate_counterpoint",
  "arguments": {
    "cantus_firmus_id": "cantus",
    "output_score_id": "counterpoint_result",
    "species": 1,
    "mode": "major"
  }
}

๐Ÿ—๏ธ Architecture

The server follows a modular architecture with clear separation of concerns:

src/music21_mcp/
โ”œโ”€โ”€ server.py          # Main FastMCP server with resilience features
โ”œโ”€โ”€ tools/             # Individual tool implementations
โ”‚   โ”œโ”€โ”€ import_tool.py
โ”‚   โ”œโ”€โ”€ key_analysis_tool.py
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ core/              # Core analyzers and algorithms
โ”‚   โ”œโ”€โ”€ harmonic_analyzer.py
โ”‚   โ”œโ”€โ”€ melodic_analyzer.py
โ”‚   โ””โ”€โ”€ ...
โ””โ”€โ”€ resilience.py      # Production resilience patterns

Resilience Patterns

  • Circuit Breaker: Prevents cascading failures with configurable thresholds
  • Rate Limiter: Token bucket algorithm with burst support
  • Resource Pool: Connection pooling with health checks
  • Memory Guard: Automatic cleanup at configurable thresholds

๐Ÿงช Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=src/music21_mcp

# Run specific test categories
pytest -m unit          # Unit tests only
pytest -m integration   # Integration tests
pytest -m "not slow"    # Skip slow tests

Test Coverage

  • Unit tests for all tools and analyzers
  • Integration tests for tool interactions
  • Performance tests for stress scenarios
  • Property-based tests for algorithmic correctness

๐Ÿšข Deployment

Environment Variables

MUSIC21_MCP_HOST=0.0.0.0
MUSIC21_MCP_PORT=8000
MUSIC21_MCP_MAX_SCORES=100
MUSIC21_MCP_MEMORY_LIMIT_MB=2048
MUSIC21_MCP_LOG_LEVEL=INFO

Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: music21-mcp-server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: music21-mcp
  template:
    spec:
      containers:
      - name: server
        image: music21-mcp-server:latest
        resources:
          requests:
            memory: "2Gi"
            cpu: "1"
          limits:
            memory: "4Gi"
            cpu: "2"

๐Ÿ“Š Monitoring

Health Check Endpoint

GET /health

Metrics Endpoint

GET /metrics  # Prometheus format

Example Metrics

  • music21_requests_total: Total requests processed
  • music21_request_duration_seconds: Request latency histogram
  • music21_scores_in_memory: Number of scores currently cached
  • music21_circuit_breaker_state: Circuit breaker states

๐Ÿค Contributing

We welcome contributions! Please see our for details.

Development Setup

# Install development dependencies
poetry install --with dev

# Run code formatters
black src tests
ruff check src tests --fix

# Run type checking
mypy src

๐Ÿ“„ License

This project is licensed under the MIT License - see the file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ฌ Support


Built with โค๏ธ for the music and AI community