-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·298 lines (265 loc) · 7.45 KB
/
index.js
File metadata and controls
executable file
·298 lines (265 loc) · 7.45 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env node
const { Server } = require("@modelcontextprotocol/sdk/server/index.js");
const {
StdioServerTransport,
} = require("@modelcontextprotocol/sdk/server/stdio.js");
const {
CallToolRequestSchema,
ErrorCode,
ListResourcesRequestSchema,
ListResourceTemplatesRequestSchema,
ListToolsRequestSchema,
McpError,
} = require("@modelcontextprotocol/sdk/types.js");
const puppeteer = require('puppeteer');
const { MICRO_APP_DOCS, DOC_TYPES, DOC_SELECTORS } = require('./config/docs.js');
/**
* MicroApp MCP服务器实现
*/
class MicroAppMcpServer {
constructor() {
this.server = new Server(
{
name: "@micro-zoe/micro-mcp",
version: "0.1.1",
},
{
capabilities: {
resources: {},
tools: {},
},
}
);
// 设置错误处理
this.server.onerror = (error) => console.error("[MCP Error]", error);
// 设置进程退出处理
process.on("SIGINT", async () => {
await this.server.close();
process.exit(0);
});
// 初始化工具处理器
this.setupToolHandlers();
}
/**
* 内容清理和格式化
*/
cleanAndFormatContent(content) {
return content
.replace(/\s+/g, ' ')
.replace(/[\n\r]+/g, '\n')
.trim();
}
/**
* 验证文档抓取参数
*/
validateCrawlDocsArgs(args) {
const docType = args?.docType || 'all';
if (!DOC_TYPES.includes(docType)) {
throw new McpError(
ErrorCode.InvalidParams,
`无效的文档类型: ${docType}。有效类型: ${DOC_TYPES.join(', ')}`
);
}
return { docType };
}
/**
* 验证API搜索参数
*/
validateSearchAPIArgs(args) {
if (!args?.query || typeof args.query !== 'string') {
throw new McpError(
ErrorCode.InvalidParams,
'搜索查询参数必须是非空字符串'
);
}
return { query: args.query };
}
/**
* 提取 Micro-app 相关内容
*/
async extractMicroAppContent(page) {
let content = '';
for (const selector of DOC_SELECTORS) {
try {
const element = await page.$(selector);
if (element) {
const elementContent = await page.evaluate(el => el.textContent, element);
if (elementContent) {
content += elementContent + '\n\n';
}
}
} catch (e) {
continue;
}
}
return this.cleanAndFormatContent(content);
}
/**
* 抓取 Micro-app 文档内容
*/
async crawlMicroAppDocs(args) {
const { docType } = this.validateCrawlDocsArgs(args);
let urlsToProcess = [];
// 获取所有文档
if (docType === 'all') {
Object.values(MICRO_APP_DOCS).forEach(category => {
urlsToProcess.push(...category);
});
} else {
// 获取该类型下的文档
urlsToProcess = MICRO_APP_DOCS[docType] || [];
// 添加所有 type 为 common 的文档
Object.values(MICRO_APP_DOCS).forEach(category => {
category.forEach(doc => {
if (doc.type === 'common' && !urlsToProcess.some(d => d.url === doc.url)) {
urlsToProcess.push(doc);
}
});
});
}
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const results = [];
console.error('urlsToProcess', urlsToProcess);
try {
for (const source of urlsToProcess) {
const page = await browser.newPage();
await page.goto(source.url, {
waitUntil: 'networkidle0',
timeout: 60000
});
const content = await this.extractMicroAppContent(page);
results.push({
name: source.name,
url: source.url,
content: content
});
await page.close();
}
} finally {
await browser.close();
}
return {
content: [{
type: 'text',
text: results.map(r => `=== ${r.name} ===\nSource: ${r.url}\n\n${r.content}\n\n`).join('\n')
}]
};
}
/**
* 设置工具处理器
*/
setupToolHandlers() {
// 注册工具列表
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'crawl_micro_app_docs',
description: '抓取 Micro-app 相关文档内容',
inputSchema: {
type: 'object',
properties: {
docType: {
type: 'string',
enum: ['guide', 'frameworks', 'api', 'others', 'all'],
description: '要抓取的文档类型'
}
}
}
},
{
name: 'get_related_links',
description: '获取与指定内容相关的链接',
inputSchema: {
type: 'object',
properties: {
content: {
type: 'string',
description: '要查找相关链接的内容'
},
maxResults: {
type: 'number',
description: '最大返回结果数量',
default: 5
}
},
required: ['content']
}
}
]
}));
// 工具调用处理
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const toolName = request.params.name;
const args = request.params.arguments;
try {
if (toolName === 'crawl_micro_app_docs') {
return await this.crawlMicroAppDocs(args);
} else if (toolName === 'get_related_links') {
return await this.getRelatedLinks(args);
} else {
throw new McpError(ErrorCode.MethodNotFound, `未知工具: ${toolName}`);
}
} catch (error) {
console.error(`工具调用错误 ${toolName}:`, error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(ErrorCode.InternalError, `工具执行错误 ${toolName}: ${error.message}`);
}
});
}
/**
* 获取相关内容链接
* @param {Object} args - 工具参数
* @returns {Promise<Object>} - 相关链接列表
*/
async getRelatedLinks(args) {
const { content, maxResults = 5 } = args;
if (!content) {
throw new McpError(ErrorCode.InvalidParams, '内容不能为空');
}
// 从所有文档中查找相关内容
const allDocs = [
...MICRO_APP_DOCS.guide,
...MICRO_APP_DOCS.features,
...MICRO_APP_DOCS.frameworks,
...MICRO_APP_DOCS.api,
...MICRO_APP_DOCS.others
];
// 根据内容关键词匹配相关文档
const contentLower = content.toLowerCase();
const relatedLinks = allDocs
.filter(doc => {
// 检查文档名称和类型是否包含关键词
return doc.name.toLowerCase().includes(contentLower) ||
doc.type.toLowerCase().includes(contentLower);
})
.map(doc => ({
title: doc.name,
url: doc.url,
description: `${doc.type} - ${doc.name}`,
type: doc.type
}))
.slice(0, maxResults);
return {
links: relatedLinks
};
}
/**
* 启动服务器
*/
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('@micro-zoe/micro-mcp 服务器已启动,运行在IDE上');
}
}
// 创建并启动服务器
const server = new MicroAppMcpServer();
server.run().catch((error) => {
console.error('严重错误:', error);
process.exit(1);
});