107 lines
3.4 KiB
JavaScript
107 lines
3.4 KiB
JavaScript
const https = require('https');
|
|
const { URL } = require('url');
|
|
|
|
const REMOTE_URL = 'https://git.aotoagent.com/mcp';
|
|
const TOKEN = 'gitea-mcp-token-2026';
|
|
|
|
let sessionId = null;
|
|
|
|
async function sendRequest(body) {
|
|
return new Promise((resolve, reject) => {
|
|
const url = new URL(REMOTE_URL);
|
|
const data = JSON.stringify(body);
|
|
|
|
const options = {
|
|
hostname: url.hostname,
|
|
port: url.port || 443,
|
|
path: url.pathname,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json, text/event-stream',
|
|
'Authorization': `Bearer ${TOKEN}`,
|
|
'Content-Length': Buffer.byteLength(data)
|
|
}
|
|
};
|
|
|
|
if (sessionId) {
|
|
options.headers['Mcp-Session-Id'] = sessionId;
|
|
}
|
|
|
|
const req = https.request(options, (res) => {
|
|
let body = '';
|
|
res.on('data', (chunk) => body += chunk);
|
|
res.on('end', () => {
|
|
if (res.headers['mcp-session-id']) {
|
|
sessionId = res.headers['mcp-session-id'];
|
|
}
|
|
resolve(body);
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
req.write(data);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
let buffer = '';
|
|
|
|
process.stdin.on('data', async (chunk) => {
|
|
buffer += chunk.toString();
|
|
|
|
// Process complete lines
|
|
while (buffer.includes('\n')) {
|
|
const newlineIndex = buffer.indexOf('\n');
|
|
const line = buffer.substring(0, newlineIndex).trim();
|
|
buffer = buffer.substring(newlineIndex + 1);
|
|
|
|
if (!line) continue;
|
|
|
|
try {
|
|
const request = JSON.parse(line);
|
|
const response = await sendRequest(request);
|
|
|
|
// Handle SSE format
|
|
if (response.startsWith('event:')) {
|
|
const lines = response.split('\n');
|
|
for (const l of lines) {
|
|
if (l.startsWith('data:')) {
|
|
process.stdout.write(l.substring(5).trim() + '\n');
|
|
}
|
|
}
|
|
} else if (response.trim()) {
|
|
process.stdout.write(response + '\n');
|
|
}
|
|
} catch (e) {
|
|
process.stderr.write(`Error: ${e.message}\n`);
|
|
}
|
|
}
|
|
});
|
|
|
|
process.stdin.on('end', () => {
|
|
// Process any remaining buffer
|
|
if (buffer.trim()) {
|
|
try {
|
|
const request = JSON.parse(buffer.trim());
|
|
sendRequest(request).then(response => {
|
|
if (response.startsWith('event:')) {
|
|
const lines = response.split('\n');
|
|
for (const l of lines) {
|
|
if (l.startsWith('data:')) {
|
|
process.stdout.write(l.substring(5).trim() + '\n');
|
|
}
|
|
}
|
|
} else if (response.trim()) {
|
|
process.stdout.write(response + '\n');
|
|
}
|
|
});
|
|
} catch (e) {
|
|
process.stderr.write(`Error: ${e.message}\n`);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
main(); |