play_track
Play a specific Spotify track by name, with optional artist name and device selection to control playback.
Instructions
Play a specific track by name and optional artist
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| trackName | Yes | Name of the track to play | |
| artistName | No | Optional artist name to help find the track | |
| deviceId | No | Optional device ID to play on |
Implementation Reference
- src/tools/play.ts:43-63 (handler)The core handler function that implements the play_track tool logic: constructs a search query, searches for the track using SpotifyClient.searchTracks, plays the first result using SpotifyClient.playTrack, and returns success details.export async function playTrack(client: SpotifyClient, trackName: string, artistName?: string, deviceId?: string) { const query = artistName ? `track:${trackName} artist:${artistName}` : trackName; const results = await client.searchTracks(query, 1); if (results.length === 0) { throw new Error(`Track "${trackName}" not found`); } const track = results[0]; await client.playTrack(track.uri, deviceId); return { success: true, message: `Playing track: ${track.name} by ${track.artist}`, track: { id: track.id, name: track.name, artist: track.artist, }, }; }
- src/server.ts:92-111 (registration)Registration of the 'play_track' tool in the ListTools handler, defining its name, description, and input schema.name: 'play_track', description: 'Play a specific track by name and optional artist', inputSchema: { type: 'object', properties: { trackName: { type: 'string', description: 'Name of the track to play', }, artistName: { type: 'string', description: 'Optional artist name to help find the track', }, deviceId: { type: 'string', description: 'Optional device ID to play on', }, }, required: ['trackName'], },
- src/server.ts:253-267 (handler)Dispatch handler in CallToolRequestSchema that invokes the playTrack function from playTools and formats the response.case 'play_track': const trackResult = await playTools.playTrack( client, args?.trackName as string, args?.artistName as string | undefined, args?.deviceId as string | undefined ); return { content: [ { type: 'text', text: JSON.stringify(trackResult, null, 2), }, ], };
- src/spotify/client.ts:121-127 (helper)Supporting method in SpotifyClient that makes the actual Spotify Web API call to play the track URI on the specified device.async playTrack(trackUri: string, deviceId?: string) { const api = await this.getApi(); await api.play({ uris: [trackUri], device_id: deviceId, }); }