mirror of
https://github.com/modelcontextprotocol/servers.git
synced 2026-04-19 08:43:28 +02:00
- Move setInterval calls from server creation to startNotificationIntervals function - Only start notification timers when a client actually connects to the SSE server - Prevents 'Not connected' error when server tries to send notifications before client connection - Fixes issue where server crashes after 5 seconds when running 'npx @modelcontextprotocol/server-everything sse' 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Ola Hungerford <olaservo@users.noreply.github.com>
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
import express from "express";
|
|
import { createServer } from "./everything.js";
|
|
|
|
console.error('Starting SSE server...');
|
|
|
|
const app = express();
|
|
|
|
const transports: Map<string, SSEServerTransport> = new Map<string, SSEServerTransport>();
|
|
|
|
app.get("/sse", async (req, res) => {
|
|
let transport: SSEServerTransport;
|
|
const { server, cleanup, startNotificationIntervals } = createServer();
|
|
|
|
if (req?.query?.sessionId) {
|
|
const sessionId = (req?.query?.sessionId as string);
|
|
transport = transports.get(sessionId) as SSEServerTransport;
|
|
console.error("Client Reconnecting? This shouldn't happen; when client has a sessionId, GET /sse should not be called again.", transport.sessionId);
|
|
} else {
|
|
// Create and store transport for new session
|
|
transport = new SSEServerTransport("/message", res);
|
|
transports.set(transport.sessionId, transport);
|
|
|
|
// Connect server to transport
|
|
await server.connect(transport);
|
|
console.error("Client Connected: ", transport.sessionId);
|
|
|
|
// Start notification intervals after client connects
|
|
startNotificationIntervals();
|
|
|
|
// Handle close of connection
|
|
server.onclose = async () => {
|
|
console.error("Client Disconnected: ", transport.sessionId);
|
|
transports.delete(transport.sessionId);
|
|
await cleanup();
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.post("/message", async (req, res) => {
|
|
const sessionId = (req?.query?.sessionId as string);
|
|
const transport = transports.get(sessionId);
|
|
if (transport) {
|
|
console.error("Client Message from", sessionId);
|
|
await transport.handlePostMessage(req, res);
|
|
} else {
|
|
console.error(`No transport found for sessionId ${sessionId}`)
|
|
}
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3001;
|
|
app.listen(PORT, () => {
|
|
console.error(`Server is running on port ${PORT}`);
|
|
});
|