Skip to content

Commit 076a1d0

Browse files
committed
Add SSE transport support to MCP bridge
1 parent f073a91 commit 076a1d0

1 file changed

Lines changed: 133 additions & 1 deletion

File tree

mcp-bridge/main.js

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,137 @@
11
#!/usr/bin/env node
2-
// Group management
2+
// MCP message handler
3+
// ===================================================================
4+
5+
async function handleMessage(line) {
6+
let msg;
7+
try {
8+
msg = JSON.parse(line);
9+
} catch {
10+
sendError(null, -32700, "Parse error");
11+
return;
12+
}
13+
14+
const { id, method, params } = msg;
15+
16+
switch (method) {
17+
case "initialize": {
18+
info("MCP initialize", { client: params?.clientInfo?.name });
19+
sendResult(id, {
20+
protocolVersion: "2024-11-05",
21+
capabilities: { tools: { listChanged: false } },
22+
serverInfo: { name: SERVER_NAME, version: SERVER_VERSION },
23+
});
24+
break;
25+
}
26+
27+
case "notifications/initialized":
28+
break;
29+
=======
30+
// ===================================================================
31+
// SSE (Server-Sent Events) transport
32+
// ===================================================================
33+
34+
/**
35+
* Initialize SSE transport.
36+
*/
37+
function initSSE() {
38+
info("Initializing SSE transport...");
39+
}
40+
41+
/**
42+
* Add an SSE client.
43+
* @param {object} client
44+
*/
45+
function addSSEClient(client) {
46+
const clientId = sseId++;
47+
sseClients.add({ id: clientId, client });
48+
info(`SSE client connected: ${clientId}`);
49+
return clientId;
50+
}
51+
52+
/**
53+
* Remove an SSE client.
54+
* @param {number} clientId
55+
*/
56+
function removeSSEClient(clientId) {
57+
const client = Array.from(sseClients).find((c) => c.id === clientId);
58+
if (client) {
59+
sseClients.delete(client);
60+
info(`SSE client disconnected: ${clientId}`);
61+
}
62+
}
63+
64+
/**
65+
* Send an SSE event to all clients.
66+
* @param {string} event
67+
* @param {object} data
68+
*/
69+
function sendSSEEvent(event, data) {
70+
const message = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
71+
sseClients.forEach((client) => {
72+
try {
73+
client.client.write(message);
74+
} catch (error) {
75+
warn(`Failed to send SSE event to client ${client.id}: ${error.message}`);
76+
}
77+
});
78+
}
79+
80+
// ===================================================================
81+
// MCP message handler
82+
// ===================================================================
83+
84+
async function handleMessage(line) {
85+
let msg;
86+
try {
87+
msg = JSON.parse(line);
88+
} catch {
89+
sendError(null, -32700, "Parse error");
90+
return;
91+
}
92+
93+
const { id, method, params } = msg;
94+
95+
switch (method) {
96+
case "initialize": {
97+
info("MCP initialize", { client: params?.clientInfo?.name });
98+
sendResult(id, {
99+
protocolVersion: "2024-11-05",
100+
capabilities: { tools: { listChanged: false } },
101+
serverInfo: { name: SERVER_NAME, version: SERVER_VERSION },
102+
});
103+
break;
104+
}
105+
106+
case "notifications/initialized":
107+
break;JSON-RPC stdio transport
108+
// ===================================================================
109+
110+
let buffer = "";
111+
const MAX_BUFFER_BYTES = 2 * 1_048_576; // 2 MB
112+
113+
// Auto-reconnect configuration
114+
const MAX_RECONNECT_ATTEMPTS = 5;
115+
const INITIAL_RECONNECT_DELAY = 1000; // 1 second
116+
let reconnectAttempts = 0;
117+
let reconnectTimeout = null;
118+
=======
119+
// ===================================================================
120+
// JSON-RPC stdio transport
121+
// ===================================================================
122+
123+
let buffer = "";
124+
const MAX_BUFFER_BYTES = 2 * 1_048_576; // 2 MB
125+
126+
// Auto-reconnect configuration
127+
const MAX_RECONNECT_ATTEMPTS = 5;
128+
const INITIAL_RECONNECT_DELAY = 1000; // 1 second
129+
let reconnectAttempts = 0;
130+
let reconnectTimeout = null;
131+
132+
// SSE (Server-Sent Events) support
133+
const sseClients = new Set();
134+
let sseId = 0;Group management
3135
// ===================================================================
4136

5137
/**

0 commit comments

Comments
 (0)