Files
Template-NPM/src/config.ts
T
Jonathan Miller 91b78f8da1 feat: initial MCP server template with placeholder-driven scaffolding
Template repository for creating MokoStandards-compliant MCP servers.
Includes 4-file src/ structure (index, client, config, types), setup
wizard, example tools, 12 CI/CD workflows, full docs, and {{placeholder}}
tokens for search-and-replace customization.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-07 14:39:19 -05:00

64 lines
2.3 KiB
TypeScript

/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
*
* This file is part of a Moko Consulting project.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* FILE INFORMATION
* DEFGROUP: {{PROJECT_NAME}}.Config
* INGROUP: {{PROJECT_NAME}}
* REPO: https://git.mokoconsulting.tech/MokoConsulting/{{PROJECT_NAME}}
* PATH: /src/config.ts
* VERSION: 01.00.00
* BRIEF: Configuration loader for {{DISPLAY_NAME}} MCP connections
*/
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { homedir } from 'node:os';
import type { ApiConfig, ApiConnection } from './types.js';
// ── Customize this ──────────────────────────────────────────────────────
// Change the filename to match your project (e.g. ".dolibarr-api-mcp.json")
const CONFIG_FILENAME = '.{{PROJECT_NAME}}.json';
// Change the env var name to match your project
const CONFIG_ENV_VAR = '{{ENV_PREFIX}}_CONFIG';
// ────────────────────────────────────────────────────────────────────────
export async function loadConfig(): Promise<ApiConfig> {
const config_path = process.env[CONFIG_ENV_VAR]
? resolve(process.env[CONFIG_ENV_VAR]!)
: resolve(homedir(), CONFIG_FILENAME);
try {
const raw = await readFile(config_path, 'utf-8');
const parsed = JSON.parse(raw) as Partial<ApiConfig>;
if (!parsed.connections || Object.keys(parsed.connections).length === 0) {
throw new Error('No connections defined in config');
}
return {
connections: parsed.connections,
defaultConnection: parsed.defaultConnection ?? Object.keys(parsed.connections)[0],
};
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(
`Failed to load config from ${config_path}: ${message}\n` +
`Create ${config_path} — see config.example.json for format.`,
);
}
}
export function getConnection(config: ApiConfig, name?: string): ApiConnection {
const key = name ?? config.defaultConnection;
const conn = config.connections[key];
if (!conn) {
throw new Error(
`Connection "${key}" not found. Available: ${Object.keys(config.connections).join(', ')}`,
);
}
return conn;
}