// Node.js 18+, no packages. Run: node mcp-client.mjs "France Inter" // Searches metadata and returns links. Does not start audio. const endpoint = 'https://worldradionow.top/mcp'; let protocol = '2025-11-25'; let sequence = 0; async function rpc(method, params, notification = false) { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 15000); try { const message = {jsonrpc: '2.0', method, params}; if (!notification) message.id = ++sequence; const response = await fetch(endpoint, { method: 'POST', signal: controller.signal, headers: {'Content-Type': 'application/json', Accept: 'application/json, text/event-stream', 'MCP-Protocol-Version': protocol, 'User-Agent': 'my-radio-app/1.0'}, body: JSON.stringify(message) }); if (!response.ok) throw new Error('MCP returned HTTP ' + response.status); if (notification) return; const value = await response.json(); if (value.error) throw new Error(value.error.message); if (value.result?.isError) throw new Error(value.result.content?.[0]?.text || 'Tool unavailable'); return value.result; } finally { clearTimeout(timeout); } } function structured(result) { return result.structuredContent || JSON.parse(result.content[0].text); } try { const initialized = await rpc('initialize', {protocolVersion: protocol, capabilities: {}, clientInfo: {name: 'my-radio-app', version: '1.0'}}); protocol = initialized.protocolVersion; await rpc('notifications/initialized', {}, true); const search = structured(await rpc('tools/call', {name: 'search_radio_stations', arguments: {q: process.argv[2] || 'France Inter', limit: 3}})); if (!search.results.length) throw new Error('No matching stations. Try a shorter name'); const player = structured(await rpc('tools/call', {name: 'get_radio_player', arguments: {id: search.results[0].id, lang: 'en'}})); console.log(JSON.stringify({provider: 'World Radio Now', snapshot_at: search.snapshot_at, credit: player.attribution, ...player}, null, 2)); } catch (error) { console.error(error.message + '. Listening links are also available at https://worldradionow.top/search/.'); process.exitCode = 1; }