Skip to main content
Glama

getTrendingVideos

Read-onlyIdempotent

Retrieves trending videos filtered by region and category to discover popular content in specific areas or categories.

Instructions

Retrieves trending videos based on region and category. Returns a list of videos that are currently popular in the specified region and category. Use this when you want to discover what's trending in specific areas or categories. To get available category IDs and their names, use the getVideoCategories tool first.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
regionCodeNoTwo-letter country code (e.g., 'US', 'GB', 'JP'). Defaults to 'US'US
categoryIdNoYouTube category ID to filter trending videos by category. Use getVideoCategories tool to get available category IDs.
maxResultsNoMaximum number of trending videos to return (1-500, default: 10)

Implementation Reference

  • The GetTrendingVideosTool class that executes the tool logic. It extends BaseTool, defers to youtubeService.getTrendingVideos(), and formats the result via formatSuccess().
    export class GetTrendingVideosTool extends BaseTool<
      typeof getTrendingVideosSchema
    > {
      name = "getTrendingVideos";
      description =
        "Retrieves trending videos based on region and category. Returns a list of videos that are currently popular in the specified region and category. Use this when you want to discover what's trending in specific areas or categories. To get available category IDs and their names, use the getVideoCategories tool first.";
      schema = getTrendingVideosSchema;
    
      protected async executeImpl(
        params: z.infer<typeof getTrendingVideosSchema>
      ): Promise<CallToolResult> {
        const trendingVideos =
          await this.container.youtubeService.getTrendingVideos(params);
    
        return formatSuccess(trendingVideos);
      }
    }
  • The core service implementation getTrendingVideos() that calls the YouTube API v3 videos.list endpoint with chart='mostPopular', handles regionCode, categoryId, maxResults parameters, and returns lean trending video data with engagement ratios.
    async getTrendingVideos(
      options: TrendingOptions
    ): Promise<LeanTrendingVideo[]> {
      const cacheKey = this.cacheService.createOperationKey(
        "getTrendingVideos",
        options
      );
    
      const operation = async (): Promise<LeanTrendingVideo[]> => {
        try {
          const { regionCode = "US", categoryId, maxResults = 10 } = options;
    
          const params: youtube_v3.Params$Resource$Videos$List = {
            part: ["snippet", "statistics", "contentDetails"],
            chart: "mostPopular",
            regionCode: regionCode,
            maxResults: maxResults,
          };
    
          if (categoryId) {
            params.videoCategoryId = categoryId;
          }
    
          const response = await this.trackCost(
            () => this.youtube.videos.list(params),
            API_COSTS["videos.list"]
          );
    
          return (
            response.data.items?.map((video) => {
              const viewCount = parseYouTubeNumber(video.statistics?.viewCount);
              const likeCount = parseYouTubeNumber(video.statistics?.likeCount);
              const commentCount = parseYouTubeNumber(
                video.statistics?.commentCount
              );
    
              return {
                id: video.id,
                title: video.snippet?.title,
                channelId: video.snippet?.channelId,
                channelTitle: video.snippet?.channelTitle,
                publishedAt: video.snippet?.publishedAt,
                duration: video.contentDetails?.duration,
                viewCount: viewCount,
                likeCount: likeCount,
                commentCount: commentCount,
                likeToViewRatio: calculateLikeToViewRatio(viewCount, likeCount),
                commentToViewRatio: calculateCommentToViewRatio(
                  viewCount,
                  commentCount
                ),
              };
            }) || []
          );
        } catch (error) {
          if (error instanceof AppError) throw error;
          throw new YouTubeApiError(
            `YouTube API call for getTrendingVideos failed`,
            error
          );
        }
      };
    
      return this.cacheService.getOrSet(
        cacheKey,
        operation,
        CACHE_TTLS.DYNAMIC,
        CACHE_COLLECTIONS.TRENDING_VIDEOS,
        options
      );
    }
  • Zod schema (getTrendingVideosSchema) defining input parameters: regionCode (default 'US'), categoryId (optional), maxResults (default 10).
    export const getTrendingVideosSchema = z.object({
      regionCode: regionCodeSchema
        .default("US")
        .describe(
          "Two-letter country code (e.g., 'US', 'GB', 'JP'). Defaults to 'US'"
        ),
      categoryId: categoryIdSchema.describe(
        "YouTube category ID to filter trending videos by category. Use getVideoCategories tool to get available category IDs."
      ), // categoryId is optional and has no default
      maxResults: maxResultsSchema
        .default(10)
        .describe(
          "Maximum number of trending videos to return (1-500, default: 10)"
        ),
    });
  • LeanTrendingVideo interface defining the output type shape: id, title, channelId, channelTitle, publishedAt, duration, viewCount, likeCount, commentCount, likeToViewRatio, commentToViewRatio.
    export interface LeanTrendingVideo {
      id: string | null | undefined;
      title: string | null | undefined;
      channelId: string | null | undefined;
      channelTitle: string | null | undefined;
      publishedAt: string | null | undefined;
      duration: string | null | undefined;
      viewCount: number | null | undefined;
      likeCount: number | null | undefined;
      commentCount: number | null | undefined;
      likeToViewRatio: number | null | undefined;
      commentToViewRatio: number | null | undefined;
    }
  • Tool registration: GetTrendingVideosTool is imported (line 14), added to TOOL_CLASSES array (line 35), and registered with the MCP server via registerTools() (line 65-87).
    import { GetTrendingVideosTool } from "./general/getTrendingVideos.js";
    import { GetVideoCategoriesTool } from "./general/getVideoCategories.js";
    import { FindConsistentOutlierChannelsTool } from "./general/findConsistentOutlierChannels.js";
    
    interface ITool {
      readonly name: string;
      readonly description: string;
      readonly schema: z.ZodObject<z.ZodRawShape>;
      execute(args: z.infer<this["schema"]>): Promise<CallToolResult>;
    }
    
    type ToolConstructor = new (container: IServiceContainer) => ITool;
    
    // 1. Maintain a list of Constructors
    const TOOL_CLASSES = [
      GetVideoDetailsTool,
      SearchVideosTool,
      GetTranscriptsTool,
      GetVideoCommentsTool,
      GetChannelStatisticsTool,
      GetChannelTopVideosTool,
      GetTrendingVideosTool,
      GetVideoCategoriesTool,
    ];
    
    export function registerTools(server: McpServer, container: IServiceContainer) {
      const hasYoutubeKey = !!process.env.YOUTUBE_API_KEY;
      const toolsToRegister: ToolConstructor[] = [];
    
      if (hasYoutubeKey) {
        // Register all standard YouTube tools
        toolsToRegister.push(...(TOOL_CLASSES as ToolConstructor[]));
    
        // Register analytics tools if connection string is present
        if (process.env.MDB_MCP_CONNECTION_STRING) {
          toolsToRegister.push(FindConsistentOutlierChannelsTool);
        }
      } else {
        // Only register tools that don't require the API key
        toolsToRegister.push(GetTranscriptsTool);
      }
    
      for (const ToolClass of toolsToRegister) {
        // Instantiate with DI container
        const toolInstance = new ToolClass(container);
    
        const humanReadableTitle = toolInstance.name
          .replace(/([A-Z])/g, " $1")
          .replace(/^./, (str: string) => str.toUpperCase());
    
        // Register with MCP Server
        server.registerTool(
          toolInstance.name,
          {
            description: toolInstance.description,
            inputSchema: toolInstance.schema,
            annotations: {
              title: humanReadableTitle,
              readOnlyHint: true,
              idempotentHint: true,
            },
          },
          // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
          (async (
            args: z.infer<typeof toolInstance.schema>
          ): Promise<CallToolResult> => {
            try {
              return await toolInstance.execute(args);
            } catch (err) {
              return formatError(err);
            }
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
          }) as any
        );
      }
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already provide readOnlyHint and idempotentHint. Description adds behavioral context about returning a list of popular videos and dependency on category IDs from another tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two concise sentences, front-loaded with purpose, no redundant information. Every sentence adds value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple read-only tool with three parameters, the description adequately explains inputs, output type, and prerequisite tool. No missing context given lack of output schema.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% with good descriptions. Description adds value by recommending getVideoCategories for categoryId and clarifying regionCode format and maxResults range.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states 'Retrieves trending videos based on region and category' with specific verb and resource, distinguishing it from sibling tools like searchVideos which is for general search.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly tells when to use ('discover what's trending in specific areas or categories') and directs to prerequisite tool getVideoCategories for category IDs.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/kirbah/mcp-youtube'

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