MCPcopy Create free account
hub / github.com/chekusu/wanman / createHttpServer

Function createHttpServer

packages/runtime/src/http-server.ts:61–108  ·  view source on GitHub ↗
(opts: HttpServerOptions)

Source from the content-addressed store, hash-verified

59}
60
61export function createHttpServer(opts: HttpServerOptions): http.Server {
62 const { port, onRpc, onEvent, onHealth } = opts;
63
64 const server = http.createServer(async (req, res) => {
65 try {
66 const url = new URL(req.url || '/', `http://localhost:${port}`);
67
68 // Health check
69 if (req.method === 'GET' && url.pathname === '/health') {
70 sendJson(res, 200, onHealth());
71 return;
72 }
73
74 // JSON-RPC endpoint
75 if (req.method === 'POST' && url.pathname === '/rpc') {
76 const rpcReq = await parseBody<JsonRpcRequest>(req);
77 if (!rpcReq.jsonrpc || rpcReq.jsonrpc !== '2.0' || !rpcReq.method) {
78 sendJson(res, 400, { error: 'Invalid JSON-RPC 2.0 request' });
79 return;
80 }
81 const rpcRes = await onRpc(rpcReq);
82 sendJson(res, 200, rpcRes);
83 return;
84 }
85
86 // External events
87 if (req.method === 'POST' && url.pathname === '/events') {
88 const event = await parseBody<ExternalEvent>(req);
89 onEvent(event);
90 sendJson(res, 200, { status: 'accepted' });
91 return;
92 }
93
94 sendJson(res, 404, { error: 'Not found' });
95 } catch (err) {
96 log.error('request error', { error: err instanceof Error ? err.message : String(err) });
97 if (!res.headersSent) {
98 sendJson(res, 500, { error: 'Internal server error' });
99 }
100 }
101 });
102
103 server.listen(port, '0.0.0.0', () => {
104 log.info(`listening on port ${port}`);
105 });
106
107 return server;
108}

Callers 2

startMethod · 0.85
startServerFunction · 0.85

Calls 4

sendJsonFunction · 0.85
errorMethod · 0.80
infoMethod · 0.80
onEventFunction · 0.70

Tested by 1

startServerFunction · 0.68