mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

config.ts

Language TypeScript2.1 KB60 lines

mcp-server/src/lib/config.ts

  1. import { ConfigError } from "./errors.js";
  2. /**
  3. * Runtime-agnostic configuration.
  4. *
  5. * This module reads no globals (no `process.env`). Config is derived from an
  6. * injected source so the same core runs under Node (stdio dev) and under the
  7. * Cloudflare Workers isolate runtime, where env arrives as a binding on the
  8. * fetch handler and `process` does not exist.
  9. */
  10. export interface PlinthConfig {
  11. /** Single dispatch endpoint. Methods are selected via the `method` param. */
  12. readonly baseUrl: string;
  13. /** Static API key with global read scope. Undefined when none is configured. */
  14. readonly apiKey: string | undefined;
  15. /**
  16. * Master safety switch, true only when PLINTH_ALLOW_LIVE_REQUESTS is "true".
  17. * While false, plinthRequest() refuses to run, because the API has no
  18. * sandbox: every call reads production data.
  19. */
  20. readonly allowLiveRequests: boolean;
  21. }
  22. /**
  23. * The subset of environment variables this server reads. Both Node's
  24. * `process.env` and a Cloudflare Workers `env` binding are structurally
  25. * assignable to this shape.
  26. */
  27. export interface PlinthEnvVars {
  28. PLINTH_API_BASE_URL?: string;
  29. PLINTH_API_KEY?: string;
  30. PLINTH_ALLOW_LIVE_REQUESTS?: string;
  31. }
  32. const DEFAULT_BASE_URL = "https://orders.meridiandisplay.example/api/v2/dispatch";
  33. /** Build config from an injected env source (process.env, Workers env, a test double). */
  34. export function configFromEnv(source: PlinthEnvVars): PlinthConfig {
  35. return {
  36. baseUrl: source.PLINTH_API_BASE_URL?.trim() || DEFAULT_BASE_URL,
  37. apiKey: source.PLINTH_API_KEY?.trim() || undefined,
  38. allowLiveRequests: source.PLINTH_ALLOW_LIVE_REQUESTS === "true",
  39. };
  40. }
  41. /**
  42. * Assert the config is safe to make a live call with. Called by plinthRequest()
  43. * before any network access.
  44. */
  45. export function assertLiveRequestsAllowed(cfg: PlinthConfig): void {
  46. if (!cfg.allowLiveRequests) {
  47. throw new ConfigError(
  48. "Live requests are disabled. This server is PRODUCTION-only (no sandbox); " +
  49. "set PLINTH_ALLOW_LIVE_REQUESTS=true once you have intentionally wired the handlers.",
  50. );
  51. }
  52. if (!cfg.apiKey) {
  53. throw new ConfigError("PLINTH_API_KEY is not set.");
  54. }
  55. }