Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server dying when an error occurs #53

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,59 @@ const createTransport = async (query: express.Request["query"]) => {
};

app.get("/sse", async (req, res) => {
console.log("New SSE connection");
try {
console.log("New SSE connection");

const backingServerTransport = await createTransport(req.query);
const backingServerTransport = await createTransport(req.query);

console.log("Connected MCP client to backing server transport");
console.log("Connected MCP client to backing server transport");

const webAppTransport = new SSEServerTransport("/message", res);
console.log("Created web app transport");
const webAppTransport = new SSEServerTransport("/message", res);
console.log("Created web app transport");

webAppTransports.push(webAppTransport);
console.log("Created web app transport");
webAppTransports.push(webAppTransport);
console.log("Created web app transport");

await webAppTransport.start();
await webAppTransport.start();

mcpProxy({
transportToClient: webAppTransport,
transportToServer: backingServerTransport,
onerror: (error) => {
console.error(error);
},
});
console.log("Set up MCP proxy");
mcpProxy({
transportToClient: webAppTransport,
transportToServer: backingServerTransport,
onerror: (error) => {
console.error(error);
},
});
console.log("Set up MCP proxy");
} catch (error) {
console.error("Error in /sse route:", error);
res.status(500).json(error);
}
});

app.post("/message", async (req, res) => {
const sessionId = req.query.sessionId;
console.log(`Received message for sessionId ${sessionId}`);

const transport = webAppTransports.find((t) => t.sessionId === sessionId);
if (!transport) {
res.status(404).send("Session not found");
return;
try {
const sessionId = req.query.sessionId;
console.log(`Received message for sessionId ${sessionId}`);

const transport = webAppTransports.find((t) => t.sessionId === sessionId);
if (!transport) {
res.status(404).end("Session not found");
return;
}
await transport.handlePostMessage(req, res);
} catch (error) {
console.error("Error in /message route:", error);
res.status(500).json(error);
}
await transport.handlePostMessage(req, res);
});

app.get("/default-environment", (req, res) => {
res.json(getDefaultEnvironment());
try {
res.json(getDefaultEnvironment());
} catch (error) {
console.error("Error in /default-environment route:", error);
res.status(500).json(error);
}
});

const PORT = process.env.PORT || 3000;
Expand Down