Spotify音乐搜索服务器

一个为AI助手提供Spotify音乐目录访问的模型上下文协议(MCP)服务器,支持歌曲、艺术家搜索和详细曲目信息获取。

  • by anuragkothare
  • Go
  • 本地部署
  • 更新于
在 GitHub 查看 anuragkothare/spotify_mcp_server

使用说明

Spotify MCP服务器

A. 模型上下文协议(MCP)服务器 它为人工智能助手提供了访问Spotify音乐目录的权限。

🎯 这是什么?

此MCP服务器使AI助手能够通过三个工具在Spotify上搜索音乐:

  • 搜索轨迹:按名称/艺术家查找歌曲
  • 搜索艺术家:查找受欢迎程度评分的艺术家
  • get_track:获取详细的赛道信息

📋 先决条件

🚀 快速开始

1.获取Spotify凭据

  1. 首选 Spotify开发者仪表板
  2. 创建新应用程序
  3. 复制您的 客户端ID 和 客户端密钥

2.设置和运行

# Clone and setup
git clone https://github.com/anuragkothare/spotify_mcp_server.git
cd spotify-mcp-server

# Configure environment
cp .env.example .env
# Edit .env with your Spotify credentials:
# SPOTIFY_CLIENT_ID=your_client_id_here
# SPOTIFY_CLIENT_SECRET=your_client_secret_here

# Run with Docker (recommended)
docker-compose up -d

# Or run locally
go mod tidy
go run ./cmd/server

3.测试

# Health check
curl http://localhost:8080/health

# List available tools
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

# Search for tracks
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/call",
    "params": {
      "name": "search_tracks",
      "arguments": {"query": "bohemian rhapsody", "limit": 5}
    }
  }'

⚙️ 配置

所需的环境变量:

SPOTIFY_CLIENT_ID=your_spotify_client_id
SPOTIFY_CLIENT_SECRET=your_spotify_client_secret

可选:

SPOTIFY_REDIRECT_URI=http://localhost:8080/callback
SERVER_PORT=8080
LOG_LEVEL=info

🐳 Docker命令

# Development
docker-compose up -d          # Start services
docker-compose logs -f        # View logs
docker-compose down           # Stop services

# Production
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

# Useful commands
docker-compose ps             # Check status
docker-compose exec spotify-mcp sh  # Access container

🎵 可用的MCP工具

搜索轨迹

{
  "name": "search_tracks",
  "arguments": {
    "query": "song name or artist",
    "limit": 10
  }
}

搜索艺术家

{
  "name": "search_artists",
  "arguments": {
    "query": "artist name",
    "limit": 10
  }
}

get_track

{
  "name": "get_track",
  "arguments": {
    "track_id": "spotify_track_id"
  }
}

🛠️ 项目结构

spotify-mcp-server/
├── cmd/server/           # Main application
├── internal/
│   ├── config/          # Configuration
│   ├── mcp/            # MCP protocol implementation
│   ├── spotify/        # Spotify API client
│   └── handlers/       # HTTP handlers
├── configs/            # Config files
├── Dockerfile          # Container build
├── docker-compose.yml  # Service orchestration
├── .env.example        # Environment template
└── Makefile           # Build commands

🚨 故障排除

“invalid_client”错误

  • 检查你的 .env 文件具有实际的Spotify凭据(不是占位符)
  • 验证凭据是否有效:在以下位置进行测试 Spotify控制台

“找不到.env文件”

  • 确保 .env 存在于项目根目录中
  • 从包含以下内容的目录运行 docker-compose.yml

Docker问题

# Clean up and restart
docker-compose down --volumes --remove-orphans
docker-compose up -d --build

# Check logs
docker-compose logs spotify-mcp

端口冲突

# Check what's using port 8080
lsof -i :8080

# Change port in .env
SERVER_PORT=8081

🔗 整合

Python示例

import requests

def search_tracks(query, limit=5):
    response = requests.post('http://localhost:8080/mcp', json={
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
            "name": "search_tracks",
            "arguments": {"query": query, "limit": limit}
        }
    })
    return response.json()

# Usage
results = search_tracks("hello adele")
print(results)

Claude桌面集成

添加到您的Claude Desktop配置中:

{
  "mcpServers": {
    "spotify": {
      "command": "docker",
      "args": [
        "run",
        "-p",
        "8080:8080",
        "--env-file",
        ".env",
        "spotify-mcp-server"
      ],
      "env": {
        "SPOTIFY_CLIENT_ID": "your_client_id",
        "SPOTIFY_CLIENT_SECRET": "your_client_secret"
      }
    }
  }
}

📄 许可证

MIT许可证-有关详细信息,请参阅许可证文件。


快速设置: 将您的Spotify凭据复制到 .env 然后跑 docker-compose up -d 🎵