add SSE connection ability
This commit is contained in:
@@ -13,12 +13,12 @@
|
||||
"@types/eventsource": "^1.1.15",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/ws": "^8.5.12",
|
||||
"eventsource": "^2.0.2",
|
||||
"tsx": "^4.19.0",
|
||||
"typescript": "^5.6.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"eventsource": "^2.0.2",
|
||||
"express": "^4.21.0",
|
||||
"mcp-typescript": "file:../packages/mcp-typescript",
|
||||
"ws": "^8.18.0",
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user