Skip to content

Commit 692c863

Browse files
authored
Revert "fix: handle the client disconnect so that the server does not crash."
1 parent f1eb488 commit 692c863

File tree

1 file changed

+17
-54
lines changed

1 file changed

+17
-54
lines changed

server/src/index.ts

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ let backingServerTransport: Transport | undefined;
141141

142142
app.get("/mcp", async (req, res) => {
143143
const sessionId = req.headers["mcp-session-id"] as string;
144-
console.log(`GET /mcp for sessionId ${sessionId}`);
144+
console.log(`Received GET message for sessionId ${sessionId}`);
145145
try {
146146
const transport = webAppTransports.get(
147147
sessionId,
@@ -160,7 +160,7 @@ app.get("/mcp", async (req, res) => {
160160

161161
app.post("/mcp", async (req, res) => {
162162
const sessionId = req.headers["mcp-session-id"] as string | undefined;
163-
console.log(`POST /mcp for sessionId ${sessionId}`);
163+
console.log(`Received POST message for sessionId ${sessionId}`);
164164
if (!sessionId) {
165165
try {
166166
console.log("New streamable-http connection");
@@ -228,7 +228,7 @@ app.post("/mcp", async (req, res) => {
228228

229229
app.get("/stdio", async (req, res) => {
230230
try {
231-
console.log("GET /stdio");
231+
console.log("New connection");
232232

233233
try {
234234
await backingServerTransport?.close();
@@ -254,53 +254,18 @@ app.get("/stdio", async (req, res) => {
254254
console.log("Created web app transport");
255255

256256
await webAppTransport.start();
257-
258-
// Handle client disconnection
259-
res.on("close", () => {
260-
console.log(
261-
`Client disconnected from session ${webAppTransport.sessionId}`,
262-
);
263-
// Clean up the transport map
264-
webAppTransports.delete(webAppTransport.sessionId);
265-
});
266-
267-
// Create a stderr handler that checks connection state
268-
const stderrHandler = (chunk: Buffer) => {
269-
// Only send if the transport exists in our map (meaning it's still active)
270-
if (webAppTransports.has(webAppTransport.sessionId)) {
271-
webAppTransport
272-
.send({
273-
jsonrpc: "2.0",
274-
method: "notifications/stderr",
275-
params: {
276-
content: chunk.toString(),
277-
},
278-
})
279-
.catch((error: any) => {
280-
console.error(
281-
`Error sending stderr data to client: ${error.message}`,
282-
);
283-
// If we hit an error sending, clean up the transport
284-
webAppTransports.delete(webAppTransport.sessionId);
285-
});
286-
}
287-
};
288-
289-
if ((backingServerTransport as StdioClientTransport).stderr) {
290-
(backingServerTransport as StdioClientTransport).stderr!.on(
291-
"data",
292-
stderrHandler,
293-
);
294-
295-
// Store the handler reference so we can remove it when client disconnects
296-
res.on("close", () => {
297-
if ((backingServerTransport as StdioClientTransport).stderr) {
298-
(
299-
backingServerTransport as StdioClientTransport
300-
).stderr!.removeListener("data", stderrHandler);
301-
}
302-
});
303-
}
257+
(backingServerTransport as StdioClientTransport).stderr!.on(
258+
"data",
259+
(chunk) => {
260+
webAppTransport.send({
261+
jsonrpc: "2.0",
262+
method: "notifications/stderr",
263+
params: {
264+
content: chunk.toString(),
265+
},
266+
});
267+
},
268+
);
304269

305270
mcpProxy({
306271
transportToClient: webAppTransport,
@@ -317,7 +282,7 @@ app.get("/stdio", async (req, res) => {
317282
app.get("/sse", async (req, res) => {
318283
try {
319284
console.log(
320-
"GET /sse (NOTE: The sse transport is deprecated and has been replaced by streamable-http)",
285+
"New SSE connection. NOTE: The sse transport is deprecated and has been replaced by streamable-http",
321286
);
322287

323288
try {
@@ -359,7 +324,7 @@ app.get("/sse", async (req, res) => {
359324
app.post("/message", async (req, res) => {
360325
try {
361326
const sessionId = req.query.sessionId;
362-
console.log(`POST /message for sessionId ${sessionId}`);
327+
console.log(`Received message for sessionId ${sessionId}`);
363328

364329
const transport = webAppTransports.get(
365330
sessionId as string,
@@ -376,15 +341,13 @@ app.post("/message", async (req, res) => {
376341
});
377342

378343
app.get("/health", (req, res) => {
379-
console.log("GET /health");
380344
res.json({
381345
status: "ok",
382346
});
383347
});
384348

385349
app.get("/config", (req, res) => {
386350
try {
387-
console.log("GET /config");
388351
res.json({
389352
defaultEnvironment,
390353
defaultCommand: values.env,

0 commit comments

Comments
 (0)