get-channel-stats
Retrieve detailed YouTube channel statistics, including subscriber count, total views, and video count, by inputting the channel ID through the Model Context Protocol.
Instructions
Get statistical information for a specific YouTube channel (subscriber count, total views, video count, etc.)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| channelId | Yes |
Implementation Reference
- src/index.ts:392-432 (handler)The main handler function for the 'get-channel-stats' tool. It fetches channel details via YouTubeService, handles errors, formats statistics (channelId, title, createdAt, subscriberCount, videoCount, viewCount, thumbnailUrl), and returns JSON.async ({ channelId }) => { try { const channelData = await youtubeService.getChannelDetails(channelId); const channel = channelData.items?.[0]; if (!channel) { return { content: [{ type: 'text', text: `Channel with ID ${channelId} not found.` }], isError: true }; } const stats = { channelId: channel.id, title: channel.snippet?.title, createdAt: channel.snippet?.publishedAt, subscriberCount: channel.statistics?.subscriberCount, videoCount: channel.statistics?.videoCount, viewCount: channel.statistics?.viewCount, thumbnailUrl: channel.snippet?.thumbnails?.default?.url }; return { content: [{ type: 'text', text: JSON.stringify(stats, null, 2) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error fetching channel statistics: ${error}` }], isError: true }; } }
- src/index.ts:389-391 (schema)Input schema for the tool using Zod: requires a non-empty string channelId.{ channelId: z.string().min(1) },
- src/index.ts:386-433 (registration)Registration of the 'get-channel-stats' tool on the MCP server with name, description, input schema, and handler function.server.tool( 'get-channel-stats', 'Get statistical information for a specific YouTube channel (subscriber count, total views, video count, etc.)', { channelId: z.string().min(1) }, async ({ channelId }) => { try { const channelData = await youtubeService.getChannelDetails(channelId); const channel = channelData.items?.[0]; if (!channel) { return { content: [{ type: 'text', text: `Channel with ID ${channelId} not found.` }], isError: true }; } const stats = { channelId: channel.id, title: channel.snippet?.title, createdAt: channel.snippet?.publishedAt, subscriberCount: channel.statistics?.subscriberCount, videoCount: channel.statistics?.videoCount, viewCount: channel.statistics?.viewCount, thumbnailUrl: channel.snippet?.thumbnails?.default?.url }; return { content: [{ type: 'text', text: JSON.stringify(stats, null, 2) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error fetching channel statistics: ${error}` }], isError: true }; } } );
- src/youtube-service.ts:81-92 (helper)Helper method in YouTubeService that performs the core API call to YouTube Data API v3 channels.list to retrieve channel snippet and statistics.async getChannelDetails(channelId: string): Promise<youtube_v3.Schema$ChannelListResponse> { try { const response = await this.youtube.channels.list({ part: ['snippet', 'statistics'], id: [channelId] }); return response.data; } catch (error) { console.error('Error getting channel details:', error); throw error; } }