@niroai/mcp-server 1.0.0 → 1.0.1
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 +39 -25
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -44,11 +44,15 @@ let sessionId = null;
|
|
|
44
44
|
*/
|
|
45
45
|
function connectSse() {
|
|
46
46
|
return new Promise((resolve, reject) => {
|
|
47
|
-
const sseUrl = `${API_URL}/mcp/sse`;
|
|
48
|
-
process.stderr.write(`[niro-mcp] Connecting to ${
|
|
47
|
+
const sseUrl = `${API_URL}/mcp/sse?api_key=${encodeURIComponent(API_KEY)}&project_id=${encodeURIComponent(PROJECT_ID)}`;
|
|
48
|
+
process.stderr.write(`[niro-mcp] Connecting to ${API_URL}/mcp/sse\n`);
|
|
49
49
|
|
|
50
50
|
const req = httpModule.get(sseUrl, {
|
|
51
|
-
headers: {
|
|
51
|
+
headers: {
|
|
52
|
+
"Accept": "text/event-stream",
|
|
53
|
+
"X-Niro-Api-Key": API_KEY,
|
|
54
|
+
"X-Niro-Project-Id": PROJECT_ID,
|
|
55
|
+
},
|
|
52
56
|
}, (res) => {
|
|
53
57
|
if (res.statusCode !== 200) {
|
|
54
58
|
reject(new Error(`SSE connection failed: HTTP ${res.statusCode}`));
|
|
@@ -57,31 +61,41 @@ function connectSse() {
|
|
|
57
61
|
|
|
58
62
|
let buffer = "";
|
|
59
63
|
|
|
64
|
+
let currentEvent = null;
|
|
65
|
+
let currentData = "";
|
|
66
|
+
|
|
60
67
|
res.on("data", (chunk) => {
|
|
61
68
|
buffer += chunk.toString();
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
69
|
+
|
|
70
|
+
// SSE events are delimited by double newline
|
|
71
|
+
let doubleNewline;
|
|
72
|
+
while ((doubleNewline = buffer.indexOf("\n\n")) !== -1) {
|
|
73
|
+
const block = buffer.slice(0, doubleNewline);
|
|
74
|
+
buffer = buffer.slice(doubleNewline + 2);
|
|
75
|
+
|
|
76
|
+
// Parse the SSE block
|
|
77
|
+
let eventName = null;
|
|
78
|
+
let dataLines = [];
|
|
79
|
+
for (const line of block.split("\n")) {
|
|
80
|
+
if (line.startsWith("event:")) {
|
|
81
|
+
eventName = line.slice(6).trim();
|
|
82
|
+
} else if (line.startsWith("data:")) {
|
|
83
|
+
dataLines.push(line.slice(5));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const data = dataLines.join("\n").trim();
|
|
88
|
+
if (!eventName || !data) continue;
|
|
89
|
+
|
|
90
|
+
if (eventName === "endpoint") {
|
|
91
|
+
const match = data.match(/sessionId=([a-f0-9-]+)/);
|
|
92
|
+
if (match) {
|
|
93
|
+
sessionId = match[1];
|
|
94
|
+
process.stderr.write(`[niro-mcp] Session established: ${sessionId}\n`);
|
|
95
|
+
resolve();
|
|
83
96
|
}
|
|
84
|
-
|
|
97
|
+
} else if (eventName === "message") {
|
|
98
|
+
process.stdout.write(data + "\n");
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
});
|