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
| Name | Required | Description | Default |
|---|---|---|---|
| trackName | Yes | The name of the track to search for |
Implementation Reference
- src/tools/youtube.ts:88-105 (handler)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, } } },
- src/tools/youtube.ts:85-87 (schema)Input schema definition for searchTrack tool using Zod.{ trackName: z.string().describe('The name of the track to search for'), },
- src/tools/youtube.ts:83-106 (registration)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, } } }, )
- src/tools/youtube.ts:17-40 (helper)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}`) } }