Skip to main content
Glama

playTrack

Search for a track on YouTube Music and open the top result in your browser. Use this tool to quickly find and play music tracks through natural language commands.

Instructions

Search for a track on YouTube Music and open the top result in the default browser.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
trackNameYesThe name of the track to search for and play

Implementation Reference

  • The core handler logic for the 'playTrack' tool. It searches for the track on YouTube using the API, extracts the top video ID, constructs the YouTube Music watch URL, opens it in the default browser, and returns a success message or handles errors.
    async ({ trackName }) => { try { const searchResults = await searchYoutubeVideo(apiKey, trackName, 1) if (searchResults.length === 0) { return { content: [{ type: 'text', text: `No search results found for: ${trackName}` }], } } const topResult = searchResults[0] const videoId = topResult?.id?.videoId const title = topResult?.snippet?.title ?? 'Unknown Title' if (!videoId) { console.error('Could not find video ID in top search result:', topResult) throw new McpError(ErrorCode.InternalError, 'Could not extract video ID from YouTube search result.') } const youtubeMusicUrl = `${YOUTUBE_MUSIC_WATCH_URL_PREFIX}${videoId}` await openUrlInBrowser(youtubeMusicUrl) return { content: [{ type: 'text', text: `Attempting to play in browser: ${title} (${youtubeMusicUrl})` }], } } catch (error: unknown) { console.error('Error in playTrack tool:', error) if (error instanceof McpError && error.code === ErrorCode.InternalError) { throw error } const message = error instanceof McpError ? error.message : error instanceof Error ? error.message : 'An unexpected error occurred during track playback.' return { content: [{ type: 'text', text: `Error playing track: ${message}` }], isError: true, } } },
  • Zod input schema for the 'playTrack' tool defining the required 'trackName' parameter.
    { trackName: z.string().describe('The name of the track to search for and play'), },
  • src/index.ts:15-15 (registration)
    Top-level call to register the YouTube Music tools, including 'playTrack', in the MCP server.
    registerToolYoutubeMusic({ mcp } as McpToolContext)
  • Platform-specific helper to open a URL in the default browser (Chrome on macOS, system default otherwise). Called by playTrack handler to play the track.
    async function openUrlInBrowser(url: string, platform: NodeJS.Platform = process.platform): Promise<void> { let command: string = '' try { if (platform === 'darwin') { const appleScript = `tell application "Google Chrome" to open location "${url}"` command = `osascript -e '${appleScript.replace(/'/g, "'\''")}'` console.log(`Executing command for macOS: ${command}`) const { stderr } = await execPromise(command) if (stderr) { console.warn('osascript stderr:', stderr) } } else if (platform === 'win32') { command = `start "" "${url}"` console.log(`Executing command for Windows: ${command}`) await execPromise(command) } else { // Assume Linux/other POSIX-like command = `xdg-open "${url}"` console.log(`Executing command for Linux/Other: ${command}`) await execPromise(command) } } catch (execError: unknown) { console.error(`Error executing command "${command}":`, execError) const errorMsg = execError instanceof Error ? execError.message : 'Unknown execution error' throw new McpError(ErrorCode.InternalError, `Failed to open URL in browser: ${errorMsg}`) } }
  • YouTube API search helper function to find video results by query. Used by playTrack to locate the track.
    async function searchYoutubeVideo( apiKey: string, query: string, maxResults: number = 5, ): Promise<YouTubeSearchResultItem[]> { try { const searchResults = await ofetch<YouTubeSearchResults>('/search', { baseURL: YOUTUBE_API_BASE_URL, query: { key: apiKey, part: 'snippet', maxResults, type: 'video', q: query, }, }) return searchResults?.items ?? [] } catch (error: unknown) { console.error('Error fetching YouTube search results:', error) const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred during YouTube search' throw new McpError(ErrorCode.InternalError, `YouTube API search failed: ${errorMessage}`) } }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/instructa/mcp-youtube-music'

If you have feedback or need assistance with the MCP directory API, please join our Discord server