Secure MCP endpoint for LAN clients

This commit is contained in:
Adolfo Reyna
2026-05-26 22:16:15 -04:00
parent e2384654fd
commit b68ca1bebd
5 changed files with 37 additions and 10 deletions
+8 -1
View File
@@ -14,9 +14,16 @@ export function getConfig() {
throw new Error("MACMINI_MCP_PORT must be an integer between 1 and 65535.");
}
return {
const config = {
host: process.env.MACMINI_MCP_HOST || "127.0.0.1",
port,
token: process.env.MACMINI_MCP_TOKEN || "",
};
const isLoopback = ["127.0.0.1", "::1", "localhost"].includes(config.host);
if (!isLoopback && !config.token) {
throw new Error("MACMINI_MCP_TOKEN is required when listening beyond localhost.");
}
return config;
}
+5 -1
View File
@@ -1,4 +1,5 @@
import http from "node:http";
import { timingSafeEqual } from "node:crypto";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { getConfig } from "./config.js";
import { createMacMiniMcpServer } from "./server.js";
@@ -9,7 +10,10 @@ function authorized(request) {
if (!config.token) {
return true;
}
return request.headers.authorization === `Bearer ${config.token}`;
const authorization = request.headers.authorization || "";
const expected = Buffer.from(`Bearer ${config.token}`);
const provided = Buffer.from(authorization);
return provided.length === expected.length && timingSafeEqual(provided, expected);
}
const service = http.createServer(async (request, response) => {