@vreippainen/hevy-mcp-server
Version:
A MCP server for Hevy
57 lines • 1.82 kB
JavaScript
import express from 'express';
import { config } from '../config.js';
export class HttpServerTransport {
app = express();
server = null;
sseClients = new Set();
constructor() {
this.app.use(express.json());
// SSE endpoint
this.app.get('/events', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
// Send initial connection message
res.write('data: {"type": "connected"}\n\n');
// Add client to the set
this.sseClients.add(res);
// Remove client when connection closes
req.on('close', () => {
this.sseClients.delete(res);
});
});
}
async start() {
return new Promise((resolve) => {
this.server = this.app.listen(config.server.port, () => {
console.error(`HTTP server running on port ${config.server.port}`);
resolve();
});
});
}
async close() {
if (this.server) {
await new Promise((resolve, reject) => {
this.server?.close((err) => {
if (err)
reject(err);
else
resolve();
});
});
}
}
async send(message) {
// Send message to all connected SSE clients
this.sseClients.forEach((client) => {
client.write(`data: ${JSON.stringify(message)}\n\n`);
});
}
async receive() {
// HTTP transport doesn't receive messages directly
return new Promise(() => { });
}
}
//# sourceMappingURL=httpTransport.js.map