Skip to main content
Glama
222wcnm

Bilibili Comments MCP

by 222wcnm

get_dynamic_comments

Retrieve comments from Bilibili dynamic posts with pagination and nested reply support. Requires valid Bilibili Cookie for authentication.

Instructions

获取 B 站动态的评论内容,支持分页和楼中楼回复。注意:需要有效的 B 站 Cookie 才能正常工作。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dynamic_idYesB 站动态 ID
pageNo页码,默认为 1
pageSizeNo每页数量,范围 1-20,默认 20
includeRepliesNo是否包含楼中楼回复
outputFormatNo输出格式: markdown 或 jsonmarkdown
cookieNoB 站 Cookie(可选)。如果已设置环境变量,则无需提供。

Implementation Reference

  • Main handler function for 'get_dynamic_comments' tool. Validates input parameters (dynamic_id, page, pageSize, includeReplies, outputFormat, cookie), fetches dynamic details, retrieves comments via Bilibili API, and returns formatted output (markdown or json). Includes error handling and validation logic.
    export async function getDynamicComments(args, api) {
        try {
            const { dynamic_id, page = 1, pageSize = 20, includeReplies = true, outputFormat = "markdown" } = args;
            const cookie = getValidCookie(args.cookie);
    
            if (!cookie) {
                throw new McpError(ErrorCode.InvalidParams, "必须提供有效的 B 站 Cookie。请通过参数传入或设置 BILIBILI_SESSDATA 环境变量。");
            }
            if (!dynamic_id) throw new McpError(ErrorCode.InvalidParams, "必须提供 dynamic_id");
            if (!validateDynamicId(dynamic_id)) {
                throw new McpError(ErrorCode.InvalidParams, `无效的 dynamic_id 格式: ${dynamic_id}。动态ID应该是长数字字符串。`);
            }
            if (pageSize < 1 || pageSize > 20) throw new McpError(ErrorCode.InvalidParams, "pageSize 必须在 1-20 之间");
            if (page < 1) throw new McpError(ErrorCode.InvalidParams, "page 必须大于等于1");
            if (!["markdown", "json"].includes(outputFormat)) throw new McpError(ErrorCode.InvalidParams, "outputFormat 必须是 markdown 或 json");
    
            // 获取动态详情,确定正确的评论参数
            const dynamicDetail = await api.getDynamicDetail(dynamic_id, cookie);
    
            const response = await api.fetchDynamicComments(
                dynamic_id, page, pageSize, cookie,
                dynamicDetail.commentType, dynamicDetail.oid
            );
    
            if (response.data.code !== 0) {
                let errorMsg = getApiErrorMessage(response.data.code, response.data.message);
                if (response.data.code === -404) {
                    errorMsg = "动态不存在或已被删除。请检查 dynamic_id 是否正确,或该动态可能已被作者删除。";
                }
                throw new McpError(ErrorCode.InternalError, `B 站 API 错误 (${response.data.code}): ${errorMsg}`);
            }
    
            // 构建楼中楼获取函数(使用正确的 oid)
            const fetchRepliesFn = (comment) => api.fetchReplies(dynamicDetail.oid, comment.rpid, cookie, dynamic_id);
    
            if (outputFormat === "json") {
                const jsonResponse = await generateJsonResponse(response.data.data, includeReplies, fetchRepliesFn);
                return { content: [{ type: "text", text: JSON.stringify(jsonResponse, null, 2) }] };
            } else {
                const markdownResponse = await generateDynamicMarkdown(
                    response.data.data, includeReplies, fetchRepliesFn, dynamicDetail.type
                );
                return { content: [{ type: "text", text: markdownResponse }] };
            }
        } catch (error) {
            if (process.env.NODE_ENV === 'development') {
                console.error(`[ERROR] 获取动态评论失败:`, error);
            }
            return { content: [{ type: "text", text: `❌ 获取动态评论失败: ${error.message}` }] };
        }
    }
  • Tool schema definition for 'get_dynamic_comments'. Defines the tool name, description, inputSchema with properties (dynamic_id, page, pageSize, includeReplies, outputFormat, cookie), required fields, and annotations for the MCP tool registry.
        name: "get_dynamic_comments",
        description: "获取 B 站动态的评论内容,支持分页和楼中楼回复。注意:需要有效的 B 站 Cookie 才能正常工作。",
        inputSchema: {
            type: "object",
            properties: {
                dynamic_id: { type: "string", description: "B 站动态 ID" },
                page: { type: "number", default: 1, description: "页码,默认为 1" },
                pageSize: { type: "number", default: 20, description: "每页数量,范围 1-20,默认 20" },
                includeReplies: { type: "boolean", default: true, description: "是否包含楼中楼回复" },
                outputFormat: { type: "string", default: "markdown", description: "输出格式: markdown 或 json" },
                cookie: { type: "string", description: "B 站 Cookie(可选)。如果已设置环境变量,则无需提供。" }
            },
            required: ["dynamic_id"]
        },
        annotations: { title: "B站动态评论获取", readOnlyHint: true, openWorldHint: false }
    }
  • src/server.js:84-85 (registration)
    Tool registration in CallToolRequestSchema handler. Routes 'get_dynamic_comments' tool calls to the getDynamicComments handler function.
    case "get_dynamic_comments":
        return await getDynamicComments(args, this.api);
  • Helper utilities imported for the get_dynamic_comments tool: McpError and ErrorCode for error handling, getValidCookie and validateDynamicId for validation, getApiErrorMessage for API error messages, and formatters for markdown/json output.
    import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
    import { getValidCookie, validateDynamicId } from '../utils/cookie.js';
    import { getApiErrorMessage } from '../utils/common.js';
    import { generateDynamicMarkdown } from '../formatters/markdown.js';
    import { generateJsonResponse } from '../formatters/json.js';
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/222wcnm/Bilibili-Comments-MCP'

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