MCP工具
这是一个用于创建MCP(模型上下文协议)工具的模块化工具包。每个工具都是一个可以独立使用的独立可执行文件。
https://github.com/modelcontextprotocol/typescript-sdk
先决条件
- Node.js 23+(或Bun/Deno/任何支持运行.ts文件的东西)
- Bun(用于构建可执行文件)
安装Bun
如果您没有安装Bun,可以使用以下方法之一进行安装:
macOS和Linux
# Using curl (recommended)
curl -fsSL https://bun.sh/install | bash
# Using Homebrew
brew install oven-sh/bun/bun
# Using npm
npm install -g bun视窗
# Using PowerShell
powershell -c "irm bun.sh/install.ps1|iex"
# Using npm
npm install -g bun
# Using Scoop
scoop install bun通过运行以下命令验证您的安装:
bun --version安装
pnpm install项目结构
该项目展示了构建MCP工具的模块化方法:
- 每个工具都在它自己的TypeScript文件中定义
src目录 - 每个工具都可以内置到
bin目录 - 主要的
index.ts提供了一个简单的“Hello World”示例
可用工具
- 买 (
src/bilibili.ts):全面的Bilibili视频分析工具,支持视频分析、AI摘要、字幕、搜索、用户配置文件、评论、danmaku和文章
Bilibili分析工具
Bilibili工具为Bilibili视频提供全面的分析功能,包括:
- 视频信息(上传者、观看次数、统计数据等)
- 人工智能生成的字幕和用户提交的字幕
- 人工智能生成的视频摘要和大纲
- 视频搜索功能
- 用户资料信息
- 评论检索
- Danmaku(项目注释)提取
购买工具设置
所需的环境变量
Bilibili工具需要通过Cookie进行身份验证。使用您的Bilibili会话数据设置这些环境变量:
# Core authentication cookies (Required)
export BILIBILI_SESSDATA="your_sessdata_here"
export BILIBILI_BILI_JCT="your_bili_jct_here"
export BILIBILI_DEDEUSERID="your_dedeuserid_here"
export BILIBILI_DEDEUSERLID_CKMD5="your_dedeuserlid_ckmd5_here"
# Browser identification cookies (Recommended for better functionality)
export BILIBILI_BUVID3="your_buvid3_here"
export BILIBILI_BUVID4="your_buvid4_here"
export BILIBILI_BUVID_FP="your_buvid_fp_here"
# Session and tracking cookies (Optional but may improve reliability)
export BILIBILI_B_NUT="your_b_nut_here"
export BILIBILI_UUID="your_uuid_here"
export BILIBILI_SID="your_sid_here"
export BILIBILI_B_LSID="your_b_lsid_here"
# LLM Subtitle Sanitization (Optional - enables subtitle cleanup)
export BILIBILI_LLM_ENDPOINT="https://api.openai.com/v1/chat/completions"
export BILIBILI_LLM_MODEL="gpt-4o-mini"
export BILIBILI_LLM_API_KEY="your_openai_api_key_here"如何获取Bilibili Cookie
- 登录您的Bilibili帐户https://www.bilibili.com
- 打开浏览器开发工具(F12)
- 转到应用程序/存储选项卡→ Cookie→ https://www.bilibili.com
- 复制所需Cookie的值:
- SESSDATA (必填) - buvid3 (可选) - bili_jct (可选) - DedeUserID (可选) - DedeUserID__ckMd5 (可选)
可用功能
- 获取_视频_信息:获取全面的视频信息
# Usage: Provide BV ID (like BV1xV4y1A7NC) or AV ID (like av123456)- 获取_视频_子标题:获取可用字幕,包括人工智能生成的字幕
# Returns list of available subtitles with download URLs
# Caches subtitles.txt with full subtitle content- get_subtitle_content:下载并解析实际字幕内容
# Parse subtitle JSON format with timestamps and text- get_a_summary:获取AI生成的视频摘要和大纲
# Returns structured summary with timestamps and key points
# Also extracts and caches subtitles.txt if available in the response- 搜索_视频:在Bilibili中搜索视频
# Comprehensive search with pagination support- get_user_info:获取详细的用户配置文件信息
# User stats, bio, level, and other profile data- 获取_视频_注释:获取带分页的视频评论
# Support for different sorting: by time, likes, or replies- get_video_danmaku:获取XML格式的danmaku(项目符号注释)
# Raw danmaku data with timestamps and styling info- 获取_文章:使用JinaAI阅读器获取Bilibili文章内容(或使用raw=true的原始元数据)
# By default: Returns full article content in markdown format via JinaAI reader
# With raw=true: Returns basic article metadata from Bilibili API
# Supports env variable JINA_READER_URL to use custom JinaAI instance- get_current_user_info:获取当前登录的用户信息
# Current user profile, stats, and login status- get_content_transcription:对视频内容进行LLM清理转录
# Automatically ensures subtitles are available, then uses LLM to remove ads,
# filler words, and transcription errors while preserving meaningful content
# Requires LLM environment variables to be configuredBilibili使用示例
# Get current user info
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_current_user_info", "arguments": {}}}' | ./bin/mcp-bilibili
# Get video information
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_video_info", "arguments": {"identifier": "BV1gqMczxEtc"}}}' | ./bin/mcp-bilibili
# Get article content using JinaAI reader (default behavior)
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_article", "arguments": {"cvid": "cv42058664"}}}' | ./bin/mcp-bilibili
# Get raw article metadata from Bilibili API
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_article", "arguments": {"cvid": "cv42058664", "raw": true}}}' | ./bin/mcp-bilibili
# Get AI summary
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_ai_summary", "arguments": {"identifier": "BV1gqMczxEtc"}}}' | ./bin/mcp-bilibili
# Search for videos
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "search_videos", "arguments": {"keyword": "明日方舟", "page": 1}}}' | ./bin/mcp-bilibili
# Get content transcription (cleaned subtitles)
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_content_transcription", "arguments": {"identifier": "BV1gqMczxEtc"}}}' | ./bin/mcp-bilibiliLLM内容转录
配置LLM环境变量后,您可以使用 get_content_transcription 用于清理视频转录的工具:
- 字幕.txt:原始完整字幕内容(由缓存
get_video_subtitles或get_ai_summary) - 转录.txt:LLM清理版本,删除了广告、填充词和转录错误
这 get_content_transcription 工具:
- 自动确保字幕可用(通话
get_video_subtitles内部(如果需要) - 使用LLM通过以下方式清理内容:
- 删除广告内容和赞助商消息 - 删除不必要的重复和填充词(如“嗯”、“呃”、过多的“那个”,"然后",等等) - 修复明显的转录错误 - 保留所有有意义的内容和技术信息 - 保持自然流动和结构 - 保留重要的上下文和解释
- 将结果缓存为
transcription.txt - 如果未配置LLM环境变量,则返回错误
当LLM需要理解视频内容,但原始字幕太冗长或包含太多噪音时,这尤其有用。
兼容任何与OpenAI兼容的API(OpenAI、Azure OpenAI、本地模型等)。
身份验证和WBI签名
该工具会自动处理:
- 为需要WBI签名的API请求生成WBI签名
- 基于Cookie的身份验证
- WBI密钥的速率限制和缓存(有效期约为24小时)
- 正确的请求头和引用者
创建自己的工具
要创建新工具,请执行以下操作:
- 在中创建一个新的TypeScript文件
src目录(例如。,src/mytool.ts) - 使用现有工具作为模板
- 将构建脚本添加到
package.json:
"build:mytool": "mkdir -p bin && bun build src/mytool.ts --compile --minify --sourcemap --outfile bin/mcp-mytool"- 更新
build:all包含新工具的脚本
用法
构建可执行文件
# Build the Bilibili tool
pnpm build生成的可执行文件将位于 bin 目录,可以直接运行:
./bin/mcp-bilibili光标注释
当使用带有游标的工具时,请始终使用可执行文件的完整路径:
/path/to/your/project/bin/mcp-bilibili或者,您可以直接使用Node运行TypeScript文件:
/path/to/node ~/path/to/project/src/bilibili.ts测试
# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverage备注
- Bilibili工具需要有效的Cookie才能实现大多数功能
- 某些API可能有速率限制-该工具包括基本缓存
- WBI签名是自动生成和缓存的,以提高效率
- 视频ID可以作为BV代码(BV1xV4y1A7NC)或AV代码(av123456)提供
- 大多数端点都支持登录和访客访问,但有些功能需要身份验证
