server.jsβ’3.9 kB
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
// CORS configuratie voor alle origins
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Cache-Control', 'Accept'],
credentials: false
}));
// Basic middleware
app.use(express.json());
// Health check endpoint
app.get('/', (req, res) => {
res.json({
status: 'MCP Server Running',
timestamp: new Date().toISOString(),
endpoints: {
sse: '/sse',
health: '/health'
},
uptime: process.uptime()
});
});
// Health check endpoint voor Render
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});
// SSE endpoint
app.get('/sse', (req, res) => {
console.log('New SSE connection established');
// SSE headers
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Cache-Control',
'Access-Control-Allow-Methods': 'GET, OPTIONS'
});
// Verstuur initiΓ«le verbinding bericht
res.write(`data: ${JSON.stringify({
type: 'connection',
message: 'SSE connection established',
timestamp: new Date().toISOString(),
server: 'MCP Server'
})}\n\n`);
// Timestamp interval - elke 5 seconden
const timestampInterval = setInterval(() => {
const data = {
type: 'timestamp',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage()
};
res.write(`data: ${JSON.stringify(data)}\n\n`);
}, 5000);
// Keep-alive ping - elke 30 seconden
const keepAliveInterval = setInterval(() => {
res.write(`: keep-alive ${Date.now()}\n\n`);
}, 30000);
// Cleanup bij disconnect
req.on('close', () => {
console.log('SSE connection closed');
clearInterval(timestampInterval);
clearInterval(keepAliveInterval);
});
req.on('error', (err) => {
console.error('SSE connection error:', err);
clearInterval(timestampInterval);
clearInterval(keepAliveInterval);
});
});
// MCP Server status endpoint
app.get('/mcp/status', (req, res) => {
res.json({
server: 'MCP Server',
version: '1.0.0',
status: 'running',
features: [
'Server-Sent Events (SSE)',
'Real-time timestamps',
'Keep-alive pings',
'CORS enabled'
],
endpoints: {
root: '/',
health: '/health',
sse: '/sse',
status: '/mcp/status'
},
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Server error:', err);
res.status(500).json({
error: 'Internal Server Error',
message: err.message,
timestamp: new Date().toISOString()
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({
error: 'Not Found',
message: `Endpoint ${req.path} not found`,
availableEndpoints: ['/', '/health', '/sse', '/mcp/status'],
timestamp: new Date().toISOString()
});
});
// Start server
app.listen(PORT, '0.0.0.0', () => {
console.log(`π MCP Server running on port ${PORT}`);
console.log(`π‘ SSE endpoint: http://localhost:${PORT}/sse`);
console.log(`π₯ Health check: http://localhost:${PORT}/health`);
console.log(`π Status: http://localhost:${PORT}/mcp/status`);
console.log(`β° Timestamp interval: 5 seconds`);
console.log(`π Keep-alive interval: 30 seconds`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down gracefully');
process.exit(0);
});