videos_searchVideos
Search YouTube videos using specific queries and return results with direct URLs for accessing content through the YouTube Data API.
Instructions
Search for videos on YouTube and return results with URLs
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| maxResults | No | Maximum number of results to return |
Implementation Reference
- src/services/video.ts:84-103 (handler)Core handler implementing YouTube video search via Google API, structuring results with video URLs.async searchVideos({ query, maxResults = 10 }: SearchParams): Promise<unknown[]> { try { this.initialize(); const response = await this.youtube.search.list({ part: ['snippet'], q: query, maxResults, type: ['video'] }); const videos = response.data.items || []; return this.createStructuredVideos(videos); } catch (error) { throw new Error(`Failed to search videos: ${error instanceof Error ? error.message : String(error)}`); } }
- src/server-utils.ts:154-174 (registration)MCP tool registration including schema, description, and delegation to VideoService handler.server.registerTool( 'videos_searchVideos', { title: 'Search Videos', description: 'Search for videos on YouTube and return results with URLs', annotations: { readOnlyHint: true, idempotentHint: true }, inputSchema: { query: z.string().describe('Search query'), maxResults: z.number().optional().describe('Maximum number of results to return'), }, }, async ({ query, maxResults }) => { const result = await videoService.searchVideos({ query, maxResults }); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } );
- src/types.ts:10-15 (schema)TypeScript interface for input parameters matching the Zod schema used in registration.* Search videos parameters */ export interface SearchParams { query: string; maxResults?: number; }
- src/services/video.ts:36-38 (helper)Helper method to map search results, adding YouTube URLs to each video object.private createStructuredVideos(videos: unknown[]): unknown[] { return videos.map(video => this.createStructuredVideo(video)).filter(Boolean); }
- src/services/video.ts:18-31 (helper)Helper method to structure individual video data by extracting ID and constructing canonical URL.private createStructuredVideo(videoData: unknown): unknown { if (!videoData) return null; // eslint-disable-next-line @typescript-eslint/no-explicit-any const v = videoData as any; const videoId = v.id || v.id?.videoId; const url = videoId ? `https://www.youtube.com/watch?v=${videoId}` : null; return { ...v, url, videoId }; }