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
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of results to return, default 10, maximum 20 | |
| keyword | Yes | Keyword to search for | |
| page | No | Page number, defaults to 1 |
Implementation Reference
- src/tools/search.ts:26-83 (handler)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)}`, }, ], } } }
- src/tools/search.ts:10-25 (schema)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"), },
- src/tools/search.ts:6-85 (registration)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)
- src/common/utils.ts:37-47 (helper)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 } }