-
Notifications
You must be signed in to change notification settings - Fork 835
Cursor: SSE error: TypeError: terminated: Body Timeout Error #270
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
Comments
I experienced the same issue — it looks like the connection drops rather quickly. I was able to keep it alive by periodically writing a keep-alive comment to the response:
Here is my setup: import express, { Request, Response } from "express";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import { createMCPServer } from "./services/mcp-service.js";
import { importGtfs } from 'gtfs';
import { gtfsConfig } from './config/gtfs-config.js';
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
const server = createMCPServer();
const PORT = process.env.PORT || 9999;
const KEEP_ALIVE_INTERVAL_MS = 25000; // Send keep-alive every 25 seconds
const app = express()
const transports: {[sessionId: string]: SSEServerTransport} = {};
const sseConnections = new Map<string, { res: Response, intervalId: NodeJS.Timeout }>();
app.get("/health", (_: Request, res: Response) => {
res.status(200).json({ status: 'healthy' });
});
app.get("/sse", async (_: Request, res: Response) => {
const transport = new SSEServerTransport('/messages', res)
const sessionId = transport.sessionId; // Get session ID from transport
transports[sessionId] = transport;
// Start keep-alive ping
const intervalId = setInterval(() => {
if (sseConnections.has(sessionId) && !res.writableEnded) {
res.write(': keepalive\n\n');
} else {
// Should not happen if close handler is working, but clear just in case
clearInterval(intervalId);
sseConnections.delete(sessionId);
}
}, KEEP_ALIVE_INTERVAL_MS);
// Store connection details
sseConnections.set(sessionId, { res, intervalId });
console.log(`[SSE Connection] Client connected: ${sessionId}, starting keep-alive.`);
res.on("close", () => {
console.log(`[SSE Connection] Client disconnected: ${sessionId}, stopping keep-alive.`);
// Clean up transport
delete transports[sessionId];
// Clean up keep-alive interval
const connection = sseConnections.get(sessionId);
if (connection) {
clearInterval(connection.intervalId);
sseConnections.delete(sessionId);
}
});
// Connect server to transport *after* setting up handlers
try {
await server.connect(transport)
} catch (error) {
console.error(`[SSE Connection] Error connecting server to transport for ${sessionId}:`, error);
// Ensure cleanup happens even if connect fails
clearInterval(intervalId);
sseConnections.delete(sessionId);
delete transports[sessionId];
if (!res.writableEnded) {
res.status(500).end('Failed to connect MCP server to transport');
}
}
});
app.post("/messages", async (req: Request, res: Response) => {
const sessionId = req.query.sessionId as string;
const transport = transports[sessionId];
if (transport) {
console.log('Transport found:', transport.sessionId);
await transport.handlePostMessage(req, res);
} else {
res.status(400).send('No transport found for sessionId');
}
});
console.log(`⭐️ Server is running on port ${PORT}`)
app.listen(PORT) |
Hi @Hanxmai! Possibly a better approach long term is to set the timeout on the request.
In the meantime, try this:
The Inspector's timeout config![]() |
Big help! Thx!!! |
…w-initialize-request-in-history feat: Show initialize request/response in History panel (modelcontextprotocol#269)
Am trying this method as well with Streamable HTTP & it appears that the response is no longer writeable by the time the interval is called. Will continue to explore solutions. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I have the same questory when i used @modelcontextprotocol/sdk in npm package to create mcp sse server:
like this: modelcontextprotocol/rust-sdk#71
To Reproduce
Steps to reproduce the behavior:
SSE error: TypeError: terminated: Body Timeout
Expected behavior
A clear and concise description of what you expected to happen.
Logs
If applicable, add logs to help explain your problem.
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: