Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/mcp/src/config.ts
T

122 lines
4.5 KiB
TypeScript

export type McpRuntimeConfig = {
nodeEnv: string;
revision: string;
listen: {
port: number;
host: string;
};
apiKeys: {
hashSecret?: string;
allowLegacyPlaintextFallback: boolean;
};
oauth: {
resourceUrl: string;
authorizationServerUrl: string;
tokenHashSecret?: string;
};
audit: {
chainSecret?: string;
};
security: {
allowedHosts: string[];
allowedOrigins: string[];
};
};
const DEFAULT_ALLOWED_HOSTS = ["mcp.hivegrid.de", "staging-mcp.hivegrid.de", "localhost", "127.0.0.1", "::1"];
const DEFAULT_ALLOWED_ORIGINS = ["https://mcp.hivegrid.de", "https://staging-mcp.hivegrid.de", "http://localhost", "http://127.0.0.1"];
const DEFAULT_RESOURCE_URL = "https://mcp.hivegrid.de";
const DEFAULT_AUTHORIZATION_SERVER_URL = "https://app.hivegrid.de";
const MIN_TOKEN_HASH_SECRET_BYTES = 32;
const MIN_AUDIT_CHAIN_SECRET_BYTES = 32;
const REQUIRED_PRODUCTION_DEPENDENCIES = ["DATABASE_URL", "REDIS_URL"] as const;
export function loadMcpRuntimeConfig(env: NodeJS.ProcessEnv = process.env): McpRuntimeConfig {
const config: McpRuntimeConfig = {
nodeEnv: env.NODE_ENV ?? "development",
revision: env.APP_REVISION ?? "unknown",
listen: {
host: env.MCP_HOST ?? "0.0.0.0",
port: parsePort(env.MCP_PORT ?? env.PORT, 8080),
},
apiKeys: {
hashSecret: nonEmpty(env.API_KEY_HASH_SECRET),
allowLegacyPlaintextFallback: env.API_KEY_LEGACY_PLAINTEXT_FALLBACK === "true",
},
oauth: {
resourceUrl: trimTrailingSlash(env.MCP_RESOURCE_URL) ?? DEFAULT_RESOURCE_URL,
authorizationServerUrl: trimTrailingSlash(env.MCP_AUTHORIZATION_SERVER_URL) ?? DEFAULT_AUTHORIZATION_SERVER_URL,
tokenHashSecret: nonEmpty(env.MCP_OAUTH_TOKEN_HASH_SECRET) ?? nonEmpty(env.API_KEY_HASH_SECRET),
},
audit: {
chainSecret: nonEmpty(env.AUDIT_CHAIN_SECRET),
},
security: {
allowedHosts: parseCsv(env.MCP_ALLOWED_HOSTS, DEFAULT_ALLOWED_HOSTS),
allowedOrigins: parseCsv(env.MCP_ALLOWED_ORIGINS, DEFAULT_ALLOWED_ORIGINS),
},
};
// Fail closed in production: OAuth token hashing falls back to a public
// source-code constant when the token hash secret is unset, which would make
// every stored token hash portable across any default-keyed deployment. A
// prod boot without it is a misconfiguration, not a soft default. The single
// resolvable secret (MCP_OAUTH_TOKEN_HASH_SECRET, or API_KEY_HASH_SECRET as a
// fallback) is what both the web issuer and this validator use, so requiring
// it here is sufficient — the legacy API-key path cannot validate any real
// key on the fallback constant anyway.
if (
config.nodeEnv === "production" &&
(!config.oauth.tokenHashSecret || Buffer.byteLength(config.oauth.tokenHashSecret, "utf8") < MIN_TOKEN_HASH_SECRET_BYTES)
) {
throw new Error(
"MCP runtime config is missing a strong production token hash secret: set MCP_OAUTH_TOKEN_HASH_SECRET (or API_KEY_HASH_SECRET) to at least 32 bytes and to the same value the web app uses. Refusing to boot on a weak or built-in hash-secret fallback.",
);
}
if (
config.nodeEnv === "production" &&
(!config.audit.chainSecret || Buffer.byteLength(config.audit.chainSecret, "utf8") < MIN_AUDIT_CHAIN_SECRET_BYTES)
) {
throw new Error(
"MCP runtime config is missing a strong production audit-chain secret: set AUDIT_CHAIN_SECRET to at least 32 bytes. Refusing to boot with forgeable audit evidence.",
);
}
if (config.nodeEnv === "production") {
for (const name of REQUIRED_PRODUCTION_DEPENDENCIES) {
if (!nonEmpty(env[name])) {
throw new Error(
`MCP runtime config is missing required production dependency ${name}. Refusing to report healthy before the dependency is configured.`,
);
}
}
}
return config;
}
function nonEmpty(raw: string | undefined): string | undefined {
const value = raw?.trim();
return value ? value : undefined;
}
function trimTrailingSlash(raw: string | undefined): string | undefined {
const value = raw?.trim();
return value ? value.replace(/\/+$/, "") : undefined;
}
function parsePort(raw: string | undefined, fallback: number): number {
const parsed = Number.parseInt(raw ?? "", 10);
if (Number.isInteger(parsed) && parsed > 0 && parsed <= 65_535) {
return parsed;
}
return fallback;
}
function parseCsv(raw: string | undefined, fallback: readonly string[]): string[] {
const values = (raw ?? "")
.split(",")
.map((value) => value.trim())
.filter(Boolean);
return values.length > 0 ? [...new Set(values)] : [...fallback];
}