通过四种不同的接口进行专业音乐分析 - MCP服务器、HTTP API、命令行工具和Python库。基于强大的music21库构建,具有协议无关架构以实现最大可靠性。
根据2025年的研究显示,MCP的成功率为40-50%,本项目提供了通往相同强大music21分析功能的多种途径:
# 安装包
pip install music21-mcp-server
# 启动服务
music21-mcp-server --mode mcp # 对于Claude桌面
music21-mcp-server --mode http # 在localhost:8000上的REST API
music21-mcp-server --mode cli # 交互式CLI
# 克隆仓库
git clone https://github.com/brightlikethelight/music21-mcp-server.git
cd music21-mcp-server
# 使用UV安装(推荐)
curl -LsSf https://astral.sh/uv/install.sh | sh
uv sync
# 或使用pip
pip install -r requirements.txt
# 配置music21语料库
python -m music21.configure
python -m music21_mcp.launcher
# 启动MCP服务器
python -m music21_mcp.launcher mcp
# 配置Claude桌面:
# ~/.config/claude-desktop/config.json
{
"mcpServers": {
"music21-analysis": {
"command": "python",
"args": ["-m", "music21_mcp.server_minimal"],
"env": {
"PYTHONPATH": "/path/to/music21-mcp-server/src"
}
}
}
}
# 启动HTTP API服务器
python -m music21_mcp.launcher http
# 打开:http://localhost:8000
# API文档:http://localhost:8000/docs
# 示例用法:
curl -X POST "http://localhost:8000/scores/import" \
-H "Content-Type: application/json" \
-d '{"score_id": "chorale", "source": "bach/bwv66.6", "source_type": "corpus"}'
curl -X POST "http://localhost:8000/analysis/key" \
-H "Content-Type: application/json" \
-d '{"score_id": "chorale"}'
# 显示CLI状态
python -m music21_mcp.launcher cli status
# 导入并分析一个巴赫合唱
python -m music21_mcp.launcher cli import chorale bach/bwv66.6 corpus
python -m music21_mcp.launcher cli key-analysis chorale
python -m music21_mcp.launcher cli harmony chorale roman
# 列出所有工具
python -m music21_mcp.launcher cli tools
from music21_mcp.adapters import create_sync_analyzer
# 创建分析器
analyzer = create_sync_analyzer()
# 导入并分析
analyzer.import_score("chorale", "bach/bwv66.6", "corpus")
key_result = analyzer.analyze_key("chorale")
harmony_result = analyzer.analyze_harmony("chorale", "roman")
print(f"调性:{key_result}")
print(f"和声:{harmony_result}")
# 快速综合分析
analysis = analyzer.quick_analysis("chorale")
# 现实测试套件(核心95%,适配器5%)
python tests/run_reality_tests.py
# 核心music21测试(必须通过)
python -m pytest tests/core/ -v
# MCP适配器测试(可能失败 - 这是预期的)
python -m pytest tests/adapters/ -v
# 安装开发依赖
uv sync --dev
# 设置预提交钩子
pre-commit install
# 运行代码检查
ruff check src/
ruff format src/
# 类型检查
mypy src/
核心价值层:
├── services.py # Music21分析服务(协议无关)
└── tools/ # 13个音乐分析工具
协议适配层:
├── adapters/mcp_adapter.py # MCP协议隔离
├── adapters/http_adapter.py # HTTP/REST API
├── adapters/cli_adapter.py # 命令行界面
└── adapters/python_adapter.py # 直接Python访问
统一入口点:
└── launcher.py # 所有接口的单一入口点
| 接口 | 成功率 | 最适合 |
|---|---|---|
| MCP | 40-50% | AI助手集成 |
| HTTP | 95%+ | 网络应用 |
| CLI | 99%+ | 自动化及脚本 |
| Python | 99%+ | 直接编程 |
# 可选配置
export MUSIC21_MCP_LOG_LEVEL=INFO
export MUSIC21_MCP_CACHE_SIZE=100
export MUSIC21_MCP_TIMEOUT=30
# 配置语料库路径(一次性设置)
python -m music21.configure
# CLI方法
python -m music21_mcp.launcher cli import chorale bach/bwv66.6 corpus
python -m music21_mcp.launcher cli key-analysis chorale
# Python方法
analyzer = create_sync_analyzer()
analyzer.import_score("chorale", "bach/bwv66.6", "corpus")
print(analyzer.analyze_key("chorale"))
# 对于Claude桌面
python -m music21_mcp.launcher mcp
# 对于网络开发
python -m music21_mcp.launcher http
# 对于命令行工作
python -m music21_mcp.launcher cli status
之前的商业版本已被简化以提高可靠性:
git checkout -b feature/amazing-featurepython tests/run_reality_tests.pygit commit -m '添加惊人的功能'git push origin feature/amazing-featureMIT许可证 - 详情见LICENSE文件。
选择适合您的接口。所有接口都提供相同强大的music21分析能力! 🎵