Skip to content

Commit 9da43bc

Browse files
claude[bot]olaservo
andcommitted
Fix SSE server crash by starting notification timers only after client connects
- 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>
1 parent fcb550e commit 9da43bc

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/everything/everything.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,6 @@ export const createServer = () => {
169169
let subsUpdateInterval: NodeJS.Timeout | undefined;
170170
let stdErrUpdateInterval: NodeJS.Timeout | undefined;
171171

172-
// Set up update interval for subscribed resources
173-
subsUpdateInterval = setInterval(() => {
174-
for (const uri of subscriptions) {
175-
server.notification({
176-
method: "notifications/resources/updated",
177-
params: { uri },
178-
});
179-
}
180-
}, 10000);
181-
182172
let logLevel: LoggingLevel = "debug";
183173
let logsUpdateInterval: NodeJS.Timeout | undefined;
184174
const messages = [
@@ -198,15 +188,30 @@ export const createServer = () => {
198188
return messageLevel < currentLevel;
199189
};
200190

201-
// Set up update interval for random log messages
202-
logsUpdateInterval = setInterval(() => {
203-
let message = {
204-
method: "notifications/message",
205-
params: messages[Math.floor(Math.random() * messages.length)],
206-
};
207-
if (!isMessageIgnored(message.params.level as LoggingLevel))
208-
server.notification(message);
209-
}, 20000);
191+
// Function to start notification intervals when a client connects
192+
const startNotificationIntervals = () => {
193+
if (!subsUpdateInterval) {
194+
subsUpdateInterval = setInterval(() => {
195+
for (const uri of subscriptions) {
196+
server.notification({
197+
method: "notifications/resources/updated",
198+
params: { uri },
199+
});
200+
}
201+
}, 10000);
202+
}
203+
204+
if (!logsUpdateInterval) {
205+
logsUpdateInterval = setInterval(() => {
206+
let message = {
207+
method: "notifications/message",
208+
params: messages[Math.floor(Math.random() * messages.length)],
209+
};
210+
if (!isMessageIgnored(message.params.level as LoggingLevel))
211+
server.notification(message);
212+
}, 20000);
213+
}
214+
};
210215

211216

212217

@@ -874,7 +879,7 @@ export const createServer = () => {
874879
if (stdErrUpdateInterval) clearInterval(stdErrUpdateInterval);
875880
};
876881

877-
return { server, cleanup };
882+
return { server, cleanup, startNotificationIntervals };
878883
};
879884

880885
const MCP_TINY_IMAGE =

src/everything/sse.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const transports: Map<string, SSEServerTransport> = new Map<string, SSEServerTra
1010

1111
app.get("/sse", async (req, res) => {
1212
let transport: SSEServerTransport;
13-
const { server, cleanup } = createServer();
13+
const { server, cleanup, startNotificationIntervals } = createServer();
1414

1515
if (req?.query?.sessionId) {
1616
const sessionId = (req?.query?.sessionId as string);
@@ -25,6 +25,9 @@ app.get("/sse", async (req, res) => {
2525
await server.connect(transport);
2626
console.error("Client Connected: ", transport.sessionId);
2727

28+
// Start notification intervals after client connects
29+
startNotificationIntervals();
30+
2831
// Handle close of connection
2932
server.onclose = async () => {
3033
console.error("Client Disconnected: ", transport.sessionId);

0 commit comments

Comments
 (0)