-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.ts
180 lines (161 loc) · 4.86 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ErrorCode,
ListToolsRequestSchema,
McpError,
} from "@modelcontextprotocol/sdk/types.js";
import { searchBilibili } from "./src/index.js";
interface BilibiliSearchResult {
title: string;
author: string;
play_count: number;
duration: string;
publish_date: string;
url: string;
bvid: string;
upic: string;
pic: string;
}
const isValidSearchArgs = (
args: any
): args is { keyword: string; page?: number; limit?: number } =>
typeof args === "object" &&
args !== null &&
typeof args.keyword === "string" &&
(args.page === undefined || typeof args.page === "number") &&
(args.limit === undefined || typeof args.limit === "number");
class BilibiliSearchServer {
private server: Server;
constructor() {
this.server = new Server(
{
name: "bilibili-search",
version: "0.1.0",
},
{
capabilities: {
tools: {},
},
}
);
this.setupToolHandlers();
this.server.onerror = (error) => console.error("[MCP Error]", error);
process.on("SIGINT", async () => {
await this.server.close();
process.exit(0);
});
}
private setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "bilibili-search",
description: "搜索B站视频内容",
inputSchema: {
type: "object",
properties: {
keyword: {
type: "string",
description: "搜索关键词",
},
page: {
type: "number",
description: "页码(默认:1)",
minimum: 1,
},
limit: {
type: "number",
description: "返回结果数量(默认:10)",
minimum: 1,
maximum: 20,
},
},
required: ["keyword"],
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name !== "bilibili-search") {
throw new McpError(
ErrorCode.MethodNotFound,
`未知工具: ${request.params.name}`
);
}
if (!isValidSearchArgs(request.params.arguments)) {
throw new McpError(ErrorCode.InvalidParams, "无效的搜索参数");
}
const keyword = request.params.arguments.keyword;
const page = request.params.arguments.page || 1;
const limit = Math.min(request.params.arguments.limit || 10, 20);
try {
const results = await this.performSearch(keyword, page, limit);
return {
content: [
{
type: "text",
text: JSON.stringify(results, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `搜索错误: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
isError: true,
};
}
});
}
private async performSearch(
keyword: string,
page: number,
limit: number
): Promise<BilibiliSearchResult[]> {
try {
// 调用src/index.ts中的searchBilibili函数获取搜索结果
const searchResults = await searchBilibili(keyword, page, limit);
// 处理视频项目
const results: BilibiliSearchResult[] = searchResults.map((video: any) => ({
title: this.cleanTitle(video.title),
author: video.author || "",
play_count: parseInt(video.play) || 0,
duration: video.duration || "",
publish_date: this.formatDate(video.pubdate),
url: video.arcurl || `https://www.bilibili.com/video/${video.bvid}`,
bvid: video.bvid || "",
upic: video.upic || "",
pic: video.pic || "",
}));
return results;
} catch (error) {
console.error("搜索B站视频时出错:", error);
throw new Error(`搜索B站视频失败: ${error instanceof Error ? error.message : String(error)}`);
}
}
private cleanTitle(title: string): string {
if (!title) return "";
return title.replace(/<em class="keyword">(.*?)<\/em>/g, "$1");
}
private formatDate(timestamp: number): string {
if (!timestamp) return "";
const date = new Date(timestamp * 1000);
return date.toISOString().split("T")[0];
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error("Bilibili Search MCP server running on stdio");
}
}
const server = new BilibiliSearchServer();
server.run().catch(console.error);