Skip to main content
Glama

searchTrack

Find tracks on YouTube Music by entering song names to locate specific music content for playback.

Instructions

Search for tracks on YouTube Music by name.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
trackNameYesThe name of the track to search for

Implementation Reference

  • Inline handler function for the searchTrack tool that performs YouTube search via helper and returns formatted results or error.
    async ({ trackName }) => { try { const searchResults = await searchYoutubeVideo(apiKey, trackName, 5) return { content: [ { type: 'text', text: JSON.stringify(searchResults, null, 2) }, ], } } catch (error: unknown) { console.error('Error in searchTrack tool:', error) const message = error instanceof McpError ? error.message : 'An unexpected error occurred during search.' return { content: [{ type: 'text', text: `Error searching YouTube: ${message}` }], isError: true, } } },
  • Input schema definition for searchTrack tool using Zod.
    { trackName: z.string().describe('The name of the track to search for'), },
  • MCP tool registration for searchTrack, specifying name, description, input schema, and handler.
    'searchTrack', 'Search for tracks on YouTube Music by name.', { trackName: z.string().describe('The name of the track to search for'), }, async ({ trackName }) => { try { const searchResults = await searchYoutubeVideo(apiKey, trackName, 5) return { content: [ { type: 'text', text: JSON.stringify(searchResults, null, 2) }, ], } } catch (error: unknown) { console.error('Error in searchTrack tool:', error) const message = error instanceof McpError ? error.message : 'An unexpected error occurred during search.' return { content: [{ type: 'text', text: `Error searching YouTube: ${message}` }], isError: true, } } }, )
  • Helper function searchYoutubeVideo used by the searchTrack handler to query the YouTube API.
    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