#
tokens: 1849/50000 5/5 files
lines: on (toggle) GitHub
raw markdown copy reset
# Directory Structure

```
├── .gitignore
├── package-lock.json
├── package.json
├── README.md
├── src
│   └── index.ts
├── tsconfig.json
└── tsconfig.tsbuildinfo
```

# Files

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
1 | build
2 | node_modules
3 | .env
```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
 1 | # Spotify-Claude MCP Server
 2 | A tool that connects to the Spotify API and uses Claude as an AI assistant for enhanced music discovery and interaction.
 3 | 
 4 | ## Overview
 5 | This project integrates Claude AI with the Spotify API through a Model Control Protocol (MCP) server. It allows users to interact with their Spotify account using natural language queries processed by Claude.
 6 | 
 7 | ## Features
 8 | Currently the claude can get the Artist information using the SpotifyAPI. This can be extended for other queries, such as getting top tracks, or genres, creating playlists, or getting user profile data
 9 | 
10 | ## Prerequisites
11 |  - Spotify Developer Account
12 |  - Spotify API credentials (Client ID and Client Secret)
13 |  - Claude Desktop
14 | 
15 | 
16 | ## Installation
17 | 
18 | 1. Clone the repository:
19 | ```
20 | git clone https://github.com/DivyamAgg24/SpotifyMCP.git
21 | cd SpotifyMCP
22 | ```
23 | 
24 | 2. Install dependencies:
25 | ```
26 | npm install
27 | ```
28 | 
29 | 3. Set up environment variables:
30 | Create a .env file in the root directory with the following variables:
31 | ```
32 | SPOTIFY_CLIENT_ID=your_spotify_client_id
33 | SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
34 | SPOTIFY_ACCESS_TOKEN=access_token_obtained_from_clientId_and_clientSecret
35 | ```
36 | 
37 | ## Configuration
38 | 
39 | Register your app in the Spotify Developer Dashboard
40 | Add http://localhost:8888/callback as a Redirect URI in your Spotify app settings
41 | Copy the Client ID and Client Secret to your .env file
42 | 
43 | Usage
44 | 
45 | 1. Build the project
46 | ```
47 | tsc -b
48 | ```
49 | 
50 | 2. Add MCP Server configuration in Claude config file
51 | 
52 | 3. Prompt Claude about the name of the artist
53 | 
54 | ## Tool Functions
55 | The MCP server exposes the following function to Claude:
56 | 
57 | get_artist(name): Retrieves information about an artist
58 | 
59 | Example Interactions
60 | ```
61 | User: "What is the artist name?"
62 | Claude: [Uses get_artist function] "The artist name is Pitbull."
63 | ```
64 | 
65 | MCP Server: Handles communication between Claude and the Spotify API
66 | Spotify API Client: Manages authentication and API requests to Spotify
67 | Claude Integration: Processes natural language and determines which functions to call
```

--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------

```json
 1 | {
 2 |     "compilerOptions": {
 3 |       "target": "ES2022",
 4 |       "module": "Node16",
 5 |       "moduleResolution": "Node16",
 6 |       "outDir": "./build",
 7 |       "rootDir": "./src",
 8 |       "strict": true,
 9 |       "esModuleInterop": true,
10 |       "skipLibCheck": true,
11 |       "forceConsistentCasingInFileNames": true
12 |     },
13 |     "include": ["src/**/*"],
14 |     "exclude": ["node_modules"]
15 |   }
```

--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------

```json
 1 | {
 2 |     "name": "spotifymcp",
 3 |     "version": "1.0.0",
 4 |     "type": "module",
 5 |     "scripts": {
 6 |         "build": "tsc",
 7 |         "test": "echo \"Error: no test specified\" && exit 1"
 8 |     },
 9 |     "keywords": [],
10 |     "author": "",
11 |     "license": "ISC",
12 |     "description": "",
13 |     "dependencies": {
14 |         "@modelcontextprotocol/sdk": "^1.7.0",
15 |         "@types/spotify-web-api-node": "^5.0.11",
16 |         "dotenv": "^16.4.7",
17 |         "spotify-web-api-node": "^5.0.2",
18 |         "zod": "^3.24.2"
19 |     },
20 |     "devDependencies": {
21 |         "@types/node": "^22.13.10",
22 |         "typescript": "^5.8.2"
23 |     }
24 | }
25 | 
```

--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------

```typescript
 1 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
 2 | import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
 3 | import dotenv from 'dotenv';
 4 | 
 5 | import { z } from "zod";
 6 | 
 7 | dotenv.config()
 8 | const server = new McpServer({
 9 |     name: "spotify",
10 |     version: "1.0.0",
11 | });
12 | 
13 | const accessToken = "BQDx7I7jtZBsLKZ7Fsoo4HVH4rQI_kAcFlq_S-zo20nT1UpNcbR9YShjl8QRAAd9Vb1eldP6SHtikO-l12kiv9ZrUEhYHQl7PcR6-MGHlTUNYXRifeXz5G5vX4G017MTpB9dUer3ykI"
14 | 
15 | server.tool(
16 |     "get_artist",
17 |     "Get Artist name",
18 |     {
19 |       method: z.string().min(3).describe("Get or post request")
20 |     },
21 |     async ({ method }) => {
22 |       const methodUpper = method.toUpperCase();
23 |       const artistData = await fetch("https://api.spotify.com/v1/artists?ids=0TnOYISbd1XYRBk9myaseg", {
24 |         headers: {
25 |           Authorization: `Bearer ${accessToken}`,
26 |         },
27 |         method
28 |     })
29 |     
30 |     if (!artistData) {
31 |     return {
32 |         content: [
33 |         {
34 |             type: "text",
35 |             text: "Failed to retrieve tracks",
36 |         },
37 |         ],
38 |     };
39 |     }
40 |     const allArtistsData = await artistData.json()
41 |     console.log(allArtistsData)
42 |     const data = allArtistsData.artists || [];
43 |     if (data.length===0) {
44 |     return {
45 |         content: [
46 |         {
47 |             type: "text",
48 |             text: `No artists`,
49 |         },
50 |         ],
51 |     };
52 |     }
53 |     let names = []
54 |     for(let i = 0; i<data.length; i++){
55 |         names.push(data[i].name)
56 |     }
57 | 
58 |     const finalOutput = `Artist name is ${names}`;
59 | 
60 |     return {
61 |         content: [
62 |             {
63 |                 type: "text",
64 |                 text: finalOutput,
65 |             },
66 |         ],
67 |         };
68 |     },
69 | )
70 | 
71 | 
72 | 
73 | async function main() {
74 |     const transport = new StdioServerTransport();
75 |     await server.connect(transport);
76 |     console.error("Spotify MCP running on studio");
77 |   }
78 |   
79 |   main().catch((error) => {
80 |     console.error("Fatal error in main():", error);
81 |     process.exit(1);
82 |   });
83 | 
```