
Spotify
STDIOKotlin MCP server for controlling Spotify playback, managing playlists, and retrieving user information.
Kotlin MCP server for controlling Spotify playback, managing playlists, and retrieving user information.
A Kotlin implementation of a Model Context Protocol (MCP) server that integrates with the Spotify Web API. This server provides tools for controlling Spotify playback, managing playlists, and retrieving user information through a standardized interface.
Clone the repository:
git clone https://github.com/yourusername/kotlin-mcp-server.git cd kotlin-mcp-server
Set up Spotify Developer credentials:
.env
file in the project root with the following content:
SPOTIFY_CLIENT_ID = "your-client-id"
SPOTIFY_CLIENT_SECRET = "your-client-secret"
For example:
SPOTIFY_CLIENT_ID = "d4k32j4kl32j4k23j4k23j4k32if"
SPOTIFY_CLIENT_SECRET = "5gd6f56fdsd6g5a6d7sd5656cvbx"
The server can be run in two modes:
Standard I/O Mode (default):
Uncomment the appropriate line in Main.kt
to run the server using Ktor with stdio:
// In Main.kt fun main() { runMcpServerUsingStdio() // runSseMcpServerUsingKtorPlugin(8080) }
This will generate an executable at:./gradlew installDist
build/install/kotlin-mcp-server/bin/kotlin-mcp-server
Connecting with VS Code's Copilot: VS Code's Copilot has a built-in MCP client that can connect to the server when running in Standard I/O mode. You can use other mcp clients like claude desktop but Code's Copilot is the easiest that I found to use. To use it:
Configure VS Code to connect to the MCP server:
{ "security.workspace.trust.untrustedFiles": "open", "terminal.integrated.fontFamily": "MesloLGS Nerd Font", "mcp": { "inputs": [], "servers": { "spotify-mcp-server": { "command": "/Users/karis/IdeaProjects/kotlin-mcp-server/build/install/kotlin-mcp-server/bin/kotlin-mcp-server", "args": [], "env": {} } } }, "git.autofetch": true }
In VS Code, open the Copilot chat panel and pick Agent Mode.
Copilot will automatically detect and connect to the MCP server
You can now interact with the Spotify tools through the Copilot interface
Server-Sent Events (SSE) Mode:
Uncomment the appropriate line in Main.kt
to run the server using Ktor with SSE:
// In Main.kt fun main() { // runMcpServerUsingStdio() runSseMcpServerUsingKtorPlugin(8080) }
Then run the application and connect to http://localhost:8080/sse
using an MCP inspector.
src/main/kotlin/mcpserver/spotify/
: Core Spotify API integration
auth/
: Authentication and token managementservices/
: Service implementations for Spotify API endpointsutils/
: Utility functions and error handlingsrc/main/kotlin/mcpserver/spotifymcp/
: MCP server implementation
tools/
: Tool implementations for the MCP serverThe server provides the following tools for interacting with Spotify:
The project uses a custom SpotifyResult<T, E>
sealed class for error handling:
SpotifyResult.Success<T>
: Contains successful response dataSpotifyResult.Failure<E>
: Contains an exception with error detailsUse the safeSpotifyApiCall
function to make API calls that return SpotifyResult
objects:
val result = safeSpotifyApiCall<ResponseType, ErrorType> { // API call code here }
To add a new tool to the MCP server:
mcpserver/spotifymcp/tools
packageServer
instance and any required servicesserver.addTool()
to register the tool with a name, description, and input schema# Run all tests ./gradlew test # Run a specific test class ./gradlew test --tests "mcpserver.spotify.services.playerservice.SpotifyPlayerServiceImplTest" # Run a specific test method ./gradlew test --tests "mcpserver.spotify.services.playerservice.SpotifyPlayerServiceImplTest.playTrack should return success when API call succeeds"
The project uses JUnit 5 for testing. Tests should be placed in the src/test/kotlin
directory, mirroring the structure
of the main source code.
For HTTP client mocking, use Ktor's MockEngine
to simulate HTTP responses. When testing services that depend on
external APIs, create mock implementations of dependencies and test both success and failure scenarios.
Contributions are welcome! Please feel free to submit a Pull Request.