@rhofkens/mcp-quotes-server
Version:
A Model Context Protocol (MCP) server that provides quotes based on user requests
104 lines • 3.93 kB
JavaScript
import { z } from "zod";
import { logger } from "../utils/logger.js";
const environmentConfigSchema = z.object({
mcpHttpEnabled: z
.string()
.optional()
.default("false")
.transform((val) => val.toLowerCase() === "true")
.describe("Enable HTTP transport for MCP server"),
mcpHttpPort: z
.string()
.optional()
.default("3000")
.transform((val) => parseInt(val, 10))
.pipe(z.number().int().min(1).max(65535))
.describe("HTTP server port (1-65535)"),
mcpHttpHost: z
.string()
.optional()
.default("localhost")
.describe("HTTP server host address"),
mcpHttpsEnabled: z
.string()
.optional()
.default("false")
.transform((val) => val.toLowerCase() === "true")
.describe("Enable HTTPS mode for HTTP transport"),
mcpHttpsCertPath: z
.string()
.optional()
.describe("Path to HTTPS certificate file"),
mcpHttpsKeyPath: z
.string()
.optional()
.describe("Path to HTTPS private key file"),
mcpHttpAllowedHosts: z
.string()
.optional()
.transform((val) => val ? val.split(",").map((h) => h.trim()) : ["127.0.0.1", "localhost"])
.describe("Comma-separated list of allowed hosts for DNS rebinding protection"),
mcpHttpAllowedOrigins: z
.string()
.optional()
.transform((val) => (val ? val.split(",").map((o) => o.trim()) : []))
.describe("Comma-separated list of allowed origins for CORS"),
});
export function parseEnvironmentConfig() {
try {
const rawConfig = {
mcpHttpEnabled: process.env.MCP_HTTP_ENABLED,
mcpHttpPort: process.env.MCP_HTTP_PORT,
mcpHttpHost: process.env.MCP_HTTP_HOST,
mcpHttpsEnabled: process.env.MCP_HTTPS_ENABLED,
mcpHttpsCertPath: process.env.MCP_HTTPS_CERT_PATH,
mcpHttpsKeyPath: process.env.MCP_HTTPS_KEY_PATH,
mcpHttpAllowedHosts: process.env.MCP_HTTP_ALLOWED_HOSTS,
mcpHttpAllowedOrigins: process.env.MCP_HTTP_ALLOWED_ORIGINS,
};
logger.debug("Parsing environment configuration", { rawConfig });
const config = environmentConfigSchema.parse(rawConfig);
logger.info("Environment configuration parsed successfully", {
httpEnabled: config.mcpHttpEnabled,
httpsEnabled: config.mcpHttpsEnabled,
port: config.mcpHttpPort,
host: config.mcpHttpHost,
allowedHosts: config.mcpHttpAllowedHosts,
allowedOrigins: config.mcpHttpAllowedOrigins,
});
return config;
}
catch (error) {
logger.error("Failed to parse environment configuration", { error });
throw new Error(`Environment configuration validation failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
export function getHttpServerConfig(envConfig) {
return {
enabled: envConfig.mcpHttpEnabled,
port: envConfig.mcpHttpPort,
host: envConfig.mcpHttpHost,
https: {
enabled: envConfig.mcpHttpsEnabled,
certPath: envConfig.mcpHttpsCertPath,
keyPath: envConfig.mcpHttpsKeyPath,
},
security: {
allowedHosts: envConfig.mcpHttpAllowedHosts,
allowedOrigins: envConfig.mcpHttpAllowedOrigins,
},
};
}
export function validateHttpsConfig(config) {
if (!config.https.enabled) {
return;
}
if (!config.https.certPath || !config.https.keyPath) {
throw new Error("HTTPS is enabled but MCP_HTTPS_CERT_PATH and MCP_HTTPS_KEY_PATH environment variables are required");
}
logger.debug("HTTPS configuration validated", {
certPath: config.https.certPath,
keyPath: config.https.keyPath,
});
}
//# sourceMappingURL=environment-config.js.map