93 lines
3.4 KiB
TypeScript
93 lines
3.4 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;
|
|
};
|
|
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";
|
|
|
|
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),
|
|
},
|
|
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) {
|
|
throw new Error(
|
|
"MCP runtime config is missing the production token hash secret: set MCP_OAUTH_TOKEN_HASH_SECRET (or API_KEY_HASH_SECRET) to the same value the web app uses. Refusing to boot on the built-in hash-secret fallback.",
|
|
);
|
|
}
|
|
|
|
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];
|
|
}
|