@datafrog-io/n2n-nexus 0.3.6 → 0.3.7

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.
@@ -39,12 +39,18 @@ export async function startGuest(targetPort, context) {
39
39
  process.stdin.removeAllListeners("data");
40
40
  console.error(`[Nexus:${guestId}] Connecting to Host at ${targetPort}...`);
41
41
  let sessionId = null;
42
- const stdioHandler = (chunk) => {
43
- if (!sessionId)
42
+ let pendingStdin = [];
43
+ let sseBuffer = "";
44
+ // Client connection should use 127.0.0.1 if host is 0.0.0.0
45
+ const connectHost = NEXUS_HOST === "0.0.0.0" ? "127.0.0.1" : NEXUS_HOST;
46
+ const forwardToHost = (chunk) => {
47
+ if (!sessionId) {
48
+ pendingStdin.push(chunk);
44
49
  return;
50
+ }
45
51
  try {
46
52
  const req = http.request({
47
- hostname: NEXUS_HOST,
53
+ hostname: connectHost,
48
54
  port: targetPort,
49
55
  path: `/mcp?sessionId=${sessionId}&id=${encodeURIComponent(guestId)}`,
50
56
  method: "POST",
@@ -56,25 +62,38 @@ export async function startGuest(targetPort, context) {
56
62
  }
57
63
  catch { /* suppress */ }
58
64
  };
65
+ const stdioHandler = (chunk) => forwardToHost(chunk);
59
66
  process.stdin.on("data", stdioHandler);
60
- http.get(`http://${NEXUS_HOST}:${targetPort}/mcp?id=${encodeURIComponent(guestId)}`, (res) => {
61
- let buffer = "";
67
+ http.get(`http://${connectHost}:${targetPort}/mcp?id=${encodeURIComponent(guestId)}`, (res) => {
62
68
  res.on("data", (chunk) => {
63
- const str = chunk.toString();
64
- buffer += str;
65
- if (!sessionId && buffer.includes("event: endpoint")) {
66
- const match = buffer.match(/sessionId=([a-f0-9-]+)/);
67
- if (match)
68
- sessionId = match[1];
69
- }
70
- if (str.includes("event: message")) {
71
- const lines = str.split("\n");
72
- const dataLine = lines.find((l) => l.startsWith("data: "));
73
- if (dataLine) {
74
- try {
75
- process.stdout.write(dataLine.substring(6) + "\n");
69
+ sseBuffer += chunk.toString();
70
+ const lines = sseBuffer.split("\n");
71
+ sseBuffer = lines.pop() || ""; // Keep trailing incomplete line
72
+ for (const line of lines) {
73
+ const cleanLine = line.trim();
74
+ if (!cleanLine)
75
+ continue;
76
+ if (cleanLine.startsWith("data: ")) {
77
+ const content = cleanLine.substring(6);
78
+ // Check if this is the endpoint/session assignment
79
+ if (!sessionId && content.includes("sessionId=")) {
80
+ const match = content.match(/sessionId=([a-f0-9-]+)/);
81
+ if (match) {
82
+ sessionId = match[1];
83
+ // console.error(`[Nexus:${guestId}] Session established: ${sessionId}`);
84
+ // Flush pending messages
85
+ const toFlush = [...pendingStdin];
86
+ pendingStdin = [];
87
+ toFlush.forEach(forwardToHost);
88
+ }
89
+ }
90
+ else if (content) {
91
+ // Assume JSON-RPC message
92
+ try {
93
+ process.stdout.write(content + "\n");
94
+ }
95
+ catch { /* ignore */ }
76
96
  }
77
- catch { /* ignore */ }
78
97
  }
79
98
  }
80
99
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datafrog-io/n2n-nexus",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Modular MCP Server for multi-AI assistant coordination",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
@@ -58,4 +58,4 @@
58
58
  "typescript-eslint": "^8.51.0",
59
59
  "vitest": "^4.0.16"
60
60
  }
61
- }
61
+ }