Skip to main content
Glama
wangshunnn

bilibili MCP Server

by wangshunnn

search_videos

Search for videos on Bilibili using keywords, specify page numbers, and control the number of results returned for focused content discovery.

Instructions

Search for videos on Bilibili

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
countNoNumber of results to return, default 10, maximum 20
keywordYesKeyword to search for
pageNoPage number, defaults to 1

Implementation Reference

  • The main execution handler for the search_videos tool. It calls searchVideos helper, filters video results, formats them into a readable text output, handles pagination and count limits, and returns MCP-formatted content blocks.
    async ({ keyword, page, count }) => {
      try {
        const searchResult = await searchVideos(keyword, page) || {}
    
        if (!searchResult.result || searchResult.result.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: `No videos found related to "${keyword}".`,
              },
            ],
          }
        }
    
        // 过滤出视频结果并限制数量
        const videoResults = searchResult.result
          .filter((item) => item.result_type === "video")?.[0]
          ?.data?.slice(0, count) as VideoSearchItem[]
    
        const formattedResults = videoResults
          .map((video, index) => {
            return [
              `${index + 1}. "${video.title}" - ${video.author}`,
              ` BV ID: ${video.bvid}`,
              ` Views: ${video.play?.toLocaleString()}`,
              ` Danmaku: ${video.danmaku?.toLocaleString()}`,
              ` Likes: ${video.like?.toLocaleString()}`,
              ` Duration: ${video.duration}`,
              ` Published: ${formatTimestamp(video.pubdate)}`,
              ` Description: ${video.description?.substring(0, 100)}${video.description?.length > 100 ? "..." : ""}`,
            ].join("\n")
          })
          .join("\n\n")
    
        return {
          content: [
            {
              type: "text",
              text: [
                `Search results for "${keyword}":`,
                formattedResults,
                `Found ${searchResult.numResults} related videos in total, currently showing ${videoResults.length} results from page ${page}.`,
              ].join("\n"),
            },
          ],
        }
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Failed to search videos: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
        }
      }
    }
  • Input validation schema using Zod for the search_videos tool parameters: keyword (required string), page (optional int >=1, default 1), count (optional int 1-20, default 10).
    {
      keyword: z.string().describe("Keyword to search for"),
      page: z
        .number()
        .int()
        .min(1)
        .default(1)
        .describe("Page number, defaults to 1"),
      count: z
        .number()
        .int()
        .min(1)
        .max(20)
        .default(10)
        .describe("Number of results to return, default 10, maximum 20"),
    },
  • Registration function for the search_videos tool on the MCP server, specifying name, description, input schema, and handler.
    export function registerSearchTools(server: McpServer): void {
      server.tool(
        "search_videos",
        "Search for videos on Bilibili",
        {
          keyword: z.string().describe("Keyword to search for"),
          page: z
            .number()
            .int()
            .min(1)
            .default(1)
            .describe("Page number, defaults to 1"),
          count: z
            .number()
            .int()
            .min(1)
            .max(20)
            .default(10)
            .describe("Number of results to return, default 10, maximum 20"),
        },
        async ({ keyword, page, count }) => {
          try {
            const searchResult = await searchVideos(keyword, page) || {}
    
            if (!searchResult.result || searchResult.result.length === 0) {
              return {
                content: [
                  {
                    type: "text",
                    text: `No videos found related to "${keyword}".`,
                  },
                ],
              }
            }
    
            // 过滤出视频结果并限制数量
            const videoResults = searchResult.result
              .filter((item) => item.result_type === "video")?.[0]
              ?.data?.slice(0, count) as VideoSearchItem[]
    
            const formattedResults = videoResults
              .map((video, index) => {
                return [
                  `${index + 1}. "${video.title}" - ${video.author}`,
                  ` BV ID: ${video.bvid}`,
                  ` Views: ${video.play?.toLocaleString()}`,
                  ` Danmaku: ${video.danmaku?.toLocaleString()}`,
                  ` Likes: ${video.like?.toLocaleString()}`,
                  ` Duration: ${video.duration}`,
                  ` Published: ${formatTimestamp(video.pubdate)}`,
                  ` Description: ${video.description?.substring(0, 100)}${video.description?.length > 100 ? "..." : ""}`,
                ].join("\n")
              })
              .join("\n\n")
    
            return {
              content: [
                {
                  type: "text",
                  text: [
                    `Search results for "${keyword}":`,
                    formattedResults,
                    `Found ${searchResult.numResults} related videos in total, currently showing ${videoResults.length} results from page ${page}.`,
                  ].join("\n"),
                },
              ],
            }
          } catch (error) {
            return {
              content: [
                {
                  type: "text",
                  text: `Failed to search videos: ${error instanceof Error ? error.message : String(error)}`,
                },
              ],
            }
          }
        }
      )
    }
  • src/index.ts:16-16 (registration)
    Top-level call to register the search tools (including search_videos) on the main MCP server instance.
    registerSearchTools(server)
  • Utility helper function that invokes the Bilibili search API via searchAPI and handles errors, used by the search_videos handler.
    export async function searchVideos(
      keyword: string,
      page: number = 1
    ): Promise<SearchResult> {
      try {
        return await searchAPI.searchVideos(keyword, page) || {}
      } catch (error) {
        console.error("Error searching videos:", error)
        throw error
      }
    }

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv1.0.0

TDQS

B3.1/5.0
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It does not mention pagination behavior, result ordering, authentication, rate limits, or what is included in the results. The schema hints at page/count but the description itself adds no behavioral context.

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

Conciseness4/5

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

The description is a single, front-loaded sentence with no filler, making it highly concise. However, it is so terse that it omits useful context, though it still earns its place.

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

Completeness2/5

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

Given the lack of output schema and annotations, and the description's brevity, the agent is left without crucial context such as return structure, pagination limits, edge cases, or conflict with siblings. The schema covers parameters but the description does not round out the tool's behavior.

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

Parameters3/5

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

Schema description coverage is 100% with self-explanatory fields (keyword, page, count). The description adds no information beyond the schema, so the baseline of 3 applies.

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?

The description clearly states the tool's purpose: searching for videos on Bilibili. The verb 'search' distinguishes it from sibling tools get_user_info and get_video_info, which are direct lookups, and it specifies the resource and platform.

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

Usage Guidelines2/5

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

The description offers no guidance on when to use this tool versus the sibling tools, nor any circumstances that would make it the preferred choice. It only states the basic functionality without any context or exclusions.

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

Deploy Server

Other Tools

Related Tools