aira-mcp 1.0.0 → 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.
- package/index.js +92 -21
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -67,7 +67,7 @@ app.post('/download', async (req, res) => {
|
|
|
67
67
|
const result = await aria2.call('addUri', [uri], options || {});
|
|
68
68
|
res.json({ success: true, gid: result });
|
|
69
69
|
} catch (error) {
|
|
70
|
-
|
|
70
|
+
process.stderr.write(`Error adding URI to aria2: ${error}\n`);
|
|
71
71
|
res.status(500).json({ error: `Failed to add URI to aria2: ${error.message}` });
|
|
72
72
|
}
|
|
73
73
|
});
|
|
@@ -85,7 +85,7 @@ app.post('/download/magnet', async (req, res) => {
|
|
|
85
85
|
const result = await aria2.call('addUri', [magnetUri], options || {});
|
|
86
86
|
res.json({ success: true, gid: result });
|
|
87
87
|
} catch (error) {
|
|
88
|
-
|
|
88
|
+
process.stderr.write(`Error adding magnet to aria2: ${error}\n`);
|
|
89
89
|
res.status(500).json({ error: `Failed to add magnet to aria2: ${error.message}` });
|
|
90
90
|
}
|
|
91
91
|
});
|
|
@@ -97,7 +97,7 @@ app.get('/download/:gid', async (req, res) => {
|
|
|
97
97
|
const result = await aria2.call('tellStatus', gid);
|
|
98
98
|
res.json({ success: true, status: result });
|
|
99
99
|
} catch (error) {
|
|
100
|
-
|
|
100
|
+
process.stderr.write(`Error getting download status from aria2: ${error}\n`);
|
|
101
101
|
res.status(500).json({ error: `Failed to get download status from aria2: ${error.message}` });
|
|
102
102
|
}
|
|
103
103
|
});
|
|
@@ -109,7 +109,7 @@ app.post('/download/:gid/pause', async (req, res) => {
|
|
|
109
109
|
await aria2.call('pause', gid);
|
|
110
110
|
res.json({ success: true, message: 'Download paused' });
|
|
111
111
|
} catch (error) {
|
|
112
|
-
|
|
112
|
+
process.stderr.write(`Error pausing download in aria2: ${error}\n`);
|
|
113
113
|
res.status(500).json({ error: `Failed to pause download in aria2: ${error.message}` });
|
|
114
114
|
}
|
|
115
115
|
});
|
|
@@ -121,7 +121,7 @@ app.post('/download/:gid/resume', async (req, res) => {
|
|
|
121
121
|
await aria2.call('unpause', gid);
|
|
122
122
|
res.json({ success: true, message: 'Download resumed' });
|
|
123
123
|
} catch (error) {
|
|
124
|
-
|
|
124
|
+
process.stderr.write(`Error resuming download in aria2: ${error}\n`);
|
|
125
125
|
res.status(500).json({ error: `Failed to resume download in aria2: ${error.message}` });
|
|
126
126
|
}
|
|
127
127
|
});
|
|
@@ -133,26 +133,97 @@ app.delete('/download/:gid', async (req, res) => {
|
|
|
133
133
|
await aria2.call('remove', gid);
|
|
134
134
|
res.json({ success: true, message: 'Download removed' });
|
|
135
135
|
} catch (error) {
|
|
136
|
-
|
|
136
|
+
process.stderr.write(`Error removing download from aria2: ${error}\n`);
|
|
137
137
|
res.status(500).json({ error: `Failed to remove download from aria2: ${error.message}` });
|
|
138
138
|
}
|
|
139
139
|
});
|
|
140
140
|
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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()) {
|
|
148
190
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
await aria2.close(); // 关闭初始连接
|
|
191
|
+
const request = JSON.parse(line);
|
|
192
|
+
const response = handleMCPRequest(request);
|
|
193
|
+
process.stdout.write(JSON.stringify(response) + '\n');
|
|
153
194
|
} catch (error) {
|
|
154
|
-
|
|
155
|
-
console.log('MCP Service will still run, but aria2 operations will fail until a valid aria2 server is available.');
|
|
195
|
+
process.stderr.write(`Error handling MCP request: ${error}\n`);
|
|
156
196
|
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
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);
|