Skip to main content
Glama

MCP Server with SSE

by Fuglys
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); });

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Fuglys/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server