@baasix/mcp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Configuration utility for Baasix MCP Server
3
+ * Handles environment variable loading and validation
4
+ */
5
+
6
+ import { config as dotenvConfig } from 'dotenv';
7
+ import { fileURLToPath } from 'url';
8
+ import { dirname, join } from 'path';
9
+ import fs from 'fs';
10
+
11
+ const __filename = fileURLToPath(import.meta.url);
12
+ const __dirname = dirname(__filename);
13
+
14
+ // Default configuration values
15
+ const DEFAULT_CONFIG = {
16
+ BAASIX_URL: 'http://localhost:8056',
17
+ BAASIX_EMAIL: 'admin@baasix.com',
18
+ BAASIX_PASSWORD: 'admin@123'
19
+ };
20
+
21
+ // Load environment variables with priority
22
+ export function loadEnvironmentConfig(options = {}) {
23
+ const {
24
+ envPath = null,
25
+ useDefaults = true,
26
+ processEnv = true
27
+ } = options;
28
+
29
+ let config = {};
30
+
31
+ // 1. Load defaults
32
+ if (useDefaults) {
33
+ config = { ...DEFAULT_CONFIG };
34
+ }
35
+
36
+ // 2. Load from .env file (look in mcp root directory)
37
+ const mcpRootPath = join(__dirname, '..');
38
+ if (envPath || fs.existsSync(join(mcpRootPath, '.env'))) {
39
+ const dotenvPath = envPath || join(mcpRootPath, '.env');
40
+ const result = dotenvConfig({ path: dotenvPath });
41
+ if (result.parsed) {
42
+ config = { ...config, ...result.parsed };
43
+ }
44
+ }
45
+
46
+ // 3. Override with process environment variables
47
+ if (processEnv) {
48
+ const envVars = [
49
+ 'BAASIX_URL',
50
+ 'BAASIX_AUTH_TOKEN',
51
+ 'BAASIX_EMAIL',
52
+ 'BAASIX_PASSWORD'
53
+ ];
54
+
55
+ envVars.forEach(key => {
56
+ if (process.env[key]) {
57
+ config[key] = process.env[key];
58
+ }
59
+ });
60
+ }
61
+
62
+ return config;
63
+ }
64
+
65
+ // Validate configuration
66
+ export function validateConfig(config) {
67
+ const errors = [];
68
+ const warnings = [];
69
+
70
+ // Required: BAASIX_URL
71
+ if (!config.BAASIX_URL) {
72
+ errors.push('BAASIX_URL is required');
73
+ }
74
+
75
+ // Authentication method validation
76
+ const hasToken = config.BAASIX_AUTH_TOKEN;
77
+ const hasCredentials = config.BAASIX_EMAIL && config.BAASIX_PASSWORD;
78
+
79
+ if (!hasToken && !hasCredentials) {
80
+ errors.push('Either BAASIX_AUTH_TOKEN or both BAASIX_EMAIL and BAASIX_PASSWORD must be provided');
81
+ }
82
+
83
+ if (hasToken && hasCredentials) {
84
+ warnings.push('Both token and credentials provided - token will take priority');
85
+ }
86
+
87
+ // URL validation
88
+ if (config.BAASIX_URL) {
89
+ try {
90
+ new URL(config.BAASIX_URL);
91
+ } catch (error) {
92
+ errors.push('BAASIX_URL must be a valid URL');
93
+ }
94
+ }
95
+
96
+ return { errors, warnings, isValid: errors.length === 0 };
97
+ }
98
+
99
+ // Create configuration with validation
100
+ export function createConfig(options = {}) {
101
+ const config = loadEnvironmentConfig(options);
102
+ const validation = validateConfig(config);
103
+
104
+ if (!validation.isValid) {
105
+ throw new Error(`Configuration validation failed:\n${validation.errors.join('\n')}`);
106
+ }
107
+
108
+ if (validation.warnings.length > 0) {
109
+ validation.warnings.forEach(warning => {
110
+ console.warn(`Warning: ${warning}`);
111
+ });
112
+ }
113
+
114
+ return config;
115
+ }
116
+
117
+ // Export default configuration loader
118
+ export default function getConfig(options = {}) {
119
+ return createConfig(options);
120
+ }