aira-mcp 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +123 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -138,22 +138,129 @@ app.delete('/download/:gid', async (req, res) => {
|
|
|
138
138
|
}
|
|
139
139
|
});
|
|
140
140
|
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
|
|
141
|
+
// MCP协议处理
|
|
142
|
+
function handleMCPRequest(request) {
|
|
143
|
+
process.stderr.write(`Received MCP request: ${JSON.stringify(request)}\n`);
|
|
144
|
+
|
|
145
|
+
// 根据请求方法返回不同的响应
|
|
146
|
+
if (request.method === 'initialize') {
|
|
147
|
+
// 处理initialize请求,返回符合MCP协议规范的响应
|
|
148
|
+
const response = {
|
|
149
|
+
jsonrpc: '2.0',
|
|
150
|
+
id: request.id,
|
|
151
|
+
result: {
|
|
152
|
+
protocolVersion: '2025-06-18',
|
|
153
|
+
capabilities: {
|
|
154
|
+
// 描述服务支持的功能
|
|
155
|
+
httpServer: {
|
|
156
|
+
port: port,
|
|
157
|
+
endpoints: {
|
|
158
|
+
'/health': 'GET',
|
|
159
|
+
'/download': 'POST',
|
|
160
|
+
'/download/magnet': 'POST',
|
|
161
|
+
'/download/{gid}': 'GET',
|
|
162
|
+
'/download/{gid}/pause': 'POST',
|
|
163
|
+
'/download/{gid}/resume': 'POST',
|
|
164
|
+
'/download/{gid}': 'DELETE'
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
aria2: {
|
|
168
|
+
supportedMethods: ['addUri', 'tellStatus', 'pause', 'unpause', 'remove']
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
serverInfo: {
|
|
172
|
+
name: 'Aira MCP Service',
|
|
173
|
+
version: '1.0.3',
|
|
174
|
+
description: 'Aria2 MCP (Model Context Protocol) Service CLI',
|
|
175
|
+
author: '',
|
|
176
|
+
license: 'ISC'
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
return response;
|
|
181
|
+
} else {
|
|
182
|
+
// 处理其他请求
|
|
183
|
+
const response = {
|
|
184
|
+
jsonrpc: '2.0',
|
|
185
|
+
id: request.id,
|
|
186
|
+
result: {
|
|
187
|
+
message: 'MCP Service is running',
|
|
188
|
+
httpPort: port,
|
|
189
|
+
status: 'ok'
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
return response;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// 启动HTTP服务器
|
|
197
|
+
app.listen(port, () => {
|
|
198
|
+
process.stderr.write(`MCP Service running at http://localhost:${port}\n`);
|
|
199
|
+
process.stderr.write(`Configured to connect to aria2 at ${aria2Secure ? 'https' : 'http'}://${aria2Host}:${aria2Port}${aria2Path}\n`);
|
|
200
|
+
|
|
201
|
+
// 稍后尝试连接aria2服务器
|
|
202
|
+
setTimeout(async () => {
|
|
203
|
+
try {
|
|
204
|
+
// 尝试连接aria2服务器
|
|
205
|
+
await aria2.open();
|
|
206
|
+
process.stderr.write('Successfully connected to aria2 server\n');
|
|
207
|
+
await aria2.close(); // 关闭初始连接
|
|
208
|
+
} catch (error) {
|
|
209
|
+
process.stderr.write(`Warning: Could not connect to aria2 server at ${aria2Secure ? 'https' : 'http'}://${aria2Host}:${aria2Port}${aria2Path}: ${error.message}\n`);
|
|
210
|
+
process.stderr.write('MCP Service will still run, but aria2 operations will fail until a valid aria2 server is available.\n');
|
|
211
|
+
}
|
|
212
|
+
}, 1000); // 延迟1秒后尝试连接
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// 处理MCP通信:读取stdin,写入stdout
|
|
216
|
+
let buffer = '';
|
|
217
|
+
|
|
218
|
+
process.stdin.on('data', (data) => {
|
|
219
|
+
buffer += data.toString();
|
|
220
|
+
|
|
221
|
+
// 简单的JSON-RPC解析,假设每个请求是完整的一行
|
|
222
|
+
const lines = buffer.split('\n');
|
|
223
|
+
buffer = lines.pop(); // 保存不完整的行
|
|
224
|
+
|
|
225
|
+
for (const line of lines) {
|
|
226
|
+
if (line.trim()) {
|
|
149
227
|
try {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
process.
|
|
153
|
-
await aria2.close(); // 关闭初始连接
|
|
228
|
+
const request = JSON.parse(line);
|
|
229
|
+
const response = handleMCPRequest(request);
|
|
230
|
+
process.stdout.write(JSON.stringify(response) + '\n');
|
|
154
231
|
} catch (error) {
|
|
155
|
-
process.stderr.write(`
|
|
156
|
-
process.stderr.write('MCP Service will still run, but aria2 operations will fail until a valid aria2 server is available.\n');
|
|
232
|
+
process.stderr.write(`Error handling MCP request: ${error}\n`);
|
|
157
233
|
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// 确保进程不会退出
|
|
239
|
+
process.stdin.resume();
|
|
240
|
+
|
|
241
|
+
// 处理进程结束信号
|
|
242
|
+
process.on('SIGINT', () => {
|
|
243
|
+
process.stderr.write('Received SIGINT, exiting...\n');
|
|
244
|
+
process.exit(0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
process.on('SIGTERM', () => {
|
|
248
|
+
process.stderr.write('Received SIGTERM, exiting...\n');
|
|
249
|
+
process.exit(0);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// 初始化MCP服务就绪通知
|
|
253
|
+
const initResponse = {
|
|
254
|
+
jsonrpc: '2.0',
|
|
255
|
+
id: null,
|
|
256
|
+
result: {
|
|
257
|
+
message: 'MCP Service initialized',
|
|
258
|
+
httpPort: port,
|
|
259
|
+
status: 'ready'
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// 发送初始化响应
|
|
264
|
+
setTimeout(() => {
|
|
265
|
+
process.stdout.write(JSON.stringify(initResponse) + '\n');
|
|
266
|
+
}, 500);
|