aira-mcp 1.0.1 → 1.0.2

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.
Files changed (2) hide show
  1. package/index.js +86 -16
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -138,22 +138,92 @@ app.delete('/download/:gid', async (req, res) => {
138
138
  }
139
139
  });
140
140
 
141
- // 启动服务器
142
- app.listen(port, () => {
143
- // 使用stderr输出日志信息,避免干扰MCPJSON输出
144
- process.stderr.write(`MCP Service running at http://localhost:${port}\n`);
145
- process.stderr.write(`Configured to connect to aria2 at ${aria2Secure ? 'https' : 'http'}://${aria2Host}:${aria2Port}${aria2Path}\n`);
146
-
147
- // 稍后尝试连接aria2服务器
148
- setTimeout(async () => {
141
+ // MCP协议处理
142
+ function handleMCPRequest(request) {
143
+ process.stderr.write(`Received MCP request: ${JSON.stringify(request)}\n`);
144
+
145
+ // 简单的MCP响应处理
146
+ const response = {
147
+ jsonrpc: '2.0',
148
+ id: request.id,
149
+ result: {
150
+ message: 'MCP Service is running',
151
+ httpPort: port,
152
+ status: 'ok'
153
+ }
154
+ };
155
+
156
+ return response;
157
+ }
158
+
159
+ // 启动HTTP服务器
160
+ app.listen(port, () => {
161
+ process.stderr.write(`MCP Service running at http://localhost:${port}\n`);
162
+ process.stderr.write(`Configured to connect to aria2 at ${aria2Secure ? 'https' : 'http'}://${aria2Host}:${aria2Port}${aria2Path}\n`);
163
+
164
+ // 稍后尝试连接aria2服务器
165
+ setTimeout(async () => {
166
+ try {
167
+ // 尝试连接aria2服务器
168
+ await aria2.open();
169
+ process.stderr.write('Successfully connected to aria2 server\n');
170
+ await aria2.close(); // 关闭初始连接
171
+ } catch (error) {
172
+ process.stderr.write(`Warning: Could not connect to aria2 server at ${aria2Secure ? 'https' : 'http'}://${aria2Host}:${aria2Port}${aria2Path}: ${error.message}\n`);
173
+ process.stderr.write('MCP Service will still run, but aria2 operations will fail until a valid aria2 server is available.\n');
174
+ }
175
+ }, 1000); // 延迟1秒后尝试连接
176
+ });
177
+
178
+ // 处理MCP通信:读取stdin,写入stdout
179
+ let buffer = '';
180
+
181
+ process.stdin.on('data', (data) => {
182
+ buffer += data.toString();
183
+
184
+ // 简单的JSON-RPC解析,假设每个请求是完整的一行
185
+ const lines = buffer.split('\n');
186
+ buffer = lines.pop(); // 保存不完整的行
187
+
188
+ for (const line of lines) {
189
+ if (line.trim()) {
149
190
  try {
150
- // 尝试连接aria2服务器
151
- await aria2.open();
152
- process.stderr.write('Successfully connected to aria2 server\n');
153
- await aria2.close(); // 关闭初始连接
191
+ const request = JSON.parse(line);
192
+ const response = handleMCPRequest(request);
193
+ process.stdout.write(JSON.stringify(response) + '\n');
154
194
  } catch (error) {
155
- process.stderr.write(`Warning: Could not connect to aria2 server at ${aria2Secure ? 'https' : 'http'}://${aria2Host}:${aria2Port}${aria2Path}: ${error.message}\n`);
156
- process.stderr.write('MCP Service will still run, but aria2 operations will fail until a valid aria2 server is available.\n');
195
+ process.stderr.write(`Error handling MCP request: ${error}\n`);
157
196
  }
158
- }, 1000); // 延迟1秒后尝试连接
159
- });
197
+ }
198
+ }
199
+ });
200
+
201
+ // 确保进程不会退出
202
+ process.stdin.resume();
203
+
204
+ // 处理进程结束信号
205
+ process.on('SIGINT', () => {
206
+ process.stderr.write('Received SIGINT, exiting...\n');
207
+ process.exit(0);
208
+ });
209
+
210
+ process.on('SIGTERM', () => {
211
+ process.stderr.write('Received SIGTERM, exiting...\n');
212
+ process.exit(0);
213
+ });
214
+
215
+ // 初始化MCP服务就绪通知
216
+ const initResponse = {
217
+ jsonrpc: '2.0',
218
+ id: null,
219
+ result: {
220
+ message: 'MCP Service initialized',
221
+ httpPort: port,
222
+ status: 'ready'
223
+ }
224
+ };
225
+
226
+ // 发送初始化响应
227
+ setTimeout(() => {
228
+ process.stdout.write(JSON.stringify(initResponse) + '\n');
229
+ }, 500);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aira-mcp",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "aira-mcp": "index.js"