feat: replace StdioTransport with StreamableHTTP + Node.js cluster
Integrates StreamableHTTP natively into the MCP server instead of using StdioServerTransport. Adds Node.js cluster support so multiple workers can handle concurrent MCP clients simultaneously. Changes: - src/index.ts: replace StdioServerTransport with StreamableHTTPServerTransport + Node.js cluster (primary forks N workers, each worker handles HTTP requests on :8080/mcp with a serializing mutex) - Dockerfile: multi-stage build (node:22-slim builder + runtime) - .gitea/workflows/docker-build.yml: CI/CD pipeline — on push to main, builds and pushes Docker image to gitea.p-lao.com registry Environment variables: - MCP_WORKERS (default: 4) — number of cluster workers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
58
src/index.ts
58
src/index.ts
@@ -1,5 +1,7 @@
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import { createServer, IncomingMessage, ServerResponse } from "node:http";
|
||||
import cluster from "node:cluster";
|
||||
import FormData from "form-data";
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import { z } from "zod";
|
||||
@@ -857,11 +859,59 @@ server.registerTool(
|
||||
},
|
||||
);
|
||||
|
||||
async function run() {
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
const NUM_WORKERS = parseInt(process.env.MCP_WORKERS ?? "4", 10);
|
||||
|
||||
if (cluster.isPrimary) {
|
||||
// ── Proceso primario: solo hace fork de workers ──
|
||||
console.log(`[docmost-mcp] Primary PID=${process.pid}, starting ${NUM_WORKERS} workers`);
|
||||
for (let i = 0; i < NUM_WORKERS; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
cluster.on("exit", (worker, code) => {
|
||||
console.log(`[docmost-mcp] Worker PID=${worker.process.pid} died (code=${code}), restarting...`);
|
||||
cluster.fork();
|
||||
});
|
||||
} else {
|
||||
// ── Worker: cada uno tiene su propio McpServer singleton ──
|
||||
// El mutex serializa las requests dentro de este worker (stateless, 1 request a la vez por worker)
|
||||
let workerBusy: Promise<void> = Promise.resolve();
|
||||
|
||||
async function handleMcpRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined, // stateless: un transport por request
|
||||
});
|
||||
await server.connect(transport);
|
||||
try {
|
||||
await transport.handleRequest(req, res);
|
||||
} finally {
|
||||
await transport.close();
|
||||
}
|
||||
}
|
||||
|
||||
const httpServer = createServer((req: IncomingMessage, res: ServerResponse) => {
|
||||
if (!req.url?.startsWith("/mcp")) {
|
||||
res.writeHead(404).end("Not found");
|
||||
return;
|
||||
}
|
||||
workerBusy = workerBusy
|
||||
.then(() => handleMcpRequest(req, res))
|
||||
.catch((err) => {
|
||||
console.error(`[docmost-mcp] Worker PID=${process.pid} error:`, err);
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(500, { "Content-Type": "application/json" }).end(
|
||||
JSON.stringify({ jsonrpc: "2.0", error: { code: -32603, message: String(err) }, id: null })
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
httpServer.listen(8080, () => {
|
||||
console.log(`[docmost-mcp] Worker PID=${process.pid} listening on port 8080, path /mcp`);
|
||||
});
|
||||
}
|
||||
|
||||
// run() vacía — el cluster ya se inició en el top-level de arriba
|
||||
async function run() {}
|
||||
run().catch((error) => {
|
||||
console.error("Fatal error running server:", error);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user