From e4513b52e12eda72ceaf5893d32dfdff79f5261f Mon Sep 17 00:00:00 2001 From: Constaline Date: Wed, 5 Mar 2025 09:51:26 +0800 Subject: [PATCH 1/2] Update instructions for running the server with HTTP SSE --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index fe4caa3f8..a50703a78 100644 --- a/README.md +++ b/README.md @@ -224,8 +224,12 @@ const server = new McpServer({ const app = express(); +let transportMap = new Map(); + app.get("/sse", async (req, res) => { const transport = new SSEServerTransport("/messages", res); + const sessionId = transport.sessionId; + transportMap.set(sessionId, transport); await server.connect(transport); }); @@ -233,6 +237,17 @@ app.post("/messages", async (req, res) => { // Note: to support multiple simultaneous connections, these messages will // need to be routed to a specific matching transport. (This logic isn't // implemented here, for simplicity.) + + const sessionId = req.query.sessionId; + if (!sessionId) { + return res.status(400).json({ error: "SessionId is not found" }); + } + + const transport = transportMap.get(sessionId); + if (!transport) { + return res.redirect("/sse"); + } + await transport.handlePostMessage(req, res); }); From 4f2ba070787b5555cdd6fab17f9c5a9b5fca8d49 Mon Sep 17 00:00:00 2001 From: Constaline Date: Sun, 9 Mar 2025 14:00:26 +0800 Subject: [PATCH 2/2] Update instructions to include transport cleanup on SSE connection close --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index a50703a78..501fd4ffb 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,13 @@ let transportMap = new Map(); app.get("/sse", async (req, res) => { const transport = new SSEServerTransport("/messages", res); const sessionId = transport.sessionId; + + // Set the onclose handler to remove the transport from the transportMap + transport.onclose = () => { + transportMap.delete(sessionId); + console.log(`Transport ${sessionId} has been closed.`); + }; + transportMap.set(sessionId, transport); await server.connect(transport); });