get_p3_current_playlist
Fetch the currently playing track on Sveriges Radio P3 with details on artist, title, album, and start/stop times.
Instructions
Fetch the currently playing song on Sveriges Radio P3 (channel 565). Returns the current song, previous song, and next song with details including artist, title, album, start/stop timestamps in UTC.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.ts:29-41 (registration)Tool registration in the TOOLS array with name 'get_p3_current_playlist', description, and input schema (empty object).
const TOOLS: Tool[] = [ { name: 'get_p3_current_playlist', description: 'Fetch the currently playing song on Sveriges Radio P3 (channel 565). ' + 'Returns the current song, previous song, and next song with details including ' + 'artist, title, album, start/stop timestamps in UTC.', inputSchema: { type: 'object', properties: {}, required: [], }, }, - src/server.ts:109-122 (handler)Call handler in server.ts: validates input with GetCurrentPlaylistSchema, calls getCurrentPlaylist, and returns JSON response.
case 'get_p3_current_playlist': { // Validate input (should be empty object) const validatedInput = GetCurrentPlaylistSchema.parse(args || {}); const result = await getCurrentPlaylist(validatedInput); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } - src/tools/current-playlist.ts:13-15 (schema)Zod schema (GetCurrentPlaylistSchema) defining empty input validation for the tool.
export const GetCurrentPlaylistSchema = z.object({}); export type GetCurrentPlaylistInput = z.infer<typeof GetCurrentPlaylistSchema>; - src/tools/current-playlist.ts:64-137 (handler)Main handler function getCurrentPlaylist: fetches playlist from SR API, finds P3 channel (ID 164), parses current/previous/next songs, and returns PlaylistResponse.
export async function getCurrentPlaylist( _input: GetCurrentPlaylistInput ): Promise<PlaylistResponse> { const apiClient = getApiClient(); const errors: string[] = []; const songs: Song[] = []; try { const response = await apiClient.getCurrentPlaylist(); const currentTime = new Date().toISOString(); // Find P3 channel in the response const p3Channel = response.channels?.find(ch => ch.id === P3_CHANNEL_ID); const playlist = p3Channel?.playlists?.playlist; if (!playlist) { throw new Error('No playlist data returned for P3 from Sveriges Radio API'); } // Parse current song const currentSongs = normalizeToArray(playlist.song); const currentSong = currentSongs.length > 0 ? parseSong(currentSongs[0], currentTime) : null; // Parse previous song const previousSongs = normalizeToArray(playlist.previoussong); const previousSong = previousSongs.length > 0 ? parseSong(previousSongs[0], currentTime) : null; // Parse next song const nextSongs = normalizeToArray(playlist.nextsong); const nextSong = nextSongs.length > 0 ? parseSong(nextSongs[0], currentTime) : null; // Add songs in order: previous, current, next if (previousSong) songs.push(previousSong); if (currentSong) songs.push(currentSong); if (nextSong) songs.push(nextSong); if (songs.length === 0) { errors.push('No songs found in the current playlist'); } return { songs, metadata: { channel: 'P3', channelId: P3_CHANNEL_ID, timestamp: currentTime, query: { type: 'current', }, }, errors: errors.length > 0 ? errors : undefined, }; } catch (error) { // Convert error to user-friendly message const errorMessage = error instanceof Error ? error.message : 'An unexpected error occurred while fetching the current playlist'; return { songs: [], metadata: { channel: 'P3', channelId: P3_CHANNEL_ID, timestamp: new Date().toISOString(), query: { type: 'current', }, }, errors: [errorMessage], }; } } - src/tools/current-playlist.ts:20-51 (helper)Helper function parseSong: converts raw SR API song data to the Song interface with duration calculation.
function parseSong(apiSong: SRApiSong | undefined, fallbackTime: string): Song | null { if (!apiSong) return null; const title = apiSong.title || 'Unknown Title'; const artist = apiSong.artist || 'Unknown Artist'; const startTime = apiSong.starttimeutc || fallbackTime; const stopTime = apiSong.stoptimeutc || fallbackTime; // Calculate duration in seconds let duration: number | undefined; try { const start = new Date(startTime).getTime(); const stop = new Date(stopTime).getTime(); if (!isNaN(start) && !isNaN(stop) && stop > start) { duration = Math.floor((stop - start) / 1000); } } catch { // Duration calculation failed, leave it undefined } return { title, artist, composer: apiSong.composer, albumName: apiSong.albumname, recordLabel: apiSong.recordlabel, startTimeUTC: startTime, stopTimeUTC: stopTime, duration, description: apiSong.description, }; }