add SSE connection ability

This commit is contained in:
Ashwin Bhat
2024-10-10 09:06:12 -07:00
parent 18025d731a
commit d32e7b6725
7 changed files with 480 additions and 21 deletions

View File

@@ -1,9 +1,15 @@
import cors from "cors";
import EventSource from "eventsource";
import { SSEServerTransport } from "mcp-typescript/server/sse.js";
import express from "express";
import { StdioClientTransport } from "mcp-typescript/client/stdio.js";
import mcpProxy from "./mcpProxy.js";
import { SSEClientTransport } from "mcp-typescript/client/sse.js";
// Polyfill EventSource for an SSE client in Node.js
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).EventSource = EventSource;
const app = express();
app.use(cors());
@@ -12,10 +18,23 @@ let transports: SSEServerTransport[] = [];
app.get("/sse", async (req, res) => {
console.log("New SSE connection");
const command = decodeURIComponent(req.query.command as string);
const args = decodeURIComponent(req.query.args as string).split(",");
const backingServerTransport = new StdioClientTransport();
await backingServerTransport.spawn({ command, args });
const transportType = req.query.transportType as string;
let backingServerTransport;
console.log("Query parameters:", req.query);
if (transportType === "stdio") {
const command = decodeURIComponent(req.query.command as string);
const args = decodeURIComponent(req.query.args as string).split(",");
backingServerTransport = new StdioClientTransport();
await backingServerTransport.spawn({ command, args });
} else if (transportType === "sse") {
const url = decodeURIComponent(req.query.url as string);
backingServerTransport = new SSEClientTransport();
await backingServerTransport.connect(new URL(url));
} else {
throw new Error("Invalid transport type specified");
}
const webAppTransport = new SSEServerTransport("/message");
transports.push(webAppTransport);

View File

@@ -9,6 +9,9 @@ export default function mcpProxy({
transportToServer: Transport;
onerror: (error: Error) => void;
}) {
let transportToClientClosed = false;
let transportToServerClosed = false;
transportToClient.onmessage = (message) => {
transportToServer.send(message).catch(onerror);
};
@@ -18,10 +21,20 @@ export default function mcpProxy({
};
transportToClient.onclose = () => {
if (transportToServerClosed) {
return;
}
transportToClientClosed = true;
transportToServer.close().catch(onerror);
};
transportToServer.onclose = () => {
if (transportToClientClosed) {
return;
}
transportToServerClosed = true;
transportToClient.close().catch(onerror);
};