forked from marcelmarais/spotify-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.ts
More file actions
610 lines (562 loc) · 16.1 KB
/
Copy pathplay.ts
File metadata and controls
610 lines (562 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import { z } from 'zod';
import type { SpotifyHandlerExtra, tool } from './types.js';
import { handleSpotifyRequest, spotifyFetch } from './utils.js';
/**
* Ensures there is an active Spotify device before attempting playback.
* If no device is currently active, transfers playback to the first available
* device and waits briefly for it to become ready.
* Returns the device_id to use, or empty string if none found.
*/
async function ensureActiveDevice(preferredDeviceId?: string): Promise<string> {
const data = await spotifyFetch<{
devices: Array<{ id: string; is_active: boolean; name: string }>;
}>('me/player/devices');
const devices = data?.devices ?? [];
if (devices.length === 0) {
throw new Error(
'No Spotify devices found. Open Spotify on any device first.',
);
}
// If a preferred device was specified and exists, use it
if (preferredDeviceId) {
const preferred = devices.find((d) => d.id === preferredDeviceId);
if (preferred) {
if (!preferred.is_active) {
await spotifyFetch('me/player', {
method: 'PUT',
body: { device_ids: [preferredDeviceId], play: false },
});
await new Promise((r) => setTimeout(r, 500));
}
return preferredDeviceId;
}
}
// Use the already-active device if there is one
const active = devices.find((d) => d.is_active);
if (active) return active.id;
// No active device — transfer to the first available one
const target = devices[0];
await spotifyFetch('me/player', {
method: 'PUT',
body: { device_ids: [target.id], play: false },
});
// Give Spotify a moment to register the transfer before we start playback
await new Promise((r) => setTimeout(r, 600));
return target.id;
}
const playMusic: tool<{
uri: z.ZodOptional<z.ZodString>;
context_uri: z.ZodOptional<z.ZodString>;
type: z.ZodOptional<z.ZodEnum<['track', 'album', 'artist', 'playlist']>>;
id: z.ZodOptional<z.ZodString>;
deviceId: z.ZodOptional<z.ZodString>;
device_id: z.ZodOptional<z.ZodString>;
offset: z.ZodOptional<z.ZodNumber>;
}> = {
name: 'playMusic',
description:
'Start playing a Spotify track, album, artist, or playlist. ' +
'Pass a Spotify URI (e.g. spotify:track:xxx, spotify:album:xxx) via "uri". ' +
'For albums/playlists you can also use "context_uri". ' +
'Device is selected automatically — "deviceId" or "device_id" are optional.',
schema: {
uri: z
.string()
.optional()
.describe(
'Spotify URI to play (e.g. spotify:track:xxx, spotify:album:xxx)',
),
context_uri: z
.string()
.optional()
.describe('Alias for uri — Spotify context URI for albums/playlists'),
type: z
.enum(['track', 'album', 'artist', 'playlist'])
.optional()
.describe('Type of item (only needed with id)'),
id: z
.string()
.optional()
.describe('Spotify ID of the item (use with type)'),
deviceId: z
.string()
.optional()
.describe('Spotify device ID to play on (auto-selected if omitted)'),
device_id: z.string().optional().describe('Alias for deviceId'),
offset: z
.number()
.optional()
.describe('Track offset within album/playlist (0-based)'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
// Normalize aliases
const deviceId = args.deviceId ?? args.device_id;
const resolvedUri = args.uri ?? args.context_uri;
const { type, id, offset } = args;
if (!(resolvedUri || (type && id))) {
return {
content: [
{
type: 'text',
text: 'Error: Must provide a "uri" (e.g. spotify:track:xxx) or both "type" and "id"',
isError: true,
},
],
};
}
let spotifyUri = resolvedUri;
if (!spotifyUri && type && id) {
spotifyUri = `spotify:${type}:${id}`;
}
// Infer type from URI if not provided
const resolvedType = type ?? spotifyUri?.split(':')?.[1];
try {
const activeDeviceId = await ensureActiveDevice(deviceId);
await handleSpotifyRequest(async (spotifyApi) => {
if (!spotifyUri) {
await spotifyApi.player.startResumePlayback(activeDeviceId);
return;
}
if (resolvedType === 'track') {
await spotifyApi.player.startResumePlayback(
activeDeviceId,
undefined,
[spotifyUri],
undefined,
offset,
);
} else {
// album, playlist, artist — use context_uri + optional offset
await spotifyApi.player.startResumePlayback(
activeDeviceId,
spotifyUri,
undefined,
offset !== undefined ? { position: offset } : undefined,
);
}
});
return {
content: [{ type: 'text', text: `Now playing: ${spotifyUri}` }],
};
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
return {
content: [
{ type: 'text', text: `Error playing music: ${msg}`, isError: true },
],
};
}
},
};
const pausePlayback: tool<{
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'pausePlayback',
description: 'Pause Spotify playback on the active device',
schema: {
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to pause playback on'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { deviceId } = args;
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.pausePlayback(deviceId || '');
});
return {
content: [
{
type: 'text',
text: 'Playback paused',
},
],
};
},
};
const skipToNext: tool<{
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'skipToNext',
description: 'Skip to the next track in the current Spotify playback queue',
schema: {
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to skip on'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { deviceId } = args;
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.skipToNext(deviceId || '');
});
return {
content: [
{
type: 'text',
text: 'Skipped to next track',
},
],
};
},
};
const skipToPrevious: tool<{
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'skipToPrevious',
description:
'Skip to the previous track in the current Spotify playback queue',
schema: {
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to skip on'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { deviceId } = args;
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.skipToPrevious(deviceId || '');
});
return {
content: [
{
type: 'text',
text: 'Skipped to previous track',
},
],
};
},
};
const createPlaylist: tool<{
name: z.ZodString;
description: z.ZodOptional<z.ZodString>;
public: z.ZodOptional<z.ZodBoolean>;
}> = {
name: 'createPlaylist',
description: 'Create a new playlist on Spotify',
schema: {
name: z.string().describe('The name of the playlist'),
description: z
.string()
.optional()
.describe('The description of the playlist'),
public: z
.boolean()
.optional()
.describe('Whether the playlist should be public'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { name, description, public: isPublic = false } = args;
const result = await handleSpotifyRequest(async (spotifyApi) => {
const me = await spotifyApi.currentUser.profile();
return await spotifyApi.playlists.createPlaylist(me.id, {
name,
description,
public: isPublic,
});
});
return {
content: [
{
type: 'text',
text: `Successfully created playlist "${name}"\nPlaylist ID: ${result.id}\nPlaylist URL: ${result.external_urls.spotify}`,
},
],
};
},
};
const addTracksToPlaylist: tool<{
playlistId: z.ZodString;
trackIds: z.ZodArray<z.ZodString>;
position: z.ZodOptional<z.ZodNumber>;
}> = {
name: 'addTracksToPlaylist',
description:
'Add tracks or podcast episodes to a Spotify playlist. ' +
'Accepts Spotify track IDs, episode IDs, or full Spotify URIs (e.g. spotify:episode:xxx).',
schema: {
playlistId: z.string().describe('The Spotify ID of the playlist'),
trackIds: z
.array(z.string())
.describe(
'Array of Spotify IDs or URIs to add. ' +
'Plain IDs are assumed to be tracks. ' +
'To add podcast episodes, pass full URIs: spotify:episode:{id}.',
),
position: z
.number()
.nonnegative()
.optional()
.describe('Position to insert the items (0-based index)'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { playlistId, trackIds, position } = args;
if (trackIds.length === 0) {
return {
content: [{ type: 'text', text: 'Error: No IDs provided' }],
};
}
try {
const uris = trackIds.map((id) =>
id.startsWith('spotify:') ? id : `spotify:track:${id}`,
);
// Hit /items directly: see spotifyFetch JSDoc for context.
await spotifyFetch(`playlists/${playlistId}/items`, {
method: 'POST',
body: {
uris,
...(position !== undefined ? { position } : {}),
},
});
return {
content: [
{
type: 'text',
text: `Successfully added ${trackIds.length} item${
trackIds.length === 1 ? '' : 's'
} to playlist (ID: ${playlistId})`,
},
],
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error adding items to playlist: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
},
};
const resumePlayback: tool<{
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'resumePlayback',
description: 'Resume Spotify playback on the active device',
schema: {
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to resume playback on'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { deviceId } = args;
try {
const activeDeviceId = await ensureActiveDevice(deviceId);
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.startResumePlayback(activeDeviceId);
});
return { content: [{ type: 'text', text: 'Playback resumed' }] };
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
return {
content: [
{
type: 'text',
text: `Error resuming playback: ${msg}`,
isError: true,
},
],
};
}
},
};
const addToQueue: tool<{
uri: z.ZodOptional<z.ZodString>;
type: z.ZodOptional<z.ZodEnum<['track', 'album', 'artist', 'playlist']>>;
id: z.ZodOptional<z.ZodString>;
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'addToQueue',
description: 'Adds a track, album, artist or playlist to the playback queue',
schema: {
uri: z
.string()
.optional()
.describe('The Spotify URI to play (overrides type and id)'),
type: z
.enum(['track', 'album', 'artist', 'playlist'])
.optional()
.describe('The type of item to play'),
id: z.string().optional().describe('The Spotify ID of the item to play'),
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to add the track to'),
},
handler: async (args) => {
const { uri, type, id, deviceId } = args;
let spotifyUri = uri;
if (!spotifyUri && type && id) {
spotifyUri = `spotify:${type}:${id}`;
}
if (!spotifyUri) {
return {
content: [
{
type: 'text',
text: 'Error: Must provide either a URI or both a type and ID',
isError: true,
},
],
};
}
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.addItemToPlaybackQueue(
spotifyUri,
deviceId || '',
);
});
return {
content: [
{
type: 'text',
text: `Added item ${spotifyUri} to queue`,
},
],
};
},
};
const setVolume: tool<{
volumePercent: z.ZodNumber;
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'setVolume',
description:
'Set the playback volume to a specific percentage (0-100). Requires Spotify Premium.',
schema: {
volumePercent: z
.number()
.min(0)
.max(100)
.describe('The volume to set (0-100)'),
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to set volume on'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { volumePercent, deviceId } = args;
try {
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.setPlaybackVolume(
Math.round(volumePercent),
deviceId || '',
);
});
return {
content: [
{
type: 'text',
text: `Volume set to ${Math.round(volumePercent)}%`,
},
],
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error setting volume: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
},
};
const adjustVolume: tool<{
adjustment: z.ZodNumber;
deviceId: z.ZodOptional<z.ZodString>;
}> = {
name: 'adjustVolume',
description:
'Adjust the playback volume up or down by a relative amount. Use positive values to increase, negative to decrease. Requires Spotify Premium.',
schema: {
adjustment: z
.number()
.min(-100)
.max(100)
.describe(
'The amount to adjust volume by (-100 to 100). Positive increases, negative decreases.',
),
deviceId: z
.string()
.optional()
.describe('The Spotify device ID to adjust volume on'),
},
handler: async (args, _extra: SpotifyHandlerExtra) => {
const { adjustment, deviceId } = args;
try {
// First get the current playback state to find current volume
const playback = await handleSpotifyRequest(async (spotifyApi) => {
return await spotifyApi.player.getPlaybackState();
});
if (!playback?.device) {
return {
content: [
{
type: 'text',
text: 'No active device found. Make sure Spotify is open and playing on a device.',
},
],
};
}
const currentVolume = playback.device.volume_percent;
if (currentVolume === null || currentVolume === undefined) {
return {
content: [
{
type: 'text',
text: 'Unable to get current volume from device.',
},
],
};
}
const newVolume = Math.min(100, Math.max(0, currentVolume + adjustment));
await handleSpotifyRequest(async (spotifyApi) => {
await spotifyApi.player.setPlaybackVolume(
Math.round(newVolume),
deviceId || '',
);
});
const direction = adjustment > 0 ? 'increased' : 'decreased';
return {
content: [
{
type: 'text',
text: `Volume ${direction} from ${currentVolume}% to ${Math.round(newVolume)}%`,
},
],
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error adjusting volume: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
},
};
export const playTools = [
playMusic,
pausePlayback,
skipToNext,
skipToPrevious,
createPlaylist,
addTracksToPlaylist,
resumePlayback,
addToQueue,
setVolume,
adjustVolume,
];