Skip to content

Commit 02bfd16

Browse files
committed
handle multiple connections
1 parent 1497dbd commit 02bfd16

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

server/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { SSEClientTransport } from "mcp-typescript/client/sse.js";
1414
const app = express();
1515
app.use(cors());
1616

17-
let transports: SSEServerTransport[] = [];
17+
let webAppTransports: SSEServerTransport[] = [];
1818

1919
app.get("/sse", async (req, res) => {
2020
console.log("New SSE connection");
@@ -43,7 +43,8 @@ app.get("/sse", async (req, res) => {
4343
}
4444

4545
const webAppTransport = new SSEServerTransport("/message");
46-
transports.push(webAppTransport);
46+
webAppTransports.push(webAppTransport);
47+
console.log("Created web app transport");
4748

4849
await webAppTransport.connectSSE(req, res);
4950

@@ -58,9 +59,10 @@ app.get("/sse", async (req, res) => {
5859
});
5960

6061
app.post("/message", async (req, res) => {
61-
console.log("Received message");
62+
const sessionId = req.query.sessionId;
63+
console.log(`Received message for sessionId ${sessionId}`);
6264

63-
const transport = transports.find((t) => true);
65+
const transport = webAppTransports.find((t) => t.sessionId === sessionId);
6466
if (!transport) {
6567
res.status(404).send("Session not found");
6668
return;

0 commit comments

Comments
 (0)